diff options
Diffstat (limited to 'crates/shirabe/src/installer')
| -rw-r--r-- | crates/shirabe/src/installer/binary_installer.rs | 28 | ||||
| -rw-r--r-- | crates/shirabe/src/installer/library_installer.rs | 52 |
2 files changed, 59 insertions, 21 deletions
diff --git a/crates/shirabe/src/installer/binary_installer.rs b/crates/shirabe/src/installer/binary_installer.rs index a76f69e..ea73f6f 100644 --- a/crates/shirabe/src/installer/binary_installer.rs +++ b/crates/shirabe/src/installer/binary_installer.rs @@ -17,6 +17,34 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use crate::util::Silencer; +/// Seam over the BinaryInstaller methods reached through LibraryInstaller, so tests can inject a +/// recording double. Composer has no PHP `BinaryInstallerInterface`; this exists only to allow the +/// `LibraryInstaller` collaborator to be mocked the way PHPUnit mocks the concrete class. +pub trait BinaryInstallerInterface: std::fmt::Debug { + fn install_binaries( + &mut self, + package: PackageInterfaceHandle, + install_path: &str, + warn_on_overwrite: bool, + ); + fn remove_binaries(&mut self, package: PackageInterfaceHandle); +} + +impl BinaryInstallerInterface for BinaryInstaller { + fn install_binaries( + &mut self, + package: PackageInterfaceHandle, + install_path: &str, + warn_on_overwrite: bool, + ) { + BinaryInstaller::install_binaries(self, package, install_path, warn_on_overwrite); + } + + fn remove_binaries(&mut self, package: PackageInterfaceHandle) { + BinaryInstaller::remove_binaries(self, package); + } +} + /// Utility to handle installation of package "bin"/binaries #[derive(Debug)] pub struct BinaryInstaller { diff --git a/crates/shirabe/src/installer/library_installer.rs b/crates/shirabe/src/installer/library_installer.rs index e1c3aec..96f38c7 100644 --- a/crates/shirabe/src/installer/library_installer.rs +++ b/crates/shirabe/src/installer/library_installer.rs @@ -10,6 +10,7 @@ use shirabe_php_shim::{ use crate::composer::PartialComposerWeakHandle; use crate::downloader::DownloadManagerInterface; use crate::installer::BinaryInstaller; +use crate::installer::BinaryInstallerInterface; use crate::installer::BinaryPresenceInterface; use crate::installer::InstallerInterface; use crate::io::IOInterface; @@ -29,7 +30,7 @@ pub struct LibraryInstaller { pub(crate) io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>, pub(crate) r#type: Option<String>, pub(crate) filesystem: std::rc::Rc<std::cell::RefCell<Filesystem>>, - pub(crate) binary_installer: BinaryInstaller, + pub(crate) binary_installer: Box<dyn BinaryInstallerInterface>, } impl LibraryInstaller { @@ -61,28 +62,31 @@ impl LibraryInstaller { .unwrap_or_default(), Some("/"), ); - let binary_installer = binary_installer.unwrap_or_else(|| { - let bin_dir = rtrim( - &composer_ref + let binary_installer: Box<dyn BinaryInstallerInterface> = match binary_installer { + Some(binary_installer) => Box::new(binary_installer), + None => { + let bin_dir = rtrim( + &composer_ref + .get_config() + .borrow_mut() + .get_str("bin-dir") + .unwrap_or_default(), + Some("/"), + ); + let bin_compat = composer_ref .get_config() .borrow_mut() - .get_str("bin-dir") - .unwrap_or_default(), - Some("/"), - ); - let bin_compat = composer_ref - .get_config() - .borrow_mut() - .get_str("bin-compat") - .unwrap_or_default(); - BinaryInstaller::new( - io.clone(), - bin_dir, - bin_compat, - Some(filesystem.clone()), - Some(vendor_dir.clone()), - ) - }); + .get_str("bin-compat") + .unwrap_or_default(); + Box::new(BinaryInstaller::new( + io.clone(), + bin_dir, + bin_compat, + Some(filesystem.clone()), + Some(vendor_dir.clone()), + )) + } + }; Self { composer, @@ -95,6 +99,12 @@ impl LibraryInstaller { } } + /// For testing only: swap the binary installer for a recording double, mirroring the + /// constructor injection PHPUnit performs with a mocked BinaryInstaller. + pub fn __set_binary_installer(&mut self, binary_installer: Box<dyn BinaryInstallerInterface>) { + self.binary_installer = binary_installer; + } + /// Make sure binaries are installed for a given package. pub fn ensure_binaries_presence(&mut self, package: PackageInterfaceHandle) { let install_path = self.get_install_path(package.clone()).unwrap(); |
