Skip to content

Commit b0fd23f

Browse files
committed
assert_stdout_contains_regex in run_make_support + variations
1 parent 3347dfb commit b0fd23f

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

src/tools/run-make-support/src/assertion_helpers.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::panic;
44
use std::path::Path;
55

6-
use crate::fs;
6+
use crate::{fs, regex};
77

88
/// Assert that `actual` is equal to `expected`.
99
#[track_caller]
@@ -47,6 +47,36 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
4747
}
4848
}
4949

50+
/// Assert that `haystack` contains the regex pattern `needle`.
51+
#[track_caller]
52+
pub fn assert_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
53+
let haystack = haystack.as_ref();
54+
let needle = needle.as_ref();
55+
let re = regex::Regex::new(needle).unwrap();
56+
if !re.is_match(haystack) {
57+
eprintln!("=== HAYSTACK ===");
58+
eprintln!("{}", haystack);
59+
eprintln!("=== NEEDLE ===");
60+
eprintln!("{}", needle);
61+
panic!("needle was not found in haystack");
62+
}
63+
}
64+
65+
/// Assert that `haystack` does not contain the regex pattern `needle`.
66+
#[track_caller]
67+
pub fn assert_not_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
68+
let haystack = haystack.as_ref();
69+
let needle = needle.as_ref();
70+
let re = regex::Regex::new(needle).unwrap();
71+
if re.is_match(haystack) {
72+
eprintln!("=== HAYSTACK ===");
73+
eprintln!("{}", haystack);
74+
eprintln!("=== NEEDLE ===");
75+
eprintln!("{}", needle);
76+
panic!("needle was unexpectedly found in haystack");
77+
}
78+
}
79+
5080
/// Assert that all files in `dir1` exist and have the same content in `dir2`
5181
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
5282
let dir2 = dir2.as_ref();

src/tools/run-make-support/src/command.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::path::Path;
66
use std::process::{Command as StdCommand, ExitStatus, Output, Stdio};
77

88
use crate::util::handle_failed_output;
9-
use crate::{assert_contains, assert_equals, assert_not_contains};
9+
use crate::{
10+
assert_contains, assert_contains_regex, assert_equals, assert_not_contains,
11+
assert_not_contains_regex,
12+
};
1013

1114
use build_helper::drop_bomb::DropBomb;
1215

@@ -192,13 +195,27 @@ impl CompletedProcess {
192195
self
193196
}
194197

198+
/// Checks that `stdout` does not contain the regex pattern `unexpected`.
199+
#[track_caller]
200+
pub fn assert_stdout_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
201+
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
202+
self
203+
}
204+
195205
/// Checks that `stdout` contains `expected`.
196206
#[track_caller]
197207
pub fn assert_stdout_contains<S: AsRef<str>>(&self, expected: S) -> &Self {
198208
assert_contains(&self.stdout_utf8(), expected);
199209
self
200210
}
201211

212+
/// Checks that `stdout` contains the regex pattern `expected`.
213+
#[track_caller]
214+
pub fn assert_stdout_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
215+
assert_contains_regex(&self.stdout_utf8(), expected);
216+
self
217+
}
218+
202219
/// Checks that trimmed `stderr` matches trimmed `expected`.
203220
#[track_caller]
204221
pub fn assert_stderr_equals<S: AsRef<str>>(&self, expected: S) -> &Self {
@@ -213,13 +230,27 @@ impl CompletedProcess {
213230
self
214231
}
215232

233+
/// Checks that `stderr` contains the regex pattern `expected`.
234+
#[track_caller]
235+
pub fn assert_stderr_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
236+
assert_contains_regex(&self.stderr_utf8(), expected);
237+
self
238+
}
239+
216240
/// Checks that `stderr` does not contain `unexpected`.
217241
#[track_caller]
218242
pub fn assert_stderr_not_contains<S: AsRef<str>>(&self, unexpected: S) -> &Self {
219243
assert_not_contains(&self.stdout_utf8(), unexpected);
220244
self
221245
}
222246

247+
/// Checks that `stderr` does not contain the regex pattern `unexpected`.
248+
#[track_caller]
249+
pub fn assert_stderr_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
250+
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
251+
self
252+
}
253+
223254
#[track_caller]
224255
pub fn assert_exit_code(&self, code: i32) -> &Self {
225256
assert!(self.output.status.code() == Some(code));

src/tools/run-make-support/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub use path_helpers::{
8484
pub use scoped_run::{run_in_tmpdir, test_while_readonly};
8585

8686
pub use assertion_helpers::{
87-
assert_contains, assert_dirs_are_equal, assert_equals, assert_not_contains,
87+
assert_contains, assert_contains_regex, assert_dirs_are_equal, assert_equals,
88+
assert_not_contains, assert_not_contains_regex,
8889
};
8990

9091
pub use string::{

tests/run-make/rlib-format-packed-bundled-libs-2/rmake.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
// in rustc flags without a compilation failure or the removal of expected symbols.
55
// See https://github.com/rust-lang/rust/pull/100101
66

7-
//FIXME(Oneirical): try it on test-various
8-
97
use run_make_support::{llvm_ar, llvm_readobj, regex, rfs, rust_lib_name, rustc};
108

119
fn main() {
1210
// Build a strangely named dependency.
1311
rustc().input("native_dep.rs").crate_type("staticlib").output("native_dep.ext").run();
1412

1513
rustc().input("rust_dep.rs").crate_type("rlib").arg("-Zpacked_bundled_libs").run();
16-
let symbols = llvm_readobj().symbols().input(rust_lib_name("rust_dep")).run().stdout_utf8();
17-
let re = regex::Regex::new("U.*native_f1").unwrap();
18-
assert!(re.is_match(&symbols));
14+
llvm_readobj()
15+
.symbols()
16+
.input(rust_lib_name("rust_dep"))
17+
.run()
18+
.assert_stdout_contains_regex("U.*native_f1");
1919
llvm_ar()
2020
.arg("t")
2121
.arg(rust_lib_name("rust_dep"))

0 commit comments

Comments
 (0)