From a12c23f04820e8200932d59620740ee83833cf38 Mon Sep 17 00:00:00 2001 From: WATANABE Yuki Date: Mon, 22 Apr 2024 01:23:37 +0900 Subject: [PATCH] Only report failed test cases in scripted test This change makes the scripted test runner report only the failed test cases in the log file. This makes it easier to see what went wrong in the test run. --- yash-cli/tests/scripted_test.rs | 39 ++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/yash-cli/tests/scripted_test.rs b/yash-cli/tests/scripted_test.rs index 658c0e40..f6a2c346 100644 --- a/yash-cli/tests/scripted_test.rs +++ b/yash-cli/tests/scripted_test.rs @@ -62,7 +62,8 @@ where // is a failed test case. Check the log file to see if there is one. let log = std::fs::read_to_string(&log_file).unwrap(); - assert!(!log.contains("FAILED"), "{}", log); + let failures = failures(&log); + assert!(failures.is_empty(), "{failures}"); } /// Runs a test subject. @@ -73,6 +74,42 @@ fn run(name: &str) { unsafe { run_with_preexec(name, || Ok(())) } } +/// Extracts the failed test cases from the log file. +fn failures(log: &str) -> String { + let mut lines = log.lines(); + let mut test_case = Vec::new(); + let mut result = String::new(); + + // Each test case in the log file is enclosed by the "%%% START: " and + // "%%% PASSED: " or "%%% FAILED: " lines. We extract lines between these + // markers and append them to the result string. + while let Some(start) = lines.find(|line| line.starts_with("%%% START: ")) { + test_case.clear(); + test_case.push(start); + for line in lines.by_ref() { + if line.starts_with("%%% PASSED: ") { + // Discard this test case + break; + } else if line.starts_with("%%% FAILED: ") { + test_case.push(line); + + // Add this test case to the result + for line in test_case.drain(..) { + result.push_str(line); + result.push('\n'); + } + result.push('\n'); + + break; + } else { + test_case.push(line); + } + } + } + + result +} + #[test] fn alias() { run("alias-p.sh")