diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-07-01 02:51:38 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-07-01 02:51:38 +0900 |
| commit | 925b2ee998a87dc20e1eced18040ea649bfed507 (patch) | |
| tree | 3e521d5b21cb8c33880a9afcf9796022428a09ec | |
| parent | 3cf319574f4fd16ded47cb608e428dd9f4c7dabc (diff) | |
| download | php-shirabe-925b2ee998a87dc20e1eced18040ea649bfed507.tar.gz php-shirabe-925b2ee998a87dc20e1eced18040ea649bfed507.tar.zst php-shirabe-925b2ee998a87dc20e1eced18040ea649bfed507.zip | |
test(archive-manager): port testArchiveTar and testArchiveCustomFileName
Wire a git downloader into the test's DownloadManager (mirroring
Factory::createDownloadManager) so the archive path clones the package
source as PHP does. Both remain #[ignore]'d on PharData tar archiving
(new_with_format/build_from_iterator are todo!() in the php-shim).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
| -rw-r--r-- | crates/shirabe/tests/package/archiver/archive_manager_test.rs | 156 |
1 files changed, 148 insertions, 8 deletions
diff --git a/crates/shirabe/tests/package/archiver/archive_manager_test.rs b/crates/shirabe/tests/package/archiver/archive_manager_test.rs index 68ee994..b275692 100644 --- a/crates/shirabe/tests/package/archiver/archive_manager_test.rs +++ b/crates/shirabe/tests/package/archiver/archive_manager_test.rs @@ -2,15 +2,20 @@ use indexmap::IndexMap; use shirabe::config::Config; -use shirabe::downloader::DownloadManager; +use shirabe::downloader::{DownloadManager, GitDownloader}; use shirabe::io::IOInterface; use shirabe::io::null_io::NullIO; use shirabe::package::CompletePackageInterfaceHandle; use shirabe::package::archiver::{ArchiveManager, PharArchiver, ZipArchiver}; use shirabe::package::handle::CompletePackageHandle; +use shirabe::util::Filesystem; +use shirabe::util::ProcessExecutor; use shirabe::util::http_downloader::HttpDownloader; use shirabe::util::r#loop::Loop; -use shirabe_php_shim::realpath; +use shirabe_external_packages::symfony::process::Process; +use shirabe_php_shim::{ + PhpMixed, file_exists, file_put_contents, realpath, sys_get_temp_dir, unlink, +}; use std::cell::RefCell; use std::rc::Rc; use tempfile::TempDir; @@ -35,8 +40,25 @@ impl TestCase { let config = Rc::new(RefCell::new(Config::new(false, None))); // The filename/unknown-format tests never drive the download path, so a mock // HttpDownloader (no curl backend) is sufficient to satisfy Loop's dependency. - let http_downloader = Rc::new(RefCell::new(HttpDownloader::__new_mock(io.clone(), config))); - let dm = Rc::new(RefCell::new(DownloadManager::new(io.clone(), false, None))); + let http_downloader = Rc::new(RefCell::new(HttpDownloader::__new_mock( + io.clone(), + config.clone(), + ))); + let process = Rc::new(RefCell::new(ProcessExecutor::new(Some(io.clone())))); + let fs = Rc::new(RefCell::new(Filesystem::new(Some(process.clone())))); + let mut dm = DownloadManager::new(io.clone(), false, Some(fs.clone())); + // Factory::createDownloadManager registers a git downloader; the archive tests clone the + // package source (source type 'git') through it. + dm.set_downloader( + "git", + Rc::new(RefCell::new(GitDownloader::new( + io.clone(), + config.clone(), + Some(process.clone()), + Some(fs.clone()), + ))), + ); + let dm = Rc::new(RefCell::new(dm)); let r#loop = Rc::new(RefCell::new(Loop::new(http_downloader, None))); let mut manager = ArchiveManager::new(dm, r#loop); @@ -66,6 +88,59 @@ impl TestCase { package.into() } + + // ref: ArchiveManagerTest::getTargetName. + fn get_target_name(&self, package: CompletePackageInterfaceHandle, format: &str) -> String { + let package_name = self.manager.get_package_filename(package).unwrap(); + format!("{}/{}.{}", self.target_dir, package_name, format) + } + + // ref: ArchiveManagerTest::setupGitRepo. + // + // PHP runs the git commands individually through ProcessExecutor; the chained form here is + // effect-equivalent for setting up the local repository the archive path clones from. + fn setup_git_repo(&self) { + file_put_contents( + &format!("{}/composer.json", self.test_dir), + br#"{"name":"faker/faker", "description": "description", "license": "MIT"}"#, + ); + + let mut process = Process::from_shell_commandline( + "git init -q && \ + git checkout -b master && \ + git config user.email \"you@example.com\" && \ + git config commit.gpgsign false && \ + git config user.name \"Your Name\" && \ + git add composer.json && \ + git commit -m \"commit composer.json\" -q", + Some(&self.test_dir), + None, + PhpMixed::Bool(false), + Some(60.0), + ) + .unwrap(); + let result = process.run(None, IndexMap::new()).unwrap(); + if result > 0 { + panic!( + "Could not set up git repo: {}", + process.get_error_output().unwrap_or_default() + ); + } + } +} + +// ref: ArchiverTestCase::skipIfNotExecutable('git'). +fn git_is_executable() -> bool { + Process::from_shell_commandline( + "git --version", + None, + None, + PhpMixed::Bool(false), + Some(60.0), + ) + .and_then(|mut p| p.run(None, IndexMap::new())) + .map(|code| code == 0) + .unwrap_or(false) } #[test] @@ -94,15 +169,80 @@ fn test_unknown_format() { // PharArchiver::archive. That builds the archive via PharData, whose build_from_iterator is // todo!() in the php-shim, so the archiving path cannot run yet. #[test] -#[ignore = "needs PharData tar archiving (build_from_iterator is todo!() in the php-shim) for ArchiveManager::archive('tar', ...)"] +#[ignore = "needs PharData tar archiving (new_with_format/build_from_iterator are todo!() in the php-shim) for ArchiveManager::archive('tar', ...)"] fn test_archive_tar() { - todo!() + if !git_is_executable() { + return; + } + + let mut test_case = TestCase::set_up(); + + test_case.setup_git_repo(); + + let package = test_case.setup_package(); + + test_case + .manager + .archive( + package.clone(), + "tar".to_string(), + test_case.target_dir.clone(), + None, + false, + ) + .unwrap(); + + let target = test_case.get_target_name(package.clone(), "tar"); + assert!(file_exists(&target)); + + let tmppath = format!( + "{}/composer_archiver/{}", + sys_get_temp_dir(), + test_case.manager.get_package_filename(package).unwrap() + ); + assert!(!file_exists(&tmppath)); + + unlink(&target); } #[test] -#[ignore = "needs PharData tar archiving (build_from_iterator is todo!() in the php-shim) for ArchiveManager::archive('tar', ...)"] +#[ignore = "needs PharData tar archiving (new_with_format/build_from_iterator are todo!() in the php-shim) for ArchiveManager::archive('tar', ...)"] fn test_archive_custom_file_name() { - todo!() + if !git_is_executable() { + return; + } + + let mut test_case = TestCase::set_up(); + + test_case.setup_git_repo(); + + let package = test_case.setup_package(); + + let file_name = "testArchiveName"; + + test_case + .manager + .archive( + package.clone(), + "tar".to_string(), + test_case.target_dir.clone(), + Some(file_name.to_string()), + false, + ) + .unwrap(); + + let target = format!("{}/{}.tar", test_case.target_dir, file_name); + + assert!(file_exists(&target)); + + let tmppath = format!( + "{}/composer_archiver/{}", + sys_get_temp_dir(), + test_case.manager.get_package_filename(package).unwrap() + ); + assert!(!file_exists(&tmppath)); + + unlink(&target); } #[test] |
