Relaxed testing of libtest outputs.
authorGilad Naaman <gilad.naaman@gmail.com>
Mon, 22 Jan 2018 20:27:50 +0000 (22:27 +0200)
committerGilad Naaman <gilad.naaman@gmail.com>
Mon, 22 Jan 2018 20:27:50 +0000 (22:27 +0200)
tests/bench.rs
tests/cargotest/support/mod.rs

index 491867a6c3ca1ece50eea16d5356f94adf2bbd3a..da29bc364b7de203e2b146049085a2270cbbb825 100644 (file)
@@ -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]")
index e5de1d3caafa23f5aaaecb7bbb56236b7e2f5bcd..c513d57b16e9e7aef0239e43055a8895762701a0 100644 (file)
@@ -348,9 +348,11 @@ pub struct Execs {
     expect_exit_code: Option<i32>,
     expect_stdout_contains: Vec<String>,
     expect_stderr_contains: Vec<String>,
+    expect_either_contains: Vec<String>,
     expect_stdout_contains_n: Vec<(String, usize)>,
     expect_stdout_not_contains: Vec<String>,
     expect_stderr_not_contains: Vec<String>,
+    expect_neither_contains: Vec<String>,
     expect_json: Option<Vec<Value>>,
 }
 
@@ -384,6 +386,11 @@ impl Execs {
         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 {
         self.expect_stdout_contains_n.push((expected.to_string(), number));
         self
@@ -399,6 +406,11 @@ impl Execs {
         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 {
         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,
     }
 }