Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion scripts/check-package-list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions scripts/test-check-harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down
15 changes: 2 additions & 13 deletions src/cli/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
7 changes: 7 additions & 0 deletions tests/cli_human_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/json-outcomes-v1.json
Original file line number Diff line number Diff line change
@@ -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"}
]
42 changes: 42 additions & 0 deletions tests/process_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 17 additions & 16 deletions tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,26 +1276,27 @@ fn inspect_process_identity_once(
#[cfg(target_os = "linux")]
fn process_arguments(pid: i32) -> Result<Inspection<Vec<OsString>>, 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<Vec<u8>>,
) -> Result<Inspection<Vec<OsString>>, 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")]
Expand Down
Loading