From dcd3c5202473ec2819371b311debff3e35e0ee58 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:39:35 +0100 Subject: [PATCH] chore: split `test_program`s into modules (#6101) # Description ## Problem\* Resolves ## 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. --- tooling/nargo_cli/build.rs | 84 +++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 11 deletions(-) diff --git a/tooling/nargo_cli/build.rs b/tooling/nargo_cli/build.rs index 7469c8be0f8..9f694080cf5 100644 --- a/tooling/nargo_cli/build.rs +++ b/tooling/nargo_cli/build.rs @@ -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, @@ -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(); @@ -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#" @@ -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#" @@ -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#" @@ -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#" @@ -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#" @@ -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(); @@ -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!( @@ -224,17 +261,25 @@ 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#" @@ -242,18 +287,26 @@ fn generate_compile_success_contract_tests(test_file: &mut File, test_data_dir: 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#" @@ -261,17 +314,25 @@ fn generate_compile_success_no_bug_tests(test_file: &mut File, test_data_dir: &P 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"); @@ -279,4 +340,5 @@ fn generate_compile_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(); }