write test output to filename specified by context

This commit is contained in:
collin 2020-08-15 23:25:34 -07:00
parent 5efa131c65
commit 9a2fe61c6f
4 changed files with 34 additions and 8 deletions

View File

@ -153,7 +153,12 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
/// Synthesizes the circuit for test functions with program input. /// Synthesizes the circuit for test functions with program input.
pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(), CompilerError> { pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(), CompilerError> {
generate_test_constraints::<F, G>(self.program, input_pairs, &self.imported_programs) generate_test_constraints::<F, G>(
self.program,
input_pairs,
&self.imported_programs,
&self.output_directory,
)
} }
/// Calls the internal generate_constraints method with arguments /// Calls the internal generate_constraints method with arguments
@ -205,6 +210,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstraintSynthesizer<F> for Compil
// Write results to file // Write results to file
let output_file = OutputFile::new(&package_name); let output_file = OutputFile::new(&package_name);
log::info!("Writing to output registers...");
output_file.write(&output_directory, result.bytes()).unwrap(); output_file.write(&output_directory, result.bytes()).unwrap();
Ok(()) Ok(())

View File

@ -8,6 +8,7 @@ use crate::{
GroupType, GroupType,
ImportParser, ImportParser,
OutputBytes, OutputBytes,
OutputFile,
}; };
use leo_typed::{Input, Program}; use leo_typed::{Input, Program};
@ -17,6 +18,7 @@ use snarkos_models::{
curves::{Field, PrimeField}, curves::{Field, PrimeField},
gadgets::r1cs::{ConstraintSystem, TestConstraintSystem}, gadgets::r1cs::{ConstraintSystem, TestConstraintSystem},
}; };
use std::path::PathBuf;
pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>( pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
cs: &mut CS, cs: &mut CS,
@ -47,6 +49,7 @@ pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
program: Program, program: Program,
input: InputPairs, input: InputPairs,
imported_programs: &ImportParser, imported_programs: &ImportParser,
output_directory: &PathBuf,
) -> Result<(), CompilerError> { ) -> Result<(), CompilerError> {
let mut resolved_program = ConstrainedProgram::<F, G>::new(); let mut resolved_program = ConstrainedProgram::<F, G>::new();
let program_name = program.get_name(); let program_name = program.get_name();
@ -64,13 +67,20 @@ pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
for (test_name, test) in tests.into_iter() { for (test_name, test) in tests.into_iter() {
let cs = &mut TestConstraintSystem::<F>::new(); let cs = &mut TestConstraintSystem::<F>::new();
let full_test_name = format!("{}::{}", program_name.clone(), test_name.to_string()); 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 // get input file name from annotation or use test_name
let input_pair = match test.input_file { let input_pair = match test.input_file {
Some(file_name) => match input.pairs.get(&file_name.name) { Some(file_id) => {
Some(pair) => pair.to_owned(), let file_name = file_id.name;
None => return Err(CompilerError::InvalidTestContext(file_name.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)?, None => default.ok_or(CompilerError::NoTestInput)?,
}; };
@ -101,7 +111,13 @@ pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
cs.is_satisfied() 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 { } else {
log::error!("test {} errored: {}", full_test_name, result.unwrap_err()); log::error!("test {} errored: {}", full_test_name, result.unwrap_err());
} }

View File

@ -40,7 +40,6 @@ impl OutputFile {
// create output file // create output file
let path = self.setup_file_path(path); let path = self.setup_file_path(path);
let mut file = File::create(&path)?; let mut file = File::create(&path)?;
log::info!("Writing to output registers...");
Ok(file.write_all(bytes)?) Ok(file.write_all(bytes)?)
} }

View File

@ -6,7 +6,7 @@ use crate::{
use leo_compiler::{compiler::Compiler, group::targets::edwards_bls12::EdwardsGroupType}; use leo_compiler::{compiler::Compiler, group::targets::edwards_bls12::EdwardsGroupType};
use leo_package::{ use leo_package::{
inputs::*, inputs::*,
outputs::OUTPUTS_DIRECTORY_NAME, outputs::{OutputsDirectory, OUTPUTS_DIRECTORY_NAME},
root::Manifest, root::Manifest,
source::{MainFile, MAIN_FILE_NAME, SOURCE_DIRECTORY_NAME}, source::{MainFile, MAIN_FILE_NAME, SOURCE_DIRECTORY_NAME},
}; };
@ -63,6 +63,9 @@ impl CLI for TestCommand {
let mut output_directory = package_path.clone(); let mut output_directory = package_path.clone();
output_directory.push(OUTPUTS_DIRECTORY_NAME); output_directory.push(OUTPUTS_DIRECTORY_NAME);
// Create the output directory
OutputsDirectory::create(&package_path)?;
// Parse the current main program file // Parse the current main program file
let program = Compiler::<Fq, EdwardsGroupType>::parse_program_without_input( let program = Compiler::<Fq, EdwardsGroupType>::parse_program_without_input(
package_name.clone(), package_name.clone(),