diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-21 04:07:16 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-21 04:07:16 +0900 |
| commit | 5f85bb006a71eef80037102936a6530510662365 (patch) | |
| tree | 4c9ff35c8fb7fe346cadc128c9d0fbbea6a47b6d /crates/shirabe | |
| parent | c3fe731783ef681e62026e723ed44f58810d404f (diff) | |
| download | php-shirabe-5f85bb006a71eef80037102936a6530510662365.tar.gz php-shirabe-5f85bb006a71eef80037102936a6530510662365.tar.zst php-shirabe-5f85bb006a71eef80037102936a6530510662365.zip | |
test(repository): port FilterRepositoryTest
All cases pass: only/exclude matching, both-filters guard, disabled child
advisories, and canonical vs non-canonical loadPackages. setUp's repository
fixture is inlined; teardown not ported.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe')
| -rw-r--r-- | crates/shirabe/tests/repository/filter_repository_test.rs | 130 | ||||
| -rw-r--r-- | crates/shirabe/tests/repository/main.rs | 1 |
2 files changed, 131 insertions, 0 deletions
diff --git a/crates/shirabe/tests/repository/filter_repository_test.rs b/crates/shirabe/tests/repository/filter_repository_test.rs index bbb46c5..1f8b64a 100644 --- a/crates/shirabe/tests/repository/filter_repository_test.rs +++ b/crates/shirabe/tests/repository/filter_repository_test.rs @@ -1 +1,131 @@ //! ref: composer/tests/Composer/Test/Repository/FilterRepositoryTest.php + +use indexmap::IndexMap; +use shirabe::package::base_package::STABILITIES; +use shirabe::repository::{ + AdvisoryProviderInterface, ArrayRepository, FilterRepository, RepositoryInterface, + RepositoryInterfaceHandle, +}; +use shirabe_semver::constraint::{AnyConstraint, MatchAllConstraint}; + +use crate::test_case::get_package; + +/// ref: FilterRepositoryTest::setUp +fn array_repo() -> RepositoryInterfaceHandle { + let repo = ArrayRepository::new(vec![ + get_package("foo/aaa", "1.0.0"), + get_package("foo/bbb", "1.0.0"), + get_package("bar/xxx", "1.0.0"), + get_package("baz/yyy", "1.0.0"), + ]) + .unwrap(); + RepositoryInterfaceHandle::new(repo) +} + +fn config(only: Option<&[&str]>, exclude: Option<&[&str]>) -> IndexMap<String, shirabe_php_shim::PhpMixed> { + use shirabe_php_shim::PhpMixed; + let mut c: IndexMap<String, PhpMixed> = IndexMap::new(); + if let Some(only) = only { + c.insert( + "only".to_string(), + PhpMixed::List(only.iter().map(|s| PhpMixed::String(s.to_string())).collect()), + ); + } + if let Some(exclude) = exclude { + c.insert( + "exclude".to_string(), + PhpMixed::List(exclude.iter().map(|s| PhpMixed::String(s.to_string())).collect()), + ); + } + c +} + +fn match_all() -> AnyConstraint { + MatchAllConstraint::new(None).into() +} + +fn stabilities() -> IndexMap<String, i64> { + STABILITIES.iter().map(|(k, v)| (k.to_string(), *v)).collect() +} + +#[test] +fn test_repo_matching() { + let all = vec!["foo/aaa", "foo/bbb", "bar/xxx", "baz/yyy"]; + let cases: Vec<(Vec<&str>, IndexMap<String, shirabe_php_shim::PhpMixed>)> = vec![ + (vec!["foo/aaa", "foo/bbb"], config(Some(&["foo/*"]), None)), + ( + vec!["foo/aaa", "baz/yyy"], + config(Some(&["foo/aaa", "baz/yyy"]), None), + ), + (vec!["bar/xxx"], config(None, Some(&["foo/*", "baz/yyy"]))), + // make sure sub-patterns are not matched without wildcard + (all.clone(), config(None, Some(&["foo/aa", "az/yyy"]))), + (vec![], config(Some(&["foo/aa", "az/yyy"]), None)), + // empty "only" means no packages allowed + (vec![], config(Some(&[]), None)), + // absent "only" means all packages allowed + (all.clone(), config(None, None)), + // empty or absent "exclude" have the same effect: none + (all.clone(), config(None, Some(&[]))), + (all.clone(), config(None, None)), + ]; + + for (expected, cfg) in cases { + let mut repo = FilterRepository::new(array_repo(), cfg).unwrap(); + let packages = repo.get_packages().unwrap(); + let names: Vec<String> = packages.iter().map(|p| p.get_name()).collect(); + + let expected: Vec<String> = expected.iter().map(|s| s.to_string()).collect(); + assert_eq!(expected, names); + } +} + +#[test] +fn test_both_filters_disallowed() { + assert!(FilterRepository::new(array_repo(), config(Some(&[]), Some(&[]))).is_err()); +} + +#[test] +fn test_security_advisories_disabled_in_child() { + let mut repo = FilterRepository::new(array_repo(), config(Some(&["foo/*"]), None)).unwrap(); + + assert!(!repo.has_security_advisories().unwrap()); + + let mut map: IndexMap<String, AnyConstraint> = IndexMap::new(); + map.insert("foo/aaa".to_string(), match_all()); + let result = repo.get_security_advisories(map, true).unwrap(); + + assert!(result.names_found.is_empty()); + assert!(result.advisories.is_empty()); +} + +#[test] +fn test_canonical_default_true() { + let mut repo = FilterRepository::new(array_repo(), config(None, None)).unwrap(); + + let mut map: IndexMap<String, Option<AnyConstraint>> = IndexMap::new(); + map.insert("foo/aaa".to_string(), Some(match_all())); + let result = repo + .load_packages(map, stabilities(), IndexMap::new(), IndexMap::new()) + .unwrap(); + + assert_eq!(1, result.packages.len()); + assert_eq!(1, result.names_found.len()); +} + +#[test] +fn test_non_canonical() { + use shirabe_php_shim::PhpMixed; + let mut cfg: IndexMap<String, PhpMixed> = IndexMap::new(); + cfg.insert("canonical".to_string(), PhpMixed::Bool(false)); + let mut repo = FilterRepository::new(array_repo(), cfg).unwrap(); + + let mut map: IndexMap<String, Option<AnyConstraint>> = IndexMap::new(); + map.insert("foo/aaa".to_string(), Some(match_all())); + let result = repo + .load_packages(map, stabilities(), IndexMap::new(), IndexMap::new()) + .unwrap(); + + assert_eq!(1, result.packages.len()); + assert_eq!(0, result.names_found.len()); +} diff --git a/crates/shirabe/tests/repository/main.rs b/crates/shirabe/tests/repository/main.rs index b2b4707..195781e 100644 --- a/crates/shirabe/tests/repository/main.rs +++ b/crates/shirabe/tests/repository/main.rs @@ -3,6 +3,7 @@ mod test_case; mod array_repository_test; mod composite_repository_test; +mod filter_repository_test; mod installed_repository_test; mod repository_factory_test; mod repository_utils_test; |
