Skip to content

Commit

Permalink
Merge pull request #207 from moonbitlang/te
Browse files Browse the repository at this point in the history
internal: output failure in json format
  • Loading branch information
lijunchen authored Aug 26, 2024
2 parents a29ee09 + 37992ec commit ae40380
Show file tree
Hide file tree
Showing 13 changed files with 85 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
15 changes: 15 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5906,3 +5906,18 @@ fn test_snapshot_test_target_js() {
expect!["Hello, world!"],
);
}

#[test]
fn moon_test_with_failure_json() {
let dir = TestDir::new("test_with_failure_json");

let output = get_err_stdout_with_args_and_replace_dir(&dir, ["test", "--test-failure-json"]);
check(
&output,
// should keep in this format, it's used in ide test explorer
expect![[r#"
{"package":"username/hello/lib1","filename":"hello.mbt","index":"0","test_name":"test_1","message":"FAILED: $ROOT/src/lib1/hello.mbt:7:3-7:25 test_1 failed"}
Total tests: 2, passed: 1, failed: 1.
"#]],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.mooncakes/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# username/hello
10 changes: 10 additions & 0 deletions crates/moon/tests/test_cases/test_with_failure_json/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "username/hello",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"license": "Apache-2.0",
"keywords": [],
"description": "",
"source": "src"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub fn hello() -> String {
"Hello, world!"
}


test "test_1" {
fail!("test_1 failed")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test "hello" {
if @lib1.hello() != "Hello, world!" {
fail!("@lib.hello() != \"Hello, world!\"")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println(@lib1.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"is-main": true,
"import": [
"username/hello/lib1"
]
}
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 ae40380

Please sign in to comment.