Skip to content

Commit

Permalink
fix: return url when config is default and fix scope lookup (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Apr 25, 2024
1 parent 6ecb552 commit 3096665
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions src/npm_rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ use self::ini::Value;

mod ini;

static EMPTY_REGISTRY_CONFIG: RegistryConfig = RegistryConfig {
auth: None,
auth_token: None,
username: None,
password: None,
email: None,
certfile: None,
keyfile: None,
};

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct RegistryConfig {
pub auth: Option<String>,
Expand Down Expand Up @@ -124,7 +134,9 @@ impl NpmRc {
.split_once("//")
.map(|(_, right)| right)?;
let start_url = match maybe_scope_name {
Some(scope_name) => Cow::Owned(format!("{}{}", registry_url, scope_name)),
Some(scope_name) => {
Cow::Owned(format!("{}{}/", registry_url, scope_name))
}
None => Cow::Borrowed(registry_url),
};
// Loop through all the paths in the url to find a match. Example:
Expand All @@ -136,7 +148,15 @@ impl NpmRc {
if let Some(config) = self.registry_configs.get(url) {
return Some((original_registry_url.into_owned(), config));
}
let next_slash_index = url[..url.len() - 1].rfind('/')?;
let Some(next_slash_index) = url[..url.len() - 1].rfind('/') else {
if original_registry_url == env_registry_url {
return None;
}
return Some((
original_registry_url.into_owned(),
&EMPTY_REGISTRY_CONFIG,
));
};
url = &url[..next_slash_index + 1];
}
}
Expand Down Expand Up @@ -375,4 +395,58 @@ registry=${VAR_FOUND}
"test${VA{R}test"
);
}

#[test]
fn test_scope_registry_url_only() {
let npm_rc = NpmRc::parse(
r#"
@example:registry=https://example.com/
"#,
&|_| None,
)
.unwrap();
{
let (registry_url, config) = npm_rc
.registry_url_and_config_for_package(
"@example/test",
"https://deno.land/npm/",
)
.unwrap();
assert_eq!(registry_url, "https://example.com/");
assert_eq!(config, &RegistryConfig::default());
}
{
assert!(npm_rc
.registry_url_and_config_for_package("test", "https://deno.land/npm/")
.is_none());
}
}

#[test]
fn test_scope_with_auth() {
let npm_rc = NpmRc::parse(
r#"
@example:registry=https://example.com/
//example.com/example/:_authToken=MY_AUTH_TOKEN
"#,
&|_| None,
)
.unwrap();
{
let (registry_url, config) = npm_rc
.registry_url_and_config_for_package(
"@example/test",
"https://deno.land/npm/",
)
.unwrap();
assert_eq!(registry_url, "https://example.com/");
assert_eq!(
config,
&RegistryConfig {
auth_token: Some("MY_AUTH_TOKEN".to_string()),
..Default::default()
}
);
}
}
}

0 comments on commit 3096665

Please sign in to comment.