Skip to content

Commit

Permalink
compiletest: Remove/don't write empty 'expected' files
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Dec 27, 2024
1 parent 19e75f4 commit d04e480
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 35 deletions.
97 changes: 67 additions & 30 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fn remove_and_create_dir_all(path: &Path) {
fs::create_dir_all(path).unwrap();
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
struct TestCx<'test> {
config: &'test Config,
props: &'test TestProps,
Expand Down Expand Up @@ -2318,32 +2318,44 @@ impl<'test> TestCx<'test> {
match output_kind {
TestOutput::Compile => {
if !self.props.dont_check_compiler_stdout {
errors += self.compare_output(
if self
.compare_output(
stdout_kind,
&normalized_stdout,
&proc_res.stdout,
&expected_stdout,
)
.should_error()
{
errors += 1;
}
}
if !self.props.dont_check_compiler_stderr {
if self
.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
.should_error()
{
errors += 1;
}
}
}
TestOutput::Run => {
if self
.compare_output(
stdout_kind,
&normalized_stdout,
&proc_res.stdout,
&expected_stdout,
);
)
.should_error()
{
errors += 1;
}
if !self.props.dont_check_compiler_stderr {
errors += self.compare_output(
stderr_kind,
&normalized_stderr,
&stderr,
&expected_stderr,
);

if self.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr) {
errors += 1;
}
}
TestOutput::Run => {
errors += self.compare_output(
stdout_kind,
&normalized_stdout,
&proc_res.stdout,
&expected_stdout,
);
errors +=
self.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr);
}
}
errors
}
Expand Down Expand Up @@ -2570,21 +2582,29 @@ impl<'test> TestCx<'test> {
}
}

// Returns `true` if output differed and was not blessed
fn compare_output(
&self,
stream: &str,
actual: &str,
actual_unnormalized: &str,
expected: &str,
) -> usize {
) -> CompareOutcome {
let expected_path =
expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);

if self.config.bless && actual.is_empty() && expected_path.exists() {
self.delete_file(&expected_path);
}

let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
// FIXME: We ignore the first line of SVG files
// because the width parameter is non-deterministic.
(true, Some(nl_e), Some(nl_a)) => expected[nl_e..] != actual[nl_a..],
_ => expected != actual,
};
if !are_different {
return 0;
return CompareOutcome::Same;
}

// Wrapper tools set by `runner` might provide extra output on failure,
Expand All @@ -2600,7 +2620,7 @@ impl<'test> TestCx<'test> {
used.retain(|line| actual_lines.contains(line));
// check if `expected` contains a subset of the lines of `actual`
if used.len() == expected_lines.len() && (expected.is_empty() == actual.is_empty()) {
return 0;
return CompareOutcome::Same;
}
if expected_lines.is_empty() {
// if we have no lines to check, force a full overwite
Expand All @@ -2626,9 +2646,6 @@ impl<'test> TestCx<'test> {
}
println!("Saved the actual {stream} to {actual_path:?}");

let expected_path =
expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);

if !self.config.bless {
if expected.is_empty() {
println!("normalized {}:\n{}\n", stream, actual);
Expand All @@ -2651,15 +2668,17 @@ impl<'test> TestCx<'test> {
self.delete_file(&old);
}

if let Err(err) = fs::write(&expected_path, &actual) {
self.fatal(&format!("failed to write {stream} to `{expected_path:?}`: {err}"));
if !actual.is_empty() {
if let Err(err) = fs::write(&expected_path, &actual) {
self.fatal(&format!("failed to write {stream} to `{expected_path:?}`: {err}"));
}
println!("Blessing the {stream} of {test_name} in {expected_path:?}");
}
println!("Blessing the {stream} of {test_name} in {expected_path:?}");
}

println!("\nThe actual {0} differed from the expected {0}.", stream);

if self.config.bless { 0 } else { 1 }
if self.config.bless { CompareOutcome::Blessed } else { CompareOutcome::Differed }
}

/// Returns whether to show the full stderr/stdout.
Expand Down Expand Up @@ -2885,3 +2904,21 @@ enum AuxType {
Dylib,
ProcMacro,
}

/// Outcome of comparing a stream to a blessed file,
/// e.g. `.stderr` and `.fixed`.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum CompareOutcome {
/// Expected and actual outputs are the same
Same,
/// Outputs differed but were blessed
Blessed,
/// Outputs differed and an error should be emitted
Differed,
}

impl CompareOutcome {
fn should_error(&self) -> bool {
matches!(self, CompareOutcome::Differed)
}
}
9 changes: 5 additions & 4 deletions src/tools/compiletest/src/runtest/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::process::Command;

use glob::glob;

use super::CompareOutcome;
use crate::common::{UI_COVERAGE, UI_COVERAGE_MAP};
use crate::runtest::{Emit, ProcRes, TestCx, WillExecute};
use crate::util::static_regex;
Expand Down Expand Up @@ -46,9 +47,9 @@ impl<'test> TestCx<'test> {
&expected_coverage_dump,
);

if coverage_dump_errors > 0 {
if coverage_dump_errors.should_error() {
self.fatal_proc_rec(
&format!("{coverage_dump_errors} errors occurred comparing coverage output."),
&format!("an error occurred comparing coverage output."),
&proc_res,
);
}
Expand Down Expand Up @@ -146,9 +147,9 @@ impl<'test> TestCx<'test> {
&expected_coverage,
);

if coverage_errors > 0 {
if coverage_errors.should_error() {
self.fatal_proc_rec(
&format!("{} errors occurred comparing coverage output.", coverage_errors),
&format!("an error occurred comparing coverage output."),
&proc_res,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/tools/compiletest/src/runtest/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl TestCx<'_> {
)
});

errors += self.compare_output("fixed", &fixed_code, &fixed_code, &expected_fixed);
errors +=
self.compare_output("fixed", &fixed_code, &fixed_code, &expected_fixed) as usize;
} else if !expected_fixed.is_empty() {
panic!(
"the `//@ run-rustfix` directive wasn't found but a `*.fixed` \
Expand Down

0 comments on commit d04e480

Please sign in to comment.