Skip to content

Commit 457f702

Browse files
committed
test(cli): unit-test partition_purls ecosystem filter
Adds a #[cfg(test)] mod tests block at the bottom of crates/socket-patch-cli/src/ecosystem_dispatch.rs covering the partition_purls helper that splits PURLs by ecosystem and applies the --ecosystems allow-list filter for apply/rollback/scan. Nine cases: - No filter, single npm PURL groups into Ecosystem::Npm. - No filter, mixed npm/pypi/cargo PURLs each land in their own bucket. - No filter, empty input returns an empty map. - No filter, duplicate PURLs are preserved (no dedupe). - No filter, unrecognized "pkg:weirdo/..." is dropped. - Allow-list ["npm"] excludes pypi PURLs. - Allow-list ["pypi"] against an npm-only input returns empty. - Allow-list ["npm","pypi"] keeps both buckets. - Empty allow-list Some(&[]) matches nothing. No production code changed; no filesystem-touching helpers exercised. Verified: cargo build/clippy/test --workspace --all-features all clean (9 new tests pass). Assisted-by: Claude Code:claude-opus-4-7
1 parent 0bd4f50 commit 457f702

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

crates/socket-patch-cli/src/ecosystem_dispatch.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,3 +704,128 @@ pub async fn find_packages_for_rollback(
704704

705705
all_packages
706706
}
707+
708+
#[cfg(test)]
709+
mod tests {
710+
use super::*;
711+
712+
#[test]
713+
fn partition_purls_no_filter_single_npm() {
714+
let purls = vec!["pkg:npm/foo@1.0".to_string()];
715+
let map = partition_purls(&purls, None);
716+
assert_eq!(map.len(), 1);
717+
assert_eq!(
718+
map.get(&Ecosystem::Npm),
719+
Some(&vec!["pkg:npm/foo@1.0".to_string()])
720+
);
721+
}
722+
723+
#[test]
724+
fn partition_purls_no_filter_mixed_ecosystems() {
725+
let purls = vec![
726+
"pkg:npm/foo@1.0".to_string(),
727+
"pkg:pypi/bar@2.0".to_string(),
728+
"pkg:cargo/baz@3.0".to_string(),
729+
];
730+
let map = partition_purls(&purls, None);
731+
assert_eq!(map.len(), 3);
732+
assert_eq!(
733+
map.get(&Ecosystem::Npm),
734+
Some(&vec!["pkg:npm/foo@1.0".to_string()])
735+
);
736+
assert_eq!(
737+
map.get(&Ecosystem::Pypi),
738+
Some(&vec!["pkg:pypi/bar@2.0".to_string()])
739+
);
740+
#[cfg(feature = "cargo")]
741+
assert_eq!(
742+
map.get(&Ecosystem::Cargo),
743+
Some(&vec!["pkg:cargo/baz@3.0".to_string()])
744+
);
745+
}
746+
747+
#[test]
748+
fn partition_purls_no_filter_empty_input() {
749+
let purls: Vec<String> = Vec::new();
750+
let map = partition_purls(&purls, None);
751+
assert!(map.is_empty());
752+
}
753+
754+
#[test]
755+
fn partition_purls_no_filter_duplicate_purls_preserved() {
756+
let purls = vec![
757+
"pkg:npm/foo@1.0".to_string(),
758+
"pkg:npm/foo@1.0".to_string(),
759+
];
760+
let map = partition_purls(&purls, None);
761+
assert_eq!(map.len(), 1);
762+
assert_eq!(
763+
map.get(&Ecosystem::Npm),
764+
Some(&vec![
765+
"pkg:npm/foo@1.0".to_string(),
766+
"pkg:npm/foo@1.0".to_string(),
767+
])
768+
);
769+
}
770+
771+
#[test]
772+
fn partition_purls_no_filter_unknown_ecosystem_dropped() {
773+
let purls = vec!["pkg:weirdo/x@1".to_string()];
774+
let map = partition_purls(&purls, None);
775+
assert!(map.is_empty());
776+
}
777+
778+
#[test]
779+
fn partition_purls_allow_list_excludes_one() {
780+
let purls = vec![
781+
"pkg:npm/foo@1.0".to_string(),
782+
"pkg:pypi/bar@2.0".to_string(),
783+
];
784+
let allowed = vec!["npm".to_string()];
785+
let map = partition_purls(&purls, Some(allowed.as_slice()));
786+
assert_eq!(map.len(), 1);
787+
assert_eq!(
788+
map.get(&Ecosystem::Npm),
789+
Some(&vec!["pkg:npm/foo@1.0".to_string()])
790+
);
791+
assert!(map.get(&Ecosystem::Pypi).is_none());
792+
}
793+
794+
#[test]
795+
fn partition_purls_allow_list_matches_none() {
796+
let purls = vec!["pkg:npm/foo@1.0".to_string()];
797+
let allowed = vec!["pypi".to_string()];
798+
let map = partition_purls(&purls, Some(allowed.as_slice()));
799+
assert!(map.is_empty());
800+
}
801+
802+
#[test]
803+
fn partition_purls_allow_list_matches_all() {
804+
let purls = vec![
805+
"pkg:npm/foo@1.0".to_string(),
806+
"pkg:pypi/bar@2.0".to_string(),
807+
];
808+
let allowed = vec!["npm".to_string(), "pypi".to_string()];
809+
let map = partition_purls(&purls, Some(allowed.as_slice()));
810+
assert_eq!(map.len(), 2);
811+
assert_eq!(
812+
map.get(&Ecosystem::Npm),
813+
Some(&vec!["pkg:npm/foo@1.0".to_string()])
814+
);
815+
assert_eq!(
816+
map.get(&Ecosystem::Pypi),
817+
Some(&vec!["pkg:pypi/bar@2.0".to_string()])
818+
);
819+
}
820+
821+
#[test]
822+
fn partition_purls_empty_allow_list_matches_nothing() {
823+
let purls = vec![
824+
"pkg:npm/foo@1.0".to_string(),
825+
"pkg:pypi/bar@2.0".to_string(),
826+
];
827+
let allowed: Vec<String> = Vec::new();
828+
let map = partition_purls(&purls, Some(allowed.as_slice()));
829+
assert!(map.is_empty());
830+
}
831+
}

0 commit comments

Comments
 (0)