Skip to content

libtest: print the type of test being run #144596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/test/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ fn on_test_event(
out: &mut dyn OutputFormatter,
) -> io::Result<()> {
match (*event).clone() {
TestEvent::TeFiltered(filtered_tests, shuffle_seed) => {
TestEvent::TeFiltered(filtered_tests, shuffle_seed, group_kind) => {
st.total = filtered_tests;
out.write_run_start(filtered_tests, shuffle_seed)?;
out.write_run_start(filtered_tests, shuffle_seed, group_kind)?;
}
TestEvent::TeFilteredOut(filtered_out) => {
st.filtered_out = filtered_out;
Expand Down
3 changes: 2 additions & 1 deletion library/test/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::test_result::TestResult;
use super::time::TestExecTime;
use super::types::{TestDesc, TestId};
use crate::TestGroupKind;

#[derive(Debug, Clone)]
pub struct CompletedTest {
Expand All @@ -28,7 +29,7 @@ impl CompletedTest {

#[derive(Debug, Clone)]
pub enum TestEvent {
TeFiltered(usize, Option<u64>),
TeFiltered(usize, Option<u64>, TestGroupKind),
TeWait(TestDesc),
TeResult(CompletedTest),
TeTimeout(TestDesc),
Expand Down
7 changes: 6 additions & 1 deletion library/test/src/formatters/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
))
}

fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> {
fn write_run_start(
&mut self,
test_count: usize,
shuffle_seed: Option<u64>,
_kind: super::TestGroupKind,
) -> io::Result<()> {
let shuffle_seed_json = if let Some(shuffle_seed) = shuffle_seed {
format!(r#", "shuffle_seed": {shuffle_seed}"#)
} else {
Expand Down
3 changes: 2 additions & 1 deletion library/test/src/formatters/junit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl<T: Write> OutputFormatter for JunitFormatter<T> {
&mut self,
_test_count: usize,
_shuffle_seed: Option<u64>,
_group_kind: super::TestGroupKind,
) -> io::Result<()> {
// We write xml header on run start
self.write_message("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
Expand Down Expand Up @@ -187,7 +188,7 @@ impl<T: Write> OutputFormatter for JunitFormatter<T> {
fn parse_class_name(desc: &TestDesc) -> (String, String) {
match desc.test_type {
TestType::UnitTest => parse_class_name_unit(desc),
TestType::DocTest => parse_class_name_doc(desc),
TestType::DocTest { merged: _ } => parse_class_name_doc(desc),
TestType::IntegrationTest => parse_class_name_integration(desc),
TestType::Unknown => (String::from("unknown"), String::from(desc.name.as_slice())),
}
Expand Down
26 changes: 25 additions & 1 deletion library/test/src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ pub(crate) trait OutputFormatter {
fn write_test_discovered(&mut self, desc: &TestDesc, test_type: &str) -> io::Result<()>;
fn write_discovery_finish(&mut self, state: &ConsoleTestDiscoveryState) -> io::Result<()>;

fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()>;
fn write_run_start(
&mut self,
test_count: usize,
shuffle_seed: Option<u64>,
kind: TestGroupKind,
) -> io::Result<()>;
fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()>;
fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()>;
fn write_result(
Expand All @@ -43,3 +48,22 @@ pub(crate) fn write_stderr_delimiter(test_output: &mut Vec<u8>, test_name: &Test
}
writeln!(test_output, "---- {test_name} stderr ----").unwrap();
}

/// Controls what type of message is printed when running tests.
#[non_exhaustive]
#[derive(Copy, Clone, Debug)]
pub enum TestGroupKind {
Regular,
DocTestMerged,
DocTestStandalone,
}

impl TestGroupKind {
fn qualifier(&self) -> &str {
match self {
TestGroupKind::Regular => "",
TestGroupKind::DocTestMerged => "merged doc",
TestGroupKind::DocTestStandalone => "standalone doc",
}
}
}
10 changes: 8 additions & 2 deletions library/test/src/formatters/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,20 @@ impl<T: Write> OutputFormatter for PrettyFormatter<T> {
))
}

fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> {
fn write_run_start(
&mut self,
test_count: usize,
shuffle_seed: Option<u64>,
kind: super::TestGroupKind,
) -> io::Result<()> {
let noun = if test_count != 1 { "tests" } else { "test" };
let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed {
format!(" (shuffle seed: {shuffle_seed})")
} else {
String::new()
};
self.write_plain(format!("\nrunning {test_count} {noun}{shuffle_seed_msg}\n"))
let qual = kind.qualifier();
self.write_plain(format!("\nrunning {test_count} {qual}{noun}{shuffle_seed_msg}\n"))
}

fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
Expand Down
10 changes: 8 additions & 2 deletions library/test/src/formatters/terse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,21 @@ impl<T: Write> OutputFormatter for TerseFormatter<T> {
Ok(())
}

fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> {
fn write_run_start(
&mut self,
test_count: usize,
shuffle_seed: Option<u64>,
kind: super::TestGroupKind,
) -> io::Result<()> {
self.total_test_count = test_count;
let noun = if test_count != 1 { "tests" } else { "test" };
let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed {
format!(" (shuffle seed: {shuffle_seed})")
} else {
String::new()
};
self.write_plain(format!("\nrunning {test_count} {noun}{shuffle_seed_msg}\n"))
let qual = kind.qualifier();
self.write_plain(format!("\nrunning {test_count} {qual}{noun}{shuffle_seed_msg}\n"))
}

fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
Expand Down
16 changes: 15 additions & 1 deletion library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use cli::TestOpts;
pub use self::ColorConfig::*;
pub use self::bench::{Bencher, black_box};
pub use self::console::run_tests_console;
pub use self::formatters::TestGroupKind;
pub use self::options::{ColorConfig, Options, OutputFormat, RunIgnored, ShouldPanic};
pub use self::types::TestName::*;
pub use self::types::*;
Expand Down Expand Up @@ -327,6 +328,19 @@ where
if !opts.bench_benchmarks {
filtered_tests = convert_benchmarks_to_tests(filtered_tests);
}
let group_kind = match filtered_tests.get(0).map(|x| x.desc.test_type) {
// if all remaining tests are the same kind of doctest, we will print a different message.
Some(ty @ TestType::DocTest { merged })
if filtered_tests.iter().all(|t| t.desc.test_type == ty) =>
{
if merged {
TestGroupKind::DocTestMerged
} else {
TestGroupKind::DocTestStandalone
}
}
_ => TestGroupKind::Regular,
};

for test in filtered_tests {
let mut desc = test.desc;
Expand All @@ -348,7 +362,7 @@ where

let shuffle_seed = get_shuffle_seed(opts);

let event = TestEvent::TeFiltered(filtered.total_len(), shuffle_seed);
let event = TestEvent::TeFiltered(filtered.total_len(), shuffle_seed, group_kind);
notify_about_test_event(event)?;

let concurrency = opts.test_threads.unwrap_or_else(get_concurrency);
Expand Down
8 changes: 4 additions & 4 deletions library/test/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ fn time_test_failure_template(test_type: TestType) -> TestResult {

#[test]
fn test_error_on_exceed() {
let types = [TestType::UnitTest, TestType::IntegrationTest, TestType::DocTest];
let types = [TestType::UnitTest, TestType::IntegrationTest, TestType::DocTest { merged: true }];

for test_type in types.iter() {
let result = time_test_failure_template(*test_type);
Expand Down Expand Up @@ -433,9 +433,9 @@ fn test_time_options_threshold() {
(TestType::IntegrationTest, integration.warn.as_millis() - 1, false, false),
(TestType::IntegrationTest, integration.warn.as_millis(), true, false),
(TestType::IntegrationTest, integration.critical.as_millis(), true, true),
(TestType::DocTest, doc.warn.as_millis() - 1, false, false),
(TestType::DocTest, doc.warn.as_millis(), true, false),
(TestType::DocTest, doc.critical.as_millis(), true, true),
(TestType::DocTest { merged: false }, doc.warn.as_millis() - 1, false, false),
(TestType::DocTest { merged: false }, doc.warn.as_millis(), true, false),
(TestType::DocTest { merged: false }, doc.critical.as_millis(), true, true),
];

for (test_type, time, expected_warn, expected_critical) in test_vector.iter() {
Expand Down
4 changes: 2 additions & 2 deletions library/test/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl TestTimeOptions {
match test.test_type {
TestType::UnitTest => self.unit_threshold.warn,
TestType::IntegrationTest => self.integration_threshold.warn,
TestType::DocTest => self.doctest_threshold.warn,
TestType::DocTest { merged: _ } => self.doctest_threshold.warn,
TestType::Unknown => time_constants::UNKNOWN_WARN,
}
}
Expand All @@ -176,7 +176,7 @@ impl TestTimeOptions {
match test.test_type {
TestType::UnitTest => self.unit_threshold.critical,
TestType::IntegrationTest => self.integration_threshold.critical,
TestType::DocTest => self.doctest_threshold.critical,
TestType::DocTest { merged: _ } => self.doctest_threshold.critical,
TestType::Unknown => time_constants::UNKNOWN_CRITICAL,
}
}
Expand Down
5 changes: 3 additions & 2 deletions library/test/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum TestType {
/// Integration-style tests are expected to be in the `tests` folder of the crate.
IntegrationTest,
/// Doctests are created by the `librustdoc` manually, so it's a different type of test.
DocTest,
DocTest { merged: bool },
/// Tests for the sources that don't follow the project layout convention
/// (e.g. tests in raw `main.rs` compiled by calling `rustc --test` directly).
Unknown,
Expand Down Expand Up @@ -252,6 +252,7 @@ pub struct TestDescAndFn {
}

impl TestDescAndFn {
/// Generate a new merged doctest
pub const fn new_doctest(
test_name: &'static str,
ignore: bool,
Expand All @@ -278,7 +279,7 @@ impl TestDescAndFn {
} else {
options::ShouldPanic::No
},
test_type: TestType::DocTest,
test_type: TestType::DocTest { merged: true },
},
testfn,
}
Expand Down
54 changes: 53 additions & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ pub(crate) fn run_tests(
opts.clone(),
Arc::clone(rustdoc_options),
unused_extern_reports.clone(),
false,
));
}
}
Expand Down Expand Up @@ -1017,7 +1018,7 @@ impl CreateRunnableDocTests {
|| self.rustdoc_options.nocapture
|| self.rustdoc_options.test_args.iter().any(|arg| arg == "--show-output");
if is_standalone {
let test_desc = self.generate_test_desc_and_fn(doctest, scraped_test);
let test_desc = self.generate_test_desc_and_fn(doctest, scraped_test, false);
self.standalone_tests.push(test_desc);
} else {
self.mergeable_tests
Expand All @@ -1038,6 +1039,7 @@ impl CreateRunnableDocTests {
&mut self,
test: DocTestBuilder,
scraped_test: ScrapedDocTest,
merged: bool,
) -> test::TestDescAndFn {
if !scraped_test.langstr.compile_fail {
self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
Expand All @@ -1049,16 +1051,66 @@ impl CreateRunnableDocTests {
self.opts.clone(),
Arc::clone(&self.rustdoc_options),
self.unused_extern_reports.clone(),
merged,
)
}
}

#[cfg(not(bootstrap))]
fn generate_test_desc_and_fn(
test: DocTestBuilder,
scraped_test: ScrapedDocTest,
opts: GlobalTestOptions,
rustdoc_options: Arc<RustdocOptions>,
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
merged: bool,
) -> test::TestDescAndFn {
let target_str = rustdoc_options.target.to_string();
let rustdoc_test_options =
IndividualTestOptions::new(&rustdoc_options, &test.test_id, scraped_test.path());

debug!("creating test {}: {}", scraped_test.name, scraped_test.text);
test::TestDescAndFn {
desc: test::TestDesc {
name: test::DynTestName(scraped_test.name.clone()),
ignore: match scraped_test.langstr.ignore {
Ignore::All => true,
Ignore::None => false,
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
},
ignore_message: None,
source_file: "",
start_line: 0,
start_col: 0,
end_line: 0,
end_col: 0,
// compiler failures are test failures
should_panic: test::ShouldPanic::No,
compile_fail: scraped_test.langstr.compile_fail,
no_run: scraped_test.no_run(&rustdoc_options),
test_type: test::TestType::DocTest { merged },
},
testfn: test::DynTestFn(Box::new(move || {
doctest_run_fn(
rustdoc_test_options,
opts,
test,
scraped_test,
rustdoc_options,
unused_externs,
)
})),
}
}

#[cfg(bootstrap)]
fn generate_test_desc_and_fn(
test: DocTestBuilder,
scraped_test: ScrapedDocTest,
opts: GlobalTestOptions,
rustdoc_options: Arc<RustdocOptions>,
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
_merged: bool,
) -> test::TestDescAndFn {
let target_str = rustdoc_options.target.to_string();
let rustdoc_test_options =
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/doctests-merge/doctest-2021.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

running 2 tests
running 2 standalone doctests
test doctest.rs - (line 4) ... ok
test doctest.rs - init (line 8) ... ok

Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/doctests-merge/doctest-2024.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

running 2 tests
running 2 merged doctests
test doctest.rs - (line 4) ... ok
test doctest.rs - init (line 8) ... ok

Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/doctests-merge/doctest-standalone.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

running 2 tests
running 2 standalone doctests
test doctest-standalone.rs - (line 4) ... ok
test doctest-standalone.rs - init (line 8) ... ok

Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/2024-doctests-checks.stdout
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

running 1 test
running 1 merged doctest
test $DIR/2024-doctests-checks.rs - Foo (line 10) ... ok

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


running 1 test
running 1 standalone doctest
test $DIR/2024-doctests-checks.rs - Foo (line 17) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/2024-doctests-crate-attribute.stdout
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

running 1 test
running 1 merged doctest
test $DIR/2024-doctests-crate-attribute.rs - Foo (line 22) ... ok

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


running 1 test
running 1 standalone doctest
test $DIR/2024-doctests-crate-attribute.rs - Foo (line 13) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/doctest/cfg-test.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

running 2 tests
running 2 standalone doctests
test $DIR/cfg-test.rs - Bar (line 27) ... ok
test $DIR/cfg-test.rs - Foo (line 19) ... ok

Expand Down
Loading
Loading