Skip to content

Commit

Permalink
chore: split test_programs into modules (#6101)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

A small change to how we generate the testcases so it's easier to see at
a glance which folder the test comes from in the nextest output.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Sep 30, 2024
1 parent 3fc62cd commit dcd3c52
Showing 1 changed file with 73 additions and 11 deletions.
84 changes: 73 additions & 11 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ fn read_test_cases(

fn generate_test_case(
test_file: &mut File,
test_type: &str,
test_name: &str,
test_dir: &std::path::Display,
test_content: &str,
Expand All @@ -90,7 +89,7 @@ fn generate_test_case(
test_file,
r#"
#[test]
fn {test_type}_{test_name}() {{
fn test_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut nargo = Command::cargo_bin("nargo").unwrap();
Expand All @@ -105,12 +104,19 @@ fn {test_type}_{test_name}() {{
fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "execution_success";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"
Expand All @@ -122,7 +128,6 @@ fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path)
if !IGNORED_BRILLIG_TESTS.contains(&test_name.as_str()) {
generate_test_case(
test_file,
test_type,
&format!("{test_name}_brillig"),
&test_dir,
r#"
Expand All @@ -132,17 +137,25 @@ fn generate_execution_success_tests(test_file: &mut File, test_data_dir: &Path)
);
}
}
writeln!(test_file, "}}").unwrap();
}

fn generate_execution_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "execution_failure";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"
Expand All @@ -151,17 +164,25 @@ fn generate_execution_failure_tests(test_file: &mut File, test_data_dir: &Path)
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());"#,
);
}
writeln!(test_file, "}}").unwrap();
}

fn generate_noir_test_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "noir_test_success";
let test_cases = read_test_cases(test_data_dir, "noir_test_success");

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"
Expand All @@ -170,16 +191,24 @@ fn generate_noir_test_success_tests(test_file: &mut File, test_data_dir: &Path)
nargo.assert().success();"#,
);
}
writeln!(test_file, "}}").unwrap();
}

fn generate_noir_test_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "noir_test_failure";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();
generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"
Expand All @@ -188,11 +217,20 @@ fn generate_noir_test_failure_tests(test_file: &mut File, test_data_dir: &Path)
nargo.assert().failure();"#,
);
}
writeln!(test_file, "}}").unwrap();
}

fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_empty";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

Expand All @@ -213,7 +251,6 @@ fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Pa

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
&format!(
Expand All @@ -224,59 +261,84 @@ fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Pa
),
);
}
writeln!(test_file, "}}").unwrap();
}

fn generate_compile_success_contract_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_contract";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"
nargo.arg("compile").arg("--force");
nargo.assert().success();"#,
);
}
writeln!(test_file, "}}").unwrap();
}

/// Generate tests for checking that the contract compiles and there are no "bugs" in stderr
fn generate_compile_success_no_bug_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_success_no_bug";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"
nargo.arg("compile").arg("--force");
nargo.assert().success().stderr(predicate::str::contains("bug:").not());"#,
);
}
writeln!(test_file, "}}").unwrap();
}

fn generate_compile_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_type = "compile_failure";
let test_cases = read_test_cases(test_data_dir, test_type);

writeln!(
test_file,
"mod {test_type} {{
use super::*;
"
)
.unwrap();
for (test_name, test_dir) in test_cases {
let test_dir = test_dir.display();

generate_test_case(
test_file,
test_type,
&test_name,
&test_dir,
r#"nargo.arg("compile").arg("--force");
nargo.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());"#,
);
}
writeln!(test_file, "}}").unwrap();
}

0 comments on commit dcd3c52

Please sign in to comment.