Merge pull request #57 from chriseth/tempdir

Properly use temporary directories for tests.
This commit is contained in:
chriseth
2023-02-27 15:49:01 +01:00
committed by GitHub
5 changed files with 32 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ build = "build.rs"
codespan-reporting = "0.11.1"
json = "0.12.4"
lalrpop-util = {version = "0.19.8", features = ["lexer"]}
mktemp = "0.5.0"
regex = "1"
[build-dependencies]

View File

@@ -18,6 +18,10 @@ fn main() {
Err(err) => err.output_to_stderr(),
}
} else {
powdr::compiler::compile_pil(Path::new(&env::args().nth(1).unwrap()), no_callback());
powdr::compiler::compile_pil(
Path::new(&env::args().nth(1).unwrap()),
&env::current_dir().unwrap(),
no_callback(),
);
}
}

View File

@@ -14,20 +14,21 @@ pub fn no_callback() -> Option<fn(&str) -> Option<ConstantNumberType>> {
/// constants and committed polynomials.
pub fn compile_pil(
pil_file: &Path,
output_dir: &Path,
query_callback: Option<impl FnMut(&str) -> Option<ConstantNumberType>>,
) {
let analyzed = analyzer::analyze(pil_file);
let (constants, degree) = constant_evaluator::generate(&analyzed);
if analyzed.constant_count() == constants.len() {
write_polys_file(
&mut BufWriter::new(&mut fs::File::create("constants.bin").unwrap()),
&mut BufWriter::new(&mut fs::File::create(output_dir.join("constants.bin")).unwrap()),
degree,
&constants,
);
println!("Wrote constants.bin.");
let commits = commit_evaluator::generate(&analyzed, &degree, &constants, query_callback);
write_polys_file(
&mut BufWriter::new(&mut fs::File::create("commits.bin").unwrap()),
&mut BufWriter::new(&mut fs::File::create(output_dir.join("commits.bin")).unwrap()),
degree,
&commits,
);
@@ -38,7 +39,7 @@ pub fn compile_pil(
let json_out = json_exporter::export(&analyzed);
let json_file = format!("{}.json", pil_file.file_name().unwrap().to_str().unwrap());
json_out
.write(&mut fs::File::create(&json_file).unwrap())
.write(&mut fs::File::create(output_dir.join(&json_file)).unwrap())
.unwrap();
println!("Wrote {json_file}.");
}

View File

@@ -345,6 +345,9 @@ mod test {
use super::*;
fn generate_json_pair(file: &str) -> (JsonValue, JsonValue) {
let temp_dir = mktemp::Temp::new_dir().unwrap();
let output_file = temp_dir.join("out.json");
let analyzed = analyzer::analyze(Path::new(file));
let json_out = export(&analyzed);
@@ -352,7 +355,12 @@ mod test {
"Please set the PILCOM environment variable to the path to the pilcom repository.",
);
let pilcom_output = Command::new("node")
.args([format!("{pilcom}/src/pil.js"), file.to_string()])
.args([
format!("{pilcom}/src/pil.js"),
file.to_string(),
"-o".to_string(),
format!("{}", output_file.to_string_lossy()),
])
.output()
.expect("failed to run pilcom");
if !pilcom_output.status.success() {
@@ -363,19 +371,10 @@ mod test {
);
}
let output_file = format!(
"{}.json",
Path::new(file)
.canonicalize()
.unwrap()
.file_name()
.unwrap()
.to_str()
.unwrap()
);
let pilcom_out = fs::read_to_string(&output_file).unwrap_or_else(|_| {
panic!("Pilcom did not generate {output_file} at the expected location.")
panic!("Pilcom did not generate {output_file:?} at the expected location.")
});
drop(temp_dir);
let pilcom_parsed = json::parse(&pilcom_out).expect("Invalid json from pilcom.");
(json_out, pilcom_parsed)
}

View File

@@ -3,19 +3,23 @@ use std::{path::Path, process::Command};
use powdr::{analyzer::ConstantNumberType, compiler};
fn verify(file_name: &str, query_callback: Option<fn(&str) -> Option<ConstantNumberType>>) {
compiler::compile_pil(Path::new(&format!("./tests/{file_name}")), query_callback);
let input_file = Path::new(&format!("./tests/{file_name}"))
.canonicalize()
.unwrap();
let temp_dir = mktemp::Temp::new_dir().unwrap();
compiler::compile_pil(&input_file, &temp_dir, query_callback);
let pilcom = std::env::var("PILCOM")
.expect("Please set the PILCOM environment variable to the path to the pilcom repository.");
let verifier_output = Command::new("node")
.args([
format!("{pilcom}/src/main_pilverifier.js"),
"commits.bin".to_string(),
format!("{}/commits.bin", temp_dir.as_path().to_string_lossy()),
"-j".to_string(),
format!("{file_name}.json"),
format!("{}/{file_name}.json", temp_dir.as_path().to_string_lossy()),
"-c".to_string(),
"constants.bin".to_string(),
format!("{}/constants.bin", temp_dir.as_path().to_string_lossy()),
])
.output()
.expect("failed to run pil verifier");
@@ -31,6 +35,8 @@ fn verify(file_name: &str, query_callback: Option<fn(&str) -> Option<ConstantNum
panic!("Verified did not say 'PIL OK': {output}");
}
}
drop(temp_dir);
}
#[test]