-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
92 lines (79 loc) · 3.19 KB
/
Copy pathbuild.rs
File metadata and controls
92 lines (79 loc) · 3.19 KB
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
use std::{env, fs, io::Write, path::Path};
use walkdir::WalkDir;
const EXT1: &str = "tao";
const EXT2: &str = "ore";
const HARNESS_DIR: &str = "harness";
const OUT_NAME: &str = "tests.rs";
const SRC_DIR: &str = "src";
const TEST_DIR: &str = "tests";
const TEST_FAILED: &str = "TEST_FAILED";
fn main() {
// Create a timestamp file
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("timestamp.txt");
let mut f = fs::File::create(dest_path).unwrap();
// write!(f, r#""{}""#, time::OffsetDateTime::now_utc()).unwrap();
write!(f, r#""{}""#, chrono::Utc::now().to_rfc3339()).unwrap();
// Generate the harness tests
let tests = generate_tests(HARNESS_DIR);
let dest_path = Path::new(&out_dir).join(OUT_NAME);
fs::write(dest_path, tests).unwrap();
println!("cargo:rerun-if-changed={SRC_DIR}");
println!("cargo:rerun-if-changed={TEST_DIR}/{HARNESS_DIR}");
}
fn generate_tests(path: &str) -> String {
let mut tests = String::new();
tests += "use std::path::Path;\n";
let mut in_dir = std::env::current_dir().unwrap();
in_dir.push(TEST_DIR);
in_dir.push(path);
for entry in WalkDir::new(&in_dir).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
let root = path.parent().unwrap();
match env::var(TEST_FAILED).unwrap_or_default() {
ref s if s.is_empty() => {
if root.file_name().unwrap() == "failing" {
continue;
}
}
_ => {
if root.file_name().unwrap() != "failing" {
continue;
}
}
}
if path.is_file() {
let ext = path.extension().unwrap();
if ext != EXT1 && ext != EXT2 {
continue;
}
let parent = root.file_name().unwrap().to_str().unwrap();
let name = path.file_stem().unwrap().to_str().unwrap();
let contents = fs::read_to_string(path).unwrap();
tests += "#[test_log::test]\n";
tests += &format!("fn {parent}_{name}() {{\n");
tests += " color_backtrace::install();\n";
tests += &format!(" let cwd = Path::new(\"{}\");\n", root.display());
tests +=
&format!(" let result = run_program(\"{name}\", r#\"{contents}\"#, &cwd.to_path_buf());\n");
let stderr = root.join(format!("{}.stderr", name));
if stderr.exists() {
tests += &format!(
" let _ = result.map_err(|e| {{assert!(diff_with_file(\"{stderr}\", \"{name}\", &e).is_ok()); Err::<(), String>(e)}});\n",
stderr = stderr.display()
);
} else {
tests += " assert!(result.is_ok());\n";
}
let stdout = root.join(format!("{}.stdout", name));
if stdout.exists() {
tests += &format!(
" let _ = result.map(|ok| {{assert!(diff_with_file(\"{stdout}\", \"{name}\", &ok.1).is_ok()); ok}});\n",
stdout = stdout.display()
);
}
tests += "}\n\n";
}
}
tests
}