//! ref: composer/tests/Composer/Test/Repository/ArtifactRepositoryTest.php use indexmap::IndexMap; use shirabe::io::{IOInterface, NullIO}; use shirabe::repository::ArtifactRepository; use shirabe_php_shim::{PhpMixed, extension_loaded}; use std::cell::RefCell; use std::rc::Rc; /// Returns true when the test should be skipped because the zip extension is /// unavailable, mirroring PHP's markTestSkipped in setUp. fn set_up() -> bool { if !extension_loaded("zip") { // markTestSkipped('You need the zip extension to run this test.') return true; } false } fn artifacts_dir() -> String { format!( "{}/../../composer/tests/Composer/Test/Repository/Fixtures/artifacts", env!("CARGO_MANIFEST_DIR") ) } fn create_repo(url: &str) -> ArtifactRepository { let mut coordinates: IndexMap = IndexMap::new(); coordinates.insert("type".to_string(), PhpMixed::String("artifact".to_string())); coordinates.insert("url".to_string(), PhpMixed::String(url.to_string())); let io: Rc> = Rc::new(RefCell::new(NullIO::new())); ArtifactRepository::new(coordinates, io).unwrap() } #[test] #[ignore = "the artifacts fixtures dir contains a .tar file (jsonInRootTarFile); scanning it routes through Tar::get_composer_json -> PharData::new which is todo!()"] fn test_extracts_configs_from_zip_archives() { if set_up() { return; } let mut expected_packages = vec![ "vendor0/package0-0.0.1".to_string(), "composer/composer-1.0.0-alpha6".to_string(), "vendor1/package2-4.3.2".to_string(), "vendor3/package1-5.4.3".to_string(), "test/jsonInRoot-1.0.0".to_string(), "test/jsonInRootTarFile-1.0.0".to_string(), "test/jsonInFirstLevel-1.0.0".to_string(), // The files not-an-artifact.zip and jsonSecondLevel are not valid // artifacts and do not get detected. ]; let mut repo = create_repo(&artifacts_dir()); let mut found_packages: Vec = repo .__get_packages() .unwrap() .iter() .map(|package| { format!( "{}-{}", package.get_pretty_name(), package.get_pretty_version() ) }) .collect(); expected_packages.sort(); found_packages.sort(); assert_eq!(expected_packages, found_packages); let tar_package: Vec<_> = repo .__get_packages() .unwrap() .into_iter() .filter(|package| package.get_pretty_name() == "test/jsonInRootTarFile") .collect(); assert_eq!(1, tar_package.len()); let tar_package = tar_package.into_iter().next_back().unwrap(); assert_eq!(Some("tar".to_string()), tar_package.get_dist_type()); } #[test] #[ignore = "the artifacts fixtures dir contains a .tar file (jsonInRootTarFile); scanning it routes through Tar::get_composer_json -> PharData::new which is todo!()"] fn test_absolute_repo_url_creates_absolute_url_packages() { if set_up() { return; } let absolute_path = artifacts_dir(); let mut repo = create_repo(&absolute_path); for package in repo.__get_packages().unwrap() { assert_eq!( package .get_dist_url() .unwrap_or_default() .find(&absolute_path.replace('\\', "/")), Some(0) ); } } #[test] #[ignore = "the relative url is resolved from the process cwd (the crate manifest dir under cargo, not the composer test root), so the artifacts dir is not found; additionally the dir contains a .tar file routing through PharData::new which is todo!()"] fn test_relative_repo_url_creates_relative_url_packages() { if set_up() { return; } let relative_path = "tests/Composer/Test/Repository/Fixtures/artifacts"; let mut repo = create_repo(relative_path); for package in repo.__get_packages().unwrap() { assert_eq!( package .get_dist_url() .unwrap_or_default() .find(relative_path), Some(0) ); } }