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

Allow round-trip via freeze command #1936

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions crates/uv/src/commands/pip_freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;
use owo_colors::OwoColorize;
use tracing::debug;

use distribution_types::Name;
use distribution_types::{InstalledDist, Name};
use platform_host::Platform;
use uv_cache::Cache;
use uv_fs::Normalized;
Expand Down Expand Up @@ -34,7 +34,18 @@ pub(crate) fn pip_freeze(cache: &Cache, strict: bool, mut printer: Printer) -> R
.iter()
.sorted_unstable_by(|a, b| a.name().cmp(b.name()).then(a.version().cmp(b.version())))
{
println!("{dist}");
match dist {
InstalledDist::Registry(dist) => {
println!("{}=={}", dist.name().bold(), dist.version);
}
InstalledDist::Url(dist) => {
if dist.editable {
println!("-e {}", dist.url);
} else {
println!("{} @ {}", dist.name().bold(), dist.url);
}
}
}
}

// Validate that the environment is consistent.
Expand Down
76 changes: 47 additions & 29 deletions crates/uv/tests/pip_freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::process::Command;

use anyhow::Result;
use assert_cmd::prelude::*;
use assert_fs::prelude::*;

use crate::common::{get_bin, uv_snapshot, TestContext};
Expand All @@ -22,19 +23,6 @@ fn command(context: &TestContext) -> Command {
command
}

/// Create a `pip sync` command with options shared across scenarios.
fn sync_command(context: &TestContext) -> Command {
let mut command = Command::new(get_bin());
command
.arg("pip")
.arg("sync")
.arg("--cache-dir")
.arg(context.cache_dir.path())
.env("VIRTUAL_ENV", context.venv.as_os_str())
.current_dir(&context.temp_dir);
command
}

/// List multiple installed packages in a virtual environment.
#[test]
fn freeze_many() -> Result<()> {
Expand All @@ -44,20 +32,15 @@ fn freeze_many() -> Result<()> {
requirements_txt.write_str("MarkupSafe==2.1.3\ntomli==2.0.1")?;

// Run `pip sync`.
uv_snapshot!(sync_command(&context)
.arg("requirements.txt"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 2 packages in [TIME]
Downloaded 2 packages in [TIME]
Installed 2 packages in [TIME]
+ markupsafe==2.1.3
+ tomli==2.0.1
"###
);
Command::new(get_bin())
.arg("pip")
.arg("sync")
.arg(requirements_txt.path())
.arg("--cache-dir")
.arg(context.cache_dir.path())
.env("VIRTUAL_ENV", context.venv.as_os_str())
.assert()
.success();

// Run `pip freeze`.
uv_snapshot!(command(&context)
Expand All @@ -79,8 +62,6 @@ fn freeze_many() -> Result<()> {
#[test]
#[cfg(unix)]
fn freeze_duplicate() -> Result<()> {
use assert_cmd::assert::OutputAssertExt;

use crate::common::{copy_dir_all, INSTA_FILTERS};

// Sync a version of `pip` into a virtual environment.
Expand Down Expand Up @@ -157,3 +138,40 @@ fn freeze_duplicate() -> Result<()> {

Ok(())
}

/// List a direct URL package in a virtual environment.
#[test]
fn freeze_url() -> Result<()> {
let context = TestContext::new("3.12");

let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("anyio\niniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl")?;

// Run `pip sync`.
Command::new(get_bin())
.arg("pip")
.arg("sync")
.arg(requirements_txt.path())
.arg("--cache-dir")
.arg(context.cache_dir.path())
.env("VIRTUAL_ENV", context.venv.as_os_str())
.assert()
.success();

// Run `pip freeze`.
uv_snapshot!(command(&context)
.arg("--strict"), @r###"
success: true
exit_code: 0
----- stdout -----
anyio==4.3.0
iniconfig @ https://files.pythonhosted.org/packages/ef/a6/62565a6e1cf69e10f5727360368e451d4b7f58beeac6173dc9db836a5b46/iniconfig-2.0.0-py3-none-any.whl

----- stderr -----
warning: The package `anyio` requires `idna >=2.8`, but it's not installed.
warning: The package `anyio` requires `sniffio >=1.1`, but it's not installed.
"###
);

Ok(())
}