Skip to content

Commit 5bb727a

Browse files
committed
compiletest: Remove/don't write empty 'expected' files
1 parent 19e75f4 commit 5bb727a

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

src/tools/compiletest/src/runtest.rs

+69-30
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn remove_and_create_dir_all(path: &Path) {
213213
fs::create_dir_all(path).unwrap();
214214
}
215215

216-
#[derive(Copy, Clone)]
216+
#[derive(Copy, Clone, Debug)]
217217
struct TestCx<'test> {
218218
config: &'test Config,
219219
props: &'test TestProps,
@@ -2318,32 +2318,47 @@ impl<'test> TestCx<'test> {
23182318
match output_kind {
23192319
TestOutput::Compile => {
23202320
if !self.props.dont_check_compiler_stdout {
2321-
errors += self.compare_output(
2321+
if self
2322+
.compare_output(
2323+
stdout_kind,
2324+
&normalized_stdout,
2325+
&proc_res.stdout,
2326+
&expected_stdout,
2327+
)
2328+
.should_error()
2329+
{
2330+
errors += 1;
2331+
}
2332+
}
2333+
if !self.props.dont_check_compiler_stderr {
2334+
if self
2335+
.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2336+
.should_error()
2337+
{
2338+
errors += 1;
2339+
}
2340+
}
2341+
}
2342+
TestOutput::Run => {
2343+
if self
2344+
.compare_output(
23222345
stdout_kind,
23232346
&normalized_stdout,
23242347
&proc_res.stdout,
23252348
&expected_stdout,
2326-
);
2349+
)
2350+
.should_error()
2351+
{
2352+
errors += 1;
23272353
}
2328-
if !self.props.dont_check_compiler_stderr {
2329-
errors += self.compare_output(
2330-
stderr_kind,
2331-
&normalized_stderr,
2332-
&stderr,
2333-
&expected_stderr,
2334-
);
2354+
2355+
if self
2356+
.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr)
2357+
.should_error()
2358+
{
2359+
errors += 1;
23352360
}
23362361
}
2337-
TestOutput::Run => {
2338-
errors += self.compare_output(
2339-
stdout_kind,
2340-
&normalized_stdout,
2341-
&proc_res.stdout,
2342-
&expected_stdout,
2343-
);
2344-
errors +=
2345-
self.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr);
2346-
}
23472362
}
23482363
errors
23492364
}
@@ -2576,15 +2591,22 @@ impl<'test> TestCx<'test> {
25762591
actual: &str,
25772592
actual_unnormalized: &str,
25782593
expected: &str,
2579-
) -> usize {
2594+
) -> CompareOutcome {
2595+
let expected_path =
2596+
expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);
2597+
2598+
if self.config.bless && actual.is_empty() && expected_path.exists() {
2599+
self.delete_file(&expected_path);
2600+
}
2601+
25802602
let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
25812603
// FIXME: We ignore the first line of SVG files
25822604
// because the width parameter is non-deterministic.
25832605
(true, Some(nl_e), Some(nl_a)) => expected[nl_e..] != actual[nl_a..],
25842606
_ => expected != actual,
25852607
};
25862608
if !are_different {
2587-
return 0;
2609+
return CompareOutcome::Same;
25882610
}
25892611

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

2629-
let expected_path =
2630-
expected_output_path(self.testpaths, self.revision, &self.config.compare_mode, stream);
2631-
26322651
if !self.config.bless {
26332652
if expected.is_empty() {
26342653
println!("normalized {}:\n{}\n", stream, actual);
@@ -2651,15 +2670,17 @@ impl<'test> TestCx<'test> {
26512670
self.delete_file(&old);
26522671
}
26532672

2654-
if let Err(err) = fs::write(&expected_path, &actual) {
2655-
self.fatal(&format!("failed to write {stream} to `{expected_path:?}`: {err}"));
2673+
if !actual.is_empty() {
2674+
if let Err(err) = fs::write(&expected_path, &actual) {
2675+
self.fatal(&format!("failed to write {stream} to `{expected_path:?}`: {err}"));
2676+
}
2677+
println!("Blessing the {stream} of {test_name} in {expected_path:?}");
26562678
}
2657-
println!("Blessing the {stream} of {test_name} in {expected_path:?}");
26582679
}
26592680

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

2662-
if self.config.bless { 0 } else { 1 }
2683+
if self.config.bless { CompareOutcome::Blessed } else { CompareOutcome::Differed }
26632684
}
26642685

26652686
/// Returns whether to show the full stderr/stdout.
@@ -2885,3 +2906,21 @@ enum AuxType {
28852906
Dylib,
28862907
ProcMacro,
28872908
}
2909+
2910+
/// Outcome of comparing a stream to a blessed file,
2911+
/// e.g. `.stderr` and `.fixed`.
2912+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2913+
enum CompareOutcome {
2914+
/// Expected and actual outputs are the same
2915+
Same,
2916+
/// Outputs differed but were blessed
2917+
Blessed,
2918+
/// Outputs differed and an error should be emitted
2919+
Differed,
2920+
}
2921+
2922+
impl CompareOutcome {
2923+
fn should_error(&self) -> bool {
2924+
matches!(self, CompareOutcome::Differed)
2925+
}
2926+
}

src/tools/compiletest/src/runtest/coverage.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ impl<'test> TestCx<'test> {
3939
let expected_coverage_dump = self.load_expected_output(kind);
4040
let actual_coverage_dump = self.normalize_output(&proc_res.stdout, &[]);
4141

42-
let coverage_dump_errors = self.compare_output(
42+
let coverage_dump_compare_outcome = self.compare_output(
4343
kind,
4444
&actual_coverage_dump,
4545
&proc_res.stdout,
4646
&expected_coverage_dump,
4747
);
4848

49-
if coverage_dump_errors > 0 {
49+
if coverage_dump_compare_outcome.should_error() {
5050
self.fatal_proc_rec(
51-
&format!("{coverage_dump_errors} errors occurred comparing coverage output."),
51+
&format!("an error occurred comparing coverage output."),
5252
&proc_res,
5353
);
5454
}
@@ -139,16 +139,16 @@ impl<'test> TestCx<'test> {
139139
self.fatal_proc_rec(&err, &proc_res);
140140
});
141141

142-
let coverage_errors = self.compare_output(
142+
let coverage_dump_compare_outcome = self.compare_output(
143143
kind,
144144
&normalized_actual_coverage,
145145
&proc_res.stdout,
146146
&expected_coverage,
147147
);
148148

149-
if coverage_errors > 0 {
149+
if coverage_dump_compare_outcome.should_error() {
150150
self.fatal_proc_rec(
151-
&format!("{} errors occurred comparing coverage output.", coverage_errors),
151+
&format!("an error occurred comparing coverage output."),
152152
&proc_res,
153153
);
154154
}

src/tools/compiletest/src/runtest/ui.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ impl TestCx<'_> {
100100
)
101101
});
102102

103-
errors += self.compare_output("fixed", &fixed_code, &fixed_code, &expected_fixed);
103+
if self
104+
.compare_output("fixed", &fixed_code, &fixed_code, &expected_fixed)
105+
.should_error()
106+
{
107+
errors += 1;
108+
}
104109
} else if !expected_fixed.is_empty() {
105110
panic!(
106111
"the `//@ run-rustfix` directive wasn't found but a `*.fixed` \

0 commit comments

Comments
 (0)