Skip to content

Commit

Permalink
feat: Enhance test information output (#3696)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #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.
  • Loading branch information
grasshopper47 authored Dec 6, 2023
1 parent 8da40b7 commit 468fbbc
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions tooling/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub(crate) fn run(
fn run_tests<S: BlackBoxFunctionSolver>(
blackbox_solver: &S,
package: &Package,
test_name: FunctionNameMatch,
fn_name: FunctionNameMatch,
show_output: bool,
compile_options: &CompileOptions,
) -> Result<(), CliError> {
Expand All @@ -95,10 +95,27 @@ fn run_tests<S: BlackBoxFunctionSolver>(
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();
Expand All @@ -116,13 +133,10 @@ fn run_tests<S: BlackBoxFunctionSolver>(
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(),
Expand All @@ -131,7 +145,7 @@ fn run_tests<S: BlackBoxFunctionSolver>(
compile_options.silence_warnings,
);
}
failing += 1;
count_failed += 1;
}
TestStatus::CompileError(err) => {
noirc_errors::reporter::report_all(
Expand All @@ -140,21 +154,37 @@ fn run_tests<S: BlackBoxFunctionSolver>(
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()))
}
}

0 comments on commit 468fbbc

Please sign in to comment.