Skip to content

Commit

Permalink
fix: fix should_fail_with (#2940)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Oct 2, 2023
1 parent 0dd1e77 commit 4f07b84
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tooling/nargo/src/ops/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ fn test_status_program_compile_fail(
return TestStatus::CompileError(diag);
}

check_expected_failure_message(test_function, &diag.diagnostic.message)
// Given "Failed constraint: 'reason'"
// This method will return "reason"
fn extract_constraint_error_message(input: &str) -> Option<String> {
input.split(": '").nth(1)?.split('\'').next().map(str::to_string)
}

check_expected_failure_message(
test_function,
&extract_constraint_error_message(&diag.diagnostic.message).unwrap(),
)
}

/// The test function compiled successfully.
Expand Down
78 changes: 78 additions & 0 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ fn main() {
let test_dir = manifest_dir.join("tests");

generate_execution_success_tests(&mut test_file, &test_dir);
generate_noir_test_success_tests(&mut test_file, &test_dir);
generate_noir_test_failure_tests(&mut test_file, &test_dir);
generate_compile_success_empty_tests(&mut test_file, &test_dir);
generate_compile_success_contract_tests(&mut test_file, &test_dir);
generate_compile_failure_tests(&mut test_file, &test_dir);
Expand Down Expand Up @@ -83,6 +85,82 @@ fn execution_success_{test_name}() {{
}
}

fn generate_noir_test_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "noir_test_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 noir_test_success_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.env("NARGO_BACKEND_PATH", path_to_mock_backend());
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("test");
cmd.assert().success();
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}

fn generate_noir_test_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "noir_test_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#"
#[test]
fn noir_test_failure_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.env("NARGO_BACKEND_PATH", path_to_mock_backend());
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("test");
cmd.assert().failure();
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}

fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "compile_success_empty";
let test_data_dir = test_data_dir.join(test_sub_dir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "should_fail_with_mismatch"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#[test(should_fail_with = "Not equal")]
fn test_main_1() {
assert_eq(0, 1, "Different string");
}

// The assert message has a space
#[test(should_fail_with = "Not equal")]
fn test_main_1() {
assert_eq(0, 1, "Not equal ");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "should_fail_with_match"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#[test(should_fail_with = "Not equal")]
fn test_main_1() {
assert_eq(0, 1, "Not equal");
}

0 comments on commit 4f07b84

Please sign in to comment.