diff options
Diffstat (limited to 'crates/shirabe/src/util')
| -rw-r--r-- | crates/shirabe/src/util/filesystem.rs | 39 | ||||
| -rw-r--r-- | crates/shirabe/src/util/github.rs | 2 | ||||
| -rw-r--r-- | crates/shirabe/src/util/perforce.rs | 82 |
3 files changed, 122 insertions, 1 deletions
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<std::rc::Rc<std::cell::RefCell<ProcessExecutor>>>, + /// Test-only seam. Always `None` in production; configured via [`Filesystem::__set_mock`]. + mock: Option<FilesystemMock>, +} + +/// 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<bool>, + 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<std::rc::Rc<std::cell::RefCell<ProcessExecutor>>>) -> 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<Path>) -> anyhow::Result<bool> { 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<bool> { + 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<String> = vec![]; let mut path = strtr(path, "\\", "/"); let mut prefix = String::new(); diff --git a/crates/shirabe/src/util/github.rs b/crates/shirabe/src/util/github.rs index dae4954..285052e 100644 --- a/crates/shirabe/src/util/github.rs +++ b/crates/shirabe/src/util/github.rs @@ -241,7 +241,7 @@ impl GitHub { Err(te) => { let code = te .downcast_ref::<crate::downloader::TransportException>() - .and_then(|t| t.get_status_code()) + .map(|t| t.get_code()) .unwrap_or(0); if code == 403 || code == 401 { self.io.write_error3( diff --git a/crates/shirabe/src/util/perforce.rs b/crates/shirabe/src/util/perforce.rs index 4d4ddef..d8cd39d 100644 --- a/crates/shirabe/src/util/perforce.rs +++ b/crates/shirabe/src/util/perforce.rs @@ -838,3 +838,85 @@ impl Perforce { .clone() } } + +/// Seam over `Perforce` so consumers can be exercised with a mock in tests. +pub trait PerforceInterface: std::fmt::Debug { + fn initialize_path(&mut self, path: &str); + fn set_stream(&mut self, stream: &str); + fn p4_login(&mut self) -> Result<()>; + fn check_stream(&mut self) -> bool; + fn write_p4_client_spec(&mut self) -> Result<()>; + fn connect_client(&mut self) -> Result<()>; + fn sync_code_base(&mut self, source_reference: Option<String>) -> Result<()>; + fn cleanup_client_spec(&mut self); + fn get_commit_logs(&mut self, from_reference: &str, to_reference: &str) -> Option<String>; + fn get_file_content(&mut self, file: &str, identifier: &str) -> Option<String>; + fn get_branches(&mut self) -> IndexMap<String, String>; + fn get_tags(&mut self) -> IndexMap<String, String>; + fn get_user(&self) -> Option<String>; + fn get_composer_information( + &mut self, + identifier: &str, + ) -> Result<Option<IndexMap<String, PhpMixed>>>; +} + +impl PerforceInterface for Perforce { + fn initialize_path(&mut self, path: &str) { + Perforce::initialize_path(self, path) + } + + fn set_stream(&mut self, stream: &str) { + Perforce::set_stream(self, stream) + } + + fn p4_login(&mut self) -> Result<()> { + Perforce::p4_login(self) + } + + fn check_stream(&mut self) -> bool { + Perforce::check_stream(self) + } + + fn write_p4_client_spec(&mut self) -> Result<()> { + Perforce::write_p4_client_spec(self) + } + + fn connect_client(&mut self) -> Result<()> { + Perforce::connect_client(self) + } + + fn sync_code_base(&mut self, source_reference: Option<String>) -> Result<()> { + Perforce::sync_code_base(self, source_reference.as_deref()) + } + + fn cleanup_client_spec(&mut self) { + Perforce::cleanup_client_spec(self) + } + + fn get_commit_logs(&mut self, from_reference: &str, to_reference: &str) -> Option<String> { + Perforce::get_commit_logs(self, from_reference, to_reference) + } + + fn get_file_content(&mut self, file: &str, identifier: &str) -> Option<String> { + Perforce::get_file_content(self, file, identifier) + } + + fn get_branches(&mut self) -> IndexMap<String, String> { + Perforce::get_branches(self) + } + + fn get_tags(&mut self) -> IndexMap<String, String> { + Perforce::get_tags(self) + } + + fn get_user(&self) -> Option<String> { + Perforce::get_user(self) + } + + fn get_composer_information( + &mut self, + identifier: &str, + ) -> Result<Option<IndexMap<String, PhpMixed>>> { + Perforce::get_composer_information(self, identifier) + } +} |
