-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Rewrite the no-input-file.stderr
test in Rust and support diff
#124257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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>, | ||
expected_name: Option<String>, | ||
actual: Option<String>, | ||
actual_name: Option<String>, | ||
} | ||
|
||
impl Diff { | ||
/// Construct a bare `diff` invocation. | ||
pub fn new() -> Self { | ||
Self { expected: None, expected_name: None, actual: None, actual_name: 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 path = path.as_ref(); | ||
let content = std::fs::read_to_string(path).expect("failed to read file"); | ||
let name = path.to_string_lossy().to_string(); | ||
|
||
self.expected = Some(content); | ||
self.expected_name = Some(name); | ||
self | ||
} | ||
|
||
/// Specify the expected output for the diff from a given text string. | ||
pub fn expected_text<T: AsRef<[u8]>>(&mut self, name: &str, text: T) -> &mut Self { | ||
self.expected = Some(String::from_utf8_lossy(text.as_ref()).to_string()); | ||
self.expected_name = Some(name.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 path = path.as_ref(); | ||
let content = std::fs::read_to_string(path).expect("failed to read file"); | ||
let name = path.to_string_lossy().to_string(); | ||
|
||
self.actual = Some(content); | ||
self.actual_name = Some(name); | ||
self | ||
} | ||
|
||
/// Specify the actual output for the diff from a given text string. | ||
pub fn actual_text<T: AsRef<[u8]>>(&mut self, name: &str, text: T) -> &mut Self { | ||
self.actual = Some(String::from_utf8_lossy(text.as_ref()).to_string()); | ||
self.actual_name = Some(name.to_string()); | ||
self | ||
} | ||
|
||
/// Executes the diff process, prints any differences to the standard error. | ||
#[track_caller] | ||
pub fn run(&self) { | ||
let expected = self.expected.as_ref().expect("expected text not set"); | ||
let actual = self.actual.as_ref().expect("actual text not set"); | ||
let expected_name = self.expected_name.as_ref().unwrap(); | ||
let actual_name = self.actual_name.as_ref().unwrap(); | ||
|
||
let output = TextDiff::from_lines(expected, actual) | ||
.unified_diff() | ||
.header(expected_name, actual_name) | ||
.to_string(); | ||
|
||
if !output.is_empty() { | ||
panic!( | ||
"test failed: `{}` is different from `{}`\n\n{}", | ||
expected_name, actual_name, output | ||
) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||
#[cfg(test)] | ||||
mod tests { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the tests here will not be executed in CI for now (I don't know how to configure it). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that is fine for now, but if you want to add support for running the unit tests in
Modulo the unstable test feature part because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I filed issue #124267 to track this. |
||||
use crate::*; | ||||
|
||||
#[test] | ||||
fn test_diff() { | ||||
let expected = "foo\nbar\nbaz\n"; | ||||
let actual = "foo\nbar\nbaz\n"; | ||||
diff().expected_text("EXPECTED_TEXT", expected).actual_text("ACTUAL_TEXT", actual).run(); | ||||
} | ||||
|
||||
#[test] | ||||
fn test_should_panic() { | ||||
let expected = "foo\nbar\nbaz\n"; | ||||
let actual = "foo\nbaz\nbar\n"; | ||||
|
||||
let output = std::panic::catch_unwind(|| { | ||||
diff() | ||||
.expected_text("EXPECTED_TEXT", expected) | ||||
.actual_text("ACTUAL_TEXT", actual) | ||||
.run(); | ||||
}) | ||||
.unwrap_err(); | ||||
|
||||
let expected_output = "\ | ||||
test failed: `EXPECTED_TEXT` is different from `ACTUAL_TEXT` | ||||
|
||||
--- EXPECTED_TEXT | ||||
+++ ACTUAL_TEXT | ||||
@@ -1,3 +1,3 @@ | ||||
foo | ||||
+baz | ||||
bar | ||||
-baz | ||||
"; | ||||
|
||||
assert_eq!(output.downcast_ref::<String>().unwrap(), expected_output); | ||||
} | ||||
} |
This file was deleted.
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().print("crate-name").run_fail_assert_exit_code(1); | ||
|
||
diff().expected_file("no-input-file.stderr").actual_text("output", output.stderr).run(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you check if you can just use thediff
from the environment? As in, I think we already assumediff
exists:rust/tests/run-make/tools.mk
Lines 21 to 22 in 7bb4f08
If that's not available, then I'm open to addingsimilar
.EDIT: Now that I think about it, the easier to run the test the better. For example, I think
grep
isn't by default available on Windows? Would be nice to not have to rely on those external dependencies if possible/reasonable especially if they can have platform-specific differences and what not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so too.