Skip to content

Commit cbdb3ee

Browse files
Simplify running tests
1 parent 48a52d9 commit cbdb3ee

File tree

5 files changed

+150
-153
lines changed

5 files changed

+150
-153
lines changed

Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ license = "MIT OR Apache-2.0"
99
crate-type = ["dylib"]
1010

1111
[[test]]
12-
name = "lang_tests_debug"
13-
path = "tests/lang_tests_debug.rs"
14-
harness = false
15-
[[test]]
16-
name = "lang_tests_release"
17-
path = "tests/lang_tests_release.rs"
12+
name = "lang_tests"
13+
path = "tests/lang_tests.rs"
1814
harness = false
1915

2016
[features]

tests/lang_tests.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//! The common code for `tests/lang_tests_*.rs`
2+
3+
#![allow(clippy::uninlined_format_args)]
4+
5+
use std::env::current_dir;
6+
use std::path::{Path, PathBuf};
7+
use std::process::Command;
8+
9+
use lang_tester::LangTester;
10+
use tempfile::TempDir;
11+
12+
fn compile_and_run_cmds(
13+
compiler_args: Vec<String>,
14+
test_target: &Option<String>,
15+
exe: &Path,
16+
) -> Vec<(&'static str, Command)> {
17+
let mut compiler = Command::new("rustc");
18+
compiler.args(compiler_args);
19+
20+
// Test command 2: run `tempdir/x`.
21+
if test_target.is_some() {
22+
let mut env_path = std::env::var("PATH").unwrap_or_default();
23+
// TODO(antoyo): find a better way to add the PATH necessary locally.
24+
env_path = format!("/opt/m68k-unknown-linux-gnu/bin:{}", env_path);
25+
compiler.env("PATH", env_path);
26+
27+
let vm_parent_dir = std::env::var("CG_GCC_VM_DIR")
28+
.map(PathBuf::from)
29+
.unwrap_or_else(|_| std::env::current_dir().unwrap());
30+
let vm_dir = "vm";
31+
let exe_filename = exe.file_name().unwrap();
32+
let vm_home_dir = vm_parent_dir.join(vm_dir).join("home");
33+
let vm_exe_path = vm_home_dir.join(exe_filename);
34+
// FIXME(antoyo): panicking here makes the test pass.
35+
let inside_vm_exe_path = PathBuf::from("/home").join(exe_filename);
36+
let mut copy = Command::new("sudo");
37+
copy.arg("cp");
38+
copy.args([exe, &vm_exe_path]);
39+
40+
let mut runtime = Command::new("sudo");
41+
runtime.args(["chroot", vm_dir, "qemu-m68k-static"]);
42+
runtime.arg(inside_vm_exe_path);
43+
runtime.current_dir(vm_parent_dir);
44+
vec![("Compiler", compiler), ("Copy", copy), ("Run-time", runtime)]
45+
} else {
46+
let runtime = Command::new(exe);
47+
vec![("Compiler", compiler), ("Run-time", runtime)]
48+
}
49+
}
50+
51+
fn main() {
52+
let tempdir = TempDir::new().expect("temp dir");
53+
let current_dir = current_dir().expect("current dir");
54+
let current_dir = current_dir.to_str().expect("current dir").to_string();
55+
56+
fn rust_filter(path: &Path) -> bool {
57+
path.is_file() && path.extension().expect("extension").to_str().expect("to_str") == "rs"
58+
}
59+
60+
#[cfg(feature = "master")]
61+
fn filter(filename: &Path) -> bool {
62+
rust_filter(filename)
63+
}
64+
65+
#[cfg(not(feature = "master"))]
66+
fn filter(filename: &Path) -> bool {
67+
if let Some(filename) = filename.to_str()
68+
&& filename.ends_with("gep.rs")
69+
{
70+
return false;
71+
}
72+
rust_filter(filename)
73+
}
74+
// TODO(antoyo): find a way to send this via a cli argument.
75+
let test_target = std::env::var("CG_GCC_TEST_TARGET").ok();
76+
77+
LangTester::new()
78+
.test_dir("tests/run")
79+
.test_path_filter(filter)
80+
.test_extract(|path| {
81+
std::fs::read_to_string(path)
82+
.expect("read file")
83+
.lines()
84+
.skip_while(|l| !l.starts_with("//"))
85+
.take_while(|l| l.starts_with("//"))
86+
.map(|l| &l[2..])
87+
.collect::<Vec<_>>()
88+
.join("\n")
89+
})
90+
.test_cmds(move |path| {
91+
// Test command 1: Compile `x.rs` into `tempdir/x`.
92+
let mut exe = PathBuf::new();
93+
exe.push(&tempdir);
94+
exe.push(path.file_stem().expect("file_stem"));
95+
let mut compiler_args = vec![
96+
format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir),
97+
"--sysroot".into(),
98+
format!("{}/build/build_sysroot/sysroot/", current_dir).into(),
99+
"-C".into(),
100+
"link-arg=-lc".into(),
101+
"--extern".into(),
102+
"mini_core=target/out/libmini_core.rlib".into(),
103+
"-o".into(),
104+
exe.to_str().expect("to_str").into(),
105+
path.to_str().expect("to_str").into(),
106+
];
107+
108+
if let Some(ref target) = test_target {
109+
compiler_args.extend_from_slice(&["--target".into(), target.into()]);
110+
111+
let linker = format!("{}-gcc", target);
112+
compiler_args.extend_from_slice(&[format!("-Clinker={}", linker)]);
113+
}
114+
115+
if let Some(flags) = option_env!("TEST_FLAGS") {
116+
for flag in flags.split_whitespace() {
117+
compiler_args.push(flag.into());
118+
}
119+
}
120+
let mut debug_args = compiler_args.clone();
121+
if test_target.is_some() {
122+
// m68k doesn't have lubsan for now
123+
debug_args
124+
.extend_from_slice(&["-C".to_string(), "llvm-args=sanitize-undefined".into()]);
125+
} else {
126+
debug_args.extend_from_slice(&[
127+
"-C".to_string(),
128+
"llvm-args=sanitize-undefined".into(),
129+
"-C".into(),
130+
"link-args=-lubsan".into(),
131+
]);
132+
}
133+
134+
compiler_args.extend_from_slice(&[
135+
"-C".into(),
136+
"opt-level=3".into(),
137+
"-C".into(),
138+
"lto=no".into(),
139+
]);
140+
141+
let mut parts = compile_and_run_cmds(debug_args, &test_target, &exe);
142+
for part in compile_and_run_cmds(compiler_args, &test_target, &exe) {
143+
parts.push(part);
144+
}
145+
parts
146+
})
147+
.run();
148+
}

tests/lang_tests_common.rs

Lines changed: 0 additions & 137 deletions
This file was deleted.

tests/lang_tests_debug.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/lang_tests_release.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)