Skip to content

Commit c8e3bbf

Browse files
committed
compiletest: Add support for //@ check-run-stdout-is-json-lines
1 parent f1770df commit c8e3bbf

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/tools/compiletest/src/header.rs

+13
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ pub struct TestProps {
129129
pub check_stdout: bool,
130130
// Check stdout & stderr for output of run-pass test
131131
pub check_run_results: bool,
132+
/// Check that stdout from running the binary is legal JSON Lines
133+
/// (i.e. each line is well-formed JSON).
134+
///
135+
/// Has no effect in tests that don't run the compiled binary.
136+
pub check_run_stdout_is_json_lines: bool,
132137
// For UI tests, allows compiler to generate arbitrary output to stdout
133138
pub dont_check_compiler_stdout: bool,
134139
// For UI tests, allows compiler to generate arbitrary output to stderr
@@ -226,6 +231,7 @@ mod directives {
226231
pub const FORCE_HOST: &'static str = "force-host";
227232
pub const CHECK_STDOUT: &'static str = "check-stdout";
228233
pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
234+
pub const CHECK_RUN_STDOUT_IS_JSON_LINES: &'static str = "check-run-stdout-is-json-lines";
229235
pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
230236
pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
231237
pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
@@ -285,6 +291,7 @@ impl TestProps {
285291
force_host: false,
286292
check_stdout: false,
287293
check_run_results: false,
294+
check_run_stdout_is_json_lines: false,
288295
dont_check_compiler_stdout: false,
289296
dont_check_compiler_stderr: false,
290297
compare_output_lines_by_subset: false,
@@ -423,6 +430,11 @@ impl TestProps {
423430
DONT_CHECK_COMPILER_STDOUT,
424431
&mut self.dont_check_compiler_stdout,
425432
);
433+
config.set_name_directive(
434+
ln,
435+
CHECK_RUN_STDOUT_IS_JSON_LINES,
436+
&mut self.check_run_stdout_is_json_lines,
437+
);
426438
config.set_name_directive(
427439
ln,
428440
DONT_CHECK_COMPILER_STDERR,
@@ -739,6 +751,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
739751
"check-fail",
740752
"check-pass",
741753
"check-run-results",
754+
"check-run-stdout-is-json-lines",
742755
"check-stdout",
743756
"check-test-line-numbers-match",
744757
"compare-output-lines-by-subset",

src/tools/compiletest/src/runtest.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3940,6 +3940,17 @@ impl<'test> TestCx<'test> {
39403940
self.fatal_proc_rec("test run succeeded!", &proc_res);
39413941
}
39423942

3943+
if self.props.check_run_stdout_is_json_lines {
3944+
for (line, n) in proc_res.stdout.lines().zip(1..) {
3945+
if serde_json::Value::from_str(line).is_err() {
3946+
self.fatal_proc_rec(
3947+
&format!("invalid JSON on line {n} of stdout: {line:?}"),
3948+
&proc_res,
3949+
);
3950+
}
3951+
}
3952+
}
3953+
39433954
if !self.props.error_patterns.is_empty() || !self.props.regex_error_patterns.is_empty()
39443955
{
39453956
// "// error-pattern" comments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ run-pass
2+
//@ check-run-stdout-is-json-lines
3+
4+
//@ revisions: good bad_list bad_obj bad_empty bad_ws
5+
//@ [bad_list] should-fail
6+
//@ [bad_obj] should-fail
7+
//@ [bad_empty] should-fail
8+
//@ [bad_ws] should-fail
9+
10+
// Check that `//@ check-run-stdout-is-json-lines` allows valid JSON lines and
11+
// rejects invalid JSON lines, even without `//@ check-run-results`.
12+
13+
fn main() {
14+
println!("true");
15+
println!(r#"[ "this is valid json" ]"#);
16+
println!(r#"{{ "key": "this is valid json" }}"#);
17+
18+
if cfg!(bad_list) {
19+
println!(r#"[ "this is invalid json", ]"#);
20+
}
21+
if cfg!(bad_obj) {
22+
println!(r#"{{ "key": "this is invalid json", }}"#);
23+
}
24+
25+
// Every line must be valid JSON, and a blank or whitespace-only string is
26+
// not valid JSON.
27+
if cfg!(bad_empty) {
28+
println!();
29+
}
30+
if cfg!(bad_ws) {
31+
println!(" \t \t ");
32+
}
33+
}

0 commit comments

Comments
 (0)