Skip to content

Commit

Permalink
feat: add nargo test --format json (#6796)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Dec 13, 2024
1 parent f21015b commit eb975ab
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 20 deletions.
24 changes: 14 additions & 10 deletions compiler/fm/src/file_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ impl FileMap {
pub fn all_file_ids(&self) -> impl Iterator<Item = &FileId> {
self.name_to_id.values()
}

pub fn get_name(&self, file_id: FileId) -> Result<PathString, Error> {
let name = self.files.get(file_id.as_usize())?.name().clone();

// See if we can make the file name a bit shorter/easier to read if it starts with the current directory
if let Some(current_dir) = &self.current_dir {
if let Ok(name_without_prefix) = name.0.strip_prefix(current_dir) {
return Ok(PathString::from_path(name_without_prefix.to_path_buf()));
}
}

Ok(name)
}
}
impl Default for FileMap {
fn default() -> Self {
Expand All @@ -97,16 +110,7 @@ impl<'a> Files<'a> for FileMap {
type Source = &'a str;

fn name(&self, file_id: Self::FileId) -> Result<Self::Name, Error> {
let name = self.files.get(file_id.as_usize())?.name().clone();

// See if we can make the file name a bit shorter/easier to read if it starts with the current directory
if let Some(current_dir) = &self.current_dir {
if let Ok(name_without_prefix) = name.0.strip_prefix(current_dir) {
return Ok(PathString::from_path(name_without_prefix.to_path_buf()));
}
}

Ok(name)
self.get_name(file_id)
}

fn source(&'a self, file_id: Self::FileId) -> Result<Self::Source, Error> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn convert_diagnostic(
diagnostic.with_message(&cd.message).with_labels(secondary_labels).with_notes(notes)
}

fn stack_trace<'files>(
pub fn stack_trace<'files>(
files: &'files impl Files<'files, FileId = fm::FileId>,
call_stack: &[Location],
) -> String {
Expand Down
30 changes: 27 additions & 3 deletions tooling/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use acvm::{BlackBoxFunctionSolver, FieldElement};
use bn254_blackbox_solver::Bn254BlackBoxSolver;
use clap::Args;
use fm::FileManager;
use formatters::{Formatter, PrettyFormatter, TerseFormatter};
use formatters::{Formatter, JsonFormatter, PrettyFormatter, TerseFormatter};
use nargo::{
insert_all_files_for_workspace_into_file_manager, ops::TestStatus, package::Package, parse_all,
prepare_package, workspace::Workspace, PrintOutput,
Expand Down Expand Up @@ -71,13 +71,16 @@ enum Format {
Pretty,
/// Display one character per test
Terse,
/// Output a JSON Lines document
Json,
}

impl Format {
fn formatter(&self) -> Box<dyn Formatter> {
match self {
Format::Pretty => Box::new(PrettyFormatter),
Format::Terse => Box::new(TerseFormatter),
Format::Json => Box::new(JsonFormatter),
}
}
}
Expand All @@ -87,6 +90,7 @@ impl Display for Format {
match self {
Format::Pretty => write!(f, "pretty"),
Format::Terse => write!(f, "terse"),
Format::Json => write!(f, "json"),
}
}
}
Expand Down Expand Up @@ -211,6 +215,12 @@ impl<'a> TestRunner<'a> {
) -> bool {
let mut all_passed = true;

for (package_name, total_test_count) in test_count_per_package {
self.formatter
.package_start_async(package_name, *total_test_count)
.expect("Could not display package start");
}

let (sender, receiver) = mpsc::channel();
let iter = &Mutex::new(tests.into_iter());
thread::scope(|scope| {
Expand All @@ -228,6 +238,10 @@ impl<'a> TestRunner<'a> {
break;
};

self.formatter
.test_start_async(&test.name, &test.package_name)
.expect("Could not display test start");

let time_before_test = std::time::Instant::now();
let (status, output) = match catch_unwind(test.runner) {
Ok((status, output)) => (status, output),
Expand Down Expand Up @@ -255,6 +269,16 @@ impl<'a> TestRunner<'a> {
time_to_run,
};

self.formatter
.test_end_async(
&test_result,
self.file_manager,
self.args.show_output,
self.args.compile_options.deny_warnings,
self.args.compile_options.silence_warnings,
)
.expect("Could not display test start");

if thread_sender.send(test_result).is_err() {
break;
}
Expand All @@ -275,7 +299,7 @@ impl<'a> TestRunner<'a> {
let total_test_count = *total_test_count;

self.formatter
.package_start(package_name, total_test_count)
.package_start_sync(package_name, total_test_count)
.expect("Could not display package start");

// Check if we have buffered test results for this package
Expand Down Expand Up @@ -485,7 +509,7 @@ impl<'a> TestRunner<'a> {
current_test_count: usize,
total_test_count: usize,
) -> std::io::Result<()> {
self.formatter.test_end(
self.formatter.test_end_sync(
test_result,
current_test_count,
total_test_count,
Expand Down
Loading

0 comments on commit eb975ab

Please sign in to comment.