Skip to content

Commit

Permalink
Merge pull request #4992 from wasmerio/fix-wasmerenv-url
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo authored Aug 7, 2024
2 parents 3fcd703 + 923998d commit efbbcad
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/cli/src/commands/auth/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Login {
}
#[cfg(test)]
{
_ = user;
false
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ impl PackageDownload {

#[cfg(test)]
mod tests {
use crate::config::UserRegistry;

use super::*;

/// Download a package from the dev registry.
Expand Down
46 changes: 40 additions & 6 deletions lib/cli/src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;

Expand Down Expand Up @@ -209,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]
Expand Down Expand Up @@ -310,4 +313,35 @@ 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())
}
}
}
10 changes: 4 additions & 6 deletions lib/registry/src/wasmer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;

Expand Down

0 comments on commit efbbcad

Please sign in to comment.