-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathbuild.rs
70 lines (60 loc) · 2.48 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::collections::HashSet;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
const GIT_COMMIT: &&str = &"GIT_COMMIT";
fn main() {
// Only use build_data if the environment variable isn't set.
if std::env::var(GIT_COMMIT).is_err() {
build_data::set_GIT_COMMIT();
build_data::set_GIT_DIRTY();
build_data::no_debug_rebuilds();
}
let out_dir = std::env::var("OUT_DIR").unwrap();
let destination = Path::new(&out_dir).join("debug.rs");
let mut test_file = File::create(destination).unwrap();
// Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD
// is the root of the repository and append the crate path
let root_dir = match std::env::var("CARGO_MANIFEST_DIR") {
Ok(dir) => PathBuf::from(dir).parent().unwrap().parent().unwrap().to_path_buf(),
Err(_) => std::env::current_dir().unwrap(),
};
let test_dir = root_dir.join("test_programs");
// Rebuild if the tests have changed
println!("cargo:rerun-if-changed=tests");
println!("cargo:rerun-if-changed=ignored-tests.txt");
println!("cargo:rerun-if-changed={}", test_dir.as_os_str().to_str().unwrap());
generate_debugger_tests(&mut test_file, &test_dir);
}
fn generate_debugger_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "execution_success";
let test_data_dir = test_data_dir.join(test_sub_dir);
let test_case_dirs =
std::fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());
let ignored_tests_contents = std::fs::read_to_string("ignored-tests.txt").unwrap();
let ignored_tests = ignored_tests_contents.lines().collect::<HashSet<_>>();
for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
let ignored = ignored_tests.contains(test_name.as_str());
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();
write!(
test_file,
r#"
#[test]
{ignored}
fn debug_{test_name}() {{
debugger_execution_success("{test_dir}");
}}
"#,
test_dir = test_dir.display(),
ignored = if ignored { "#[ignore]" } else { "" },
)
.expect("Could not write templated test file.");
}
}