From 468fbbca43e33b23bc662bf1d36dcb79830a291c Mon Sep 17 00:00:00 2001 From: grasshopper47 <49437873+grasshopper47@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:11:39 +0100 Subject: [PATCH] feat: Enhance test information output (#3696) # Description ## Problem\* Resolves https://github.com/noir-lang/noir/issues/3686 ## Summary\* Improved the output of tests by including passed/failed count in the last line print. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** 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/src/cli/test_cmd.rs | 66 +++++++++++++++++++-------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/tooling/nargo_cli/src/cli/test_cmd.rs b/tooling/nargo_cli/src/cli/test_cmd.rs index e117d8555a5..1b6dbcab34a 100644 --- a/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/tooling/nargo_cli/src/cli/test_cmd.rs @@ -82,7 +82,7 @@ pub(crate) fn run( fn run_tests( blackbox_solver: &S, package: &Package, - test_name: FunctionNameMatch, + fn_name: FunctionNameMatch, show_output: bool, compile_options: &CompileOptions, ) -> Result<(), CliError> { @@ -95,10 +95,27 @@ fn run_tests( compile_options.silence_warnings, )?; - let test_functions = context.get_all_test_functions_in_crate_matching(&crate_id, test_name); + let test_functions = context.get_all_test_functions_in_crate_matching(&crate_id, fn_name); + let count_all = test_functions.len(); + if count_all == 0 { + return match &fn_name { + FunctionNameMatch::Anything => { + Err(CliError::Generic(format!("[{}] Found 0 tests.", package.name))) + } + FunctionNameMatch::Exact(pattern) => Err(CliError::Generic(format!( + "[{}] Found 0 tests matching input '{pattern}'.", + package.name + ))), + FunctionNameMatch::Contains(pattern) => Err(CliError::Generic(format!( + "[{}] Found 0 tests containing '{pattern}'.", + package.name + ))), + }; + } - println!("[{}] Running {} test functions", package.name, test_functions.len()); - let mut failing = 0; + let plural = if count_all == 1 { "" } else { "s" }; + println!("[{}] Running {count_all} test function{plural}", package.name); + let mut count_failed = 0; let writer = StandardStream::stderr(ColorChoice::Always); let mut writer = writer.lock(); @@ -116,13 +133,10 @@ fn run_tests( writeln!(writer, "ok").expect("Failed to write to stdout"); } TestStatus::Fail { message, error_diagnostic } => { - let writer = StandardStream::stderr(ColorChoice::Always); - let mut writer = writer.lock(); writer .set_color(ColorSpec::new().set_fg(Some(Color::Red))) .expect("Failed to set color"); - writeln!(writer, "{message}").expect("Failed to write to stdout"); - writer.reset().expect("Failed to reset writer"); + writeln!(writer, "{message}\n").expect("Failed to write to stdout"); if let Some(diag) = error_diagnostic { noirc_errors::reporter::report_all( context.file_manager.as_file_map(), @@ -131,7 +145,7 @@ fn run_tests( compile_options.silence_warnings, ); } - failing += 1; + count_failed += 1; } TestStatus::CompileError(err) => { noirc_errors::reporter::report_all( @@ -140,21 +154,37 @@ fn run_tests( compile_options.deny_warnings, compile_options.silence_warnings, ); - failing += 1; + count_failed += 1; } } writer.reset().expect("Failed to reset writer"); } - if failing == 0 { - write!(writer, "[{}] ", package.name).expect("Failed to write to stdout"); + write!(writer, "[{}] ", package.name).expect("Failed to write to stdout"); + + if count_failed == 0 { writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).expect("Failed to set color"); - writeln!(writer, "All tests passed").expect("Failed to write to stdout"); + writeln!(writer, "{count_all} test{plural} passed").expect("Failed to write to stdout"); + writer.reset().expect("Failed to reset writer"); + + Ok(()) } else { - let plural = if failing == 1 { "" } else { "s" }; - return Err(CliError::Generic(format!("[{}] {failing} test{plural} failed", package.name))); - } + let count_passed = count_all - count_failed; + let plural_failed = if count_failed == 1 { "" } else { "s" }; + let plural_passed = if count_passed == 1 { "" } else { "s" }; + + if count_passed != 0 { + writer + .set_color(ColorSpec::new().set_fg(Some(Color::Green))) + .expect("Failed to set color"); + write!(writer, "{count_passed} test{plural_passed} passed, ",) + .expect("Failed to write to stdout"); + } + writer.set_color(ColorSpec::new().set_fg(Some(Color::Red))).expect("Failed to set color"); + writeln!(writer, "{count_failed} test{plural_failed} failed") + .expect("Failed to write to stdout"); + writer.reset().expect("Failed to reset writer"); - writer.reset().expect("Failed to reset writer"); - Ok(()) + Err(CliError::Generic(String::new())) + } }