From e98823e599eb375b30037cc714710e3309d927d1 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 27 Jun 2026 17:21:00 +0900 Subject: 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) --- crates/shirabe/src/util/filesystem.rs | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'crates/shirabe/src/util/filesystem.rs') diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs index 6dd2ab9..b224767 100644 --- a/crates/shirabe/src/util/filesystem.rs +++ b/crates/shirabe/src/util/filesystem.rs @@ -22,15 +22,41 @@ use crate::util::Silencer; #[derive(Debug)] pub struct Filesystem { process_executor: Option>>, + /// Test-only seam. Always `None` in production; configured via [`Filesystem::__set_mock`]. + mock: Option, +} + +/// Test-only seam mirroring the PHP FileDownloaderTest mock of `Filesystem`. +#[derive(Debug, Default)] +pub struct FilesystemMock { + /// When `Some`, `remove_directory_async` returns it without touching disk and counts the call. + pub remove_directory_async_result: Option, + pub remove_directory_async_calls: usize, + /// When true, `normalize_path` returns its argument unchanged. + pub normalize_path_identity: bool, } impl Filesystem { pub fn new(executor: Option>>) -> Self { Self { process_executor: executor, + mock: None, } } + /// For testing only: install the [`FilesystemMock`] seam used by FileDownloaderTest. + pub fn __set_mock(&mut self, mock: FilesystemMock) { + self.mock = Some(mock); + } + + /// For testing only: number of `remove_directory_async` calls intercepted by the seam. + pub fn __remove_directory_async_calls(&self) -> usize { + self.mock + .as_ref() + .map(|m| m.remove_directory_async_calls) + .unwrap_or(0) + } + pub fn remove(&mut self, file: impl AsRef) -> anyhow::Result { let file = file.as_ref(); if is_dir(file) { @@ -140,6 +166,13 @@ impl Filesystem { /// Uses the process component if proc_open is enabled on the PHP /// installation. pub async fn remove_directory_async(&mut self, directory: &str) -> anyhow::Result { + if let Some(mock) = self.mock.as_mut() + && let Some(result) = mock.remove_directory_async_result + { + mock.remove_directory_async_calls += 1; + return Ok(result); + } + let edge_case_result = self.remove_edge_cases(directory, true)?; if let Some(r) = edge_case_result { return Ok(r); @@ -702,6 +735,12 @@ impl Filesystem { /// Normalize a path. This replaces backslashes with slashes, removes ending /// slash and collapses redundant separators and up-level references. pub fn normalize_path(&self, path: &str) -> String { + if let Some(mock) = &self.mock + && mock.normalize_path_identity + { + return path.to_string(); + } + let mut parts: Vec = vec![]; let mut path = strtr(path, "\\", "/"); let mut prefix = String::new(); -- cgit v1.3.1