From 9a2fe61c6fd03b026ceef28da7de41f5deb826e0 Mon Sep 17 00:00:00 2001 From: collin Date: Sat, 15 Aug 2020 23:25:34 -0700 Subject: [PATCH] write test output to filename specified by context --- compiler/src/compiler.rs | 10 +++++++++- compiler/src/constraints/constraints.rs | 26 ++++++++++++++++++++----- compiler/src/output/output_file.rs | 1 - leo/commands/test.rs | 5 ++++- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 2405efb79b..60ee516f8e 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -153,7 +153,12 @@ impl> Compiler { /// Synthesizes the circuit for test functions with program input. pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(), CompilerError> { - generate_test_constraints::(self.program, input_pairs, &self.imported_programs) + generate_test_constraints::( + self.program, + input_pairs, + &self.imported_programs, + &self.output_directory, + ) } /// Calls the internal generate_constraints method with arguments @@ -205,6 +210,9 @@ impl> ConstraintSynthesizer for Compil // Write results to file let output_file = OutputFile::new(&package_name); + + log::info!("Writing to output registers..."); + output_file.write(&output_directory, result.bytes()).unwrap(); Ok(()) diff --git a/compiler/src/constraints/constraints.rs b/compiler/src/constraints/constraints.rs index 6fefa8f259..5880bdadc7 100644 --- a/compiler/src/constraints/constraints.rs +++ b/compiler/src/constraints/constraints.rs @@ -8,6 +8,7 @@ use crate::{ GroupType, ImportParser, OutputBytes, + OutputFile, }; use leo_typed::{Input, Program}; @@ -17,6 +18,7 @@ use snarkos_models::{ curves::{Field, PrimeField}, gadgets::r1cs::{ConstraintSystem, TestConstraintSystem}, }; +use std::path::PathBuf; pub fn generate_constraints, CS: ConstraintSystem>( cs: &mut CS, @@ -47,6 +49,7 @@ pub fn generate_test_constraints>( program: Program, input: InputPairs, imported_programs: &ImportParser, + output_directory: &PathBuf, ) -> Result<(), CompilerError> { let mut resolved_program = ConstrainedProgram::::new(); let program_name = program.get_name(); @@ -64,13 +67,20 @@ pub fn generate_test_constraints>( for (test_name, test) in tests.into_iter() { let cs = &mut TestConstraintSystem::::new(); let full_test_name = format!("{}::{}", program_name.clone(), test_name.to_string()); + let mut output_file_name = program_name.clone(); // get input file name from annotation or use test_name let input_pair = match test.input_file { - Some(file_name) => match input.pairs.get(&file_name.name) { - Some(pair) => pair.to_owned(), - None => return Err(CompilerError::InvalidTestContext(file_name.name)), - }, + Some(file_id) => { + let file_name = file_id.name; + + output_file_name = file_name.clone(); + + match input.pairs.get(&file_name) { + Some(pair) => pair.to_owned(), + None => return Err(CompilerError::InvalidTestContext(file_name)), + } + } None => default.ok_or(CompilerError::NoTestInput)?, }; @@ -101,7 +111,13 @@ pub fn generate_test_constraints>( cs.is_satisfied() ); - // write result to file + // write result to file + let output = result?; + let output_file = OutputFile::new(&output_file_name); + + log::info!("\tWriting output to registers in `{}.out` ...", output_file_name); + + output_file.write(output_directory, output.bytes()).unwrap(); } else { log::error!("test {} errored: {}", full_test_name, result.unwrap_err()); } diff --git a/compiler/src/output/output_file.rs b/compiler/src/output/output_file.rs index d60f60321d..b495422919 100644 --- a/compiler/src/output/output_file.rs +++ b/compiler/src/output/output_file.rs @@ -40,7 +40,6 @@ impl OutputFile { // create output file let path = self.setup_file_path(path); let mut file = File::create(&path)?; - log::info!("Writing to output registers..."); Ok(file.write_all(bytes)?) } diff --git a/leo/commands/test.rs b/leo/commands/test.rs index 1523c24761..ec998675b2 100644 --- a/leo/commands/test.rs +++ b/leo/commands/test.rs @@ -6,7 +6,7 @@ use crate::{ use leo_compiler::{compiler::Compiler, group::targets::edwards_bls12::EdwardsGroupType}; use leo_package::{ inputs::*, - outputs::OUTPUTS_DIRECTORY_NAME, + outputs::{OutputsDirectory, OUTPUTS_DIRECTORY_NAME}, root::Manifest, source::{MainFile, MAIN_FILE_NAME, SOURCE_DIRECTORY_NAME}, }; @@ -63,6 +63,9 @@ impl CLI for TestCommand { let mut output_directory = package_path.clone(); output_directory.push(OUTPUTS_DIRECTORY_NAME); + // Create the output directory + OutputsDirectory::create(&package_path)?; + // Parse the current main program file let program = Compiler::::parse_program_without_input( package_name.clone(),