Add tests for `uv cache clean` (#5356)

This commit is contained in:
Charlie Marsh 2024-07-23 14:03:02 -04:00 committed by GitHub
parent 337a1c2083
commit 07d038b90c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 149 additions and 17 deletions

View File

@ -11,6 +11,7 @@ extend-ignore-re = [
"FrIeNdLy-\\._\\.-bArD", "FrIeNdLy-\\._\\.-bArD",
"I borken you cache", "I borken you cache",
"eb1ba5f5", "eb1ba5f5",
"e8208120cae3ba69",
"github_pat_[0-9a-zA-Z_]+", "github_pat_[0-9a-zA-Z_]+",
"LICENSEs", "LICENSEs",
"astroid", "astroid",

View File

@ -0,0 +1,133 @@
#![cfg(all(feature = "python", feature = "pypi"))]
use anyhow::Result;
use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use common::uv_snapshot;
use crate::common::TestContext;
mod common;
/// `cache clean` should remove all packages.
#[test]
fn clean_all() -> Result<()> {
let context = TestContext::new("3.12");
let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("typing-extensions\niniconfig")?;
// Install a requirement, to populate the cache.
context
.pip_sync()
.arg("requirements.txt")
.assert()
.success();
uv_snapshot!(context.filters(), context.clean().arg("--verbose"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
DEBUG uv [VERSION] ([COMMIT] DATE)
Clearing cache at: [CACHE_DIR]/
Removed 28 files ([SIZE])
"###);
Ok(())
}
/// `cache clean iniconfig` should remove a single package (`iniconfig`).
#[test]
fn clean_package_pypi() -> Result<()> {
let context = TestContext::new("3.12");
let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("anyio\niniconfig")?;
// Install a requirement, to populate the cache.
context
.pip_sync()
.arg("requirements.txt")
.assert()
.success();
// Assert that the `.rkyv` file is created for `iniconfig`.
let rkyv = context
.cache_dir
.child("simple-v10")
.child("pypi")
.child("iniconfig.rkyv");
assert!(
rkyv.exists(),
"Expected the `.rkyv` file to exist for `iniconfig`"
);
uv_snapshot!(context.filters(), context.clean().arg("--verbose").arg("iniconfig"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
DEBUG uv [VERSION] ([COMMIT] DATE)
Removed 4 files for iniconfig ([SIZE])
"###);
// Assert that the `.rkyv` file is removed for `iniconfig`.
assert!(
!rkyv.exists(),
"Expected the `.rkyv` file to be removed for `iniconfig`"
);
Ok(())
}
/// `cache clean iniconfig` should remove a single package (`iniconfig`).
#[test]
fn clean_package_index() -> Result<()> {
let context = TestContext::new("3.12");
let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("anyio\niniconfig")?;
// Install a requirement, to populate the cache.
context
.pip_sync()
.arg("requirements.txt")
.arg("--index-url")
.arg("https://test.pypi.org/simple")
.assert()
.success();
// Assert that the `.rkyv` file is created for `iniconfig`.
let rkyv = context
.cache_dir
.child("simple-v10")
.child("index")
.child("e8208120cae3ba69")
.child("iniconfig.rkyv");
assert!(
rkyv.exists(),
"Expected the `.rkyv` file to exist for `iniconfig`"
);
uv_snapshot!(context.filters(), context.clean().arg("--verbose").arg("iniconfig"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
DEBUG uv [VERSION] ([COMMIT] DATE)
Removed 4 files for iniconfig ([SIZE])
"###);
// Assert that the `.rkyv` file is removed for `iniconfig`.
assert!(
!rkyv.exists(),
"Expected the `.rkyv` file to be removed for `iniconfig`"
);
Ok(())
}

View File

@ -1,25 +1,15 @@
#![cfg(all(feature = "python", feature = "pypi"))] #![cfg(all(feature = "python", feature = "pypi"))]
use std::process::Command;
use anyhow::Result; use anyhow::Result;
use assert_cmd::prelude::*; use assert_cmd::prelude::*;
use assert_fs::prelude::*; use assert_fs::prelude::*;
use common::uv_snapshot; use common::uv_snapshot;
use crate::common::{get_bin, TestContext}; use crate::common::TestContext;
mod common; mod common;
/// Create a `cache prune` command with options shared across scenarios.
fn prune_command(context: &TestContext) -> Command {
let mut command = Command::new(get_bin());
command.arg("cache").arg("prune");
context.add_shared_args(&mut command);
command
}
/// `cache prune` should be a no-op if there's nothing out-of-date in the cache. /// `cache prune` should be a no-op if there's nothing out-of-date in the cache.
#[test] #[test]
fn prune_no_op() -> Result<()> { fn prune_no_op() -> Result<()> {
@ -35,7 +25,7 @@ fn prune_no_op() -> Result<()> {
.assert() .assert()
.success(); .success();
uv_snapshot!(context.filters(), prune_command(&context).arg("--verbose"), @r###" uv_snapshot!(context.filters(), context.prune().arg("--verbose"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -68,7 +58,7 @@ fn prune_stale_directory() -> Result<()> {
let simple = context.cache_dir.child("simple-v4"); let simple = context.cache_dir.child("simple-v4");
simple.create_dir_all()?; simple.create_dir_all()?;
uv_snapshot!(context.filters(), prune_command(&context).arg("--verbose"), @r###" uv_snapshot!(context.filters(), context.prune().arg("--verbose"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -123,7 +113,7 @@ fn prune_cached_env() {
]) ])
.collect(); .collect();
uv_snapshot!(filters, prune_command(&context).arg("--verbose"), @r###" uv_snapshot!(filters, context.prune().arg("--verbose"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -167,7 +157,7 @@ fn prune_stale_symlink() -> Result<()> {
]) ])
.collect(); .collect();
uv_snapshot!(filters, prune_command(&context).arg("--verbose"), @r###" uv_snapshot!(filters, context.prune().arg("--verbose"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----

View File

@ -548,10 +548,18 @@ impl TestContext {
command command
} }
/// Create a `uv clean` command. /// Create a `uv cache clean` command.
pub fn clean(&self) -> Command { pub fn clean(&self) -> Command {
let mut command = Command::new(get_bin()); let mut command = Command::new(get_bin());
command.arg("clean"); command.arg("cache").arg("clean");
self.add_shared_args(&mut command);
command
}
/// Create a `uv cache prune` command.
pub fn prune(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("cache").arg("prune");
self.add_shared_args(&mut command); self.add_shared_args(&mut command);
command command
} }