Skip to content

Commit dff10b1

Browse files
committed
compiletest: Extract libtest-specific executor code to a submodule
1 parent 3117e19 commit dff10b1

File tree

3 files changed

+109
-103
lines changed

3 files changed

+109
-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

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

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

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

574574
// Check the outcome reported by libtest.
575575
match res {

0 commit comments

Comments
 (0)