Skip to content

Commit

Permalink
Merge pull request #330 from moonbitlang/fix_coverage_builtin
Browse files Browse the repository at this point in the history
fix coverage builtin
  • Loading branch information
Young-Flash authored Sep 20, 2024
2 parents 507657e + d0ca128 commit 13f54ef
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
56 changes: 34 additions & 22 deletions crates/moon/src/cli/generate_test_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct GenerateTestDriverSubcommand {
pub driver_kind: DriverKind,
}

fn moonc_gen_test_info(files: &[PathBuf], target_dir: &Path) -> anyhow::Result<String> {
fn moonc_gen_test_info(files: &[PathBuf], output_path: &Path) -> anyhow::Result<String> {
let mut generated = std::process::Command::new("moonc")
.arg("gen-test-info")
.arg("-json")
Expand All @@ -68,24 +68,21 @@ fn moonc_gen_test_info(files: &[PathBuf], target_dir: &Path) -> anyhow::Result<S
.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_FILE);
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 test_info_json_path = output_path;
std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(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());
Expand Down Expand Up @@ -191,9 +188,24 @@ pub fn generate_test_driver(
files,
moonc_opt.build_opt.debug_flag,
moonc_opt.build_opt.target_backend,
);
let mbts_test_data =
moonc_gen_test_info(&backend_filtered, &target_dir.join(pkg.rel.fs_full_name()))?;
)
.into_iter()
.filter(|file| {
// workaround for skip test coverage.mbt in builtin when --enable-coverage is specified
!(moonc_opt.build_opt.enable_coverage
&& pkgname == "moonbitlang/core/builtin"
&& file.to_str().unwrap().contains("coverage.mbt"))
})
.collect();

let mbts_test_data = moonc_gen_test_info(
&backend_filtered,
&target_dir.join(pkg.rel.fs_full_name()).join(format!(
"__{}_{}",
cmd.driver_kind.to_string(),
TEST_INFO_FILE,
)),
)?;

if pkg.is_main && mbts_test_data.contains("(__test_") {
eprintln!(
Expand Down
38 changes: 25 additions & 13 deletions crates/moonbuild/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,18 +477,33 @@ fn convert_moonc_test_info(
filter_file: Option<&String>,
sort_input: bool,
) -> anyhow::Result<IndexMap<PathBuf, FileTestInfo>> {
let content = std::fs::read_to_string(test_info_file)
.context(format!("failed to read {}", test_info_file.display()))?;
let mut test_info_files = vec![];
for (files, driver_kind) in [
(&pkg.files, DriverKind::Internal),
(&pkg.wbtest_files, DriverKind::Whitebox),
(&pkg.test_files, DriverKind::Blackbox),
] {
if !files.is_empty() {
test_info_files.push(test_info_file.join(format!(
"__{}_{}",
driver_kind.to_string(),
TEST_INFO_FILE
)));
}
}

let mut moonc_test_info = MooncGenTestInfo {
no_args_tests: IndexMap::new(),
with_args_tests: IndexMap::new(),
};
// 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::<MooncGenTestInfo>(line) {
moonc_test_info.no_args_tests.extend(info.no_args_tests);
moonc_test_info.with_args_tests.extend(info.with_args_tests);
}
for test_info_file in test_info_files {
let content = std::fs::read_to_string(&test_info_file)
.context(format!("failed to read {}", test_info_file.display()))?;

let info = serde_json_lenient::from_str::<MooncGenTestInfo>(&content)
.context(format!("failed to parse {}", test_info_file.display()))?;
moonc_test_info.no_args_tests.extend(info.no_args_tests);
moonc_test_info.with_args_tests.extend(info.with_args_tests);
}

let mut current_pkg_test_info = IndexMap::new();
Expand Down Expand Up @@ -575,12 +590,9 @@ pub fn run_test(
}

// convert moonc test info
let test_info_file = moonbuild_opt
.target_dir
.join(pkg.rel.fs_full_name())
.join(TEST_INFO_FILE);
let test_info_file_dir = moonbuild_opt.target_dir.join(pkg.rel.fs_full_name());
let current_pkg_test_info = convert_moonc_test_info(
&test_info_file,
&test_info_file_dir,
pkg,
moonc_opt.link_opt.output_format.to_str(),
filter_file,
Expand Down

0 comments on commit 13f54ef

Please sign in to comment.