-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
99 lines (78 loc) · 2.58 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::fs::read_dir;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use walkdir::WalkDir;
fn visit_dirs(dir: &Path) -> io::Result<Vec<String>> {
let mut res = vec![];
if dir.is_dir() {
for entry in read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
res.extend(visit_dirs(&path)?);
} else if let Some(ext) = path.extension() {
if ext == "rk" && path.file_name().unwrap() == "main.rk" {
res.push(entry.path().to_str().unwrap().to_string());
}
}
}
}
Ok(res)
}
fn schedule_rerun_if_folder_changed(path: &Path) {
for entry in WalkDir::new(path) {
let entry = entry.unwrap();
if entry.path() == path {
continue;
}
if entry.path().is_dir() {
schedule_rerun_if_folder_changed(&entry.path());
} else {
println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
}
}
}
// build script's entry point
fn main() {
schedule_rerun_if_folder_changed(&PathBuf::from("src/lib/testcases/"));
let out_dir = "src/lib";
let destination = Path::new(&out_dir).join("tests.rs");
let mut output_file = File::create(&destination).unwrap();
// write test file header, put `use`, `const` etc there
write_header(&mut output_file);
for file in visit_dirs(Path::new(&"src/lib/testcases".to_string())).unwrap() {
write_test(&mut output_file, &file);
}
}
fn write_test(output_file: &mut File, path: &str) {
let path = path.replace("src/lib/", "");
let name = path.replace("./", "").replace("/", "_").replace(".rk", "");
write!(
output_file,
include_str!("src/lib/testcases/test_template"),
name = name,
path = path
)
.unwrap();
}
fn write_header(output_file: &mut File) {
write!(
output_file,
r##"use std::path::PathBuf;
#[allow(dead_code)]
fn run(path: &str, input: &str, expected_ret: &str, expected_output: &str) {{
let mut config = super::Config::default();
config.project_config.entry_point = PathBuf::from(path);
config.quiet = true;
let expected_ret = expected_ret.parse::<i64>().unwrap();
let (ret_code, stdout) = super::helpers::test_utils::run(path, input.to_string(), config);
assert_eq!(expected_ret, ret_code);
assert_eq!(expected_output, stdout);
}}
"##
)
.unwrap();
}