diff --git a/Cargo.toml b/Cargo.toml index 1643724f6..ad865aa17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/bin/compiler.rs b/src/bin/compiler.rs index abae0d0d0..6c03b1081 100644 --- a/src/bin/compiler.rs +++ b/src/bin/compiler.rs @@ -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(), + ); } } diff --git a/src/compiler.rs b/src/compiler.rs index 6a3488140..0932f4d8d 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -14,20 +14,21 @@ pub fn no_callback() -> Option Option> { /// constants and committed polynomials. pub fn compile_pil( pil_file: &Path, + output_dir: &Path, query_callback: Option Option>, ) { 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, °ree, &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}."); } diff --git a/src/json_exporter/mod.rs b/src/json_exporter/mod.rs index 09fd15218..089fd0bea 100644 --- a/src/json_exporter/mod.rs +++ b/src/json_exporter/mod.rs @@ -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) } diff --git a/tests/integration.rs b/tests/integration.rs index aba77eae9..296c9cb62 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -3,19 +3,23 @@ use std::{path::Path, process::Command}; use powdr::{analyzer::ConstantNumberType, compiler}; fn verify(file_name: &str, query_callback: Option Option>) { - 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 Option