From 07d038b90c89af247100385310e8c32394d664b0 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 23 Jul 2024 14:03:02 -0400 Subject: [PATCH] Add tests for `uv cache clean` (#5356) --- _typos.toml | 1 + crates/uv/tests/cache_clean.rs | 133 +++++++++++++++++++++++++++++++++ crates/uv/tests/cache_prune.rs | 20 ++--- crates/uv/tests/common/mod.rs | 12 ++- 4 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 crates/uv/tests/cache_clean.rs diff --git a/_typos.toml b/_typos.toml index 5e78f814e..7bd98fc79 100644 --- a/_typos.toml +++ b/_typos.toml @@ -11,6 +11,7 @@ extend-ignore-re = [ "FrIeNdLy-\\._\\.-bArD", "I borken you cache", "eb1ba5f5", + "e8208120cae3ba69", "github_pat_[0-9a-zA-Z_]+", "LICENSEs", "astroid", diff --git a/crates/uv/tests/cache_clean.rs b/crates/uv/tests/cache_clean.rs new file mode 100644 index 000000000..c1400befa --- /dev/null +++ b/crates/uv/tests/cache_clean.rs @@ -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(()) +} diff --git a/crates/uv/tests/cache_prune.rs b/crates/uv/tests/cache_prune.rs index 222537aab..81166b013 100644 --- a/crates/uv/tests/cache_prune.rs +++ b/crates/uv/tests/cache_prune.rs @@ -1,25 +1,15 @@ #![cfg(all(feature = "python", feature = "pypi"))] -use std::process::Command; - use anyhow::Result; use assert_cmd::prelude::*; use assert_fs::prelude::*; use common::uv_snapshot; -use crate::common::{get_bin, TestContext}; +use crate::common::TestContext; 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. #[test] fn prune_no_op() -> Result<()> { @@ -35,7 +25,7 @@ fn prune_no_op() -> Result<()> { .assert() .success(); - uv_snapshot!(context.filters(), prune_command(&context).arg("--verbose"), @r###" + uv_snapshot!(context.filters(), context.prune().arg("--verbose"), @r###" success: true exit_code: 0 ----- stdout ----- @@ -68,7 +58,7 @@ fn prune_stale_directory() -> Result<()> { let simple = context.cache_dir.child("simple-v4"); 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 exit_code: 0 ----- stdout ----- @@ -123,7 +113,7 @@ fn prune_cached_env() { ]) .collect(); - uv_snapshot!(filters, prune_command(&context).arg("--verbose"), @r###" + uv_snapshot!(filters, context.prune().arg("--verbose"), @r###" success: true exit_code: 0 ----- stdout ----- @@ -167,7 +157,7 @@ fn prune_stale_symlink() -> Result<()> { ]) .collect(); - uv_snapshot!(filters, prune_command(&context).arg("--verbose"), @r###" + uv_snapshot!(filters, context.prune().arg("--verbose"), @r###" success: true exit_code: 0 ----- stdout ----- diff --git a/crates/uv/tests/common/mod.rs b/crates/uv/tests/common/mod.rs index fac8bb072..c539e5f98 100644 --- a/crates/uv/tests/common/mod.rs +++ b/crates/uv/tests/common/mod.rs @@ -548,10 +548,18 @@ impl TestContext { command } - /// Create a `uv clean` command. + /// Create a `uv cache clean` command. pub fn clean(&self) -> Command { 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); command }