diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 43e9cc2..2562854 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -247,3 +247,18 @@ jobs: scripts/check-package-list.sh /tmp/package.list - name: Publish dry-run run: cargo publish --dry-run --locked + - name: Packaged bin tests + run: | + package_root=$(mktemp -d) + trap 'rm -rf "$package_root"' EXIT + CARGO_TARGET_DIR="$package_root/target" cargo package --locked --no-verify + set -- "$package_root"/target/package/atx-*.crate + if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then + printf 'expected one freshly built source archive\n' >&2 + exit 1 + fi + archive=$1 + package_name=$(basename "$archive" .crate) + tar -xzf "$archive" -C "$package_root" + CARGO_TARGET_DIR="$GITHUB_WORKSPACE/target" cargo test --locked --bins \ + --manifest-path "$package_root/$package_name/Cargo.toml" diff --git a/scripts/README.md b/scripts/README.md index d4a00e4..406b710 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -27,3 +27,6 @@ wrong tool versions fail with an actionable message. `check-package-list.sh PACKAGE_LIST` checks Cargo's package file list. Embedded test fixtures are required and allowed; other tests and internal directories are excluded. Paths must be relative and contain no traversal components. + +The required CI package job also tests the bin from a freshly extracted source +archive. Workspace tests alone cannot catch a missing packaged test fixture. diff --git a/scripts/check-package-list.sh b/scripts/check-package-list.sh index 08067be..86b6d56 100755 --- a/scripts/check-package-list.sh +++ b/scripts/check-package-list.sh @@ -10,7 +10,7 @@ fi list=$1 for required in Cargo.toml Cargo.lock README.md CHANGELOG.md LICENSE-MIT src/main.rs \ - tests/fixtures/exit-codes-v1.json; do + tests/fixtures/exit-codes-v1.json tests/fixtures/json-outcomes-v1.json; do if ! grep -Fx "$required" "$list" >/dev/null; then printf 'package is missing %s\n' "$required" >&2 exit 1 diff --git a/scripts/test-check-harness.sh b/scripts/test-check-harness.sh index 148625d..f12e02f 100755 --- a/scripts/test-check-harness.sh +++ b/scripts/test-check-harness.sh @@ -123,6 +123,7 @@ README.md src/main.rs tests/fixtures/exit-codes-v1.json tests/fixtures/json-error-v1.json +tests/fixtures/json-outcomes-v1.json tests/fixtures/migrations/v0.sql LIST @@ -215,12 +216,14 @@ for mode in static full; do done done -missing_fixture="$tmp/package-missing-fixture" -grep -Fxv tests/fixtures/exit-codes-v1.json "$ATX_PACKAGE_LIST" >"$missing_fixture" -if scripts/check-package-list.sh "$missing_fixture" >/dev/null 2>&1; then - printf 'package policy accepted a missing embedded fixture\n' >&2 - exit 1 -fi +for fixture in tests/fixtures/exit-codes-v1.json tests/fixtures/json-outcomes-v1.json; do + missing_fixture="$tmp/package-missing-fixture" + grep -Fxv "$fixture" "$ATX_PACKAGE_LIST" >"$missing_fixture" + if scripts/check-package-list.sh "$missing_fixture" >/dev/null 2>&1; then + printf 'package policy accepted missing embedded fixture: %s\n' "$fixture" >&2 + exit 1 + fi +done for mode in quick static full msrv; do : >"$ATX_FAKE_LOG" diff --git a/src/cli/json.rs b/src/cli/json.rs index 29cabdd..3dce55f 100644 --- a/src/cli/json.rs +++ b/src/cli/json.rs @@ -149,20 +149,9 @@ mod tests { RunOutcome::Interrupted("outcome unknown after restart".to_owned()), RunOutcome::Cancelled("cancelled by user".to_owned()), ]; - let docs = include_str!("../../docs/json-api.md"); - let section = docs - .split("### Outcome examples") - .nth(1) - .expect("outcome section"); - let example = section - .split("```json\n") - .nth(1) - .expect("JSON example") - .split("```") - .next() - .expect("example end"); let expected: serde_json::Value = - serde_json::from_str(example).expect("documented outcomes"); + serde_json::from_str(include_str!("../../tests/fixtures/json-outcomes-v1.json")) + .expect("outcome contract fixture"); let actual = serde_json::to_value(&outcomes).expect("outcomes"); assert_eq!(actual, expected); assert_eq!( diff --git a/tests/cli_human_output.rs b/tests/cli_human_output.rs index be44307..450af32 100644 --- a/tests/cli_human_output.rs +++ b/tests/cli_human_output.rs @@ -186,6 +186,13 @@ fn documented_version_example_matches_the_binary() { assert_eq!(actual, expected); } +#[test] +fn documented_outcome_examples_match_the_serializer_fixture() { + let expected: Value = serde_json::from_str(include_str!("fixtures/json-outcomes-v1.json")) + .expect("outcome contract fixture"); + assert_eq!(documented_example("### Outcome examples"), expected); +} + #[test] fn documented_error_example_matches_the_binary() { let root = tempdir().expect("root"); diff --git a/tests/fixtures/json-outcomes-v1.json b/tests/fixtures/json-outcomes-v1.json new file mode 100644 index 0000000..fcccc1c --- /dev/null +++ b/tests/fixtures/json-outcomes-v1.json @@ -0,0 +1,7 @@ +[ + {"kind": "exit", "value": 0}, + {"kind": "signal", "value": 15}, + {"kind": "failure", "value": "command could not start"}, + {"kind": "interrupted", "value": "outcome unknown after restart"}, + {"kind": "cancelled", "value": "cancelled by user"} +] diff --git a/tests/process_lifecycle.rs b/tests/process_lifecycle.rs index 7d9bcbe..7d58146 100644 --- a/tests/process_lifecycle.rs +++ b/tests/process_lifecycle.rs @@ -162,6 +162,48 @@ fn linux_cmdline_parser_preserves_interior_empty_arguments() { ); } +#[cfg(target_os = "linux")] +#[test] +fn linux_cmdline_read_classifies_exit_and_preserves_real_errors() { + use std::io; + use support::{Inspection, classify_linux_cmdline_read}; + + let path = Path::new("/proc/42/cmdline"); + for code in [libc::ESRCH, libc::ENOENT] { + assert_eq!( + classify_linux_cmdline_read(path, Err(io::Error::from_raw_os_error(code))), + Ok(Inspection::Absent) + ); + } + for code in [libc::EACCES, libc::EPERM] { + assert_eq!( + classify_linux_cmdline_read(path, Err(io::Error::from_raw_os_error(code))), + Ok(Inspection::Transient) + ); + } + assert_eq!( + classify_linux_cmdline_read(path, Ok(Vec::new())), + Ok(Inspection::Transient) + ); + assert_eq!( + classify_linux_cmdline_read(path, Ok(b"atx\0\0job\0".to_vec())), + Ok(Inspection::Present( + vec!["atx", "", "job"] + .into_iter() + .map(std::ffi::OsString::from) + .collect() + )) + ); + for error in [ + io::Error::from_raw_os_error(libc::EIO), + io::Error::other("read fault"), + ] { + let error = + classify_linux_cmdline_read(path, Err(error)).expect_err("unexpected read failure"); + assert!(error.contains("/proc/42/cmdline"), "{error}"); + } +} + #[test] fn repeated_fixture_teardown_stops_session_supervisors() { for _ in 0..10 { diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 9d7f47a..4c6f946 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -1276,26 +1276,27 @@ fn inspect_process_identity_once( #[cfg(target_os = "linux")] fn process_arguments(pid: i32) -> Result>, String> { let path = PathBuf::from(format!("/proc/{pid}/cmdline")); - let bytes = match fs::read(&path) { - Ok(bytes) => bytes, + classify_linux_cmdline_read(&path, fs::read(&path)) +} + +#[cfg(target_os = "linux")] +/// Keep departed tasks separate from transient and unexpected inspection failures. +pub(crate) fn classify_linux_cmdline_read( + path: &Path, + result: io::Result>, +) -> Result>, String> { + match result { + Ok(bytes) if bytes.is_empty() => Ok(Inspection::Transient), + Ok(bytes) => Ok(Inspection::Present(parse_nul_arguments(&bytes))), Err(error) - if matches!( - error.kind(), - io::ErrorKind::NotFound | io::ErrorKind::PermissionDenied - ) => + if error.kind() == io::ErrorKind::NotFound + || error.raw_os_error() == Some(libc::ESRCH) => { - return Ok(if error.kind() == io::ErrorKind::NotFound { - Inspection::Absent - } else { - Inspection::Transient - }); + Ok(Inspection::Absent) } - Err(error) => return Err(format!("read {}: {error}", path.display())), - }; - if bytes.is_empty() { - return Ok(Inspection::Transient); + Err(error) if error.kind() == io::ErrorKind::PermissionDenied => Ok(Inspection::Transient), + Err(error) => Err(format!("read {}: {error}", path.display())), } - Ok(Inspection::Present(parse_nul_arguments(&bytes))) } #[cfg(target_os = "linux")]