aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/cache_test.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-27 17:21:00 +0900
committernsfisis <nsfisis@gmail.com>2026-06-27 17:26:28 +0900
commite98823e599eb375b30037cc714710e3309d927d1 (patch)
treee1689ec164086798fc46aa6a58d1e914cf4873b2 /crates/shirabe/tests/cache_test.rs
parent20f620bdd0b5764ed2e9812dc0772f907b0d6f29 (diff)
downloadphp-shirabe-e98823e599eb375b30037cc714710e3309d927d1.tar.gz
php-shirabe-e98823e599eb375b30037cc714710e3309d927d1.tar.zst
php-shirabe-e98823e599eb375b30037cc714710e3309d927d1.zip
test: port Composer tests unblocked by mockall, add seams
Port 11 categories of previously-ignored Composer tests now reachable with the mockall crate: DownloadManager, VCS/Perforce/File downloaders, VersionSelector, PlatformRepository, Auditor, installer/FilesystemRepository, RootPackageLoader, util auth/http, commands, and Cache. Extract test seams additively on concrete structs as *Interface traits (Runtime, HhvmDetector, VersionGuesser, RepositorySet, Perforce, BinaryInstaller) plus mock-field seams (Cache, Filesystem); consumers take trait objects. Mocks are defined locally in the test crates via mockall::mock!, since automock-generated mocks are cfg(test)-gated and invisible across the integration-test boundary. dataProviders are ported in full; tests blocked by unported shims stay #[ignore] with documented reasons rather than reduced or weakened. Fix product bugs surfaced by the ports: - util/github: use the exception code, not the HTTP status, for 401/403 - advisory: serialize empty audit maps as [] to match PHP json_encode - repository/filesystem and downloader/file: fix RefCell double-borrow panics Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/tests/cache_test.rs')
-rw-r--r--crates/shirabe/tests/cache_test.rs62
1 files changed, 48 insertions, 14 deletions
diff --git a/crates/shirabe/tests/cache_test.rs b/crates/shirabe/tests/cache_test.rs
index 2e18856..7705714 100644
--- a/crates/shirabe/tests/cache_test.rs
+++ b/crates/shirabe/tests/cache_test.rs
@@ -4,7 +4,7 @@ use std::cell::RefCell;
use std::fs;
use std::rc::Rc;
-use shirabe::cache::Cache;
+use shirabe::cache::{Cache, CacheMock, GcFinderMock};
use shirabe::io::IOInterface;
use shirabe::io::null_io::NullIO;
use shirabe::util::filesystem::Filesystem;
@@ -27,8 +27,10 @@ fn set_up() -> SetUp {
files.push(path);
}
- // The finder/filesystem/IO mocks and the Cache mock overriding getFinder are not ported.
- let cache: Cache = todo!();
+ // PHP mocks Cache::getFinder and keeps the real Filesystem; here the CacheMock finder seam plays
+ // that role and the Cache otherwise operates on the real temp directory.
+ let io: Rc<RefCell<dyn IOInterface>> = Rc::new(RefCell::new(NullIO::new()));
+ let cache = Cache::new(io, root.path().to_str().unwrap(), None, None, false);
SetUp { root, files, cache }
}
@@ -56,25 +58,57 @@ impl Drop for TearDown {
}
}
-// In PHP these mock Cache::getFinder() to feed the gc() routine a controlled set of
-// files. getFinder is pub(crate) and cannot be overridden from a test, so the
-// finder-driven removal paths cannot be exercised faithfully here.
-#[ignore = "requires mocking Cache::get_finder (pub(crate), PHPUnit MockObject) to feed gc() a controlled Finder iterator; not overridable from a test"]
#[test]
fn test_remove_outdated_files() {
- let SetUp { root, files, cache } = set_up();
+ let SetUp {
+ root,
+ files,
+ mut cache,
+ } = set_up();
let _tear_down = TearDown::new(root.path().to_path_buf());
- let _ = (&files, &cache);
- todo!()
+
+ // The date('until ...') finder yields the outdated entries (files 1..3).
+ let outdated = files[1..].to_vec();
+ cache.__set_mock(CacheMock {
+ finder: Some(GcFinderMock {
+ outdated,
+ by_accessed_time: Vec::new(),
+ }),
+ ..Default::default()
+ });
+
+ cache.gc(600, 1024 * 1024 * 1024);
+
+ for (i, file) in files.iter().enumerate().skip(1) {
+ assert!(!file.exists(), "cached.file{i}.zip should be removed");
+ }
+ assert!(files[0].exists(), "cached.file0.zip should still exist");
}
-#[ignore = "requires mocking Cache::get_finder (pub(crate), PHPUnit MockObject) to feed gc() a controlled Finder iterator; not overridable from a test"]
#[test]
fn test_remove_files_when_cache_is_too_large() {
- let SetUp { root, files, cache } = set_up();
+ let SetUp {
+ root,
+ files,
+ mut cache,
+ } = set_up();
let _tear_down = TearDown::new(root.path().to_path_buf());
- let _ = (&files, &cache);
- todo!()
+
+ // The date filter matches nothing; the size-bound pass walks all files by accessed time.
+ cache.__set_mock(CacheMock {
+ finder: Some(GcFinderMock {
+ outdated: Vec::new(),
+ by_accessed_time: files.clone(),
+ }),
+ ..Default::default()
+ });
+
+ cache.gc(600, 1500);
+
+ for (i, file) in files.iter().enumerate().take(3) {
+ assert!(!file.exists(), "cached.file{i}.zip should be removed");
+ }
+ assert!(files[3].exists(), "cached.file3.zip should still exist");
}
#[test]