Skip to content

Commit 6fda3e5

Browse files
committed
compiletest: Extract libtest-specific executor code to a submodule
1 parent 4c23295 commit 6fda3e5

File tree

3 files changed

+113
-103
lines changed

3 files changed

+113
-103
lines changed

Diff for: src/tools/compiletest/src/executor.rs

+1-102
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,11 @@
44
//! This will hopefully make it easier to migrate away from libtest someday.
55
66
use std::borrow::Cow;
7-
use std::io;
87
use std::sync::Arc;
98

109
use crate::common::{Config, TestPaths};
1110

12-
/// Delegates to libtest to run the list of collected tests.
13-
///
14-
/// Returns `Ok(true)` if all tests passed, or `Ok(false)` if one or more tests failed.
15-
pub(crate) fn execute_tests(config: &Config, tests: Vec<CollectedTest>) -> io::Result<bool> {
16-
let opts = test_opts(config);
17-
let tests = tests.into_iter().map(|t| t.into_libtest()).collect::<Vec<_>>();
18-
19-
test::run_tests_console(&opts, tests)
20-
}
11+
pub(crate) mod libtest;
2112

2213
/// Information needed to create a `test::TestDescAndFn`.
2314
pub(crate) struct CollectedTest {
@@ -35,45 +26,6 @@ pub(crate) struct CollectedTestDesc {
3526
pub(crate) should_panic: ShouldPanic,
3627
}
3728

38-
impl CollectedTest {
39-
fn into_libtest(self) -> test::TestDescAndFn {
40-
let Self { desc, config, testpaths, revision } = self;
41-
let CollectedTestDesc { name, ignore, ignore_message, should_panic } = desc;
42-
43-
// Libtest requires the ignore message to be a &'static str, so we might
44-
// have to leak memory to create it. This is fine, as we only do so once
45-
// per test, so the leak won't grow indefinitely.
46-
let ignore_message = ignore_message.map(|msg| match msg {
47-
Cow::Borrowed(s) => s,
48-
Cow::Owned(s) => &*String::leak(s),
49-
});
50-
51-
let desc = test::TestDesc {
52-
name: test::DynTestName(name),
53-
ignore,
54-
ignore_message,
55-
source_file: "",
56-
start_line: 0,
57-
start_col: 0,
58-
end_line: 0,
59-
end_col: 0,
60-
should_panic: should_panic.to_libtest(),
61-
compile_fail: false,
62-
no_run: false,
63-
test_type: test::TestType::Unknown,
64-
};
65-
66-
// This closure is invoked when libtest returns control to compiletest
67-
// to execute the test.
68-
let testfn = test::DynTestFn(Box::new(move || {
69-
crate::runtest::run(config, &testpaths, revision.as_deref());
70-
Ok(())
71-
}));
72-
73-
test::TestDescAndFn { desc, testfn }
74-
}
75-
}
76-
7729
/// Whether console output should be colored or not.
7830
#[derive(Copy, Clone, Default, Debug)]
7931
pub enum ColorConfig {
@@ -83,16 +35,6 @@ pub enum ColorConfig {
8335
NeverColor,
8436
}
8537

86-
impl ColorConfig {
87-
fn to_libtest(self) -> test::ColorConfig {
88-
match self {
89-
Self::AutoColor => test::ColorConfig::AutoColor,
90-
Self::AlwaysColor => test::ColorConfig::AlwaysColor,
91-
Self::NeverColor => test::ColorConfig::NeverColor,
92-
}
93-
}
94-
}
95-
9638
/// Format of the test results output.
9739
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
9840
pub enum OutputFormat {
@@ -105,52 +47,9 @@ pub enum OutputFormat {
10547
Json,
10648
}
10749

108-
impl OutputFormat {
109-
fn to_libtest(self) -> test::OutputFormat {
110-
match self {
111-
Self::Pretty => test::OutputFormat::Pretty,
112-
Self::Terse => test::OutputFormat::Terse,
113-
Self::Json => test::OutputFormat::Json,
114-
}
115-
}
116-
}
117-
11850
/// Whether test is expected to panic or not.
11951
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
12052
pub(crate) enum ShouldPanic {
12153
No,
12254
Yes,
12355
}
124-
125-
impl ShouldPanic {
126-
fn to_libtest(self) -> test::ShouldPanic {
127-
match self {
128-
Self::No => test::ShouldPanic::No,
129-
Self::Yes => test::ShouldPanic::Yes,
130-
}
131-
}
132-
}
133-
134-
fn test_opts(config: &Config) -> test::TestOpts {
135-
test::TestOpts {
136-
exclude_should_panic: false,
137-
filters: config.filters.clone(),
138-
filter_exact: config.filter_exact,
139-
run_ignored: if config.run_ignored { test::RunIgnored::Yes } else { test::RunIgnored::No },
140-
format: config.format.to_libtest(),
141-
logfile: None,
142-
run_tests: true,
143-
bench_benchmarks: true,
144-
nocapture: config.nocapture,
145-
color: config.color.to_libtest(),
146-
shuffle: false,
147-
shuffle_seed: None,
148-
test_threads: None,
149-
skip: config.skip.clone(),
150-
list: false,
151-
options: test::Options::new(),
152-
time_options: None,
153-
force_run_in_process: false,
154-
fail_fast: config.fail_fast,
155-
}
156-
}

Diff for: src/tools/compiletest/src/executor/libtest.rs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//! This submodule encapsulates all of the code that actually interacts with
2+
//! libtest, so that it can be easily removed after the new executor becomes
3+
//! the default.
4+
5+
use std::borrow::Cow;
6+
use std::io;
7+
8+
use crate::common::Config;
9+
use crate::executor::{CollectedTest, CollectedTestDesc, ColorConfig, OutputFormat, ShouldPanic};
10+
11+
/// Delegates to libtest to run the list of collected tests.
12+
///
13+
/// Returns `Ok(true)` if all tests passed, or `Ok(false)` if one or more tests failed.
14+
pub(crate) fn execute_tests(config: &Config, tests: Vec<CollectedTest>) -> io::Result<bool> {
15+
let opts = test_opts(config);
16+
let tests = tests.into_iter().map(|t| t.into_libtest()).collect::<Vec<_>>();
17+
18+
test::run_tests_console(&opts, tests)
19+
}
20+
21+
impl CollectedTest {
22+
fn into_libtest(self) -> test::TestDescAndFn {
23+
let Self { desc, config, testpaths, revision } = self;
24+
let CollectedTestDesc { name, ignore, ignore_message, should_panic } = desc;
25+
26+
// Libtest requires the ignore message to be a &'static str, so we might
27+
// have to leak memory to create it. This is fine, as we only do so once
28+
// per test, so the leak won't grow indefinitely.
29+
let ignore_message = ignore_message.map(|msg| match msg {
30+
Cow::Borrowed(s) => s,
31+
Cow::Owned(s) => &*String::leak(s),
32+
});
33+
34+
let desc = test::TestDesc {
35+
name: test::DynTestName(name),
36+
ignore,
37+
ignore_message,
38+
source_file: "",
39+
start_line: 0,
40+
start_col: 0,
41+
end_line: 0,
42+
end_col: 0,
43+
should_panic: should_panic.to_libtest(),
44+
compile_fail: false,
45+
no_run: false,
46+
test_type: test::TestType::Unknown,
47+
};
48+
49+
// This closure is invoked when libtest returns control to compiletest
50+
// to execute the test.
51+
let testfn = test::DynTestFn(Box::new(move || {
52+
crate::runtest::run(config, &testpaths, revision.as_deref());
53+
Ok(())
54+
}));
55+
56+
test::TestDescAndFn { desc, testfn }
57+
}
58+
}
59+
60+
impl ColorConfig {
61+
fn to_libtest(self) -> test::ColorConfig {
62+
match self {
63+
Self::AutoColor => test::ColorConfig::AutoColor,
64+
Self::AlwaysColor => test::ColorConfig::AlwaysColor,
65+
Self::NeverColor => test::ColorConfig::NeverColor,
66+
}
67+
}
68+
}
69+
70+
impl OutputFormat {
71+
fn to_libtest(self) -> test::OutputFormat {
72+
match self {
73+
Self::Pretty => test::OutputFormat::Pretty,
74+
Self::Terse => test::OutputFormat::Terse,
75+
Self::Json => test::OutputFormat::Json,
76+
}
77+
}
78+
}
79+
80+
impl ShouldPanic {
81+
fn to_libtest(self) -> test::ShouldPanic {
82+
match self {
83+
Self::No => test::ShouldPanic::No,
84+
Self::Yes => test::ShouldPanic::Yes,
85+
}
86+
}
87+
}
88+
89+
fn test_opts(config: &Config) -> test::TestOpts {
90+
test::TestOpts {
91+
exclude_should_panic: false,
92+
filters: config.filters.clone(),
93+
filter_exact: config.filter_exact,
94+
run_ignored: if config.run_ignored { test::RunIgnored::Yes } else { test::RunIgnored::No },
95+
format: config.format.to_libtest(),
96+
logfile: None,
97+
run_tests: true,
98+
bench_benchmarks: true,
99+
nocapture: config.nocapture,
100+
color: config.color.to_libtest(),
101+
shuffle: false,
102+
shuffle_seed: None,
103+
test_threads: None,
104+
skip: config.skip.clone(),
105+
list: false,
106+
options: test::Options::new(),
107+
time_options: None,
108+
force_run_in_process: false,
109+
fail_fast: config.fail_fast,
110+
}
111+
}

Diff for: src/tools/compiletest/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ pub fn run_tests(config: Arc<Config>) {
573573
// Delegate to libtest to filter and run the big list of structures created
574574
// during test discovery. When libtest decides to run a test, it will
575575
// return control to compiletest by invoking a closure.
576-
let res = crate::executor::execute_tests(&config, tests);
576+
let res = crate::executor::libtest::execute_tests(&config, tests);
577577

578578
// Check the outcome reported by libtest.
579579
match res {

0 commit comments

Comments
 (0)