-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
72 lines (65 loc) · 1.84 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
use glob::glob;
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("generated_tests.rs");
let mut file = fs::File::create(dest_path).unwrap();
let resources = get_test_resources("tests/testsuite/**/*.json");
for resource in resources {
if resource.contains("/skip/") {
writeln!(
file,
r#"
#[test]
#[ignore]
fn test_{}() {{
test_case(r"{}");
}}
"#,
sanitize_filename(&resource),
resource
)
.unwrap();
} else {
writeln!(
file,
r#"
#[test]
fn test_{}() {{
test_case(r"{}");
}}
"#,
sanitize_filename(&resource),
resource
)
.unwrap();
}
}
}
fn get_test_resources(pattern: &str) -> Vec<String> {
glob(pattern)
.expect("Failed to read glob pattern")
.filter_map(Result::ok)
.filter(|path| !path.to_string_lossy().contains("datasets")) // Exclude datasets folder
.map(|path| path.to_string_lossy().into_owned())
.collect()
}
fn sanitize_filename(filename: &str) -> String {
let mut sanitized = String::new();
let mut prev_was_underscore = false;
for c in filename.chars() {
if c.is_alphanumeric() {
if prev_was_underscore {
sanitized.push('_');
prev_was_underscore = false;
}
sanitized.push(c.to_ascii_lowercase());
} else {
prev_was_underscore = true;
}
}
sanitized
}