From 63fe99a6ff1c8fb00bc4a84cd85bc4675d914534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 17 Nov 2022 10:48:26 +0100 Subject: [PATCH] Fix wasmer-registry cargo test --- lib/registry/src/config.rs | 1 - lib/registry/src/lib.rs | 57 +++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/registry/src/config.rs b/lib/registry/src/config.rs index 63ae4ac3dff..ac2c3e2f051 100644 --- a/lib/registry/src/config.rs +++ b/lib/registry/src/config.rs @@ -276,7 +276,6 @@ impl PartialWapmConfig { #[cfg(test)] pub fn get_folder(test_name: &str) -> Result { - let test_name = std::env::var("WASMER_REGISTRY_TEST_NAME").unwrap(); let test_dir = std::env::temp_dir().join("test_wasmer").join(test_name); let _ = std::fs::create_dir_all(&test_dir); Ok(test_dir.to_path_buf()) diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 281a5f74562..b50c0a03f86 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -53,7 +53,7 @@ pub fn get_package_local_dir( pub fn try_finding_local_command(#[cfg(test)] test_name: &str, cmd: &str) -> Option { #[cfg(test)] - let local_packages = get_all_local_packages(None, test_name); + let local_packages = get_all_local_packages(test_name, None); #[cfg(not(test))] let local_packages = get_all_local_packages(None); for p in local_packages { @@ -179,13 +179,22 @@ fn get_all_names_in_dir(dir: &PathBuf) -> Vec<(PathBuf, String)> { /// Returns a list of all locally installed packages pub fn get_all_local_packages( - registry: Option<&str>, #[cfg(test)] test_name: &str, + registry: Option<&str>, ) -> Vec { let mut packages = Vec::new(); let registries = match registry { Some(s) => vec![s.to_string()], - None => get_all_available_registries().unwrap_or_default(), + None => { + #[cfg(test)] + { + get_all_available_registries(test_name).unwrap_or_default() + } + #[cfg(not(test))] + { + get_all_available_registries().unwrap_or_default() + } + } }; let mut registry_hosts = registries @@ -249,7 +258,7 @@ pub fn get_local_package( #[cfg(not(test))] let local_packages = get_all_local_packages(registry); #[cfg(test)] - let local_packages = get_all_local_packages(registry, test_name); + let local_packages = get_all_local_packages(test_name, registry); local_packages .iter() @@ -350,15 +359,17 @@ pub enum GetIfPackageHasNewVersionResult { #[test] fn test_get_if_package_has_new_version() { + const TEST_NAME: &str = "test_get_if_package_has_new_version"; let fake_registry = "https://h0.com"; let fake_name = "namespace0/project1"; let fake_version = "1.0.0"; - let package_path = get_package_local_dir("h0.com", fake_name, fake_version).unwrap(); + let package_path = get_package_local_dir(TEST_NAME, "h0.com", fake_name, fake_version).unwrap(); let _ = std::fs::remove_file(&package_path.join("wapm.toml")); let _ = std::fs::remove_file(&package_path.join("wapm.toml")); let r1 = get_if_package_has_new_version( + TEST_NAME, fake_registry, "namespace0/project1", Some(fake_version.to_string()), @@ -375,11 +386,12 @@ fn test_get_if_package_has_new_version() { } ); - let package_path = get_package_local_dir("h0.com", fake_name, fake_version).unwrap(); + let package_path = get_package_local_dir(TEST_NAME, "h0.com", fake_name, fake_version).unwrap(); std::fs::create_dir_all(&package_path).unwrap(); std::fs::write(&package_path.join("wapm.toml"), b"").unwrap(); let r1 = get_if_package_has_new_version( + TEST_NAME, fake_registry, "namespace0/project1", Some(fake_version.to_string()), @@ -723,7 +735,16 @@ pub fn install_package( None => { let registries = match registry { Some(s) => vec![s.to_string()], - None => get_all_available_registries()?, + None => { + #[cfg(test)] + { + get_all_available_registries(test_name)? + } + #[cfg(not(test))] + { + get_all_available_registries()? + } + } }; let mut url_of_package = None; @@ -854,6 +875,7 @@ pub fn test_if_registry_present(registry: &str) -> Result { pub fn get_all_available_registries(#[cfg(test)] test_name: &str) -> Result, String> { #[cfg(test)] let config = PartialWapmConfig::from_file(test_name)?; + #[cfg(not(test))] let config = PartialWapmConfig::from_file()?; let mut registries = Vec::new(); @@ -875,6 +897,8 @@ pub fn get_all_available_registries(#[cfg(test)] test_name: &str) -> Result>()