Skip to content

Commit 942cf6c

Browse files
committed
compiletest: Refactor and document compare_output
1 parent 19e75f4 commit 942cf6c

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

src/tools/compiletest/src/runtest.rs

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,32 +2318,44 @@ 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.compare_output(stderr_kind, &normalized_stderr, &stderr, &expected_stderr) {
2356+
errors += 1;
23352357
}
23362358
}
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-
}
23472359
}
23482360
errors
23492361
}
@@ -2570,21 +2582,22 @@ impl<'test> TestCx<'test> {
25702582
}
25712583
}
25722584

2585+
// Returns `true` if output differed and was not blessed
25732586
fn compare_output(
25742587
&self,
25752588
stream: &str,
25762589
actual: &str,
25772590
actual_unnormalized: &str,
25782591
expected: &str,
2579-
) -> usize {
2592+
) -> bool {
25802593
let are_different = match (self.force_color_svg(), expected.find('\n'), actual.find('\n')) {
25812594
// FIXME: We ignore the first line of SVG files
25822595
// because the width parameter is non-deterministic.
25832596
(true, Some(nl_e), Some(nl_a)) => expected[nl_e..] != actual[nl_a..],
25842597
_ => expected != actual,
25852598
};
25862599
if !are_different {
2587-
return 0;
2600+
return CompareOutcome::Same;
25882601
}
25892602

25902603
// Wrapper tools set by `runner` might provide extra output on failure,
@@ -2600,7 +2613,7 @@ impl<'test> TestCx<'test> {
26002613
used.retain(|line| actual_lines.contains(line));
26012614
// check if `expected` contains a subset of the lines of `actual`
26022615
if used.len() == expected_lines.len() && (expected.is_empty() == actual.is_empty()) {
2603-
return 0;
2616+
return CompareOutcome::Same;
26042617
}
26052618
if expected_lines.is_empty() {
26062619
// if we have no lines to check, force a full overwite
@@ -2659,7 +2672,7 @@ impl<'test> TestCx<'test> {
26592672

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

2662-
if self.config.bless { 0 } else { 1 }
2675+
if self.config.bless { CompareOutcome::Blessed } else { CompareOutcome::Differed }
26632676
}
26642677

26652678
/// Returns whether to show the full stderr/stdout.
@@ -2885,3 +2898,21 @@ enum AuxType {
28852898
Dylib,
28862899
ProcMacro,
28872900
}
2901+
2902+
/// Outcome of comparing a stream to a blessed file,
2903+
/// e.g. `.stderr` and `.fixed`.
2904+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2905+
enum CompareOutcome {
2906+
/// Expected and actual outputs are the same
2907+
Same,
2908+
/// Outputs differed but were blessed
2909+
Blessed,
2910+
/// Outputs differed and an error should be emitted
2911+
Differed,
2912+
}
2913+
2914+
impl CompareOutcome {
2915+
fn should_error(&self) -> bool {
2916+
matches!(self, CompareOutcome::Differed)
2917+
}
2918+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::process::Command;
66

77
use glob::glob;
88

9+
use super::CompareOutcome;
910
use crate::common::{UI_COVERAGE, UI_COVERAGE_MAP};
1011
use crate::runtest::{Emit, ProcRes, TestCx, WillExecute};
1112
use crate::util::static_regex;
@@ -46,9 +47,9 @@ impl<'test> TestCx<'test> {
4647
&expected_coverage_dump,
4748
);
4849

49-
if coverage_dump_errors > 0 {
50+
if coverage_dump_errors.should_error() {
5051
self.fatal_proc_rec(
51-
&format!("{coverage_dump_errors} errors occurred comparing coverage output."),
52+
&format!("an error occurred comparing coverage output."),
5253
&proc_res,
5354
);
5455
}
@@ -146,9 +147,9 @@ impl<'test> TestCx<'test> {
146147
&expected_coverage,
147148
);
148149

149-
if coverage_errors > 0 {
150+
if coverage_errors.should_error() {
150151
self.fatal_proc_rec(
151-
&format!("{} errors occurred comparing coverage output.", coverage_errors),
152+
&format!("an error occurred comparing coverage output."),
152153
&proc_res,
153154
);
154155
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ impl TestCx<'_> {
100100
)
101101
});
102102

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

0 commit comments

Comments
 (0)