Skip to content

Commit

Permalink
Rewrite no-input-file.stderr test in Rust and support diff
Browse files Browse the repository at this point in the history
  • Loading branch information
JoverZhang committed Apr 22, 2024
1 parent fb89862 commit be5e647
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3343,6 +3343,7 @@ version = "0.0.0"
dependencies = [
"object 0.34.0",
"regex",
"similar",
"wasmparser",
]

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

[[package]]
name = "similar"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa42c91313f1d05da9b26f267f931cf178d4aba455b4c4622dd7355eb80c6640"

[[package]]
name = "siphasher"
version = "0.3.11"
Expand Down
1 change: 1 addition & 0 deletions src/tools/run-make-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ edition = "2021"

[dependencies]
object = "0.34.0"
similar = "2.5.0"
wasmparser = "0.118.2"
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
64 changes: 64 additions & 0 deletions src/tools/run-make-support/src/diff/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use similar::TextDiff;
use std::path::Path;

#[cfg(test)]
mod tests;

pub fn diff() -> Diff {
Diff::new()
}

#[derive(Debug)]
pub struct Diff {
expected: Option<String>,
actual: Option<String>,
}

impl Diff {
/// Construct a bare `diff` invocation.
pub fn new() -> Self {
Self { expected: None, actual: None }
}

/// Specify the expected output for the diff from a file.
pub fn expected_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
let content = std::fs::read_to_string(path).expect("failed to read file");
self.expected = Some(content);
self
}

/// Specify the expected output for the diff from a given text string.
pub fn expected_text<T: AsRef<[u8]>>(&mut self, text: T) -> &mut Self {
self.expected = Some(String::from_utf8_lossy(text.as_ref()).to_string());
self
}

/// Specify the actual output for the diff from a file.
pub fn actual_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
let content = std::fs::read_to_string(path).expect("failed to read file");
self.actual = Some(content);
self
}

/// Specify the actual output for the diff from a given text string.
pub fn actual_text<T: AsRef<[u8]>>(&mut self, text: T) -> &mut Self {
self.actual = Some(String::from_utf8_lossy(text.as_ref()).to_string());
self
}

/// Executes the diff process, prints any differences to the standard error.
pub fn run(&self) {
let output = self.do_run();
if !output.is_empty() {
panic!("{}", output)
}
}

/// Compares the expected and actual texts and generates a diff output as a string.
fn do_run(&self) -> String {
let expected = self.expected.as_ref().expect("expected text not set");
let actual = self.actual.as_ref().expect("actual text not set");

TextDiff::from_lines(expected, actual).unified_diff().header("expect", "actual").to_string()
}
}
38 changes: 38 additions & 0 deletions src/tools/run-make-support/src/diff/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_diff() {
let expected = "foo\nbar\nbaz\n";
let actual = "foo\nbar\nbaz\n";
diff().expected_text(expected).actual_text(actual).run();
}

#[test]
#[should_panic]
fn test_do_run() {
let expected = "foo\nbar\nbaz\n";
let actual = "foo\nbar\nqux\n";
diff().expected_text(expected).actual_text(actual).run();
}

#[test]
fn test_do_run_fail() {
let expected = "foo\nbar\nbaz\n";
let actual = "foo\nbaz\nbar\n";
let output = diff().expected_text(expected).actual_text(actual).do_run();

assert_eq!(
output,
r#"--- expect
+++ actual
@@ -1,3 +1,3 @@
foo
+baz
bar
-baz
"#,
);
}
}
2 changes: 2 additions & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

pub mod cc;
pub mod clang;
pub mod diff;
pub mod llvm_readobj;
pub mod run;
pub mod rustc;
Expand All @@ -20,6 +21,7 @@ pub use wasmparser;

pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
pub use clang::{clang, Clang};
pub use diff::{diff, Diff};
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
pub use run::{run, run_fail};
pub use rustc::{aux_build, rustc, Rustc};
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ run-make/no-builtins-attribute/Makefile
run-make/no-builtins-lto/Makefile
run-make/no-cdylib-as-rdylib/Makefile
run-make/no-duplicate-libs/Makefile
run-make/no-input-file/Makefile
run-make/no-intermediate-extras/Makefile
run-make/obey-crate-type-flag/Makefile
run-make/optimization-remarks-dir-pgo/Makefile
Expand Down
4 changes: 0 additions & 4 deletions tests/run-make/no-input-file/Makefile

This file was deleted.

9 changes: 9 additions & 0 deletions tests/run-make/no-input-file/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern crate run_make_support;

use run_make_support::{diff, rustc};

fn main() {
let output = rustc().arg("--print").arg("crate-name").run_fail();

diff().expected_file("no-input-file.stderr").actual_text(output.stderr).run();
}

0 comments on commit be5e647

Please sign in to comment.