Skip to content

Commit

Permalink
feat: fuzzy matching for moon test --package
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchen committed Sep 26, 2024
1 parent 9e76256 commit 976dd3b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fs4 = { version = "0.8.3", features = ["sync"] }
ariadne = { version = "0.4.1", features = ["auto-color"] }
clap_complete = { version = "4.5.4" }
schemars = "0.8"
nucleo-matcher = "0.3.1"

[profile.release]
debug = false
Expand Down
48 changes: 47 additions & 1 deletion crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ use moonutil::dirs::PackageDirs;
use moonutil::module::ModuleDB;
use moonutil::mooncakes::sync::AutoSyncFlags;
use moonutil::mooncakes::RegistryConfig;
use moonutil::package::Package;
use n2::trace;
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -210,7 +212,51 @@ fn run_test_internal(
&moonbuild_opt,
)?;

let package_filter = moonbuild_opt.get_package_filter();
let (package_filter, moonbuild_opt) = if let Some(filter_package) = moonbuild_opt
.test_opt
.as_ref()
.and_then(|opt| opt.filter_package.as_ref())
{
let all_packages: HashSet<String> = module
.get_all_packages()
.iter()
.map(|pkg| pkg.0.to_string())
.collect();

let mut final_set = HashSet::new();
for pkg in filter_package {
let needle = pkg.display().to_string();
if all_packages.contains(&needle) {
// exact matching
final_set.insert(needle);
} else {
let xs = moonutil::fuzzy_match::fuzzy_match(&needle, &all_packages);
if let Some(xs) = xs {
final_set.extend(xs);
}
}
}

let moonbuild_opt = MoonbuildOpt {
test_opt: Some(TestOpt {
filter_package: Some(
final_set
.clone()
.into_iter()
.map(|it| PathBuf::from(it))
.collect(),
),
..moonbuild_opt.test_opt.unwrap()
}),
..moonbuild_opt
};

let package_filter = Some(move |pkg: &Package| final_set.contains(&pkg.full_name()));
(package_filter, moonbuild_opt)
} else {
(None, moonbuild_opt)
};

for (_, pkg) in module.get_filtered_packages_mut(package_filter) {
if pkg.is_third_party || pkg.is_main {
continue;
Expand Down
1 change: 1 addition & 0 deletions crates/moonutil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ log.workspace = true
thiserror.workspace = true
schemars.workspace = true
line-index.workspace = true
nucleo-matcher.workspace = true

[dev-dependencies]
expect-test.workspace = true
Expand Down
56 changes: 56 additions & 0 deletions crates/moonutil/src/fuzzy_match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pub fn fuzzy_match<T: AsRef<str>>(
needle: T,
haystack: impl IntoIterator<Item = T>,
) -> Option<Vec<String>> {
let mut matcher = nucleo_matcher::Matcher::new(nucleo_matcher::Config::DEFAULT.match_paths());
let matches = nucleo_matcher::pattern::Pattern::parse(
needle.as_ref(),
nucleo_matcher::pattern::CaseMatching::Ignore,
nucleo_matcher::pattern::Normalization::Smart,
)
.match_list(haystack, &mut matcher);
if matches.is_empty() {
None
} else {
Some(
matches
.into_iter()
.map(|m| m.0.as_ref().to_string())
.collect(),
)
}
}

#[test]
fn test_fuzzy() {
let haystack = [
"moonbitlang/core/builtin",
"moonbitlang/core/int",
"moonbitlang/core/list",
"moonbitlang/core/list/internal",
"moonbitlang/core/hashmap",
];
let result = fuzzy_match("mci", haystack);
expect_test::expect![[r#"
Some(
[
"moonbitlang/core/int",
"moonbitlang/core/list/internal",
"moonbitlang/core/list",
"moonbitlang/core/builtin",
],
)
"#]]
.assert_debug_eq(&result);

let result = fuzzy_match("moonbitlang/core/list", haystack);
expect_test::expect![[r#"
Some(
[
"moonbitlang/core/list",
"moonbitlang/core/list/internal",
],
)
"#]]
.assert_debug_eq(&result);
}
1 change: 1 addition & 0 deletions crates/moonutil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod common;
pub mod cond_expr;
pub mod dependency;
pub mod dirs;
pub mod fuzzy_match;
pub mod git;
pub mod graph;
pub mod module;
Expand Down

0 comments on commit 976dd3b

Please sign in to comment.