diff --git a/crates/moonbuild/src/gen/gen_check.rs b/crates/moonbuild/src/gen/gen_check.rs index 5561eb7a..f78cd0f2 100644 --- a/crates/moonbuild/src/gen/gen_check.rs +++ b/crates/moonbuild/src/gen/gen_check.rs @@ -18,6 +18,7 @@ use super::cmd_builder::CommandBuilder; use super::n2_errors::{N2Error, N2ErrorKind}; +use super::util::self_in_test_import; use crate::gen::MiAlias; use anyhow::bail; use indexmap::map::IndexMap; @@ -198,9 +199,7 @@ fn pkg_with_test_to_check_item( pkg: &Package, moonc_opt: &MooncOpt, ) -> anyhow::Result { - let self_in_test_import = pkg.test_imports.iter().any(|import| { - import.path.make_full_path() == format!("{}/{}", pkg.root.full_name(), pkg.rel.full_name()) - }); + let self_in_test_import = self_in_test_import(pkg); if !self_in_test_import && pkg diff --git a/crates/moonbuild/src/gen/gen_runtest.rs b/crates/moonbuild/src/gen/gen_runtest.rs index 96f49f0c..baf1bf84 100644 --- a/crates/moonbuild/src/gen/gen_runtest.rs +++ b/crates/moonbuild/src/gen/gen_runtest.rs @@ -30,6 +30,7 @@ use moonutil::path::{ImportPath, PathComponent}; use petgraph::graph::NodeIndex; use super::cmd_builder::CommandBuilder; +use super::util::self_in_test_import; use super::{is_self_coverage_lib, is_skip_coverage_lib}; use std::collections::HashSet; use std::path::PathBuf; @@ -418,9 +419,7 @@ pub fn gen_package_blackbox_test( moonc_opt: &MooncOpt, patch_file: Option, ) -> anyhow::Result { - let self_in_test_import = pkg.test_imports.iter().any(|import| { - import.path.make_full_path() == format!("{}/{}", pkg.root.full_name(), pkg.rel.full_name()) - }); + let self_in_test_import = self_in_test_import(pkg); if !self_in_test_import && pkg diff --git a/crates/moonbuild/src/gen/util.rs b/crates/moonbuild/src/gen/util.rs index 92ca027b..658c871b 100644 --- a/crates/moonbuild/src/gen/util.rs +++ b/crates/moonbuild/src/gen/util.rs @@ -112,3 +112,17 @@ pub fn nodes_to_pkg_sources(m: &ModuleDB, nodes: &[String]) -> Vec<(String, Stri pub fn graph_to_dot(m: &ModuleDB) { println!("{:?}", Dot::with_config(&m.graph, &[Config::EdgeNoLabel])); } + +pub fn self_in_test_import(pkg: &Package) -> bool { + // for package in the same level of mod, like "Yoorkin/prettyprinter", pkg.rel.full_name() is_empty + // current_pkg_full_path should be "Yoorkin/prettyprinter" instead of "Yoorkin/prettyprinter/" + let current_pkg_full_path = if pkg.rel.full_name().is_empty() { + pkg.root.full_name() + } else { + format!("{}/{}", pkg.root.full_name(), pkg.rel.full_name()) + }; + + pkg.test_imports + .iter() + .any(|import| import.path.make_full_path() == current_pkg_full_path) +}