Skip to content

Commit 1a15d90

Browse files
committed
assert_stdout_contains_regex in run_make_support + variations
1 parent d5c073e commit 1a15d90

File tree

5 files changed

+99
-11
lines changed

5 files changed

+99
-11
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/external_deps/llvm.rs

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ pub fn llvm_ar() -> LlvmAr {
3636
LlvmAr::new()
3737
}
3838

39+
/// Construct a new `llvm-nm` invocation. This assumes that `llvm-nm` is available
40+
/// at `$LLVM_BIN_DIR/llvm-nm`.
41+
pub fn llvm_nm() -> LlvmNm {
42+
LlvmNm::new()
43+
}
44+
3945
/// A `llvm-readobj` invocation builder.
4046
#[derive(Debug)]
4147
#[must_use]
@@ -71,11 +77,19 @@ pub struct LlvmAr {
7177
cmd: Command,
7278
}
7379

80+
/// A `llvm-nm` invocation builder.
81+
#[derive(Debug)]
82+
#[must_use]
83+
pub struct LlvmNm {
84+
cmd: Command,
85+
}
86+
7487
crate::macros::impl_common_helpers!(LlvmReadobj);
7588
crate::macros::impl_common_helpers!(LlvmProfdata);
7689
crate::macros::impl_common_helpers!(LlvmFilecheck);
7790
crate::macros::impl_common_helpers!(LlvmObjdump);
7891
crate::macros::impl_common_helpers!(LlvmAr);
92+
crate::macros::impl_common_helpers!(LlvmNm);
7993

8094
/// Generate the path to the bin directory of LLVM.
8195
#[must_use]
@@ -244,3 +258,19 @@ impl LlvmAr {
244258
self
245259
}
246260
}
261+
262+
impl LlvmNm {
263+
/// Construct a new `llvm-nm` invocation. This assumes that `llvm-nm` is available
264+
/// at `$LLVM_BIN_DIR/llvm-nm`.
265+
pub fn new() -> Self {
266+
let llvm_nm = llvm_bin_dir().join("llvm-nm");
267+
let cmd = Command::new(llvm_nm);
268+
Self { cmd }
269+
}
270+
271+
/// Provide an input file.
272+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
273+
self.cmd.arg(path.as_ref());
274+
self
275+
}
276+
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub use cc::{cc, cxx, extra_c_flags, extra_cxx_flags, Cc};
4848
pub use clang::{clang, Clang};
4949
pub use htmldocck::htmldocck;
5050
pub use llvm::{
51-
llvm_ar, llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr, LlvmFilecheck,
52-
LlvmObjdump, LlvmProfdata, LlvmReadobj,
51+
llvm_ar, llvm_filecheck, llvm_nm, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr,
52+
LlvmFilecheck, LlvmNm, LlvmObjdump, LlvmProfdata, LlvmReadobj,
5353
};
5454
pub use python::python_command;
5555
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};
@@ -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

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
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-
9-
use run_make_support::{llvm_ar, llvm_readobj, regex, rfs, rust_lib_name, rustc};
7+
use run_make_support::{llvm_ar, llvm_nm, 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_nm().input(rust_lib_name("rust_dep")).run().assert_stdout_contains_regex("U.*native_f1");
1915
llvm_ar()
2016
.arg("t")
2117
.arg(rust_lib_name("rust_dep"))

0 commit comments

Comments
 (0)