Skip to content

Commit 36066d8

Browse files
committed
UI tests extract the regular output from the 'rendered' field in json
1 parent f7361a8 commit 36066d8

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/tools/compiletest/src/json.rs

+19
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ struct DiagnosticCode {
5757
explanation: Option<String>,
5858
}
5959

60+
pub fn extract_rendered(output: &str, proc_res: &ProcRes) -> String {
61+
output.lines()
62+
.filter_map(|line| if line.starts_with('{') {
63+
match json::decode::<Diagnostic>(line) {
64+
Ok(diagnostic) => diagnostic.rendered,
65+
Err(error) => {
66+
proc_res.fatal(Some(&format!("failed to decode compiler output as json: \
67+
`{}`\noutput: {}\nline: {}",
68+
error,
69+
line,
70+
output)));
71+
}
72+
}
73+
} else {
74+
None
75+
})
76+
.collect()
77+
}
78+
6079
pub fn parse_output(file_name: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
6180
output.lines()
6281
.flat_map(|line| parse_line(file_name, line, output, proc_res))

src/tools/compiletest/src/runtest.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,11 @@ actual:\n\
14031403
rustc.args(&["--error-format", "json"]);
14041404
}
14051405
}
1406+
Ui => {
1407+
if !self.props.compile_flags.iter().any(|s| s.starts_with("--error-format")) {
1408+
rustc.args(&["--error-format", "json"]);
1409+
}
1410+
}
14061411
MirOpt => {
14071412
rustc.args(&[
14081413
"-Zdump-mir=all",
@@ -1427,7 +1432,6 @@ actual:\n\
14271432
Codegen |
14281433
Rustdoc |
14291434
RunMake |
1430-
Ui |
14311435
CodegenUnits => {
14321436
// do not use JSON output
14331437
}
@@ -2211,7 +2215,12 @@ actual:\n\
22112215
}
22122216

22132217
fn run_ui_test(&self) {
2214-
let proc_res = self.compile_test();
2218+
// if the user specified a format in the ui test
2219+
// print the output to the stderr file, otherwise extract
2220+
// the rendered error messages from json and print them
2221+
let explicit = self.props.compile_flags.iter().any(|s| s.starts_with("--error-format"));
2222+
2223+
let mut proc_res = self.compile_test();
22152224

22162225
let expected_stderr_path = self.expected_output_path("stderr");
22172226
let expected_stderr = self.load_expected_output(&expected_stderr_path);
@@ -2220,14 +2229,24 @@ actual:\n\
22202229
let expected_stdout = self.load_expected_output(&expected_stdout_path);
22212230

22222231
let normalized_stdout =
2223-
self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout);
2232+
self.normalize_output(&proc_res.stdout, &self.props.normalize_stdout, explicit);
2233+
2234+
let stderr = if explicit {
2235+
proc_res.stderr.clone()
2236+
} else {
2237+
json::extract_rendered(&proc_res.stderr, &proc_res)
2238+
};
2239+
22242240
let normalized_stderr =
2225-
self.normalize_output(&proc_res.stderr, &self.props.normalize_stderr);
2241+
self.normalize_output(&stderr, &self.props.normalize_stderr, explicit);
22262242

22272243
let mut errors = 0;
22282244
errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
22292245
errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
22302246

2247+
// rewrite the output to the human readable one (shown in case of errors)
2248+
proc_res.stderr = normalized_stderr;
2249+
22312250
if errors > 0 {
22322251
println!("To update references, run this command from build directory:");
22332252
let relative_path_to_file =
@@ -2421,11 +2440,13 @@ actual:\n\
24212440
mir_dump_dir
24222441
}
24232442

2424-
fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
2443+
fn normalize_output(
2444+
&self,
2445+
output: &str,
2446+
custom_rules: &[(String, String)],
2447+
json: bool,
2448+
) -> String {
24252449
let parent_dir = self.testpaths.file.parent().unwrap();
2426-
let cflags = self.props.compile_flags.join(" ");
2427-
let json = cflags.contains("--error-format json") ||
2428-
cflags.contains("--error-format pretty-json");
24292450
let parent_dir_str = if json {
24302451
parent_dir.display().to_string().replace("\\", "\\\\")
24312452
} else {

0 commit comments

Comments
 (0)