From f66ac370bd9f5abccf6d4e455df3f9cc2bb490f3 Mon Sep 17 00:00:00 2001 From: Gilad Naaman Date: Mon, 22 Jan 2018 22:27:50 +0200 Subject: [PATCH] Relaxed testing of libtest outputs. --- tests/bench.rs | 13 +++++++------ tests/cargotest/support/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/tests/bench.rs b/tests/bench.rs index 491867a6c..da29bc364 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -286,17 +286,18 @@ fn cargo_bench_failing_test() { assert_that(process(&p.bin("foo")), execs().with_stdout("hello\n")); - assert_that(p.cargo("bench"), + // Force libtest into serial execution so that the test header will be printed. + assert_that(p.cargo("bench").arg("--").arg("--test-threads=1"), execs().with_stdout_contains("test bench_hello ... ") - .with_stderr_contains(format!("\ + .with_either_contains(format!("\ [COMPILING] foo v0.5.0 ({}) [FINISHED] release [optimized] target(s) in [..] [RUNNING] target[/]release[/]deps[/]foo-[..][EXE] thread '[..]' panicked at 'assertion failed: \ `(left == right)`[..]", p.url())) - .with_stderr_contains("[..]left: `\"hello\"`[..]") - .with_stderr_contains("[..]right: `\"nope\"`[..]") - .with_stderr_contains("[..]src[/]main.rs:15[..]") + .with_either_contains("[..]left: `\"hello\"`[..]") + .with_either_contains("[..]right: `\"nope\"`[..]") + .with_either_contains("[..]src[/]main.rs:15[..]") .with_status(101)); } @@ -993,7 +994,7 @@ fn test_bench_no_fail_fast() { }"#) .build(); - assert_that(p.cargo("bench").arg("--no-fail-fast"), + assert_that(p.cargo("bench").arg("--no-fail-fast").arg("--").arg("--test-threads=1"), execs().with_status(101) .with_stderr_contains("\ [RUNNING] target[/]release[/]deps[/]foo-[..][EXE]") diff --git a/tests/cargotest/support/mod.rs b/tests/cargotest/support/mod.rs index e5de1d3ca..c513d57b1 100644 --- a/tests/cargotest/support/mod.rs +++ b/tests/cargotest/support/mod.rs @@ -348,9 +348,11 @@ pub struct Execs { expect_exit_code: Option, expect_stdout_contains: Vec, expect_stderr_contains: Vec, + expect_either_contains: Vec, expect_stdout_contains_n: Vec<(String, usize)>, expect_stdout_not_contains: Vec, expect_stderr_not_contains: Vec, + expect_neither_contains: Vec, expect_json: Option>, } @@ -384,6 +386,11 @@ impl Execs { self } + pub fn with_either_contains(mut self, expected: S) -> Execs { + self.expect_either_contains.push(expected.to_string()); + self + } + pub fn with_stdout_contains_n(mut self, expected: S, number: usize) -> Execs { self.expect_stdout_contains_n.push((expected.to_string(), number)); self @@ -399,6 +406,11 @@ impl Execs { self } + pub fn with_neither_contains(mut self, expected: S) -> Execs { + self.expect_neither_contains.push(expected.to_string()); + self + } + pub fn with_json(mut self, expected: &str) -> Execs { self.expect_json = Some(expected.split("\n\n").map(|obj| { obj.parse().unwrap() @@ -449,6 +461,26 @@ impl Execs { self.match_std(Some(expect), &actual.stderr, "stderr", &actual.stdout, MatchKind::NotPresent)?; } + for expect in self.expect_neither_contains.iter() { + self.match_std(Some(expect), &actual.stdout, "stdout", + &actual.stdout, MatchKind::NotPresent)?; + + self.match_std(Some(expect), &actual.stderr, "stderr", + &actual.stderr, MatchKind::NotPresent)?; + } + + for expect in self.expect_either_contains.iter() { + let match_std = self.match_std(Some(expect), &actual.stdout, "stdout", + &actual.stdout, MatchKind::Partial); + let match_err = self.match_std(Some(expect), &actual.stderr, "stderr", + &actual.stderr, MatchKind::Partial); + + if let (Err(_), Err(_)) = (match_std, match_err) { + Err(format!("expected to find:\n\ + {}\n\n\ + did not find in either output.", expect))?; + } + } if let Some(ref objects) = self.expect_json { let stdout = str::from_utf8(&actual.stdout) @@ -762,9 +794,11 @@ pub fn execs() -> Execs { expect_exit_code: None, expect_stdout_contains: Vec::new(), expect_stderr_contains: Vec::new(), + expect_either_contains: Vec::new(), expect_stdout_contains_n: Vec::new(), expect_stdout_not_contains: Vec::new(), expect_stderr_not_contains: Vec::new(), + expect_neither_contains: Vec::new(), expect_json: None, } } -- 2.30.2