Skip to content

Commit

Permalink
Use Metadata10 to parse PKG-INFO of legacy editable
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed May 8, 2024
1 parent bd7860d commit 60098f5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions crates/distribution-types/src/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl InstalledDist {
return Ok(None);
}
};
let metadata = match pypi_types::Metadata23::parse_pkg_info(&content) {
let metadata = match pypi_types::Metadata10::parse_pkg_info(&content) {
Ok(metadata) => metadata,
Err(err) => {
warn!("Failed to parse metadata for {path:?}: {err}");
Expand All @@ -178,7 +178,7 @@ impl InstalledDist {

return Ok(Some(Self::LegacyEditable(InstalledLegacyEditable {
name: metadata.name,
version: metadata.version,
version: Version::from_str(&metadata.version)?,
egg_link: path.to_path_buf(),
target,
target_url: url,
Expand Down
6 changes: 5 additions & 1 deletion crates/pypi-types/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ pub(crate) struct Project {
#[serde(rename_all = "kebab-case")]
pub struct Metadata10 {
pub name: PackageName,
pub version: String,
}

impl Metadata10 {
Expand All @@ -296,7 +297,10 @@ impl Metadata10 {
.get_first_value("Name")
.ok_or(MetadataError::FieldNotFound("Name"))?,
)?;
Ok(Self { name })
let version = headers
.get_first_value("Version")
.ok_or(MetadataError::FieldNotFound("Version"))?;
Ok(Self { name, version })
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/pip_freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ fn freeze_with_legacy_editable() -> Result<()> {
.child("zstandard.egg-info")
.child("PKG-INFO")
.write_str(
"Metadata-Version: 2.2
"Metadata-Version: 2.1
Name: zstandard
Version: 0.22.0
",
Expand Down
51 changes: 50 additions & 1 deletion crates/uv/tests/pip_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ fn list_legacy_editable() -> Result<()> {
.child("zstandard.egg-info")
.child("PKG-INFO")
.write_str(
"Metadata-Version: 2.2
"Metadata-Version: 2.1
Name: zstandard
Version: 0.22.0
",
Expand Down Expand Up @@ -626,3 +626,52 @@ Version: 0.22.0

Ok(())
}

#[test]
fn list_legacy_editable_invalid_version() -> Result<()> {
let context = TestContext::new("3.12");

let site_packages = ChildPath::new(context.site_packages());

let target = context.temp_dir.child("paramiko_project");
target.child("paramiko.egg-info").create_dir_all()?;
target
.child("paramiko.egg-info")
.child("PKG-INFO")
.write_str(
"Metadata-Version: 1.0
Name: paramiko
Version: 0.1-bulbasaur
",
)?;
site_packages
.child("paramiko.egg-link")
.write_str(target.path().to_str().unwrap())?;

let filters = context
.filters()
.into_iter()
.chain(vec![(r"\-\-\-\-\-\-+.*", "[UNDERLINE]"), (" +", " ")])
.collect::<Vec<_>>();

uv_snapshot!(filters, Command::new(get_bin())
.arg("pip")
.arg("list")
.arg("--editable")
.arg("--cache-dir")
.arg(context.cache_dir.path())
.env("VIRTUAL_ENV", context.venv.as_os_str())
.env("UV_NO_WRAP", "1")
.current_dir(&context.temp_dir), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Failed to read metadata: from [SITE_PACKAGES]/paramiko.egg-link
Caused by: after parsing '0.1b0', found 'ulbasaur', which is not part of a valid version
"###
);

Ok(())
}
2 changes: 1 addition & 1 deletion crates/uv/tests/pip_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ fn uninstall_legacy_editable() -> Result<()> {
.child("zstandard.egg-info")
.child("PKG-INFO")
.write_str(
"Metadata-Version: 2.2
"Metadata-Version: 2.1
Name: zstandard
Version: 0.22.0
",
Expand Down

0 comments on commit 60098f5

Please sign in to comment.