Skip to content

Commit 3740ba2

Browse files
committed
Auto merge of rust-lang#84863 - ABouttefeux:libtest, r=m-ou-se
Show test type during prints Test output can sometimes be confusing. For example doctest with the no_run argument are displayed the same way than test that are run. During rust-lang#83857 I got the feedback that test output can be confusing. For the moment test output is ``` test $DIR/test-type.rs - f (line 12) ... ignored test $DIR/test-type.rs - f (line 15) ... ok test $DIR/test-type.rs - f (line 21) ... ok test $DIR/test-type.rs - f (line 6) ... ok ``` I propose to change output by indicating the test type as ``` test $DIR/test-type.rs - f (line 12) ... ignored test $DIR/test-type.rs - f (line 15) - compile ... ok test $DIR/test-type.rs - f (line 21) - compile fail ... ok test $DIR/test-type.rs - f (line 6) ... ok ``` by indicating the test type after the test name (and in the case of doctest after the function name and line) and before the "...". ------------ Note: this is a proof of concept, the implementation is probably not optimal as the properties added in `TestDesc` are only use in the display and does not represent actual change of behavior, maybe `TestType::DocTest` could have fields
2 parents 9a57617 + 6de13c3 commit 3740ba2

18 files changed

+219
-18
lines changed

compiler/rustc_builtin_macros/src/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ pub fn expand_test_or_bench(
254254
"allow_fail",
255255
cx.expr_bool(sp, should_fail(&cx.sess, &item)),
256256
),
257+
// compile_fail: true | false
258+
field("compile_fail", cx.expr_bool(sp, false)),
259+
// no_run: true | false
260+
field("no_run", cx.expr_bool(sp, false)),
257261
// should_panic: ...
258262
field(
259263
"should_panic",

library/test/src/formatters/pretty.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ impl<T: Write> PrettyFormatter<T> {
169169

170170
fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
171171
let name = desc.padded_name(self.max_name_len, desc.name.padding());
172-
self.write_plain(&format!("test {} ... ", name))?;
172+
if let Some(test_mode) = desc.test_mode() {
173+
self.write_plain(&format!("test {} - {} ... ", name, test_mode))?;
174+
} else {
175+
self.write_plain(&format!("test {} ... ", name))?;
176+
}
173177

174178
Ok(())
175179
}

library/test/src/formatters/terse.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ impl<T: Write> TerseFormatter<T> {
158158

159159
fn write_test_name(&mut self, desc: &TestDesc) -> io::Result<()> {
160160
let name = desc.padded_name(self.max_name_len, desc.name.padding());
161-
self.write_plain(&format!("test {} ... ", name))?;
161+
if let Some(test_mode) = desc.test_mode() {
162+
self.write_plain(&format!("test {} - {} ... ", name, test_mode))?;
163+
} else {
164+
self.write_plain(&format!("test {} ... ", name))?;
165+
}
162166

163167
Ok(())
164168
}

library/test/src/tests.rs

+76
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
6161
ignore: true,
6262
should_panic: ShouldPanic::No,
6363
allow_fail: false,
64+
#[cfg(not(bootstrap))]
65+
compile_fail: false,
66+
#[cfg(not(bootstrap))]
67+
no_run: false,
6468
test_type: TestType::Unknown,
6569
},
6670
testfn: DynTestFn(Box::new(move || {})),
@@ -71,6 +75,10 @@ fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
7175
ignore: false,
7276
should_panic: ShouldPanic::No,
7377
allow_fail: false,
78+
#[cfg(not(bootstrap))]
79+
compile_fail: false,
80+
#[cfg(not(bootstrap))]
81+
no_run: false,
7482
test_type: TestType::Unknown,
7583
},
7684
testfn: DynTestFn(Box::new(move || {})),
@@ -89,6 +97,10 @@ pub fn do_not_run_ignored_tests() {
8997
ignore: true,
9098
should_panic: ShouldPanic::No,
9199
allow_fail: false,
100+
#[cfg(not(bootstrap))]
101+
compile_fail: false,
102+
#[cfg(not(bootstrap))]
103+
no_run: false,
92104
test_type: TestType::Unknown,
93105
},
94106
testfn: DynTestFn(Box::new(f)),
@@ -108,6 +120,10 @@ pub fn ignored_tests_result_in_ignored() {
108120
ignore: true,
109121
should_panic: ShouldPanic::No,
110122
allow_fail: false,
123+
#[cfg(not(bootstrap))]
124+
compile_fail: false,
125+
#[cfg(not(bootstrap))]
126+
no_run: false,
111127
test_type: TestType::Unknown,
112128
},
113129
testfn: DynTestFn(Box::new(f)),
@@ -131,6 +147,10 @@ fn test_should_panic() {
131147
ignore: false,
132148
should_panic: ShouldPanic::Yes,
133149
allow_fail: false,
150+
#[cfg(not(bootstrap))]
151+
compile_fail: false,
152+
#[cfg(not(bootstrap))]
153+
no_run: false,
134154
test_type: TestType::Unknown,
135155
},
136156
testfn: DynTestFn(Box::new(f)),
@@ -154,6 +174,10 @@ fn test_should_panic_good_message() {
154174
ignore: false,
155175
should_panic: ShouldPanic::YesWithMessage("error message"),
156176
allow_fail: false,
177+
#[cfg(not(bootstrap))]
178+
compile_fail: false,
179+
#[cfg(not(bootstrap))]
180+
no_run: false,
157181
test_type: TestType::Unknown,
158182
},
159183
testfn: DynTestFn(Box::new(f)),
@@ -182,6 +206,10 @@ fn test_should_panic_bad_message() {
182206
ignore: false,
183207
should_panic: ShouldPanic::YesWithMessage(expected),
184208
allow_fail: false,
209+
#[cfg(not(bootstrap))]
210+
compile_fail: false,
211+
#[cfg(not(bootstrap))]
212+
no_run: false,
185213
test_type: TestType::Unknown,
186214
},
187215
testfn: DynTestFn(Box::new(f)),
@@ -214,6 +242,10 @@ fn test_should_panic_non_string_message_type() {
214242
ignore: false,
215243
should_panic: ShouldPanic::YesWithMessage(expected),
216244
allow_fail: false,
245+
#[cfg(not(bootstrap))]
246+
compile_fail: false,
247+
#[cfg(not(bootstrap))]
248+
no_run: false,
217249
test_type: TestType::Unknown,
218250
},
219251
testfn: DynTestFn(Box::new(f)),
@@ -238,6 +270,10 @@ fn test_should_panic_but_succeeds() {
238270
ignore: false,
239271
should_panic,
240272
allow_fail: false,
273+
#[cfg(not(bootstrap))]
274+
compile_fail: false,
275+
#[cfg(not(bootstrap))]
276+
no_run: false,
241277
test_type: TestType::Unknown,
242278
},
243279
testfn: DynTestFn(Box::new(f)),
@@ -270,6 +306,10 @@ fn report_time_test_template(report_time: bool) -> Option<TestExecTime> {
270306
ignore: false,
271307
should_panic: ShouldPanic::No,
272308
allow_fail: false,
309+
#[cfg(not(bootstrap))]
310+
compile_fail: false,
311+
#[cfg(not(bootstrap))]
312+
no_run: false,
273313
test_type: TestType::Unknown,
274314
},
275315
testfn: DynTestFn(Box::new(f)),
@@ -303,6 +343,10 @@ fn time_test_failure_template(test_type: TestType) -> TestResult {
303343
ignore: false,
304344
should_panic: ShouldPanic::No,
305345
allow_fail: false,
346+
#[cfg(not(bootstrap))]
347+
compile_fail: false,
348+
#[cfg(not(bootstrap))]
349+
no_run: false,
306350
test_type,
307351
},
308352
testfn: DynTestFn(Box::new(f)),
@@ -340,6 +384,10 @@ fn typed_test_desc(test_type: TestType) -> TestDesc {
340384
ignore: false,
341385
should_panic: ShouldPanic::No,
342386
allow_fail: false,
387+
#[cfg(not(bootstrap))]
388+
compile_fail: false,
389+
#[cfg(not(bootstrap))]
390+
no_run: false,
343391
test_type,
344392
}
345393
}
@@ -451,6 +499,10 @@ pub fn exclude_should_panic_option() {
451499
ignore: false,
452500
should_panic: ShouldPanic::Yes,
453501
allow_fail: false,
502+
#[cfg(not(bootstrap))]
503+
compile_fail: false,
504+
#[cfg(not(bootstrap))]
505+
no_run: false,
454506
test_type: TestType::Unknown,
455507
},
456508
testfn: DynTestFn(Box::new(move || {})),
@@ -473,6 +525,10 @@ pub fn exact_filter_match() {
473525
ignore: false,
474526
should_panic: ShouldPanic::No,
475527
allow_fail: false,
528+
#[cfg(not(bootstrap))]
529+
compile_fail: false,
530+
#[cfg(not(bootstrap))]
531+
no_run: false,
476532
test_type: TestType::Unknown,
477533
},
478534
testfn: DynTestFn(Box::new(move || {})),
@@ -565,6 +621,10 @@ pub fn sort_tests() {
565621
ignore: false,
566622
should_panic: ShouldPanic::No,
567623
allow_fail: false,
624+
#[cfg(not(bootstrap))]
625+
compile_fail: false,
626+
#[cfg(not(bootstrap))]
627+
no_run: false,
568628
test_type: TestType::Unknown,
569629
},
570630
testfn: DynTestFn(Box::new(testfn)),
@@ -642,6 +702,10 @@ pub fn test_bench_no_iter() {
642702
ignore: false,
643703
should_panic: ShouldPanic::No,
644704
allow_fail: false,
705+
#[cfg(not(bootstrap))]
706+
compile_fail: false,
707+
#[cfg(not(bootstrap))]
708+
no_run: false,
645709
test_type: TestType::Unknown,
646710
};
647711

@@ -662,6 +726,10 @@ pub fn test_bench_iter() {
662726
ignore: false,
663727
should_panic: ShouldPanic::No,
664728
allow_fail: false,
729+
#[cfg(not(bootstrap))]
730+
compile_fail: false,
731+
#[cfg(not(bootstrap))]
732+
no_run: false,
665733
test_type: TestType::Unknown,
666734
};
667735

@@ -676,6 +744,10 @@ fn should_sort_failures_before_printing_them() {
676744
ignore: false,
677745
should_panic: ShouldPanic::No,
678746
allow_fail: false,
747+
#[cfg(not(bootstrap))]
748+
compile_fail: false,
749+
#[cfg(not(bootstrap))]
750+
no_run: false,
679751
test_type: TestType::Unknown,
680752
};
681753

@@ -684,6 +756,10 @@ fn should_sort_failures_before_printing_them() {
684756
ignore: false,
685757
should_panic: ShouldPanic::No,
686758
allow_fail: false,
759+
#[cfg(not(bootstrap))]
760+
compile_fail: false,
761+
#[cfg(not(bootstrap))]
762+
no_run: false,
687763
test_type: TestType::Unknown,
688764
};
689765

library/test/src/types.rs

+34
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ pub struct TestDesc {
124124
pub ignore: bool,
125125
pub should_panic: options::ShouldPanic,
126126
pub allow_fail: bool,
127+
#[cfg(not(bootstrap))]
128+
pub compile_fail: bool,
129+
#[cfg(not(bootstrap))]
130+
pub no_run: bool,
127131
pub test_type: TestType,
128132
}
129133

@@ -140,6 +144,36 @@ impl TestDesc {
140144
}
141145
}
142146
}
147+
148+
/// Returns None for ignored test or that that are just run, otherwise give a description of the type of test.
149+
/// Descriptions include "should panic", "compile fail" and "compile".
150+
#[cfg(not(bootstrap))]
151+
pub fn test_mode(&self) -> Option<&'static str> {
152+
if self.ignore {
153+
return None;
154+
}
155+
match self.should_panic {
156+
options::ShouldPanic::Yes | options::ShouldPanic::YesWithMessage(_) => {
157+
return Some("should panic");
158+
}
159+
options::ShouldPanic::No => {}
160+
}
161+
if self.allow_fail {
162+
return Some("allow fail");
163+
}
164+
if self.compile_fail {
165+
return Some("compile fail");
166+
}
167+
if self.no_run {
168+
return Some("compile");
169+
}
170+
None
171+
}
172+
173+
#[cfg(bootstrap)]
174+
pub fn test_mode(&self) -> Option<&'static str> {
175+
None
176+
}
143177
}
144178

145179
#[derive(Debug)]

src/librustdoc/doctest.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ impl Tester for Collector {
880880
let target = self.options.target.clone();
881881
let target_str = target.to_string();
882882
let unused_externs = self.unused_extern_reports.clone();
883+
let no_run = config.no_run || options.no_run;
883884
if !config.compile_fail {
884885
self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
885886
}
@@ -941,13 +942,16 @@ impl Tester for Collector {
941942
// compiler failures are test failures
942943
should_panic: testing::ShouldPanic::No,
943944
allow_fail: config.allow_fail,
945+
#[cfg(not(bootstrap))]
946+
compile_fail: config.compile_fail,
947+
#[cfg(not(bootstrap))]
948+
no_run,
944949
test_type: testing::TestType::DocTest,
945950
},
946951
testfn: testing::DynTestFn(box move || {
947952
let report_unused_externs = |uext| {
948953
unused_externs.lock().unwrap().push(uext);
949954
};
950-
let no_run = config.no_run || options.no_run;
951955
let res = run_test(
952956
&test,
953957
&cratename,

src/test/rustdoc-ui/failed-doctest-compile-fail.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
running 1 test
3-
test $DIR/failed-doctest-compile-fail.rs - Foo (line 9) ... FAILED
3+
test $DIR/failed-doctest-compile-fail.rs - Foo (line 9) - compile fail ... FAILED
44

55
failures:
66

src/test/rustdoc-ui/failed-doctest-missing-codes.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
running 1 test
3-
test $DIR/failed-doctest-missing-codes.rs - Foo (line 9) ... FAILED
3+
test $DIR/failed-doctest-missing-codes.rs - Foo (line 9) - compile fail ... FAILED
44

55
failures:
66

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
running 1 test
3-
test $DIR/issue-80992.rs - test (line 7) ... ok
3+
test $DIR/issue-80992.rs - test (line 7) - compile fail ... ok
44

55
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
66

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
running 7 tests
3-
test $DIR/no-run-flag.rs - f (line 11) ... ok
3+
test $DIR/no-run-flag.rs - f (line 11) - compile ... ok
44
test $DIR/no-run-flag.rs - f (line 14) ... ignored
5-
test $DIR/no-run-flag.rs - f (line 17) ... ok
6-
test $DIR/no-run-flag.rs - f (line 23) ... ok
7-
test $DIR/no-run-flag.rs - f (line 28) ... ok
8-
test $DIR/no-run-flag.rs - f (line 32) ... ok
9-
test $DIR/no-run-flag.rs - f (line 8) ... ok
5+
test $DIR/no-run-flag.rs - f (line 17) - compile ... ok
6+
test $DIR/no-run-flag.rs - f (line 23) - compile fail ... ok
7+
test $DIR/no-run-flag.rs - f (line 28) - compile ... ok
8+
test $DIR/no-run-flag.rs - f (line 32) - compile ... ok
9+
test $DIR/no-run-flag.rs - f (line 8) - compile ... ok
1010

1111
test result: ok. 6 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in $TIME
1212

src/test/rustdoc-ui/test-type.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// compile-flags: --test --test-args=--test-threads=1
2+
// check-pass
3+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
4+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
5+
6+
/// ```
7+
/// let a = true;
8+
/// ```
9+
/// ```should_panic
10+
/// panic!()
11+
/// ```
12+
/// ```ignore (incomplete-code)
13+
/// fn foo() {
14+
/// ```
15+
/// ```no_run
16+
/// loop {
17+
/// println!("Hello, world");
18+
/// }
19+
/// ```
20+
/// fails to compile
21+
/// ```compile_fail
22+
/// let x = 5;
23+
/// x += 2; // shouldn't compile!
24+
/// ```
25+
26+
pub fn f() {}

0 commit comments

Comments
 (0)