diff options
Diffstat (limited to 'crates/shirabe')
| -rw-r--r-- | crates/shirabe/src/util/filesystem.rs | 17 | ||||
| -rw-r--r-- | crates/shirabe/tests/downloader/git_downloader_test.rs | 56 |
2 files changed, 57 insertions, 16 deletions
diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs index 3516b284..51885cc3 100644 --- a/crates/shirabe/src/util/filesystem.rs +++ b/crates/shirabe/src/util/filesystem.rs @@ -27,9 +27,9 @@ pub struct Filesystem { /// 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. + /// When `Some`, `remove_directory_async` returns it without touching disk and records the call. pub remove_directory_async_result: Option<bool>, - pub remove_directory_async_calls: usize, + pub remove_directory_async_paths: Vec<String>, /// When true, `normalize_path` returns its argument unchanged. pub normalize_path_identity: bool, } @@ -49,10 +49,16 @@ impl Filesystem { /// For testing only: number of `remove_directory_async` calls intercepted by the seam. pub fn __remove_directory_async_calls(&self) -> usize { + self.__remove_directory_async_paths().len() + } + + /// For testing only: the directories passed to `remove_directory_async` calls intercepted by + /// the seam, in call order (ref PHPUnit's `->with($this->equalTo($this->workingDir))`). + pub fn __remove_directory_async_paths(&self) -> Vec<String> { self.mock .as_ref() - .map(|m| m.remove_directory_async_calls) - .unwrap_or(0) + .map(|m| m.remove_directory_async_paths.clone()) + .unwrap_or_default() } pub fn remove(&mut self, file: impl AsRef<Path>) -> anyhow::Result<bool> { @@ -177,7 +183,8 @@ impl Filesystem { if let Some(mock) = fs.mock.as_mut() && let Some(result) = mock.remove_directory_async_result { - mock.remove_directory_async_calls += 1; + mock.remove_directory_async_paths + .push(directory.to_string()); return Ok(result); } diff --git a/crates/shirabe/tests/downloader/git_downloader_test.rs b/crates/shirabe/tests/downloader/git_downloader_test.rs index f7c82066..41852300 100644 --- a/crates/shirabe/tests/downloader/git_downloader_test.rs +++ b/crates/shirabe/tests/downloader/git_downloader_test.rs @@ -16,7 +16,7 @@ use shirabe::package::Mirror; use shirabe::package::handle::{CompletePackageHandle, PackageInterfaceHandle}; use shirabe::util::Git as GitUtil; use shirabe::util::ProcessExecutor; -use shirabe::util::filesystem::Filesystem; +use shirabe::util::filesystem::{Filesystem, FilesystemMock}; use shirabe_php_shim::PhpMixed; use shirabe_semver::VersionParser; use tempfile::TempDir; @@ -1110,20 +1110,54 @@ fn test_not_using_downgrading_with_references() { }); } -#[ignore = "PHP mocks Filesystem::removeDirectoryAsync (asserting it is called once with the \ - working dir). With no Filesystem mock, the real removeDirectoryAsync drives the \ - Filesystem's own ProcessExecutor for `rm -rf`, which requires a Composer\\Loop and \ - cannot be redirected through the mocked ProcessExecutor"] +#[serial] #[test] fn test_remove() { let working_dir = set_up(); let _tear_down = TearDown::new(working_dir.path().to_path_buf()); - let _ = &working_dir; - // TODO(phase-d): PHP mocks Filesystem::removeDirectoryAsync (asserting it is called once - // with the working dir). With no Filesystem mock, the real removeDirectoryAsync drives - // the Filesystem's own ProcessExecutor for `rm -rf`, which requires a Composer\Loop and - // cannot be redirected through the mocked ProcessExecutor. - todo!() + let working_dir_str = working_dir.path().to_string_lossy().into_owned(); + + let package = get_package("dummy/pkg", "1.0.0", None, None); + let (process, _guard) = get_process_executor_mock( + vec![ + cmd(vec!["git", "show-ref", "--head", "-d"]), + cmd(vec!["git", "status", "--porcelain", "--untracked-files=no"]), + ], + true, + Default::default(), + ); + + Filesystem::new(None) + .ensure_directory_exists(&format!("{}/.git", working_dir_str)) + .unwrap(); + + let mut filesystem = Filesystem::new(None); + filesystem.__set_mock(FilesystemMock { + remove_directory_async_result: Some(true), + ..Default::default() + }); + let filesystem = std::rc::Rc::new(std::cell::RefCell::new(filesystem)); + + let downloader = get_downloader_mock(None, None, process, Some(filesystem.clone())); + run(async { + downloader + .prepare("uninstall", package.clone(), &working_dir_str, None) + .await + .unwrap(); + downloader + .remove(package.clone(), &working_dir_str) + .await + .unwrap(); + downloader + .cleanup("uninstall", package, &working_dir_str, None) + .await + .unwrap(); + }); + + assert_eq!( + filesystem.borrow().__remove_directory_async_paths(), + vec![working_dir_str] + ); } #[serial] |
