From 31c1f11253e489b2049706b06f8b2dffa4dc6788 Mon Sep 17 00:00:00 2001 From: grasshopper47 Date: Tue, 5 Dec 2023 20:14:41 +0100 Subject: [PATCH 1/3] Enhance test information output --- tooling/nargo_cli/src/cli/test_cmd.rs | 46 ++++++++++++++++++++------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/tooling/nargo_cli/src/cli/test_cmd.rs b/tooling/nargo_cli/src/cli/test_cmd.rs index e117d8555a5..2d1ed0fb2a3 100644 --- a/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/tooling/nargo_cli/src/cli/test_cmd.rs @@ -96,9 +96,13 @@ fn run_tests( )?; let test_functions = context.get_all_test_functions_in_crate_matching(&crate_id, test_name); + let count_all = test_functions.len(); + if count_all == 0 { + return Err(CliError::Generic(format!("[{}] Found 0 tests matching input", package.name))); + } - println!("[{}] Running {} test functions", package.name, test_functions.len()); - let mut failing = 0; + println!("[{}] Running {count_all} test functions", package.name); + let mut count_failed = 0; let writer = StandardStream::stderr(ColorChoice::Always); let mut writer = writer.lock(); @@ -131,7 +135,7 @@ fn run_tests( compile_options.silence_warnings, ); } - failing += 1; + count_failed += 1; } TestStatus::CompileError(err) => { noirc_errors::reporter::report_all( @@ -140,21 +144,39 @@ 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 { + let plural = if count_all == 1 { "" } else { "s" }; + 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"); + write!(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())) + } } From 225621ec9ef580be5d97eeb2b7c4af912de0d551 Mon Sep 17 00:00:00 2001 From: grasshopper47 Date: Wed, 6 Dec 2023 18:45:40 +0100 Subject: [PATCH 2/3] Include input search string in error output; light cleanup of Fail matcher --- tooling/nargo_cli/src/cli/test_cmd.rs | 38 ++++++++++++++++----------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/tooling/nargo_cli/src/cli/test_cmd.rs b/tooling/nargo_cli/src/cli/test_cmd.rs index 2d1ed0fb2a3..5b5c8a1c8f5 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,13 +95,26 @@ 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 Err(CliError::Generic(format!("[{}] Found 0 tests matching input", package.name))); + 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 {count_all} test functions", package.name); + 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); @@ -114,19 +127,16 @@ fn run_tests( match run_test(blackbox_solver, &context, test_function, show_output, compile_options) { TestStatus::Pass { .. } => { - writer - .set_color(ColorSpec::new().set_fg(Some(Color::Green))) + writer.set_color(ColorSpec::new() + .set_fg(Some(Color::Green))) .expect("Failed to set color"); 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))) + 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(), @@ -153,8 +163,6 @@ fn run_tests( write!(writer, "[{}] ", package.name).expect("Failed to write to stdout"); if count_failed == 0 { - let plural = if count_all == 1 { "" } else { "s" }; - writer.set_color(ColorSpec::new().set_fg(Some(Color::Green))).expect("Failed to set color"); writeln!(writer, "{count_all} test{plural} passed").expect("Failed to write to stdout"); writer.reset().expect("Failed to reset writer"); @@ -173,7 +181,7 @@ fn run_tests( .expect("Failed to write to stdout"); } writer.set_color(ColorSpec::new().set_fg(Some(Color::Red))).expect("Failed to set color"); - write!(writer, "{count_failed} test{plural_failed} failed") + writeln!(writer, "{count_failed} test{plural_failed} failed") .expect("Failed to write to stdout"); writer.reset().expect("Failed to reset writer"); From 8f501ad2a138984a149f9ffa9c23ef0da2f4b030 Mon Sep 17 00:00:00 2001 From: grasshopper47 Date: Wed, 6 Dec 2023 19:27:27 +0100 Subject: [PATCH 3/3] cargo fmt --- tooling/nargo_cli/src/cli/test_cmd.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tooling/nargo_cli/src/cli/test_cmd.rs b/tooling/nargo_cli/src/cli/test_cmd.rs index 5b5c8a1c8f5..1b6dbcab34a 100644 --- a/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/tooling/nargo_cli/src/cli/test_cmd.rs @@ -127,14 +127,14 @@ fn run_tests( match run_test(blackbox_solver, &context, test_function, show_output, compile_options) { TestStatus::Pass { .. } => { - writer.set_color(ColorSpec::new() - .set_fg(Some(Color::Green))) + writer + .set_color(ColorSpec::new().set_fg(Some(Color::Green))) .expect("Failed to set color"); writeln!(writer, "ok").expect("Failed to write to stdout"); } TestStatus::Fail { message, error_diagnostic } => { - writer.set_color(ColorSpec::new() - .set_fg(Some(Color::Red))) + writer + .set_color(ColorSpec::new().set_fg(Some(Color::Red))) .expect("Failed to set color"); writeln!(writer, "{message}\n").expect("Failed to write to stdout"); if let Some(diag) = error_diagnostic {