Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with reconstructing channel and filename from local channel #231

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions crates/rattler_conda_types/src/conda_lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,28 +314,22 @@ pub enum ConversionError {
}

/// Package filename from the url
fn filename_from_url(url: &Url) -> Option<String> {
let path = url.path();
let filename = path.split('/').last()?;
Some(filename.to_string())
fn file_name_from_url(url: &Url) -> Option<&str> {
let path = url.path_segments()?;
path.last()
}

/// Channel from url, this is everything before the filename and the subdir
/// So for example: https://conda.anaconda.org/conda-forge/ is a channel name
/// that we parse from something like: https://conda.anaconda.org/conda-forge/osx-64/python-3.11.0-h4150a38_1_cpython.conda
fn channel_from_url(url: &Url) -> Option<String> {
// Get http or https from the url
let scheme = url.scheme();
// Retrieve the host from the url
let host = url.host()?;
let path: Vec<_> = url.path_segments()?.collect();
// We expect it to be in the format <channel>/<subdir>/<filename>
if path.len() < 3 {
return None;
}
let url = path.first()?;
// Reconstruct the url
Some(format!("{}://{}/{}", scheme, host, url))
fn channel_from_url(url: &Url) -> Option<Url> {
let mut result = url.clone();

// Strip the last two path segments. We assume the first one contains the file_name, and the
// other the subdirectory.
result.path_segments_mut().ok()?.pop().pop();

Some(result)
}

impl TryFrom<&LockedDependency> for RepoDataRecord {
Expand Down Expand Up @@ -368,9 +362,11 @@ impl TryFrom<LockedDependency> for RepoDataRecord {
_ => None,
};
let channel = channel_from_url(&value.url)
.ok_or_else(|| ConversionError::Missing("channel in url".to_string()))?;
let file_name = filename_from_url(&value.url)
.ok_or_else(|| ConversionError::Missing("channel in url".to_string()))?;
.ok_or_else(|| ConversionError::Missing("channel in url".to_string()))?
.to_string();
let file_name = file_name_from_url(&value.url)
.ok_or_else(|| ConversionError::Missing("filename in url".to_string()))?
.to_owned();
let build = value
.build
.ok_or_else(|| ConversionError::Missing("build".to_string()))?;
Expand Down Expand Up @@ -446,12 +442,12 @@ impl From<&str> for Channel {

#[cfg(test)]
mod test {
use super::PackageHashes;
use crate::conda_lock::CondaLock;
use super::{channel_from_url, file_name_from_url, CondaLock, PackageHashes};
use crate::Platform;
use insta::assert_yaml_snapshot;
use serde_yaml::from_str;
use std::path::Path;
use url::Url;

#[test]
fn test_package_hashes() {
Expand Down Expand Up @@ -538,4 +534,32 @@ mod test {
);
})
}

#[test]
fn test_channel_from_url() {
assert_eq!(channel_from_url(&Url::parse("https://conda.anaconda.org/conda-forge/osx-64/python-3.11.0-h4150a38_1_cpython.conda").unwrap()), Some(Url::parse("https://conda.anaconda.org/conda-forge").unwrap()));
assert_eq!(
channel_from_url(
&Url::parse(
"file:///C:/Users/someone/AppData/Local/Temp/.tmpsasJ7b/noarch/foo-1-0.conda"
)
.unwrap()
),
Some(Url::parse("file:///C:/Users/someone/AppData/Local/Temp/.tmpsasJ7b").unwrap())
);
}

#[test]
fn test_file_name_from_url() {
assert_eq!(file_name_from_url(&Url::parse("https://conda.anaconda.org/conda-forge/osx-64/python-3.11.0-h4150a38_1_cpython.conda").unwrap()), Some("python-3.11.0-h4150a38_1_cpython.conda"));
assert_eq!(
file_name_from_url(
&Url::parse(
"file:///C:/Users/someone/AppData/Local/Temp/.tmpsasJ7b/noarch/foo-1-0.conda"
)
.unwrap()
),
Some("foo-1-0.conda")
);
}
}