summaryrefslogtreecommitdiff
path: root/build.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-12-08 11:15:04 +0100
committerLaurenz <laurmaedje@gmail.com>2019-12-08 11:36:18 +0100
commit64f938b449b7ff5e53b6a06ed943bf9dedc1014b (patch)
treec8237458ea10c21e9b3ce9fc2c02bdd7df9a1fca /build.rs
parentf364395e1d774456500ea61bb7b931f48a62ddfa (diff)
Improve testers ♻
Diffstat (limited to 'build.rs')
-rw-r--r--build.rs41
1 files changed, 28 insertions, 13 deletions
diff --git a/build.rs b/build.rs
index c91db8e0..1254e33d 100644
--- a/build.rs
+++ b/build.rs
@@ -1,34 +1,49 @@
-use std::fs;
+use std::fs::{self, create_dir_all, read_dir, read_to_string};
use std::ffi::OsStr;
-fn main() {
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ create_dir_all("tests/cache")?;
+
+ // Make sure the script reruns if this file changes or files are
+ // added/deleted in the parsing folder.
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=tests/parsing");
- fs::create_dir_all("tests/cache").unwrap();
+ // Compile all parser tests into a single giant vector.
+ let mut code = "vec![".to_string();
- let paths = fs::read_dir("tests/parsing").unwrap()
- .map(|entry| entry.unwrap().path())
- .filter(|path| path.extension() == Some(OsStr::new("rs")));
+ for entry in read_dir("tests/parsing")? {
+ let path = entry?.path();
+ if path.extension() != Some(OsStr::new("rs")) {
+ continue;
+ }
- let mut code = "vec![".to_string();
- for path in paths {
- let name = path.file_stem().unwrap().to_str().unwrap();
- let file = fs::read_to_string(&path).unwrap();
+ let name = path
+ .file_stem().ok_or("expected file stem")?
+ .to_string_lossy();
+ // Make sure this also reruns if the contents of a file in parsing
+ // change. This is not ensured by rerunning only on the folder.
println!("cargo:rerun-if-changed=tests/parsing/{}.rs", name);
code.push_str(&format!("(\"{}\", tokens!{{", name));
+ // Replace the `=>` arrows with a double arrow indicating the line
+ // number in the middle, such that the tester can tell which line number
+ // a test originated from.
+ let file = read_to_string(&path)?;
for (index, line) in file.lines().enumerate() {
- let mut line = line.replace("=>", &format!("=>({})=>", index + 1));
- line.push('\n');
+ let line = line.replace("=>", &format!("=>({})=>", index + 1));
code.push_str(&line);
+ code.push('\n');
}
code.push_str("}),");
}
+
code.push(']');
- fs::write("tests/cache/parsing.rs", code).unwrap();
+ fs::write("tests/cache/parse", code)?;
+
+ Ok(())
}