Skip to content

Commit

Permalink
Add MatrixConfig to cover brillig
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Nov 4, 2024
1 parent 2a0edaa commit 85a6b2b
Showing 1 changed file with 61 additions and 31 deletions.
92 changes: 61 additions & 31 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,49 @@ fn read_test_cases(
})
}

fn generate_test_case(
struct MatrixConfig {
vary_brillig: bool,
vary_inliner: bool,
}

impl Default for MatrixConfig {
fn default() -> Self {
Self { vary_brillig: false, vary_inliner: true }
}
}

/// Generate all test cases for a given test directory.
/// These will be executed serially, but independently from other test directories.
/// Be careful not to run multiple tests on the same directory concurrently because
/// they can override each others' compilation artifact files.
fn generate_test_cases(
test_file: &mut File,
test_name: &str,
test_dir: &std::path::Display,
test_command: &str,
test_content: &str,
matrix_config: &MatrixConfig,
) {
let brillig_cases = if matrix_config.vary_brillig { "[false, true]" } else { "[false]" };
let inliner_cases = if matrix_config.vary_inliner { "[i64::MIN, 0, i64::MAX]" } else { "[0]" };
write!(
test_file,
r#"
#[test_case::test_matrix(
[i64::MIN, 0, i64::MAX]
{brillig_cases},
{inliner_cases}
)]
fn test_{test_name}(inliner_aggressiveness: i64) {{
fn test_{test_name}(force_brillig: bool, inliner_aggressiveness: i64) {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
nargo.arg("{test_command}").arg("--force");
nargo.arg("--inliner-aggressiveness").arg(inliner_aggressiveness.to_string());
if force_brillig {{
nargo.arg("--force-brillig");
}}
{test_content}
}}
"#
Expand All @@ -129,26 +152,19 @@ fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path)
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"execute",
r#"
nargo.assert().success();"#,
nargo.assert().success();
"#,
&MatrixConfig {
vary_brillig: !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()),
vary_inliner: true,
},
);

if !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()) {
generate_test_case(
test_file,
&format!("{test_name}_brillig"),
&test_dir,
"execute",
r#"
nargo.arg("--force-brillig");
nargo.assert().success();"#,
);
}
}
writeln!(test_file, "}}").unwrap();
}
Expand All @@ -167,13 +183,15 @@ fn generate_execution_failure_tests(test_file: &mut File, test_data_dir: &Path)
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"execute",
r#"
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());"#,
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand All @@ -193,13 +211,15 @@ fn generate_noir_test_success_tests(test_file: &mut File, test_data_dir: &Path)
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"test",
r#"
nargo.assert().success();"#,
nargo.assert().success();
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand All @@ -218,13 +238,15 @@ fn generate_noir_test_failure_tests(test_file: &mut File, test_data_dir: &Path)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"test",
r#"
nargo.assert().failure();"#,
nargo.assert().failure();
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand Down Expand Up @@ -267,16 +289,18 @@ fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Pa
assert_eq!(num_opcodes.as_u64().expect("number of opcodes should fit in a u64"), 0);
"#;

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"info",
&format!(
r#"
nargo.arg("--json");
{assert_zero_opcodes}"#,
{assert_zero_opcodes}
"#,
),
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand All @@ -296,13 +320,15 @@ fn generate_compile_success_contract_tests(test_file: &mut File, test_data_dir:
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"compile",
r#"
nargo.assert().success().stderr(predicate::str::contains("warning:").not());"#,
nargo.assert().success().stderr(predicate::str::contains("warning:").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand All @@ -323,13 +349,15 @@ fn generate_compile_success_no_bug_tests(test_file: &mut File, test_data_dir: &P
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"compile",
r#"
nargo.assert().success().stderr(predicate::str::contains("bug:").not());"#,
nargo.assert().success().stderr(predicate::str::contains("bug:").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand All @@ -349,13 +377,15 @@ fn generate_compile_failure_tests(test_file: &mut File, test_data_dir: &Path) {
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
generate_test_cases(
test_file,
&test_name,
&test_dir,
"compile",
r#"
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());"#,
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());
"#,
&MatrixConfig::default(),
);
}
writeln!(test_file, "}}").unwrap();
Expand Down

0 comments on commit 85a6b2b

Please sign in to comment.