Relaxed testing of libtest outputs.

This commit is contained in:
Gilad Naaman 2018-01-22 22:27:50 +02:00
parent 47136a3497
commit f66ac370bd
2 changed files with 41 additions and 6 deletions

View file

@ -286,17 +286,18 @@ fn cargo_bench_failing_test() {
assert_that(process(&p.bin("foo")), assert_that(process(&p.bin("foo")),
execs().with_stdout("hello\n")); 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 ... ") execs().with_stdout_contains("test bench_hello ... ")
.with_stderr_contains(format!("\ .with_either_contains(format!("\
[COMPILING] foo v0.5.0 ({}) [COMPILING] foo v0.5.0 ({})
[FINISHED] release [optimized] target(s) in [..] [FINISHED] release [optimized] target(s) in [..]
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE] [RUNNING] target[/]release[/]deps[/]foo-[..][EXE]
thread '[..]' panicked at 'assertion failed: \ thread '[..]' panicked at 'assertion failed: \
`(left == right)`[..]", p.url())) `(left == right)`[..]", p.url()))
.with_stderr_contains("[..]left: `\"hello\"`[..]") .with_either_contains("[..]left: `\"hello\"`[..]")
.with_stderr_contains("[..]right: `\"nope\"`[..]") .with_either_contains("[..]right: `\"nope\"`[..]")
.with_stderr_contains("[..]src[/]main.rs:15[..]") .with_either_contains("[..]src[/]main.rs:15[..]")
.with_status(101)); .with_status(101));
} }
@ -993,7 +994,7 @@ fn test_bench_no_fail_fast() {
}"#) }"#)
.build(); .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) execs().with_status(101)
.with_stderr_contains("\ .with_stderr_contains("\
[RUNNING] target[/]release[/]deps[/]foo-[..][EXE]") [RUNNING] target[/]release[/]deps[/]foo-[..][EXE]")

View file

@ -348,9 +348,11 @@ pub struct Execs {
expect_exit_code: Option<i32>, expect_exit_code: Option<i32>,
expect_stdout_contains: Vec<String>, expect_stdout_contains: Vec<String>,
expect_stderr_contains: Vec<String>, expect_stderr_contains: Vec<String>,
expect_either_contains: Vec<String>,
expect_stdout_contains_n: Vec<(String, usize)>, expect_stdout_contains_n: Vec<(String, usize)>,
expect_stdout_not_contains: Vec<String>, expect_stdout_not_contains: Vec<String>,
expect_stderr_not_contains: Vec<String>, expect_stderr_not_contains: Vec<String>,
expect_neither_contains: Vec<String>,
expect_json: Option<Vec<Value>>, expect_json: Option<Vec<Value>>,
} }
@ -384,6 +386,11 @@ impl Execs {
self self
} }
pub fn with_either_contains<S: ToString>(mut self, expected: S) -> Execs {
self.expect_either_contains.push(expected.to_string());
self
}
pub fn with_stdout_contains_n<S: ToString>(mut self, expected: S, number: usize) -> Execs { pub fn with_stdout_contains_n<S: ToString>(mut self, expected: S, number: usize) -> Execs {
self.expect_stdout_contains_n.push((expected.to_string(), number)); self.expect_stdout_contains_n.push((expected.to_string(), number));
self self
@ -399,6 +406,11 @@ impl Execs {
self self
} }
pub fn with_neither_contains<S: ToString>(mut self, expected: S) -> Execs {
self.expect_neither_contains.push(expected.to_string());
self
}
pub fn with_json(mut self, expected: &str) -> Execs { pub fn with_json(mut self, expected: &str) -> Execs {
self.expect_json = Some(expected.split("\n\n").map(|obj| { self.expect_json = Some(expected.split("\n\n").map(|obj| {
obj.parse().unwrap() obj.parse().unwrap()
@ -449,6 +461,26 @@ impl Execs {
self.match_std(Some(expect), &actual.stderr, "stderr", self.match_std(Some(expect), &actual.stderr, "stderr",
&actual.stdout, MatchKind::NotPresent)?; &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 { if let Some(ref objects) = self.expect_json {
let stdout = str::from_utf8(&actual.stdout) let stdout = str::from_utf8(&actual.stdout)
@ -762,9 +794,11 @@ pub fn execs() -> Execs {
expect_exit_code: None, expect_exit_code: None,
expect_stdout_contains: Vec::new(), expect_stdout_contains: Vec::new(),
expect_stderr_contains: Vec::new(), expect_stderr_contains: Vec::new(),
expect_either_contains: Vec::new(),
expect_stdout_contains_n: Vec::new(), expect_stdout_contains_n: Vec::new(),
expect_stdout_not_contains: Vec::new(), expect_stdout_not_contains: Vec::new(),
expect_stderr_not_contains: Vec::new(), expect_stderr_not_contains: Vec::new(),
expect_neither_contains: Vec::new(),
expect_json: None, expect_json: None,
} }
} }