Skip to content
Open
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
4 changes: 3 additions & 1 deletion tests/testkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ A filter selects tests whose name contains it. `run` also takes:
skipped.
- `--timeout SECS` (default 900): a test that runs longer gets SIGTERM, then
SIGKILL after `--grace SECS` (default 10).
- `--junit PATH`: writes a JUnit report with one test suite per family.
- `--junit PATH`: writes a JUnit report with one test suite per family. The
report is rewritten after each test, so a run that is stopped from outside
still leaves the results so far.

Each test runs in its own process group. When the test's process exits, `run`
kills anything left in the group, such as CLI processes the test started.
Expand Down
29 changes: 28 additions & 1 deletion tests/testkit/testkit/src/program/junit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ pub fn write(path: &Path, report: &Report) -> std::io::Result<()> {
std::fs::create_dir_all(parent)?;
}
let xml = report.to_string().map_err(std::io::Error::other)?;
std::fs::write(path, xml)
// Replace the file in one step, so a run that is killed while writing
// leaves the previous report, not a truncated one.
let mut partial = path.as_os_str().to_owned();
partial.push(".partial");
let partial = std::path::PathBuf::from(partial);
std::fs::write(&partial, xml)?;
std::fs::rename(&partial, path)
}

#[cfg(test)]
Expand Down Expand Up @@ -125,4 +131,25 @@ mod tests {
assert!(xml.contains(expected), "missing {expected} in\n{xml}");
}
}

#[test]
fn write_replaces_the_report_and_leaves_no_partial_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("results/junit.xml");
let one = [TestRun {
test: &PASSED,
outcome: Outcome::Passed,
duration: None,
stdout: Vec::new(),
stderr: Vec::new(),
}];
super::write(&path, &report("p", &one)).unwrap();
super::write(&path, &report("p", &[])).unwrap();
let xml = std::fs::read_to_string(&path).unwrap();
assert!(xml.contains(r#"tests="0""#), "{xml}");
assert_eq!(
std::fs::read_dir(path.parent().unwrap()).unwrap().count(),
1
);
}
}
7 changes: 7 additions & 0 deletions tests/testkit/testkit/src/program/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ pub fn run(program: &str, exe: &Path, tests: &[Test], options: &Options) -> Exit
),
}
runs.push(run);
// Keep the report current, so a run that is stopped from outside, such
// as by a run-level timeout, still leaves the results so far.
if let Some(path) = &options.junit
&& let Err(error) = junit::write(path, &junit::report(program, &runs))
{
eprintln!("{program}: write {}: {error}", path.display());
}
}

let not_run = selected_count - runs.len();
Expand Down
Loading