From 9639ded890f3a3f1312ae29f744ff175ba3c0a88 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Wed, 7 Aug 2024 14:39:00 +0200 Subject: [PATCH 1/3] fix(cli/WasmerEnv): strip `registry.` only if hostname starts with it --- lib/cli/src/config/env.rs | 11 +++++------ lib/registry/src/wasmer_env.rs | 10 ++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index 0048627d1b5..572913c2cb4 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -56,12 +56,11 @@ impl WasmerEnv { let mut url = self.registry_endpoint()?; url.set_path(""); - let domain = url - .host_str() - .context("url has no host")? - .strip_prefix("registry.") - .context("could not derive registry public url")? - .to_string(); + let mut domain = url.host_str().context("url has no host")?.to_string(); + if domain.starts_with("registry.") { + domain = domain.strip_prefix("registry.").unwrap().to_string(); + } + url.set_host(Some(&domain)) .context("could not derive registry public url")?; diff --git a/lib/registry/src/wasmer_env.rs b/lib/registry/src/wasmer_env.rs index 5ba71834693..dbcc267a717 100644 --- a/lib/registry/src/wasmer_env.rs +++ b/lib/registry/src/wasmer_env.rs @@ -46,12 +46,10 @@ impl WasmerEnv { let mut url = self.registry_endpoint()?; url.set_path(""); - let domain = url - .host_str() - .context("url has no host")? - .strip_prefix("registry.") - .context("could not derive registry public url")? - .to_string(); + let mut domain = url.host_str().context("url has no host")?.to_string(); + if domain.starts_with("registry.") { + domain = domain.strip_prefix("registry.").unwrap().to_string(); + } url.set_host(Some(&domain)) .context("could not derive registry public url")?; From 919224fcadb44af564318a52877a9b8432f384d3 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Wed, 7 Aug 2024 15:13:28 +0200 Subject: [PATCH 2/3] fix(cli): Add tests and cleanup spurious warnings --- lib/cli/src/commands/auth/login/mod.rs | 1 + lib/cli/src/commands/package/download.rs | 2 -- lib/cli/src/config/env.rs | 32 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/cli/src/commands/auth/login/mod.rs b/lib/cli/src/commands/auth/login/mod.rs index 0c3a2bf8dee..9a2da3cc5da 100644 --- a/lib/cli/src/commands/auth/login/mod.rs +++ b/lib/cli/src/commands/auth/login/mod.rs @@ -181,6 +181,7 @@ impl Login { } #[cfg(test)] { + _ = user; false } } else { diff --git a/lib/cli/src/commands/package/download.rs b/lib/cli/src/commands/package/download.rs index 8a9a7a48ea9..c4a79e24611 100644 --- a/lib/cli/src/commands/package/download.rs +++ b/lib/cli/src/commands/package/download.rs @@ -279,8 +279,6 @@ impl PackageDownload { #[cfg(test)] mod tests { - use crate::config::UserRegistry; - use super::*; /// Download a package from the dev registry. diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index 572913c2cb4..8cf1f801c28 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -208,6 +208,10 @@ mod tests { [[registry.tokens]] registry = "https://registry.wasmer.io/graphql" token = "prod-token" + + [[registry.tokens]] + registry = "http://localhost:11/graphql" + token = "invalid" "#; #[test] @@ -309,4 +313,32 @@ mod tests { assert_eq!(env.token().unwrap(), "prod-token"); assert_eq!(env.cache_dir(), expected_cache_dir); } + + #[test] + fn registries_have_public_url() { + let temp = TempDir::new().unwrap(); + std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap(); + + let inputs = [ + ("https://wasmer.io/", "https://registry.wasmer.io/graphql"), + ("https://wasmer.wtf/", "https://registry.wasmer.wtf/graphql"), + ("https://wasmer.wtf/", "https://registry.wasmer.wtf/graphql"), + ("https://wasmer.wtf/", "https://registry.wasmer.wtf/something/else"), + ("https://wasmer.wtf/", "https://wasmer.wtf/graphql"), + ("https://wasmer.wtf/", "https://wasmer.wtf/graphql"), + ("http://localhost:8000/", "http://localhost:8000/graphql"), + ("http://localhost:8000/", "http://localhost:8000/graphql"), + ]; + + for (want, input) in inputs { + let env = WasmerEnv { + wasmer_dir: temp.path().to_path_buf(), + registry: Some(UserRegistry::from(input)), + cache_dir: temp.path().join("cache").to_path_buf(), + token: None, + }; + + assert_eq!(want, &env.registry_public_url().unwrap().to_string()) + } + } } From 923998d2348c22cbee821e3e227d2b99fe9a49a5 Mon Sep 17 00:00:00 2001 From: Edoardo Marangoni Date: Wed, 7 Aug 2024 16:15:06 +0200 Subject: [PATCH 3/3] chore: Make linter happy --- lib/cli/src/config/env.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/config/env.rs b/lib/cli/src/config/env.rs index 8cf1f801c28..0a3fbf9d1fc 100644 --- a/lib/cli/src/config/env.rs +++ b/lib/cli/src/config/env.rs @@ -323,7 +323,10 @@ mod tests { ("https://wasmer.io/", "https://registry.wasmer.io/graphql"), ("https://wasmer.wtf/", "https://registry.wasmer.wtf/graphql"), ("https://wasmer.wtf/", "https://registry.wasmer.wtf/graphql"), - ("https://wasmer.wtf/", "https://registry.wasmer.wtf/something/else"), + ( + "https://wasmer.wtf/", + "https://registry.wasmer.wtf/something/else", + ), ("https://wasmer.wtf/", "https://wasmer.wtf/graphql"), ("https://wasmer.wtf/", "https://wasmer.wtf/graphql"), ("http://localhost:8000/", "http://localhost:8000/graphql"),