Skip to content

Commit 63fae6a

Browse files
committed
Introduce helpers for common test case commands
1 parent 397fafa commit 63fae6a

File tree

1 file changed

+117
-109
lines changed

1 file changed

+117
-109
lines changed

build_system/tests.rs

Lines changed: 117 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,112 +15,83 @@ static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example");
1515

1616
struct TestCase {
1717
config: &'static str,
18-
func: &'static dyn Fn(&TestRunner),
18+
cmd: TestCaseCmd,
19+
}
20+
21+
enum TestCaseCmd {
22+
Custom { func: &'static dyn Fn(&TestRunner) },
23+
BuildLib { source: &'static str, crate_types: &'static str },
24+
BuildBinAndRun { source: &'static str, args: &'static [&'static str] },
25+
JitBin { source: &'static str, args: &'static str },
1926
}
2027

2128
impl TestCase {
22-
const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
23-
Self { config, func }
29+
// FIXME reduce usage of custom test case commands
30+
const fn custom(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self {
31+
Self { config, cmd: TestCaseCmd::Custom { func } }
32+
}
33+
34+
const fn build_lib(
35+
config: &'static str,
36+
source: &'static str,
37+
crate_types: &'static str,
38+
) -> Self {
39+
Self { config, cmd: TestCaseCmd::BuildLib { source, crate_types } }
40+
}
41+
42+
const fn build_bin_and_run(
43+
config: &'static str,
44+
source: &'static str,
45+
args: &'static [&'static str],
46+
) -> Self {
47+
Self { config, cmd: TestCaseCmd::BuildBinAndRun { source, args } }
48+
}
49+
50+
const fn jit_bin(config: &'static str, source: &'static str, args: &'static str) -> Self {
51+
Self { config, cmd: TestCaseCmd::JitBin { source, args } }
2452
}
2553
}
2654

2755
const NO_SYSROOT_SUITE: &[TestCase] = &[
28-
TestCase::new("build.mini_core", &|runner| {
29-
runner.run_rustc(["example/mini_core.rs", "--crate-type", "lib,dylib"]);
30-
}),
31-
TestCase::new("build.example", &|runner| {
32-
runner.run_rustc(["example/example.rs", "--crate-type", "lib"]);
33-
}),
34-
TestCase::new("jit.mini_core_hello_world", &|runner| {
35-
let mut jit_cmd = runner.rustc_command([
36-
"-Zunstable-options",
37-
"-Cllvm-args=mode=jit",
38-
"-Cprefer-dynamic",
39-
"example/mini_core_hello_world.rs",
40-
"--cfg",
41-
"jit",
42-
]);
43-
jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd");
44-
spawn_and_wait(jit_cmd);
45-
46-
eprintln!("[JIT-lazy] mini_core_hello_world");
47-
let mut jit_cmd = runner.rustc_command([
48-
"-Zunstable-options",
49-
"-Cllvm-args=mode=jit-lazy",
50-
"-Cprefer-dynamic",
51-
"example/mini_core_hello_world.rs",
52-
"--cfg",
53-
"jit",
54-
]);
55-
jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd");
56-
spawn_and_wait(jit_cmd);
57-
}),
58-
TestCase::new("aot.mini_core_hello_world", &|runner| {
59-
runner.run_rustc(["example/mini_core_hello_world.rs"]);
60-
runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]);
61-
}),
56+
TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib,dylib"),
57+
TestCase::build_lib("build.example", "example/example.rs", "lib"),
58+
TestCase::jit_bin("jit.mini_core_hello_world", "example/mini_core_hello_world.rs", "abc bcd"),
59+
TestCase::build_bin_and_run(
60+
"aot.mini_core_hello_world",
61+
"example/mini_core_hello_world.rs",
62+
&["abc", "bcd"],
63+
),
6264
];
6365

6466
const BASE_SYSROOT_SUITE: &[TestCase] = &[
65-
TestCase::new("aot.arbitrary_self_types_pointers_and_wrappers", &|runner| {
66-
runner.run_rustc(["example/arbitrary_self_types_pointers_and_wrappers.rs"]);
67-
runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []);
68-
}),
69-
TestCase::new("aot.issue_91827_extern_types", &|runner| {
70-
runner.run_rustc(["example/issue-91827-extern-types.rs"]);
71-
runner.run_out_command("issue-91827-extern-types", []);
72-
}),
73-
TestCase::new("build.alloc_system", &|runner| {
74-
runner.run_rustc(["example/alloc_system.rs", "--crate-type", "lib"]);
75-
}),
76-
TestCase::new("aot.alloc_example", &|runner| {
77-
runner.run_rustc(["example/alloc_example.rs"]);
78-
runner.run_out_command("alloc_example", []);
79-
}),
80-
TestCase::new("jit.std_example", &|runner| {
81-
runner.run_rustc([
82-
"-Zunstable-options",
83-
"-Cllvm-args=mode=jit",
84-
"-Cprefer-dynamic",
85-
"example/std_example.rs",
86-
]);
87-
88-
eprintln!("[JIT-lazy] std_example");
89-
runner.run_rustc([
90-
"-Zunstable-options",
91-
"-Cllvm-args=mode=jit-lazy",
92-
"-Cprefer-dynamic",
93-
"example/std_example.rs",
94-
]);
95-
}),
96-
TestCase::new("aot.std_example", &|runner| {
97-
runner.run_rustc(["example/std_example.rs"]);
98-
runner.run_out_command("std_example", ["arg"]);
99-
}),
100-
TestCase::new("aot.dst_field_align", &|runner| {
101-
runner.run_rustc(["example/dst-field-align.rs"]);
102-
runner.run_out_command("dst-field-align", []);
103-
}),
104-
TestCase::new("aot.subslice-patterns-const-eval", &|runner| {
105-
runner.run_rustc(["example/subslice-patterns-const-eval.rs"]);
106-
runner.run_out_command("subslice-patterns-const-eval", []);
107-
}),
108-
TestCase::new("aot.track-caller-attribute", &|runner| {
109-
runner.run_rustc(["example/track-caller-attribute.rs"]);
110-
runner.run_out_command("track-caller-attribute", []);
111-
}),
112-
TestCase::new("aot.float-minmax-pass", &|runner| {
113-
runner.run_rustc(["example/float-minmax-pass.rs"]);
114-
runner.run_out_command("float-minmax-pass", []);
115-
}),
116-
TestCase::new("aot.mod_bench", &|runner| {
117-
runner.run_rustc(["example/mod_bench.rs"]);
118-
runner.run_out_command("mod_bench", []);
119-
}),
120-
TestCase::new("aot.issue-72793", &|runner| {
121-
runner.run_rustc(["example/issue-72793.rs"]);
122-
runner.run_out_command("issue-72793", []);
123-
}),
67+
TestCase::build_bin_and_run(
68+
"aot.arbitrary_self_types_pointers_and_wrappers",
69+
"example/arbitrary_self_types_pointers_and_wrappers.rs",
70+
&[],
71+
),
72+
TestCase::build_bin_and_run(
73+
"aot.issue_91827_extern_types",
74+
"example/issue-91827-extern-types.rs",
75+
&[],
76+
),
77+
TestCase::build_lib("build.alloc_system", "example/alloc_system.rs", "lib"),
78+
TestCase::build_bin_and_run("aot.alloc_example", "example/alloc_example.rs", &[]),
79+
TestCase::jit_bin("jit.std_example", "example/std_example.rs", ""),
80+
TestCase::build_bin_and_run("aot.std_example", "example/std_example.rs", &["arg"]),
81+
TestCase::build_bin_and_run("aot.dst_field_align", "example/dst-field-align.rs", &[]),
82+
TestCase::build_bin_and_run(
83+
"aot.subslice-patterns-const-eval",
84+
"example/subslice-patterns-const-eval.rs",
85+
&[],
86+
),
87+
TestCase::build_bin_and_run(
88+
"aot.track-caller-attribute",
89+
"example/track-caller-attribute.rs",
90+
&[],
91+
),
92+
TestCase::build_bin_and_run("aot.float-minmax-pass", "example/float-minmax-pass.rs", &[]),
93+
TestCase::build_bin_and_run("aot.mod_bench", "example/mod_bench.rs", &[]),
94+
TestCase::build_bin_and_run("aot.issue-72793", "example/issue-72793.rs", &[]),
12495
];
12596

12697
pub(crate) static RAND_REPO: GitRepo =
@@ -147,7 +118,7 @@ static LIBCORE_TESTS: CargoProject =
147118
CargoProject::new(&SYSROOT_SRC.join("library/core/tests"), "core_tests");
148119

149120
const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
150-
TestCase::new("test.rust-random/rand", &|runner| {
121+
TestCase::custom("test.rust-random/rand", &|runner| {
151122
spawn_and_wait(RAND.clean(&runner.target_compiler.cargo, &runner.dirs));
152123

153124
if runner.is_native {
@@ -162,11 +133,11 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
162133
spawn_and_wait(build_cmd);
163134
}
164135
}),
165-
TestCase::new("test.simple-raytracer", &|runner| {
136+
TestCase::custom("test.simple-raytracer", &|runner| {
166137
spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.host_compiler.cargo, &runner.dirs));
167138
spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs));
168139
}),
169-
TestCase::new("test.libcore", &|runner| {
140+
TestCase::custom("test.libcore", &|runner| {
170141
spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo, &runner.dirs));
171142

172143
if runner.is_native {
@@ -178,7 +149,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
178149
spawn_and_wait(build_cmd);
179150
}
180151
}),
181-
TestCase::new("test.regex-shootout-regex-dna", &|runner| {
152+
TestCase::custom("test.regex-shootout-regex-dna", &|runner| {
182153
spawn_and_wait(REGEX.clean(&runner.target_compiler.cargo, &runner.dirs));
183154

184155
// newer aho_corasick versions throw a deprecation warning
@@ -232,7 +203,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
232203
}
233204
}
234205
}),
235-
TestCase::new("test.regex", &|runner| {
206+
TestCase::custom("test.regex", &|runner| {
236207
spawn_and_wait(REGEX.clean(&runner.host_compiler.cargo, &runner.dirs));
237208

238209
// newer aho_corasick versions throw a deprecation warning
@@ -259,7 +230,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[
259230
spawn_and_wait(build_cmd);
260231
}
261232
}),
262-
TestCase::new("test.portable-simd", &|runner| {
233+
TestCase::custom("test.portable-simd", &|runner| {
263234
spawn_and_wait(PORTABLE_SIMD.clean(&runner.host_compiler.cargo, &runner.dirs));
264235

265236
let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler, &runner.dirs);
@@ -366,7 +337,7 @@ impl TestRunner {
366337
}
367338

368339
pub fn run_testsuite(&self, tests: &[TestCase]) {
369-
for &TestCase { config, func } in tests {
340+
for TestCase { config, cmd } in tests {
370341
let (tag, testname) = config.split_once('.').unwrap();
371342
let tag = tag.to_uppercase();
372343
let is_jit_test = tag == "JIT";
@@ -378,7 +349,47 @@ impl TestRunner {
378349
eprintln!("[{tag}] {testname}");
379350
}
380351

381-
func(self);
352+
match *cmd {
353+
TestCaseCmd::Custom { func } => func(self),
354+
TestCaseCmd::BuildLib { source, crate_types } => {
355+
self.run_rustc([source, "--crate-type", crate_types]);
356+
}
357+
TestCaseCmd::BuildBinAndRun { source, args } => {
358+
self.run_rustc([source]);
359+
self.run_out_command(
360+
source.split('/').last().unwrap().split('.').next().unwrap(),
361+
args,
362+
);
363+
}
364+
TestCaseCmd::JitBin { source, args } => {
365+
let mut jit_cmd = self.rustc_command([
366+
"-Zunstable-options",
367+
"-Cllvm-args=mode=jit",
368+
"-Cprefer-dynamic",
369+
source,
370+
"--cfg",
371+
"jit",
372+
]);
373+
if !args.is_empty() {
374+
jit_cmd.env("CG_CLIF_JIT_ARGS", args);
375+
}
376+
spawn_and_wait(jit_cmd);
377+
378+
eprintln!("[JIT-lazy] {testname}");
379+
let mut jit_cmd = self.rustc_command([
380+
"-Zunstable-options",
381+
"-Cllvm-args=mode=jit-lazy",
382+
"-Cprefer-dynamic",
383+
source,
384+
"--cfg",
385+
"jit",
386+
]);
387+
if !args.is_empty() {
388+
jit_cmd.env("CG_CLIF_JIT_ARGS", args);
389+
}
390+
spawn_and_wait(jit_cmd);
391+
}
392+
}
382393
}
383394
}
384395

@@ -410,10 +421,7 @@ impl TestRunner {
410421
spawn_and_wait(self.rustc_command(args));
411422
}
412423

413-
fn run_out_command<'a, I>(&self, name: &str, args: I)
414-
where
415-
I: IntoIterator<Item = &'a str>,
416-
{
424+
fn run_out_command<'a>(&self, name: &str, args: &[&str]) {
417425
let mut full_cmd = vec![];
418426

419427
// Prepend the RUN_WRAPPER's
@@ -425,7 +433,7 @@ impl TestRunner {
425433
BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).join(name).to_str().unwrap().to_string(),
426434
);
427435

428-
for arg in args.into_iter() {
436+
for arg in args {
429437
full_cmd.push(arg.to_string());
430438
}
431439

0 commit comments

Comments
 (0)