Skip to content

Commit

Permalink
feat: add name() to channel (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts authored Jan 25, 2024
1 parent b002f38 commit 0128bce
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/rattler_conda_types/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ impl Channel {
}
}

/// Returns the name of the channel
pub fn name(&self) -> &str {
match self.base_url().scheme() {
// The name of the channel is only defined for http and https channels.
// If the name is not defined we return the base url.
"https" | "http" => self
.name
.as_deref()
.unwrap_or_else(|| self.base_url.as_str()),
_ => self.base_url.as_str(),
}
}

/// Returns the base Url of the channel. This does not include the platform part.
pub fn base_url(&self) -> &Url {
&self.base_url
Expand Down Expand Up @@ -442,6 +455,7 @@ mod tests {
Url::from_str("https://conda.anaconda.org/conda-forge/").unwrap()
);
assert_eq!(channel.name.as_deref(), Some("conda-forge"));
assert_eq!(channel.name(), "conda-forge");
assert_eq!(channel.platforms, None);

assert_eq!(channel, Channel::from_name("conda-forge/", None, &config));
Expand All @@ -458,6 +472,7 @@ mod tests {
Url::from_str("https://conda.anaconda.org/conda-forge/").unwrap()
);
assert_eq!(channel.name.as_deref(), Some("conda-forge"));
assert_eq!(channel.name(), "conda-forge");
assert_eq!(channel.platforms, None);
assert_eq!(
channel.base_url().to_string(),
Expand All @@ -471,6 +486,7 @@ mod tests {

let channel = Channel::from_str("file:///var/channels/conda-forge", &config).unwrap();
assert_eq!(channel.name.as_deref(), Some("conda-forge"));
assert_eq!(channel.name(), "file:///var/channels/conda-forge/");
assert_eq!(
channel.base_url,
Url::from_str("file:///var/channels/conda-forge/").unwrap()
Expand All @@ -484,6 +500,12 @@ mod tests {
let current_dir = std::env::current_dir().expect("no current dir?");
let channel = Channel::from_str("./dir/does/not_exist", &config).unwrap();
assert_eq!(channel.name.as_deref(), Some("./dir/does/not_exist"));
assert_eq!(
channel.name(),
Url::from_directory_path(absolute_path(Path::new("./dir/does/not_exist")))
.unwrap()
.as_str()
);
assert_eq!(channel.platforms, None);
assert_eq!(
channel.base_url().to_file_path().unwrap(),
Expand All @@ -502,6 +524,7 @@ mod tests {
);
assert_eq!(channel.name, None);
assert_eq!(channel.platforms, None);
assert_eq!(channel.name(), "http://localhost:1234/");

let noarch_url = channel.platform_url(Platform::NoArch);
assert_eq!(noarch_url.to_string(), "http://localhost:1234/noarch/");
Expand Down

0 comments on commit 0128bce

Please sign in to comment.