From a1bba23d964d6171650e8917dc858ac91bfdf2e4 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Mon, 2 Sep 2024 13:52:47 +0800 Subject: [PATCH 1/5] internal:use gen-test-info -json & remove `tests` in driver --- crates/moon/src/cli/generate_test_driver.rs | 37 ++++--------- .../template/js_test_driver_template.mbt | 1 - .../template/js_test_driver_template2.mbt | 1 - .../template/test_driver_template.mbt | 1 - .../template/test_driver_template2.mbt | 1 - crates/moonutil/src/common.rs | 53 +++++++++++++++++++ 6 files changed, 64 insertions(+), 30 deletions(-) diff --git a/crates/moon/src/cli/generate_test_driver.rs b/crates/moon/src/cli/generate_test_driver.rs index 55eb54a7..5584ae62 100644 --- a/crates/moon/src/cli/generate_test_driver.rs +++ b/crates/moon/src/cli/generate_test_driver.rs @@ -22,9 +22,9 @@ use colored::Colorize; use mooncake::pkg::sync::auto_sync; use moonutil::cli::UniversalFlags; use moonutil::common::{ - lower_surface_targets, DriverKind, MoonbuildOpt, RunMode, TargetBackend, TestOpt, - BLACKBOX_TEST_DRIVER, INTERNAL_TEST_DRIVER, MOONBITLANG_CORE, MOON_TEST_DELIMITER_BEGIN, - MOON_TEST_DELIMITER_END, WHITEBOX_TEST_DRIVER, + lower_surface_targets, DriverKind, MoonbuildOpt, MooncGenTestInfo, RunMode, TargetBackend, + TestOpt, BLACKBOX_TEST_DRIVER, INTERNAL_TEST_DRIVER, MOONBITLANG_CORE, + MOON_TEST_DELIMITER_BEGIN, MOON_TEST_DELIMITER_END, WHITEBOX_TEST_DRIVER, }; use moonutil::dirs::PackageDirs; use moonutil::mooncakes::sync::AutoSyncFlags; @@ -48,6 +48,7 @@ pub struct GeneratedTestDriverSubcommand { fn moonc_gen_test_info(files: &[PathBuf]) -> anyhow::Result { let mut generated = std::process::Command::new("moonc") .arg("gen-test-info") + .arg("-json") .args(files) .stdout(std::process::Stdio::piped()) .spawn() @@ -60,7 +61,8 @@ fn moonc_gen_test_info(files: &[PathBuf]) -> anyhow::Result { .read_to_string(&mut out) .with_context(|| gen_error_message(files))?; generated.wait()?; - return Ok(out); + let t: MooncGenTestInfo = serde_json_lenient::from_str(&out)?; + return Ok(t.to_mbt()); fn gen_error_message(files: &[PathBuf]) -> String { format!( @@ -175,7 +177,7 @@ pub fn generate_test_driver( } fn generate_driver(data: &str, pkgname: &str, target_backend: Option) -> String { - let index = data.find(" let with_args_tests =").unwrap_or(data.len()); + let index = data.find("let with_args_tests =").unwrap_or(data.len()); let only_no_arg_tests = !data[index..].contains("__test_"); // TODO: need refactor @@ -196,16 +198,11 @@ fn generate_driver(data: &str, pkgname: &str, target_backend: Option Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n\ - let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n", + "let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n", &data[0..index], ) .replace( - "let tests = {", - "let _tests: Map[String, Array[(() -> Unit!Error, Array[String])]] = {", - ) - .replace( - " let no_args_tests = {", + "let no_args_tests = {", "let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = {", ) .replace("{PACKAGE}", pkgname) @@ -225,11 +222,9 @@ fn generate_driver(data: &str, pkgname: &str, target_backend: Option Unit!Error, Array[String])]] = { }\n", "") .replace("let no_args_tests : Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { }\n", "") .replace("let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[String])]] = { }\n", "") .replace("// REPLACE ME 0\n", &data.replace(" let", "let")) - .replace("let tests =", "let tests : Map[String, Array[(() -> Unit!Error, Array[String])]] =") .replace("let no_args_tests =", "let no_args_tests : Map[String, Map[Int, (() -> Unit!Error, Array[String])]] =") .replace("let with_args_tests =", "let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[String])]] =") .replace("{PACKAGE}", pkgname) @@ -253,14 +248,9 @@ fn generate_driver(data: &str, pkgname: &str, target_backend: Option Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n\ - let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n", + "let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n", &data[0..index].replace(" let", "let"), ) - .replace( - "let tests = {", - "let _tests: Map[String, Array[(() -> Unit!Error, Array[String])]] = {", - ) .replace( "let no_args_tests = {", "let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = {", @@ -283,15 +273,10 @@ fn generate_driver(data: &str, pkgname: &str, target_backend: Option Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n\ - let no_args_tests : Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n\ + "let no_args_tests : Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n\ let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED\n", &data.replace(" let ", "let "), ) - .replace( - "let tests = {", - "let _tests: Map[String, Array[(() -> Unit!Error, Array[String])]] = {", - ) .replace( "let no_args_tests = {", "let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = {", diff --git a/crates/moonbuild/template/js_test_driver_template.mbt b/crates/moonbuild/template/js_test_driver_template.mbt index 58d46fd6..45087edd 100644 --- a/crates/moonbuild/template/js_test_driver_template.mbt +++ b/crates/moonbuild/template/js_test_driver_template.mbt @@ -117,7 +117,6 @@ fn moonbit_test_driver_internal_apply_filter( ret } -let tests: Map[String, Array[(() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED let no_args_tests : Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED diff --git a/crates/moonbuild/template/js_test_driver_template2.mbt b/crates/moonbuild/template/js_test_driver_template2.mbt index 2e35d0a4..0cfadc0f 100644 --- a/crates/moonbuild/template/js_test_driver_template2.mbt +++ b/crates/moonbuild/template/js_test_driver_template2.mbt @@ -83,7 +83,6 @@ fn moonbit_test_driver_internal_apply_filter( ret } -let tests: Map[String, Array[(() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED pub fn execute(file : String, index : Int) -> Unit { diff --git a/crates/moonbuild/template/test_driver_template.mbt b/crates/moonbuild/template/test_driver_template.mbt index e233f16c..6b1e89d3 100644 --- a/crates/moonbuild/template/test_driver_template.mbt +++ b/crates/moonbuild/template/test_driver_template.mbt @@ -117,7 +117,6 @@ fn moonbit_test_driver_internal_apply_filter( ret } -let tests : Map[String, Array[(() -> Unit!Error, Array[String])]] = { } let no_args_tests : Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[String])]] = { } // REPLACE ME 0 diff --git a/crates/moonbuild/template/test_driver_template2.mbt b/crates/moonbuild/template/test_driver_template2.mbt index 9b802e9f..577429cf 100644 --- a/crates/moonbuild/template/test_driver_template2.mbt +++ b/crates/moonbuild/template/test_driver_template2.mbt @@ -83,7 +83,6 @@ fn moonbit_test_driver_internal_apply_filter( ret } -let tests: Map[String, Array[(() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED let no_args_tests: Map[String, Map[Int, (() -> Unit!Error, Array[String])]] = { } // WILL BE REPLACED fn main { let file_filter: String? = Some(moonbit_test_driver_internal_get_file_name()) diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index b3a00050..2dd14979 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -721,3 +721,56 @@ impl DriverKind { pub const INTERNAL_TEST_DRIVER: &str = "__generated_driver_for_internal_test.mbt"; pub const WHITEBOX_TEST_DRIVER: &str = "__generated_driver_for_whitebox_test.mbt"; pub const BLACKBOX_TEST_DRIVER: &str = "__generated_driver_for_blackbox_test.mbt"; + +#[derive(Serialize, Deserialize, Debug)] +pub struct TestInfo { + pub index: usize, + pub func: String, + pub name: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct MooncGenTestInfo { + pub tests: HashMap>, + pub no_args_tests: HashMap>, + pub with_args_tests: HashMap>, +} + +impl MooncGenTestInfo { + pub fn to_mbt(&self) -> String { + let mut result = String::new(); + let default_name = "".to_string(); + + result.push_str("let no_args_tests = {\n"); + for (file, tests) in &self.no_args_tests { + result.push_str(&format!(" \"{}\": {{\n", file)); + for test in tests { + result.push_str(&format!( + " {}: ({}, [\"{}\"]),\n", + test.index, + test.func, + test.name.as_ref().unwrap_or(&default_name) + )); + } + result.push_str(" },\n"); + } + result.push_str("}\n\n"); + + result.push_str("let with_args_tests = {\n"); + for (file, tests) in &self.with_args_tests { + result.push_str(&format!(" \"{}\": {{\n", file)); + for test in tests { + result.push_str(&format!( + " {}: ({}, [\"{}\"]),\n", + test.index, + test.func, + test.name.as_ref().unwrap_or(&default_name) + )); + } + result.push_str(" },\n"); + } + result.push_str("}\n"); + + result + } +} From 521b3ebdfabc133692ad0ca5b7a1f01e631fe704 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Tue, 3 Sep 2024 14:26:46 +0800 Subject: [PATCH 2/5] internal: use moonc test info in run_test --- crates/moon/src/cli/generate_test_driver.rs | 27 +++++- crates/moon/src/cli/test.rs | 67 -------------- crates/moonbuild/src/entry.rs | 97 +++++++++++++++++---- crates/moonbuild/src/runtest.rs | 2 + crates/moonutil/src/common.rs | 16 ++-- crates/moonutil/src/module.rs | 4 - crates/moonutil/src/scan.rs | 1 - 7 files changed, 117 insertions(+), 97 deletions(-) diff --git a/crates/moon/src/cli/generate_test_driver.rs b/crates/moon/src/cli/generate_test_driver.rs index 5584ae62..55fee918 100644 --- a/crates/moon/src/cli/generate_test_driver.rs +++ b/crates/moon/src/cli/generate_test_driver.rs @@ -29,7 +29,7 @@ use moonutil::common::{ use moonutil::dirs::PackageDirs; use moonutil::mooncakes::sync::AutoSyncFlags; use moonutil::mooncakes::RegistryConfig; -use std::io::Read; +use std::io::{Read, Write}; use std::path::{Path, PathBuf}; /// Test the current package @@ -45,7 +45,7 @@ pub struct GeneratedTestDriverSubcommand { pub driver_kind: DriverKind, } -fn moonc_gen_test_info(files: &[PathBuf]) -> anyhow::Result { +fn moonc_gen_test_info(files: &[PathBuf], target_dir: &Path) -> anyhow::Result { let mut generated = std::process::Command::new("moonc") .arg("gen-test-info") .arg("-json") @@ -61,6 +61,26 @@ fn moonc_gen_test_info(files: &[PathBuf]) -> anyhow::Result { .read_to_string(&mut out) .with_context(|| gen_error_message(files))?; generated.wait()?; + + // append whitebox blackbox internal test info to test_info.json + { + let test_info_json_path = target_dir.join("test_info.json"); + out.push('\n'); + std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&test_info_json_path) + .context(format!( + "failed to open file: {}", + test_info_json_path.display() + ))? + .write_all(out.as_bytes()) + .context(format!( + "failed to write file: {}", + test_info_json_path.display() + ))?; + } + let t: MooncGenTestInfo = serde_json_lenient::from_str(&out)?; return Ok(t.to_mbt()); @@ -155,7 +175,8 @@ pub fn generate_test_driver( let backend_filtered: Vec = moonutil::common::backend_filter(files, moonc_opt.link_opt.target_backend); - let mbts_test_data = moonc_gen_test_info(&backend_filtered)?; + let mbts_test_data = + moonc_gen_test_info(&backend_filtered, &target_dir.join(pkg.rel.full_name()))?; if pkg.is_main && mbts_test_data.contains("(__test_") { eprintln!( diff --git a/crates/moon/src/cli/test.rs b/crates/moon/src/cli/test.rs index f13ba052..51dacf9e 100644 --- a/crates/moon/src/cli/test.rs +++ b/crates/moon/src/cli/test.rs @@ -18,7 +18,6 @@ use anyhow::Context; use colored::Colorize; -use indexmap::IndexMap; use moonbuild::dry_run; use moonbuild::entry; use mooncake::pkg::sync::auto_sync; @@ -231,72 +230,6 @@ fn run_test_internal( pkg.generated_test_drivers .push(GeneratedTestDriver::BlackboxTest(blackbox_generated_file)); } - - let no_exist = (None, IndexMap::new()); - module.test_info.insert( - pkgname.clone(), - [no_exist.clone(), no_exist.clone(), no_exist.clone()], - ); - let current_pkg_test_info = module.test_info.get_mut(pkgname).unwrap(); - - let backend_filter_files = - moonutil::common::backend_filter(&pkg.files, moonc_opt.link_opt.target_backend); - let backend_filter_wbtest_files = - moonutil::common::backend_filter(&pkg.wbtest_files, moonc_opt.link_opt.target_backend); - let backend_filter_test_files = - moonutil::common::backend_filter(&pkg.test_files, moonc_opt.link_opt.target_backend); - - for file in backend_filter_files - .iter() - .chain(backend_filter_wbtest_files.iter()) - .chain(backend_filter_test_files.iter()) - { - // workaround for skip test coverage.mbt in builtin when --enable-coverage is specified - if moonc_opt.build_opt.enable_coverage - && pkgname == "moonbitlang/core/builtin" - && file.to_str().unwrap().contains("coverage.mbt") - { - continue; - } - let content = std::fs::read_to_string(file)?; - let filename = file.file_name().unwrap().to_str().unwrap(); - if let Some(ref filter_file) = filter_file { - if filter_file != filename { - continue; - } - } - - // todo: refactor this when we have a json info from moonc - let (test_type, index) = if filename.ends_with("_test.mbt") { - ("blackbox", 0) - } else if filename.ends_with("_wbtest.mbt") { - ("whitebox", 1) - } else { - ("internal", 2) - }; - - let mut test_block_nums_in_current_file = 0; - let artifact_path = pkg - .artifact - .with_file_name(format!("{}.{test_type}_test.wat", pkg.last_name())) - .with_extension(moonc_opt.link_opt.output_format.to_str()); - - for line in content.lines() { - if line.starts_with("test ") { - pkg.files_contain_test_block.push(file.clone()); - test_block_nums_in_current_file += 1; - } - } - - if test_block_nums_in_current_file > 0 { - let (artifact_opt, map) = &mut current_pkg_test_info[index]; - if artifact_opt.is_none() { - *artifact_opt = Some(artifact_path.clone()); - } - let test_block_count = map.entry(filename.into()).or_insert(0); - *test_block_count += test_block_nums_in_current_file; - } - } } moonc_opt.build_opt.warn_lists = module diff --git a/crates/moonbuild/src/entry.rs b/crates/moonbuild/src/entry.rs index 573d6d2d..56213955 100644 --- a/crates/moonbuild/src/entry.rs +++ b/crates/moonbuild/src/entry.rs @@ -16,7 +16,9 @@ // // For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn. +use indexmap::IndexMap; use moonutil::module::ModuleDB; +use moonutil::package::Package; use moonutil::path::PathComponent; use n2::progress::{DumbConsoleProgress, FancyConsoleProgress, Progress}; use n2::terminal; @@ -34,7 +36,9 @@ use crate::check::normal::write_pkg_lst; use crate::expect::{apply_snapshot, render_snapshot_fail}; use crate::runtest::TestStatistics; -use moonutil::common::{MoonbuildOpt, MooncOpt, TargetBackend, TestArtifacts}; +use moonutil::common::{ + MbtTestInfo, MoonbuildOpt, MooncGenTestInfo, MooncOpt, TargetBackend, TestArtifacts, +}; use std::sync::{Arc, Mutex}; @@ -310,6 +314,67 @@ impl std::fmt::Display for TestResult { } } +// (pkg's runnable artifacts, ) for blackbox whitebox internal test +#[allow(clippy::type_complexity)] +fn convert_moonc_test_info( + test_info_file: &Path, + pkg: &Package, + output_format: &str, + filter_file: Option<&String>, +) -> anyhow::Result, IndexMap>)>> { + let content = std::fs::read_to_string(test_info_file) + .context(format!("failed to read {}", test_info_file.display()))?; + let mut moonc_test_info = MooncGenTestInfo { + no_args_tests: IndexMap::new(), + with_args_tests: IndexMap::new(), + }; + for line in content.split('\n') { + if let Ok(info) = serde_json_lenient::from_str::(line) { + moonc_test_info.no_args_tests.extend(info.no_args_tests); + moonc_test_info.with_args_tests.extend(info.with_args_tests); + } + } + + let no_exist = (None, IndexMap::>::new()); + let mut current_pkg_test_info = vec![no_exist.clone(), no_exist.clone(), no_exist.clone()]; + + for (filename, test_info) in moonc_test_info + .no_args_tests + .into_iter() + .chain(moonc_test_info.with_args_tests.into_iter()) + { + if test_info.is_empty() { + continue; + } + if let Some(filter_file) = filter_file { + if filename != *filter_file { + continue; + } + } + let (test_type, index) = if filename.ends_with("_test.mbt") { + ("blackbox", 0) + } else if filename.ends_with("_wbtest.mbt") { + ("whitebox", 1) + } else { + ("internal", 2) + }; + + let artifact_path = pkg + .artifact + .with_file_name(format!("{}.{test_type}_test.wat", pkg.last_name())) + .with_extension(output_format); + + let (artifact_opt, map) = &mut current_pkg_test_info[index]; + if artifact_opt.is_none() { + *artifact_opt = Some(artifact_path); + } + let mbt_test_info = map.entry(filename).or_insert(vec![]); + mbt_test_info.extend(test_info); + } + + Ok(current_pkg_test_info) +} + #[allow(clippy::too_many_arguments)] pub fn run_test( moonc_opt: &MooncOpt, @@ -342,7 +407,7 @@ pub fn run_test( let mut test_artifacts = TestArtifacts { artifacts_path: vec![], }; - for (pkgname, _) in module + for (pkgname, pkg) in module .packages .iter() .filter(|(_, p)| !(p.is_main || p.is_third_party)) @@ -353,29 +418,31 @@ pub fn run_test( } } - let current_pkg_test_info = module.test_info.get(pkgname).unwrap(); - for (artifact_path, map) in current_pkg_test_info { + // convert moonc test info + let test_info_file = target_dir.join(pkg.rel.full_name()).join("test_info.json"); + let current_pkg_test_info = convert_moonc_test_info( + &test_info_file, + pkg, + moonc_opt.link_opt.output_format.to_str(), + filter_file, + )?; + + for (artifact_path, file_test_info_map) in current_pkg_test_info { if artifact_path.is_none() { continue; } - let artifact_path = artifact_path.as_ref().unwrap(); + let artifact_path = artifact_path.unwrap(); let mut test_args = TestArgs { package: pkgname.clone(), file_and_index: vec![], }; - for (file_name, test_count) in map { - if let Some(filter_file) = filter_file { - if file_name != filter_file { - continue; - } - } - + for (file_name, test_count) in file_test_info_map { let range; if let Some(filter_index) = filter_index { range = filter_index..(filter_index + 1); } else { - range = 0..(*test_count); + range = 0..(test_count.len() as u32); } let mut args = vec![]; @@ -420,7 +487,7 @@ pub fn run_test( let mut result = trace::scope("test", || async { execute_test( moonc_opt.build_opt.target_backend, - artifact_path, + &artifact_path, target_dir, &test_args, ) @@ -436,7 +503,7 @@ pub fn run_test( module, auto_update, test_verbose_output, - artifact_path, + &artifact_path, target_dir, printed, ) diff --git a/crates/moonbuild/src/runtest.rs b/crates/moonbuild/src/runtest.rs index 6b3d0ef4..b6c181bf 100644 --- a/crates/moonbuild/src/runtest.rs +++ b/crates/moonbuild/src/runtest.rs @@ -176,6 +176,8 @@ async fn run( } } } else { + println!("path: {:?}", path); + println!("args: {:?}", args); res.push(Err(TestFailedStatus::Others(String::from( "No test output found", )))); diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index 2dd14979..3b39700d 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -23,6 +23,7 @@ use anyhow::{bail, Context}; use clap::ValueEnum; use colored::Colorize; use fs4::FileExt; +use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, HashSet}; use std::fs; @@ -722,18 +723,19 @@ pub const INTERNAL_TEST_DRIVER: &str = "__generated_driver_for_internal_test.mbt pub const WHITEBOX_TEST_DRIVER: &str = "__generated_driver_for_whitebox_test.mbt"; pub const BLACKBOX_TEST_DRIVER: &str = "__generated_driver_for_blackbox_test.mbt"; -#[derive(Serialize, Deserialize, Debug)] -pub struct TestInfo { - pub index: usize, +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct MbtTestInfo { + pub index: u32, pub func: String, pub name: Option, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct MooncGenTestInfo { - pub tests: HashMap>, - pub no_args_tests: HashMap>, - pub with_args_tests: HashMap>, + // filename, test_info + // pub tests: HashMap>, + pub no_args_tests: IndexMap>, + pub with_args_tests: IndexMap>, } impl MooncGenTestInfo { diff --git a/crates/moonutil/src/module.rs b/crates/moonutil/src/module.rs index 9d40166a..0d03591b 100644 --- a/crates/moonutil/src/module.rs +++ b/crates/moonutil/src/module.rs @@ -31,15 +31,11 @@ use std::collections::HashSet; use std::fmt::Debug; use std::path::{Path, PathBuf}; -// Map) for inline whitebox blackbox test> -type TestInfo = IndexMap, IndexMap); 3]>; - #[derive(Debug, Clone)] pub struct ModuleDB { pub source_dir: PathBuf, pub name: String, pub packages: IndexMap, - pub test_info: TestInfo, pub entries: Vec, // index of entry packages pub deps: Vec, pub graph: DiGraph, diff --git a/crates/moonutil/src/scan.rs b/crates/moonutil/src/scan.rs index 0d3f40b0..a3fb5e05 100644 --- a/crates/moonutil/src/scan.rs +++ b/crates/moonutil/src/scan.rs @@ -474,7 +474,6 @@ pub fn scan( graph, backend: moonc_opt.link_opt.target_backend.to_backend_ext().into(), source: mod_desc.source, - test_info: IndexMap::new(), }; module.validate()?; From ed848b19d87cec192cd98bd78c345fce9bb9bf6f Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Tue, 3 Sep 2024 15:43:13 +0800 Subject: [PATCH 3/5] driver: move check_panic into moon --- crates/moonbuild/src/entry.rs | 41 ++++++++++++++----- crates/moonbuild/src/runtest.rs | 36 +++++++++++++--- .../template/js_test_driver_template.mbt | 29 +------------ .../template/js_test_driver_template2.mbt | 31 +------------- .../template/test_driver_template.mbt | 33 +-------------- .../template/test_driver_template2.mbt | 29 +------------ 6 files changed, 67 insertions(+), 132 deletions(-) diff --git a/crates/moonbuild/src/entry.rs b/crates/moonbuild/src/entry.rs index 56213955..6396e4ed 100644 --- a/crates/moonbuild/src/entry.rs +++ b/crates/moonbuild/src/entry.rs @@ -36,9 +36,7 @@ use crate::check::normal::write_pkg_lst; use crate::expect::{apply_snapshot, render_snapshot_fail}; use crate::runtest::TestStatistics; -use moonutil::common::{ - MbtTestInfo, MoonbuildOpt, MooncGenTestInfo, MooncOpt, TargetBackend, TestArtifacts, -}; +use moonutil::common::{MoonbuildOpt, MooncGenTestInfo, MooncOpt, TargetBackend, TestArtifacts}; use std::sync::{Arc, Mutex}; @@ -321,7 +319,12 @@ fn convert_moonc_test_info( pkg: &Package, output_format: &str, filter_file: Option<&String>, -) -> anyhow::Result, IndexMap>)>> { +) -> anyhow::Result< + Vec<( + Option, + IndexMap>>, + )>, +> { let content = std::fs::read_to_string(test_info_file) .context(format!("failed to read {}", test_info_file.display()))?; let mut moonc_test_info = MooncGenTestInfo { @@ -335,7 +338,10 @@ fn convert_moonc_test_info( } } - let no_exist = (None, IndexMap::>::new()); + let no_exist = ( + None, + IndexMap::>>::new(), + ); let mut current_pkg_test_info = vec![no_exist.clone(), no_exist.clone(), no_exist.clone()]; for (filename, test_info) in moonc_test_info @@ -368,8 +374,8 @@ fn convert_moonc_test_info( if artifact_opt.is_none() { *artifact_opt = Some(artifact_path); } - let mbt_test_info = map.entry(filename).or_insert(vec![]); - mbt_test_info.extend(test_info); + let mbt_test_info = map.entry(filename).or_insert(IndexMap::new()); + mbt_test_info.extend(test_info.iter().map(|it| (it.index, it.name.clone()))); } Ok(current_pkg_test_info) @@ -437,7 +443,7 @@ pub fn run_test( package: pkgname.clone(), file_and_index: vec![], }; - for (file_name, test_count) in file_test_info_map { + for (file_name, test_count) in &file_test_info_map { let range; if let Some(filter_index) = filter_index { range = filter_index..(filter_index + 1); @@ -490,6 +496,7 @@ pub fn run_test( &artifact_path, target_dir, &test_args, + &file_test_info_map, ) .await }) @@ -506,6 +513,7 @@ pub fn run_test( &artifact_path, target_dir, printed, + &file_test_info_map, ) .await?; } @@ -581,13 +589,20 @@ async fn execute_test( artifact_path: &Path, target_dir: &Path, args: &TestArgs, + file_test_info_map: &IndexMap>>, ) -> anyhow::Result>> { match target_backend { TargetBackend::Wasm | TargetBackend::WasmGC => { - crate::runtest::run_wat(artifact_path, target_dir, args).await + crate::runtest::run_wat(artifact_path, target_dir, args, file_test_info_map).await } TargetBackend::Js => { - crate::runtest::run_js(&artifact_path.with_extension("cjs"), target_dir, args).await + crate::runtest::run_js( + &artifact_path.with_extension("cjs"), + target_dir, + args, + file_test_info_map, + ) + .await } } } @@ -603,6 +618,7 @@ async fn handle_test_result( artifact_path: &Path, target_dir: &Path, printed: Arc, + file_test_info_map: &IndexMap>>, ) -> anyhow::Result<()> { let output_failure_in_json = moonbuild_opt .test_opt @@ -656,6 +672,7 @@ async fn handle_test_result( artifact_path, target_dir, &test_args, + file_test_info_map, ) .await? .first() @@ -675,6 +692,7 @@ async fn handle_test_result( artifact_path, target_dir, &test_args, + file_test_info_map, ) .await? .first() @@ -745,6 +763,7 @@ async fn handle_test_result( artifact_path, target_dir, &test_args, + file_test_info_map, ) .await? .first() @@ -770,6 +789,7 @@ async fn handle_test_result( artifact_path, target_dir, &test_args, + file_test_info_map, ) .await? .first() @@ -799,6 +819,7 @@ async fn handle_test_result( artifact_path, target_dir, &test_args, + file_test_info_map, ) .await? .first() diff --git a/crates/moonbuild/src/runtest.rs b/crates/moonbuild/src/runtest.rs index b6c181bf..91dc0747 100644 --- a/crates/moonbuild/src/runtest.rs +++ b/crates/moonbuild/src/runtest.rs @@ -22,6 +22,7 @@ use crate::section_capture::{handle_stdout, SectionCapture}; use super::gen; use anyhow::{bail, Context}; +use indexmap::IndexMap; use moonutil::common::{ MoonbuildOpt, MooncOpt, MOON_COVERAGE_DELIMITER_BEGIN, MOON_COVERAGE_DELIMITER_END, MOON_TEST_DELIMITER_BEGIN, MOON_TEST_DELIMITER_END, @@ -65,23 +66,26 @@ pub async fn run_wat( path: &Path, target_dir: &Path, args: &TestArgs, + file_test_info_map: &IndexMap>>, ) -> anyhow::Result>> { // put "--test-mode" at the front of args let mut _args = vec!["--test-mode".to_string()]; _args.push(serde_json_lenient::to_string(args).unwrap()); - run("moonrun", path, target_dir, &_args).await + run("moonrun", path, target_dir, &_args, file_test_info_map).await } pub async fn run_js( path: &Path, target_dir: &Path, args: &TestArgs, + file_test_info_map: &IndexMap>>, ) -> anyhow::Result>> { run( "node", path, target_dir, &[serde_json_lenient::to_string(args).unwrap()], + file_test_info_map, ) .await } @@ -91,6 +95,7 @@ async fn run( path: &Path, target_dir: &Path, args: &[String], + file_test_info_map: &IndexMap>>, ) -> anyhow::Result>> { let mut execution = tokio::process::Command::new(command) .arg(path) @@ -152,8 +157,31 @@ async fn run( test_statistics.push(a); } - for test_statistic in test_statistics { - let return_message = &test_statistic.message; + for mut test_statistic in test_statistics { + let filename = &test_statistic.filename; + let index = &test_statistic.index.parse::().unwrap(); + let test_name = file_test_info_map + .get(filename) + .unwrap() + .get(index) + .unwrap() + .as_ref() + .unwrap_or(&test_statistic.index); + + if test_name.starts_with("panic") { + // should panic but not panic + if test_statistic.message.is_empty() { + test_statistic.message = "panic is expected".to_string(); + } + // it does panic, treat it as ok + else { + test_statistic.message = "".to_string(); + } + } + + test_statistic.test_name = test_name.clone(); + + let return_message = test_statistic.message.clone(); if return_message.is_empty() { res.push(Ok(test_statistic)); } else if return_message.starts_with(EXPECT_FAILED) { @@ -176,8 +204,6 @@ async fn run( } } } else { - println!("path: {:?}", path); - println!("args: {:?}", args); res.push(Err(TestFailedStatus::Others(String::from( "No test output found", )))); diff --git a/crates/moonbuild/template/js_test_driver_template.mbt b/crates/moonbuild/template/js_test_driver_template.mbt index 45087edd..c6148753 100644 --- a/crates/moonbuild/template/js_test_driver_template.mbt +++ b/crates/moonbuild/template/js_test_driver_template.mbt @@ -1,27 +1,8 @@ // Generated by moon test. -fn moonbit_test_driver_internal_catch_exception[A](body : A, on_exn : A) -> A = "%control.catch" - -fn moonbit_test_driver_internal_check_panic[A, B, E : Error]( - f : () -> A!E, - does_panic : () -> B!E, - no_panic : () -> B!E -) -> B!E { - moonbit_test_driver_internal_catch_exception( - { - f!() |> ignore - no_panic!() - }, - does_panic!(), - ) -} fn moonbit_test_driver_internal_error_to_string(x : Error) -> String = "%error.to_string" -fn moonbit_test_driver_internal_startswith_panic(s : String) -> Bool { - s.length() >= 5 && s[0] == 'p' && s[1] == 'a' && s[2] == 'n' && s[3] == 'i' && - s[4] == 'c' -} typealias TestDriver_No_Args_Function = () -> Unit!Error @@ -155,15 +136,7 @@ pub fn execute(file : String, index : Int) -> Unit { f!(it) } } - if moonbit_test_driver_internal_startswith_panic(name) { - moonbit_test_driver_internal_check_panic!( - f, - fn() { () }, - fn() { raise Failure("panic is expected") }, - ) - } else { - f!() - } + f!() succ_idx -= 1 test_names[succ_idx] = name } catch { diff --git a/crates/moonbuild/template/js_test_driver_template2.mbt b/crates/moonbuild/template/js_test_driver_template2.mbt index 0cfadc0f..1d82ec02 100644 --- a/crates/moonbuild/template/js_test_driver_template2.mbt +++ b/crates/moonbuild/template/js_test_driver_template2.mbt @@ -1,28 +1,7 @@ // Generated by moon test. -fn moonbit_test_driver_internal_catch_exception[A](body : A, on_exn : A) -> A = "%control.catch" - -fn moonbit_test_driver_internal_check_panic[A, B, E : Error]( - f : () -> A!E, - does_panic : () -> B!E, - no_panic : () -> B!E -) -> B!E { - moonbit_test_driver_internal_catch_exception( - { - f!() |> ignore - no_panic!() - }, - does_panic!(), - ) -} - fn moonbit_test_driver_internal_error_to_string(x : Error) -> String = "%error.to_string" -fn moonbit_test_driver_internal_startswith_panic(s : String) -> Bool { - s.length() >= 5 && s[0] == 'p' && s[1] == 'a' && s[2] == 'n' && s[3] == 'i' && - s[4] == 'c' -} - typealias TestDriver_No_Args_Function = () -> Unit!Error typealias TestDriver_No_Args_Map = @moonbitlang/core/builtin.Map[ @@ -112,15 +91,7 @@ pub fn execute(file : String, index : Int) -> Unit { let f = match item.f { TestDriver__F::F0(f) => f } - if moonbit_test_driver_internal_startswith_panic(name) { - moonbit_test_driver_internal_check_panic!( - f, - fn() { () }, - fn() { raise Failure("panic is expected") }, - ) - } else { - f!() - } + f!() succ_idx -= 1 test_names[succ_idx] = name } catch { diff --git a/crates/moonbuild/template/test_driver_template.mbt b/crates/moonbuild/template/test_driver_template.mbt index 6b1e89d3..09655143 100644 --- a/crates/moonbuild/template/test_driver_template.mbt +++ b/crates/moonbuild/template/test_driver_template.mbt @@ -1,28 +1,7 @@ // Generated by moon test. -fn moonbit_test_driver_internal_catch_exception[A](body : A, on_exn : A) -> A = "%control.catch" - -fn moonbit_test_driver_internal_check_panic[A, B, E : Error]( - f : () -> A!E, - does_panic : () -> B!E, - no_panic : () -> B!E -) -> B!E { - moonbit_test_driver_internal_catch_exception( - { - f!() |> ignore - no_panic!() - }, - does_panic!(), - ) -} - fn moonbit_test_driver_internal_error_to_string(x : Error) -> String = "%error.to_string" -fn moonbit_test_driver_internal_startswith_panic(s : String) -> Bool { - s.length() >= 5 && s[0] == 'p' && s[1] == 'a' && s[2] == 'n' && s[3] == 'i' && - s[4] == 'c' -} - typealias TestDriver_No_Args_Function = () -> Unit!Error typealias TestDriver_With_Args_Function = (@moonbitlang/core/test.T) -> Unit!Error @@ -122,7 +101,7 @@ let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[Strin // REPLACE ME 0 fn main { - let _ = tests + // let _ = tests let file_filter: String? = Some(moonbit_test_driver_internal_get_file_name()) let index = moonbit_test_driver_internal_get_index() let index_filter : Int? = Some(index) @@ -158,15 +137,7 @@ fn main { f!(it) } } - if moonbit_test_driver_internal_startswith_panic(name) { - moonbit_test_driver_internal_check_panic!( - f, - fn() { () }, - fn() { raise Failure("panic is expected") }, - ) - } else { - f!() - } + f!() succ_idx -= 1 test_names[succ_idx] = name } catch { diff --git a/crates/moonbuild/template/test_driver_template2.mbt b/crates/moonbuild/template/test_driver_template2.mbt index 577429cf..2bd5f34d 100644 --- a/crates/moonbuild/template/test_driver_template2.mbt +++ b/crates/moonbuild/template/test_driver_template2.mbt @@ -1,27 +1,8 @@ // Generated by moon test. -fn moonbit_test_driver_internal_catch_exception[A](body : A, on_exn : A) -> A = "%control.catch" - -fn moonbit_test_driver_internal_check_panic[A, B, E : Error]( - f : () -> A!E, - does_panic : () -> B!E, - no_panic : () -> B!E -) -> B!E { - moonbit_test_driver_internal_catch_exception( - { - f!() |> ignore - no_panic!() - }, - does_panic!(), - ) -} fn moonbit_test_driver_internal_error_to_string(x : Error) -> String = "%error.to_string" -fn moonbit_test_driver_internal_startswith_panic(s : String) -> Bool { - s.length() >= 5 && s[0] == 'p' && s[1] == 'a' && s[2] == 'n' && s[3] == 'i' && - s[4] == 'c' -} typealias TestDriver_No_Args_Function = () -> Unit!Error @@ -112,15 +93,7 @@ fn main { let f = match item.f { TestDriver__F::F0(f) => f } - if moonbit_test_driver_internal_startswith_panic(name) { - moonbit_test_driver_internal_check_panic!( - f, - fn() { () }, - fn() { raise Failure("panic is expected") }, - ) - } else { - f!() - } + f!() succ_idx -= 1 test_names[succ_idx] = name } catch { From ea7860173b0aedbab0ac86399adb583d5001fc6b Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Tue, 3 Sep 2024 16:59:42 +0800 Subject: [PATCH 4/5] internal: replace backslash --- crates/moonbuild/template/js_driver.js | 2 +- crates/moonrun/src/template/js_glue.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/moonbuild/template/js_driver.js b/crates/moonbuild/template/js_driver.js index ca0e4e2d..520e593b 100644 --- a/crates/moonbuild/template/js_driver.js +++ b/crates/moonbuild/template/js_driver.js @@ -8,7 +8,7 @@ for (param of testParams) { execute(param[0], parseInt(param[1])); } catch (e) { console.log("----- BEGIN MOON TEST RESULT -----") - console.log(`{"package": "${packageName}", "filename": "${param[0]}", "index": "${param[1]}", "test_name": "${param[1]}", "message": "${e.stack.toString().split('\n').join('\\n')}"}`); + console.log(`{"package": "${packageName}", "filename": "${param[0]}", "index": "${param[1]}", "test_name": "${param[1]}", "message": "${e.stack.toString().replaceAll("\\", "\\\\").split('\n').join('\\n')}"}`); console.log("----- END MOON TEST RESULT -----") } } diff --git a/crates/moonrun/src/template/js_glue.js b/crates/moonrun/src/template/js_glue.js index 94aa87e8..5746d800 100644 --- a/crates/moonrun/src/template/js_glue.js +++ b/crates/moonrun/src/template/js_glue.js @@ -51,7 +51,7 @@ try { instance.exports._start(); } catch (e) { console.log("----- BEGIN MOON TEST RESULT -----") - console.log(`{"package": "${packageName}", "filename": "${param[0]}", "index": "${param[1]}", "test_name": "${param[1]}", "message": "${e.stack.toString().split('\n').join('\\n')}"}`); + console.log(`{"package": "${packageName}", "filename": "${param[0]}", "index": "${param[1]}", "test_name": "${param[1]}", "message": "${e.stack.toString().replaceAll("\\", "\\\\").split('\n').join('\\n')}"}`); console.log("----- END MOON TEST RESULT -----") } } From ede5fb277ff29b67c6b33157f7da5e24f4bf81c8 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Wed, 4 Sep 2024 11:52:15 +0800 Subject: [PATCH 5/5] internal: tweak review --- crates/moon/src/cli/generate_test_driver.rs | 6 +- crates/moon/tests/test_cases/mod.rs | 110 +++++++++++++----- crates/moonbuild/src/entry.rs | 65 +++++------ crates/moonbuild/src/runtest.rs | 15 +-- .../template/test_driver_template.mbt | 1 - crates/moonutil/src/common.rs | 16 ++- 6 files changed, 130 insertions(+), 83 deletions(-) diff --git a/crates/moon/src/cli/generate_test_driver.rs b/crates/moon/src/cli/generate_test_driver.rs index 55fee918..cf9215a9 100644 --- a/crates/moon/src/cli/generate_test_driver.rs +++ b/crates/moon/src/cli/generate_test_driver.rs @@ -24,7 +24,7 @@ use moonutil::cli::UniversalFlags; use moonutil::common::{ lower_surface_targets, DriverKind, MoonbuildOpt, MooncGenTestInfo, RunMode, TargetBackend, TestOpt, BLACKBOX_TEST_DRIVER, INTERNAL_TEST_DRIVER, MOONBITLANG_CORE, - MOON_TEST_DELIMITER_BEGIN, MOON_TEST_DELIMITER_END, WHITEBOX_TEST_DRIVER, + MOON_TEST_DELIMITER_BEGIN, MOON_TEST_DELIMITER_END, TEST_INFO_FILE, WHITEBOX_TEST_DRIVER, }; use moonutil::dirs::PackageDirs; use moonutil::mooncakes::sync::AutoSyncFlags; @@ -64,7 +64,7 @@ fn moonc_gen_test_info(files: &[PathBuf], target_dir: &Path) -> anyhow::Result = moonutil::common::backend_filter(files, moonc_opt.link_opt.target_backend); let mbts_test_data = - moonc_gen_test_info(&backend_filtered, &target_dir.join(pkg.rel.full_name()))?; + moonc_gen_test_info(&backend_filtered, &target_dir.join(pkg.rel.fs_full_name()))?; if pkg.is_main && mbts_test_data.contains("(__test_") { eprintln!( diff --git a/crates/moon/tests/test_cases/mod.rs b/crates/moon/tests/test_cases/mod.rs index d9ee8d2a..2a04d6fc 100644 --- a/crates/moon/tests/test_cases/mod.rs +++ b/crates/moon/tests/test_cases/mod.rs @@ -1013,13 +1013,13 @@ fn test_moon_test_filter_package() { ], ), expect![[r#" - test hello_0 - test hello_1 - test hello_2 test A test B test C test D + test hello_0 + test hello_1 + test hello_2 Total tests: 7, passed: 7, failed: 0. "#]], ); @@ -1036,9 +1036,9 @@ fn test_moon_test_filter_package() { ], ), expect![[r#" + test A test hello_0 test hello_1 - test A Total tests: 3, passed: 3, failed: 0. "#]], ); @@ -1062,11 +1062,11 @@ fn test_moon_test_filter_multi_package() { ], ), expect![[r#" - Hello from lib7 Hello from lib1 Hello from lib2 + Hello from lib7 Hello from lib3 Hello from lib4 @@ -1090,18 +1090,18 @@ fn test_moon_test_filter_multi_package() { ], ), expect![[r#" - Hello from lib7 Hello from lib1 Hello from lib2 + Hello from lib7 Hello from lib3 Hello from lib4 - Hello from lib6 Hello from lib3 Hello from lib7 + Hello from lib6 Total tests: 7, passed: 7, failed: 0. "#]], ); @@ -1225,14 +1225,20 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" - Hello from lib7 Hello from lib1 Hello from lib2 + Hello from lib7 Total tests: 2, passed: 2, failed: 0. "#]], ); @@ -1240,7 +1246,13 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib1", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib1", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" Hello from lib3 @@ -1252,7 +1264,13 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib2", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib2", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" Hello from lib4 @@ -1263,13 +1281,19 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib3", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib3", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" - Hello from lib6 Hello from lib3 Hello from lib7 + Hello from lib6 Total tests: 3, passed: 3, failed: 0. "#]], ); @@ -1277,12 +1301,18 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib4", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib4", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" Hello from lib5 - Hello from lib7 Hello from lib5 + Hello from lib7 Total tests: 3, passed: 3, failed: 0. "#]], ); @@ -1290,7 +1320,13 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib5", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib5", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" Hello from lib5 @@ -1301,7 +1337,13 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib6", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib6", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" Hello from lib6 @@ -1312,11 +1354,17 @@ fn test_moon_test_filter_package_with_test_imports() { check( &get_stdout_with_args( &dir, - ["test", "-p", "username/hello/lib7", "--no-parallelize"], + [ + "test", + "-p", + "username/hello/lib7", + "--sort-input", + "--no-parallelize", + ], ), expect![[r#" - Hello from lib6 Hello from lib7 + Hello from lib6 Total tests: 2, passed: 2, failed: 0. "#]], ); @@ -1625,10 +1673,10 @@ fn test_moon_test_hello_exec_fntest() { check( &get_stdout_with_args(&dir, ["test", "-v", "--sort-input", "--no-parallelize"]), expect![[r#" - test in lib/hello_test.mbt - test moonbitlang/hello/lib/hello_wbtest.mbt::0 ok test in lib/hello.mbt test moonbitlang/hello/lib/hello.mbt::0 ok + test in lib/hello_test.mbt + test moonbitlang/hello/lib/hello_wbtest.mbt::0 ok Total tests: 2, passed: 2, failed: 0. "#]], ); @@ -2096,22 +2144,22 @@ fn test_moon_inline_test_order() { check( &get_stdout_with_args(&dir, ["test", "-v", "--sort-input", "--no-parallelize"]), expect![[r#" - A_test.mbt::init - A_test.mbt::test_hello_A - test username/hello/A/A_wbtest.mbt::0 ok - test username/hello/A/A_wbtest.mbt::1 ok executing A executing A::hello.mbt::test_A test username/hello/A/hello.mbt::0 ok test username/hello/A/hello.mbt::1 ok - B_test.mbt::init - B_test.mbt::test_hello_B - test username/hello/B/B_wbtest.mbt::0 ok - test username/hello/B/B_wbtest.mbt::1 ok + A_test.mbt::init + A_test.mbt::test_hello_A + test username/hello/A/A_wbtest.mbt::0 ok + test username/hello/A/A_wbtest.mbt::1 ok executing B executing B::hello.mbt::test_B test username/hello/B/hello.mbt::0 ok test username/hello/B/hello.mbt::1 ok + B_test.mbt::init + B_test.mbt::test_hello_B + test username/hello/B/B_wbtest.mbt::0 ok + test username/hello/B/B_wbtest.mbt::1 ok Total tests: 8, passed: 8, failed: 0. "#]], ); @@ -3424,9 +3472,9 @@ fn test_moon_test_release() { ["test", "--release", "--sort-input", "--no-parallelize"], ), expect![[r#" + test A test hello_0 test hello_1 - test A Total tests: 3, passed: 3, failed: 0. "#]], ); @@ -5992,9 +6040,9 @@ fn test_js() { check( &output, expect![[r#" + test A test hello_0 test hello_1 - test A Total tests: 3, passed: 3, failed: 0. "#]], ); diff --git a/crates/moonbuild/src/entry.rs b/crates/moonbuild/src/entry.rs index 6396e4ed..0030349c 100644 --- a/crates/moonbuild/src/entry.rs +++ b/crates/moonbuild/src/entry.rs @@ -36,7 +36,10 @@ use crate::check::normal::write_pkg_lst; use crate::expect::{apply_snapshot, render_snapshot_fail}; use crate::runtest::TestStatistics; -use moonutil::common::{MoonbuildOpt, MooncGenTestInfo, MooncOpt, TargetBackend, TestArtifacts}; +use moonutil::common::{ + DriverKind, FileName, MoonbuildOpt, MooncGenTestInfo, MooncOpt, TargetBackend, TestArtifacts, + TestBlockIndex, TestName, TEST_INFO_FILE, +}; use std::sync::{Arc, Mutex}; @@ -312,37 +315,29 @@ impl std::fmt::Display for TestResult { } } -// (pkg's runnable artifacts, ) for blackbox whitebox internal test -#[allow(clippy::type_complexity)] +pub type FileTestInfo = IndexMap>>; fn convert_moonc_test_info( test_info_file: &Path, pkg: &Package, output_format: &str, filter_file: Option<&String>, -) -> anyhow::Result< - Vec<( - Option, - IndexMap>>, - )>, -> { + sort_input: bool, +) -> anyhow::Result> { let content = std::fs::read_to_string(test_info_file) .context(format!("failed to read {}", test_info_file.display()))?; let mut moonc_test_info = MooncGenTestInfo { no_args_tests: IndexMap::new(), with_args_tests: IndexMap::new(), }; - for line in content.split('\n') { + // there are three line of json(internal, blackbox, whitebox) in test_info.json, merge them into one + for line in content.lines() { if let Ok(info) = serde_json_lenient::from_str::(line) { moonc_test_info.no_args_tests.extend(info.no_args_tests); moonc_test_info.with_args_tests.extend(info.with_args_tests); } } - let no_exist = ( - None, - IndexMap::>>::new(), - ); - let mut current_pkg_test_info = vec![no_exist.clone(), no_exist.clone(), no_exist.clone()]; + let mut current_pkg_test_info = IndexMap::new(); for (filename, test_info) in moonc_test_info .no_args_tests @@ -357,25 +352,28 @@ fn convert_moonc_test_info( continue; } } - let (test_type, index) = if filename.ends_with("_test.mbt") { - ("blackbox", 0) + let test_type = if filename.ends_with("_test.mbt") { + DriverKind::Blackbox.to_string() } else if filename.ends_with("_wbtest.mbt") { - ("whitebox", 1) + DriverKind::Whitebox.to_string() } else { - ("internal", 2) + DriverKind::Internal.to_string() }; - let artifact_path = pkg .artifact .with_file_name(format!("{}.{test_type}_test.wat", pkg.last_name())) .with_extension(output_format); - let (artifact_opt, map) = &mut current_pkg_test_info[index]; - if artifact_opt.is_none() { - *artifact_opt = Some(artifact_path); - } - let mbt_test_info = map.entry(filename).or_insert(IndexMap::new()); - mbt_test_info.extend(test_info.iter().map(|it| (it.index, it.name.clone()))); + current_pkg_test_info + .entry(artifact_path) + .or_insert(IndexMap::new()) + .entry(filename) + .or_insert(IndexMap::new()) + .extend(test_info.iter().map(|it| (it.index, it.name.clone()))); + } + + if sort_input { + current_pkg_test_info.sort_keys(); } Ok(current_pkg_test_info) @@ -425,19 +423,20 @@ pub fn run_test( } // convert moonc test info - let test_info_file = target_dir.join(pkg.rel.full_name()).join("test_info.json"); + let test_info_file = target_dir.join(pkg.rel.fs_full_name()).join(TEST_INFO_FILE); let current_pkg_test_info = convert_moonc_test_info( &test_info_file, pkg, moonc_opt.link_opt.output_format.to_str(), filter_file, + moonbuild_opt.sort_input, )?; for (artifact_path, file_test_info_map) in current_pkg_test_info { - if artifact_path.is_none() { - continue; - } - let artifact_path = artifact_path.unwrap(); + // if artifact_path.is_none() { + // continue; + // } + // let artifact_path = artifact_path.unwrap(); let mut test_args = TestArgs { package: pkgname.clone(), @@ -589,7 +588,7 @@ async fn execute_test( artifact_path: &Path, target_dir: &Path, args: &TestArgs, - file_test_info_map: &IndexMap>>, + file_test_info_map: &FileTestInfo, ) -> anyhow::Result>> { match target_backend { TargetBackend::Wasm | TargetBackend::WasmGC => { @@ -618,7 +617,7 @@ async fn handle_test_result( artifact_path: &Path, target_dir: &Path, printed: Arc, - file_test_info_map: &IndexMap>>, + file_test_info_map: &FileTestInfo, ) -> anyhow::Result<()> { let output_failure_in_json = moonbuild_opt .test_opt diff --git a/crates/moonbuild/src/runtest.rs b/crates/moonbuild/src/runtest.rs index 91dc0747..954c071a 100644 --- a/crates/moonbuild/src/runtest.rs +++ b/crates/moonbuild/src/runtest.rs @@ -16,13 +16,12 @@ // // For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn. -use crate::entry::{TestArgs, TestFailedStatus}; +use crate::entry::{FileTestInfo, TestArgs, TestFailedStatus}; use crate::expect::{snapshot_eq, ERROR, EXPECT_FAILED, FAILED, RUNTIME_ERROR, SNAPSHOT_TESTING}; use crate::section_capture::{handle_stdout, SectionCapture}; use super::gen; use anyhow::{bail, Context}; -use indexmap::IndexMap; use moonutil::common::{ MoonbuildOpt, MooncOpt, MOON_COVERAGE_DELIMITER_BEGIN, MOON_COVERAGE_DELIMITER_END, MOON_TEST_DELIMITER_BEGIN, MOON_TEST_DELIMITER_END, @@ -66,7 +65,7 @@ pub async fn run_wat( path: &Path, target_dir: &Path, args: &TestArgs, - file_test_info_map: &IndexMap>>, + file_test_info_map: &FileTestInfo, ) -> anyhow::Result>> { // put "--test-mode" at the front of args let mut _args = vec!["--test-mode".to_string()]; @@ -78,7 +77,7 @@ pub async fn run_js( path: &Path, target_dir: &Path, args: &TestArgs, - file_test_info_map: &IndexMap>>, + file_test_info_map: &FileTestInfo, ) -> anyhow::Result>> { run( "node", @@ -95,7 +94,7 @@ async fn run( path: &Path, target_dir: &Path, args: &[String], - file_test_info_map: &IndexMap>>, + file_test_info_map: &FileTestInfo, ) -> anyhow::Result>> { let mut execution = tokio::process::Command::new(command) .arg(path) @@ -162,10 +161,8 @@ async fn run( let index = &test_statistic.index.parse::().unwrap(); let test_name = file_test_info_map .get(filename) - .unwrap() - .get(index) - .unwrap() - .as_ref() + .and_then(|m| m.get(index)) + .and_then(|s| s.as_ref()) .unwrap_or(&test_statistic.index); if test_name.starts_with("panic") { diff --git a/crates/moonbuild/template/test_driver_template.mbt b/crates/moonbuild/template/test_driver_template.mbt index 09655143..a77b4e0c 100644 --- a/crates/moonbuild/template/test_driver_template.mbt +++ b/crates/moonbuild/template/test_driver_template.mbt @@ -101,7 +101,6 @@ let with_args_tests : Map[String, Map[Int, ((@test.T) -> Unit!Error, Array[Strin // REPLACE ME 0 fn main { - // let _ = tests let file_filter: String? = Some(moonbit_test_driver_internal_get_file_name()) let index = moonbit_test_driver_internal_get_index() let index_filter : Int? = Some(index) diff --git a/crates/moonutil/src/common.rs b/crates/moonutil/src/common.rs index 3b39700d..1de81bc3 100644 --- a/crates/moonutil/src/common.rs +++ b/crates/moonutil/src/common.rs @@ -51,6 +51,8 @@ pub const WATCH_MODE_DIR: &str = "watch"; pub const MOON_SNAPSHOT_DELIMITER_BEGIN: &str = "----- BEGIN MOONBIT SNAPSHOT TESTING -----"; pub const MOON_SNAPSHOT_DELIMITER_END: &str = "----- END MOONBIT SNAPSHOT TESTING -----"; +pub const TEST_INFO_FILE: &str = "test_info.json"; + pub fn startswith_and_trim(s: &str, t: &str) -> String { if s.starts_with(t) { s.replacen(t, "", 1) @@ -723,19 +725,21 @@ pub const INTERNAL_TEST_DRIVER: &str = "__generated_driver_for_internal_test.mbt pub const WHITEBOX_TEST_DRIVER: &str = "__generated_driver_for_whitebox_test.mbt"; pub const BLACKBOX_TEST_DRIVER: &str = "__generated_driver_for_blackbox_test.mbt"; +pub type FileName = String; +pub type TestName = String; +pub type TestBlockIndex = u32; + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MbtTestInfo { - pub index: u32, + pub index: TestBlockIndex, pub func: String, - pub name: Option, + pub name: Option, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MooncGenTestInfo { - // filename, test_info - // pub tests: HashMap>, - pub no_args_tests: IndexMap>, - pub with_args_tests: IndexMap>, + pub no_args_tests: IndexMap>, + pub with_args_tests: IndexMap>, } impl MooncGenTestInfo {