Skip to content

Commit 16cbb46

Browse files
authored
Merge pull request #30 from RalfJung/exit-status
improve error printed when the exit status is wrong
2 parents 7e75139 + 2764937 commit 16cbb46

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

src/lib.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,13 @@ pub fn run_tests_generic(
345345
eprintln!();
346346
for error in errors {
347347
match error {
348-
Error::ExitStatus(mode, exit_status) => eprintln!("{mode} got {exit_status}"),
348+
Error::ExitStatus {
349+
mode,
350+
status,
351+
expected,
352+
} => {
353+
eprintln!("{mode} test got {status}, but expected {expected}")
354+
}
349355
Error::PatternNotFound {
350356
pattern,
351357
definition_line,
@@ -364,10 +370,10 @@ pub fn run_tests_generic(
364370
);
365371
}
366372
Error::NoPatternsFound => {
367-
eprintln!("{}", "no error patterns found in failure test".red());
373+
eprintln!("{}", "no error patterns found in fail test".red());
368374
}
369375
Error::PatternFoundInPassTest => {
370-
eprintln!("{}", "error pattern found in success test".red())
376+
eprintln!("{}", "error pattern found in pass test".red())
371377
}
372378
Error::OutputDiffers {
373379
path,
@@ -525,7 +531,11 @@ fn parse_and_test_file(path: PathBuf, config: &Config) -> Vec<TestRun> {
525531
#[derive(Debug)]
526532
enum Error {
527533
/// Got an invalid exit status for the given mode.
528-
ExitStatus(Mode, ExitStatus),
534+
ExitStatus {
535+
mode: Mode,
536+
status: ExitStatus,
537+
expected: i32,
538+
},
529539
PatternNotFound {
530540
pattern: Pattern,
531541
definition_line: usize,
@@ -852,11 +862,19 @@ pub enum Mode {
852862

853863
impl Mode {
854864
fn ok(self, status: ExitStatus) -> Errors {
855-
match (status.code(), self) {
856-
(Some(1), Mode::Fail { .. }) | (Some(101), Mode::Panic) | (Some(0), Mode::Pass) => {
857-
vec![]
858-
}
859-
_ => vec![Error::ExitStatus(self, status)],
865+
let expected = match self {
866+
Mode::Pass => 0,
867+
Mode::Panic => 101,
868+
Mode::Fail { .. } => 1,
869+
};
870+
if status.code() == Some(expected) {
871+
vec![]
872+
} else {
873+
vec![Error::ExitStatus {
874+
mode: self,
875+
status,
876+
expected,
877+
}]
860878
}
861879
}
862880
}

tests/integration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn run(name: &str, mode: Mode) -> Result<()> {
4848
config.stderr_filter("( +Running [^(]+).*", "$1");
4949
config.stderr_filter(" *Blocking waiting for.*\n", "");
5050
config.stderr_filter(" *(Compiling|Downloaded|Downloading) .*\n", "");
51+
config.stderr_filter(r#""--out-dir"(,)? "[^"]+""#, r#""--out-dir"$1 "$$TMP"#);
5152
config.stderr_filter(
5253
"( *process didn't exit successfully: `[^-]+)-[0-9a-f]+",
5354
"$1-HASH",

tests/integrations/basic-fail/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integrations/basic-fail/Cargo.stderr

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
Finished test [unoptimized + debuginfo] target(s)
22
Running unittests src/lib.rs
33
Running tests/ui_tests.rs
4-
Compiler flags: ["--error-format=json", "--edition=2021"]
4+
Compiler flags: ["--error-format=json", "--out-dir", "$TMP, "--edition=2021"]
55
Building test dependencies...
66
tests/actual_tests/bad_pattern.rs ... FAILED
7+
tests/actual_tests/exit_code_fail.rs ... FAILED
78
tests/actual_tests/filters.rs ... FAILED
89
tests/actual_tests/foomp.rs ... FAILED
910

1011
tests/actual_tests/bad_pattern.rs FAILED:
11-
command: "rustc" "--error-format=json" "--edition=2021" "--extern" "basic_fail=$DIR/$DIR/../../../target/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/$DIR/../../../target/debug" "-L" "$DIR/$DIR/../../../target/debug" "tests/actual_tests/bad_pattern.rs"
12+
command: "rustc" "--error-format=json" "--out-dir" "$TMP "--edition=2021" "--extern" "basic_fail=$DIR/$DIR/../../../target/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/$DIR/../../../target/debug" "-L" "$DIR/$DIR/../../../target/debug" "tests/actual_tests/bad_pattern.rs"
1213

1314
substring `miesmätsched types` not found in stderr output
1415
expected because of pattern here: tests/actual_tests/bad_pattern.rs:5
@@ -37,6 +38,17 @@ For more information about this error, try `rustc --explain E0308`.
3738

3839

3940

41+
tests/actual_tests/exit_code_fail.rs FAILED:
42+
command: "rustc" "--error-format=json" "--out-dir" "$TMP "--edition=2021" "--extern" "basic_fail=$DIR/$DIR/../../../target/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/$DIR/../../../target/debug" "-L" "$DIR/$DIR/../../../target/debug" "tests/actual_tests/exit_code_fail.rs"
43+
44+
fail test got exit status: 0, but expected 1
45+
46+
no error patterns found in fail test
47+
48+
full stderr:
49+
50+
51+
4052
tests/actual_tests/filters.rs FAILED:
4153
command: "parse comments"
4254

@@ -47,7 +59,7 @@ full stderr:
4759

4860

4961
tests/actual_tests/foomp.rs FAILED:
50-
command: "rustc" "--error-format=json" "--edition=2021" "--extern" "basic_fail=$DIR/$DIR/../../../target/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/$DIR/../../../target/debug" "-L" "$DIR/$DIR/../../../target/debug" "tests/actual_tests/foomp.rs"
62+
command: "rustc" "--error-format=json" "--out-dir" "$TMP "--edition=2021" "--extern" "basic_fail=$DIR/$DIR/../../../target/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/$DIR/../../../target/debug" "-L" "$DIR/$DIR/../../../target/debug" "tests/actual_tests/foomp.rs"
5163

5264
actual output differed from expected
5365
--- tests/actual_tests/foomp.stderr
@@ -93,10 +105,11 @@ For more information about this error, try `rustc --explain E0308`.
93105

94106
FAILURES:
95107
tests/actual_tests/bad_pattern.rs
108+
tests/actual_tests/exit_code_fail.rs
96109
tests/actual_tests/filters.rs
97110
tests/actual_tests/foomp.rs
98111

99-
test result: FAIL. 3 tests failed, 0 tests passed, 0 ignored, 0 filtered out
112+
test result: FAIL. 4 tests failed, 0 tests passed, 0 ignored, 0 filtered out
100113
error: test failed, to rerun pass `--test ui_tests`
101114

102115
Caused by:

tests/integrations/basic-fail/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77

88
[dev-dependencies]
99
ui_test = { path = "../../.." }
10+
tempfile = "3.3.0"
1011

1112
[[test]]
1213
name = "ui_tests"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

tests/integrations/basic-fail/tests/ui_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ fn main() -> ui_test::color_eyre::Result<()> {
1717
.dependency_builder
1818
.envs
1919
.push(("CARGO_TARGET_DIR".into(), path.into()));
20+
21+
// hide binaries generated for successfully passing tests
22+
let tmp_dir = tempfile::tempdir()?;
23+
config.args.push("--out-dir".into());
24+
config.args.push(tmp_dir.path().as_os_str().to_owned());
25+
2026
config.args.push("--edition=2021".into());
2127
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
2228
config.stdout_filter("in ([0-9]m )?[0-9\\.]+s", "");

0 commit comments

Comments
 (0)