Skip to content

Commit cb0aa98

Browse files
authored
Unrolled build for rust-lang#135036
Rollup merge of rust-lang#135036 - jieyouxu:rmake-be-quiet, r=compiler-errors run-make-support: adjust assertion printing, add some basic sanity checks cc ``@Noratrieb`` I think we may have unintentionally regressed this recently and double-printed (or printed even when the assertions didn't fail). This PR should condition the detail dumps only when the assertions fail. Added some basic sanity checks for the assertions helpers except for the directory comparisons. That particular helper is not robust against symlinks, and I intend to address it in a follow-up (issue is rust-lang#135037). r? bootstrap (or compiler)
2 parents 3f43b1a + 6175d73 commit cb0aa98

File tree

3 files changed

+235
-107
lines changed

3 files changed

+235
-107
lines changed

Diff for: src/tools/run-make-support/src/assertion_helpers.rs

-107
This file was deleted.
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//! Collection of assertions and assertion-related helpers.
2+
3+
#[cfg(test)]
4+
mod tests;
5+
6+
use std::panic;
7+
use std::path::Path;
8+
9+
use crate::{fs, regex};
10+
11+
/// Assert that `actual` is equal to `expected`.
12+
#[track_caller]
13+
pub fn assert_equals<A: AsRef<str>, E: AsRef<str>>(actual: A, expected: E) {
14+
let actual = actual.as_ref();
15+
let expected = expected.as_ref();
16+
17+
if actual != expected {
18+
eprintln!("=== ACTUAL TEXT ===");
19+
eprintln!("{}", actual);
20+
eprintln!("=== EXPECTED ===");
21+
eprintln!("{}", expected);
22+
panic!("expected text does not match actual text");
23+
}
24+
}
25+
26+
struct SearchDetails<'assertion_name, 'haystack, 'needle> {
27+
assertion_name: &'assertion_name str,
28+
haystack: &'haystack str,
29+
needle: &'needle str,
30+
}
31+
32+
impl<'assertion_name, 'haystack, 'needle> SearchDetails<'assertion_name, 'haystack, 'needle> {
33+
fn dump(&self) {
34+
eprintln!("{}:", self.assertion_name);
35+
eprintln!("=== HAYSTACK ===");
36+
eprintln!("{}", self.haystack);
37+
eprintln!("=== NEEDLE ===");
38+
eprintln!("{}", self.needle);
39+
}
40+
}
41+
42+
/// Assert that `haystack` contains `needle`.
43+
#[track_caller]
44+
pub fn assert_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
45+
let haystack = haystack.as_ref();
46+
let needle = needle.as_ref();
47+
if !haystack.contains(needle) {
48+
SearchDetails { assertion_name: "assert_contains", haystack, needle }.dump();
49+
panic!("needle was not found in haystack");
50+
}
51+
}
52+
53+
/// Assert that `haystack` does not contain `needle`.
54+
#[track_caller]
55+
pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
56+
let haystack = haystack.as_ref();
57+
let needle = needle.as_ref();
58+
if haystack.contains(needle) {
59+
SearchDetails { assertion_name: "assert_not_contains", haystack, needle }.dump();
60+
panic!("needle was unexpectedly found in haystack");
61+
}
62+
}
63+
64+
/// Assert that `haystack` contains the regex `needle`.
65+
#[track_caller]
66+
pub fn assert_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
67+
let haystack = haystack.as_ref();
68+
let needle = needle.as_ref();
69+
let re = regex::Regex::new(needle).unwrap();
70+
if !re.is_match(haystack) {
71+
SearchDetails { assertion_name: "assert_contains_regex", haystack, needle }.dump();
72+
panic!("regex was not found in haystack");
73+
}
74+
}
75+
76+
/// Assert that `haystack` does not contain the regex `needle`.
77+
#[track_caller]
78+
pub fn assert_not_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
79+
let haystack = haystack.as_ref();
80+
let needle = needle.as_ref();
81+
let re = regex::Regex::new(needle).unwrap();
82+
if re.is_match(haystack) {
83+
SearchDetails { assertion_name: "assert_not_contains_regex", haystack, needle }.dump();
84+
panic!("regex was unexpectedly found in haystack");
85+
}
86+
}
87+
88+
/// Assert that `haystack` contains regex `needle` an `expected_count` number of times.
89+
#[track_caller]
90+
pub fn assert_count_is<H: AsRef<str>, N: AsRef<str>>(
91+
expected_count: usize,
92+
haystack: H,
93+
needle: N,
94+
) {
95+
let haystack = haystack.as_ref();
96+
let needle = needle.as_ref();
97+
98+
let actual_count = haystack.matches(needle).count();
99+
if expected_count != actual_count {
100+
let count_fmt = format!(
101+
"assert_count_is (expected_count = {expected_count}, actual_count = {actual_count})"
102+
);
103+
SearchDetails { assertion_name: &count_fmt, haystack, needle }.dump();
104+
panic!(
105+
"regex did not appear {expected_count} times in haystack (expected_count = \
106+
{expected_count}, actual_count = {actual_count})"
107+
);
108+
}
109+
}
110+
111+
/// Assert that all files in `dir1` exist and have the same content in `dir2`
112+
// FIXME(#135037): not robust against symlinks, lacks sanity test coverage.
113+
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
114+
let dir2 = dir2.as_ref();
115+
fs::read_dir_entries(dir1, |entry_path| {
116+
let entry_name = entry_path.file_name().unwrap();
117+
if entry_path.is_dir() {
118+
assert_dirs_are_equal(&entry_path, &dir2.join(entry_name));
119+
} else {
120+
let path2 = dir2.join(entry_name);
121+
let file1 = fs::read(&entry_path);
122+
let file2 = fs::read(&path2);
123+
124+
// We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display.
125+
// Why not using String? Because there might be minified files or even potentially
126+
// binary ones, so that would display useless output.
127+
assert!(
128+
file1 == file2,
129+
"`{}` and `{}` have different content",
130+
entry_path.display(),
131+
path2.display(),
132+
);
133+
}
134+
});
135+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! Basic sanity checks for assertion helpers.
2+
use super::*;
3+
4+
mod test_assert_equals {
5+
use super::*;
6+
7+
#[test]
8+
fn assert_equals_same() {
9+
assert_equals("foo", "foo");
10+
assert_equals("", "");
11+
}
12+
13+
#[test]
14+
#[should_panic]
15+
fn assert_equals_different() {
16+
assert_equals("foo", "bar");
17+
}
18+
}
19+
20+
mod test_assert_contains {
21+
use super::*;
22+
23+
#[test]
24+
fn assert_contains_yes() {
25+
assert_contains("", "");
26+
assert_contains(" ", "");
27+
assert_contains("a", "a");
28+
assert_contains("ab", "a");
29+
}
30+
31+
#[test]
32+
#[should_panic]
33+
fn assert_contains_no() {
34+
assert_contains("a", "b");
35+
}
36+
}
37+
38+
mod test_assert_not_contains {
39+
use super::*;
40+
41+
#[test]
42+
fn assert_not_contains_yes() {
43+
assert_not_contains("a", "b");
44+
}
45+
46+
#[test]
47+
#[should_panic]
48+
fn assert_not_contains_no() {
49+
assert_not_contains(" ", "");
50+
}
51+
}
52+
53+
mod assert_contains_regex {
54+
use super::*;
55+
56+
#[test]
57+
fn assert_contains_regex_yes() {
58+
assert_contains_regex("", "");
59+
assert_contains_regex("", ".*");
60+
assert_contains_regex("abcde", ".*");
61+
assert_contains_regex("abcde", ".+");
62+
}
63+
64+
#[test]
65+
#[should_panic]
66+
fn assert_contains_regex_no() {
67+
assert_contains_regex("", ".+");
68+
}
69+
}
70+
71+
mod assert_not_contains_regex_regex {
72+
use super::*;
73+
74+
#[test]
75+
fn assert_not_contains_regex_yes() {
76+
assert_not_contains_regex("abc", "d");
77+
}
78+
79+
#[test]
80+
#[should_panic]
81+
fn assert_not_contains_regex_no() {
82+
assert_not_contains_regex("abc", ".*");
83+
}
84+
}
85+
86+
mod test_assert_count_is {
87+
use super::*;
88+
89+
#[test]
90+
fn assert_count_is_yes() {
91+
assert_count_is(0, "", "b");
92+
assert_count_is(3, "abcbdb", "b");
93+
}
94+
95+
#[test]
96+
#[should_panic]
97+
fn assert_count_is_no() {
98+
assert_count_is(2, "abcbdb", "b");
99+
}
100+
}

0 commit comments

Comments
 (0)