Skip to content

Commit

Permalink
internal: output failure in json format
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Aug 26, 2024
1 parent a29ee09 commit 840ce7d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions crates/moon/src/cli/generate_test_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub fn generate_test_driver(
filter_file: filter_file.clone(),
filter_index,
limit: 256,
test_failure_json: false,
}),
fmt_opt: None,
sort_input,
Expand Down
4 changes: 4 additions & 0 deletions crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub struct TestSubcommand {

#[clap(long)]
pub no_parallelize: bool,

#[clap(long)]
pub test_failure_json: bool,
}

pub fn run_test(cli: UniversalFlags, cmd: TestSubcommand) -> anyhow::Result<i32> {
Expand Down Expand Up @@ -185,6 +188,7 @@ fn run_test_internal(
filter_file: filter_file.clone(),
filter_index,
limit,
test_failure_json: cmd.test_failure_json,
}),
sort_input,
run_mode,
Expand Down
43 changes: 28 additions & 15 deletions crates/moonbuild/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ async fn handle_test_result(
target_dir: &Path,
printed: Arc<AtomicBool>,
) -> anyhow::Result<()> {
let output_failure_in_json = moonbuild_opt
.test_opt
.as_ref()
.map(|it| it.test_failure_json)
.unwrap_or(false);
for item in test_res_for_cur_pkg {
match item {
Ok(ok_ts) => {
Expand Down Expand Up @@ -626,27 +631,35 @@ async fn handle_test_result(
);
}
Err(TestFailedStatus::RuntimeError(err_ts) | TestFailedStatus::Failed(err_ts)) => {
println!(
"test {}/{}::{} {}: {}",
err_ts.package,
err_ts.filename,
err_ts.test_name,
"failed".bold().red(),
err_ts.message,
);
if output_failure_in_json {
println!("{}", serde_json_lenient::to_string(err_ts)?);
} else {
println!(
"test {}/{}::{} {}: {}",
err_ts.package,
err_ts.filename,
err_ts.test_name,
"failed".bold().red(),
err_ts.message,
);
}
}
Err(TestFailedStatus::Others(e)) => {
eprintln!("{}: {}", "failed".red(), e);
}
Err(TestFailedStatus::ExpectTestFailed(origin_err)) => {
if !auto_update {
println!(
"test {}/{}::{} {}",
origin_err.package,
origin_err.filename,
origin_err.test_name,
"failed".bold().red(),
);
if output_failure_in_json {
println!("{}", serde_json_lenient::to_string(&origin_err)?);
} else {
println!(
"test {}/{}::{} {}",
origin_err.package,
origin_err.filename,
origin_err.test_name,
"failed".bold().red(),
);
}
let _ = crate::expect::render_expect_fail(&origin_err.message);
}
if auto_update {
Expand Down
1 change: 1 addition & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ pub struct TestOpt {
pub filter_file: Option<String>,
pub filter_index: Option<u32>,
pub limit: u32,
pub test_failure_json: bool,
}

impl TestOpt {
Expand Down

0 comments on commit 840ce7d

Please sign in to comment.