Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: separate integration test cases into directories based on expected result #2198

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ result
*.vk
**/Verifier.toml
**/target
!crates/nargo_cli/tests/test_data/*/target
!crates/nargo_cli/tests/test_data/*/target/witness.tr
!crates/nargo_cli/tests/execution_success/*/target
!crates/nargo_cli/tests/execution_success/*/target/witness.tr
124 changes: 85 additions & 39 deletions crates/nargo_cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rustc_version::{version, Version};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -32,43 +31,25 @@ fn main() {
let destination = Path::new(&out_dir).join("execute.rs");
let mut test_file = File::create(destination).unwrap();

generate_tests(&mut test_file);
}

fn load_conf(conf_path: &Path) -> BTreeMap<String, Vec<String>> {
let config_str = std::fs::read_to_string(conf_path).unwrap();

let mut conf_data = match toml::from_str(&config_str) {
Ok(t) => t,
Err(_) => {
BTreeMap::from([("exclude".to_string(), Vec::new()), ("fail".to_string(), Vec::new())])
}
};
if conf_data.get("exclude").is_none() {
conf_data.insert("exclude".to_string(), Vec::new());
}
if conf_data.get("fail").is_none() {
conf_data.insert("fail".to_string(), Vec::new());
}
conf_data
}

fn generate_tests(test_file: &mut File) {
// Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD
// is the root of the repository and append the crate path
let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") {
Ok(dir) => PathBuf::from(dir),
Err(_) => std::env::current_dir().unwrap().join("crates").join("nargo_cli"),
};
let test_sub_dir = "test_data";
let test_data_dir = manifest_dir.join("tests").join(test_sub_dir);
let config_path = test_data_dir.join("config.toml");
let test_dir = manifest_dir.join("tests");

generate_execution_success_tests(&mut test_file, &test_dir);
generate_compile_success_tests(&mut test_file, &test_dir);
generate_compile_failure_tests(&mut test_file, &test_dir);
}

// Load config.toml file from `test_data` directory
let config_data: BTreeMap<String, Vec<String>> = load_conf(&config_path);
fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "execution_success";
let test_data_dir = test_data_dir.join(test_sub_dir);

let test_case_dirs =
fs::read_dir(&test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());

for test_dir in test_case_dirs {
let test_name =
Expand All @@ -80,28 +61,93 @@ fn generate_tests(test_file: &mut File) {
};
let test_dir = &test_dir.path();

let exclude_macro =
if config_data["exclude"].contains(&test_name) { "#[ignore]" } else { "" };
write!(
test_file,
r#"
#[test]
fn execution_success_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");

let should_fail = config_data["fail"].contains(&test_name);
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("execute");

cmd.assert().success();
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}

fn generate_compile_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "compile_success";
let test_data_dir = test_data_dir.join(test_sub_dir);

let test_case_dirs =
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());

for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();

write!(
test_file,
r#"
#[test]
fn compile_success_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");

let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("info");

// `compile_success` tests should be able to compile down to an empty circuit.
cmd.assert().stdout(predicate::str::contains("Total ACIR opcodes generated for language PLONKCSat {{ width: 3 }}: 0"));
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}

fn generate_compile_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "compile_failure";
let test_data_dir = test_data_dir.join(test_sub_dir);

let test_case_dirs =
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());

for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();

write!(
test_file,
r#"
{exclude_macro}
#[test]
fn execute_{test_sub_dir}_{test_name}() {{
fn compile_failure_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");

let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("execute");

if {should_fail} {{
cmd.assert().failure();
}} else {{
cmd.assert().success();
}}
cmd.assert().failure();
}}
"#,
test_dir = test_dir.display(),
Expand Down
1 change: 1 addition & 0 deletions crates/nargo_cli/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod tests {
// Some of these imports are consumed by the injected tests
use assert_cmd::prelude::*;
use predicates::prelude::*;
use tempdir::TempDir;

use std::collections::BTreeMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"array","length":5,"type":{"kind":"integer","sign":"unsigned","width":32}},"visibility":"private"},{"name":"z","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"t","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"index","type":{"kind":"array","length":5,"type":{"kind":"field"}},"visibility":"private"},{"name":"index2","type":{"kind":"array","length":5,"type":{"kind":"field"}},"visibility":"private"},{"name":"offset","type":{"kind":"field"},"visibility":"private"},{"name":"sublen","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"index":[8,9,10,11,12],"index2":[13,14,15,16,17],"offset":[18],"sublen":[19],"t":[7],"x":[1,2,3,4,5],"z":[6]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/+1d6XITRxBuS7LBHAZibOxwbUziAAG8q8OWCAkYCDnIfd8BgWRsCAFCpUIVyQ/yFnm4vArZxrNidr0yRc3XU9vFTtXUHpZ7vj7m69lxa/0PEf1L620k7hVzDKzrSua6mrmuZa5HzfWokTuakV81vzNq3RvLyNhiyRixZGyN+3jct8V9e9x3mJ9VrM/sjPtE3HfFfXfc95gxq7SxjZjjOXMM3VprC05WmAPXVXYkKHswRsWSudccp6x74+aYxBa3McsfiZ84RpZpo69GrPOK+Ux1k8+MDJEzbt1Lfn/CwkI4m4RjBI+1cMKSiQYcJRORHRjQ04k5Za4px3igsVOTqBEuNpu9pXovakRXw3qn226FzVZ3sR21o1a7db3ebjR67WZ7qdPtLIWdqNnoRf1Wp943eux1l9UwssIpnI7hMEKqgP04Viz9JQnIO7lNm+M+697zkNuc9XvDyG2Onk1ueXJKchveBuQ2bRmTr9mRQWZMMLmJTUhXWX/gdAw3s58roU8Ddd4H9CvAfgMC87RajRD699dbNweuOkKfMcdZ615J6BiZXgh9htKEzo4MMmOiCd2eRK7kNkM4cpsl3OSuUX5zlZ+0RB6a4Gap+BhfFsDIrSKI0zU29wNk9Z6EZqdepRxiEfDTfpys0MZ7wDqvZXzHLeErAVKOKDNO1o6ipC3lpAMCcg8SLvil9D6I91GKUIpu06ShiW8WiPMQoYiv39OalA8pwHiYSEVSPgyMzQAWm51rvpJyQDJJ+RXrvEzKjjIDY1C03DkqdlJmvefwPhJNymibJq3ISfk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