Skip to content

Commit be5e647

Browse files
committed
Rewrite no-input-file.stderr test in Rust and support diff
1 parent fb89862 commit be5e647

File tree

8 files changed

+121
-5
lines changed

8 files changed

+121
-5
lines changed

Cargo.lock

+7
Original file line numberDiff line numberDiff line change
@@ -3343,6 +3343,7 @@ version = "0.0.0"
33433343
dependencies = [
33443344
"object 0.34.0",
33453345
"regex",
3346+
"similar",
33463347
"wasmparser",
33473348
]
33483349

@@ -5138,6 +5139,12 @@ version = "1.3.0"
51385139
source = "registry+https://github.com/rust-lang/crates.io-index"
51395140
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
51405141

5142+
[[package]]
5143+
name = "similar"
5144+
version = "2.5.0"
5145+
source = "registry+https://github.com/rust-lang/crates.io-index"
5146+
checksum = "fa42c91313f1d05da9b26f267f931cf178d4aba455b4c4622dd7355eb80c6640"
5147+
51415148
[[package]]
51425149
name = "siphasher"
51435150
version = "0.3.11"

src/tools/run-make-support/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
object = "0.34.0"
8+
similar = "2.5.0"
89
wasmparser = "0.118.2"
910
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use similar::TextDiff;
2+
use std::path::Path;
3+
4+
#[cfg(test)]
5+
mod tests;
6+
7+
pub fn diff() -> Diff {
8+
Diff::new()
9+
}
10+
11+
#[derive(Debug)]
12+
pub struct Diff {
13+
expected: Option<String>,
14+
actual: Option<String>,
15+
}
16+
17+
impl Diff {
18+
/// Construct a bare `diff` invocation.
19+
pub fn new() -> Self {
20+
Self { expected: None, actual: None }
21+
}
22+
23+
/// Specify the expected output for the diff from a file.
24+
pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
25+
let content = std::fs::read_to_string(path).expect("failed to read file");
26+
self.expected = Some(content);
27+
self
28+
}
29+
30+
/// Specify the expected output for the diff from a given text string.
31+
pub fn expected_text<T: AsRef<[u8]>>(&mut self, text: T) -> &mut Self {
32+
self.expected = Some(String::from_utf8_lossy(text.as_ref()).to_string());
33+
self
34+
}
35+
36+
/// Specify the actual output for the diff from a file.
37+
pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
38+
let content = std::fs::read_to_string(path).expect("failed to read file");
39+
self.actual = Some(content);
40+
self
41+
}
42+
43+
/// Specify the actual output for the diff from a given text string.
44+
pub fn actual_text<T: AsRef<[u8]>>(&mut self, text: T) -> &mut Self {
45+
self.actual = Some(String::from_utf8_lossy(text.as_ref()).to_string());
46+
self
47+
}
48+
49+
/// Executes the diff process, prints any differences to the standard error.
50+
pub fn run(&self) {
51+
let output = self.do_run();
52+
if !output.is_empty() {
53+
panic!("{}", output)
54+
}
55+
}
56+
57+
/// Compares the expected and actual texts and generates a diff output as a string.
58+
fn do_run(&self) -> String {
59+
let expected = self.expected.as_ref().expect("expected text not set");
60+
let actual = self.actual.as_ref().expect("actual text not set");
61+
62+
TextDiff::from_lines(expected, actual).unified_diff().header("expect", "actual").to_string()
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use super::*;
4+
5+
#[test]
6+
fn test_diff() {
7+
let expected = "foo\nbar\nbaz\n";
8+
let actual = "foo\nbar\nbaz\n";
9+
diff().expected_text(expected).actual_text(actual).run();
10+
}
11+
12+
#[test]
13+
#[should_panic]
14+
fn test_do_run() {
15+
let expected = "foo\nbar\nbaz\n";
16+
let actual = "foo\nbar\nqux\n";
17+
diff().expected_text(expected).actual_text(actual).run();
18+
}
19+
20+
#[test]
21+
fn test_do_run_fail() {
22+
let expected = "foo\nbar\nbaz\n";
23+
let actual = "foo\nbaz\nbar\n";
24+
let output = diff().expected_text(expected).actual_text(actual).do_run();
25+
26+
assert_eq!(
27+
output,
28+
r#"--- expect
29+
+++ actual
30+
@@ -1,3 +1,3 @@
31+
foo
32+
+baz
33+
bar
34+
-baz
35+
"#,
36+
);
37+
}
38+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
pub mod cc;
77
pub mod clang;
8+
pub mod diff;
89
pub mod llvm_readobj;
910
pub mod run;
1011
pub mod rustc;
@@ -20,6 +21,7 @@ pub use wasmparser;
2021

2122
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
2223
pub use clang::{clang, Clang};
24+
pub use diff::{diff, Diff};
2325
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
2426
pub use run::{run, run_fail};
2527
pub use rustc::{aux_build, rustc, Rustc};

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ run-make/no-builtins-attribute/Makefile
189189
run-make/no-builtins-lto/Makefile
190190
run-make/no-cdylib-as-rdylib/Makefile
191191
run-make/no-duplicate-libs/Makefile
192-
run-make/no-input-file/Makefile
193192
run-make/no-intermediate-extras/Makefile
194193
run-make/obey-crate-type-flag/Makefile
195194
run-make/optimization-remarks-dir-pgo/Makefile

tests/run-make/no-input-file/Makefile

-4
This file was deleted.

tests/run-make/no-input-file/rmake.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{diff, rustc};
4+
5+
fn main() {
6+
let output = rustc().arg("--print").arg("crate-name").run_fail();
7+
8+
diff().expected_file("no-input-file.stderr").actual_text(output.stderr).run();
9+
}

0 commit comments

Comments
 (0)