From ff1a896dd024f7e01ddd5cb92babf895fa0fc2c1 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 4 Oct 2024 17:41:25 +0300 Subject: [PATCH] Ignore `UV_CACHE_DIR` in `help` tests (#7895) --- .editorconfig | 3 +++ .github/workflows/ci.yml | 18 ++++++++++-------- crates/uv/tests/common/mod.rs | 14 ++++++++++++++ crates/uv/tests/help.rs | 20 +++++++++++--------- crates/uv/tests/pip_uninstall.rs | 11 +++-------- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/.editorconfig b/.editorconfig index 05f81d381..b20d4ba54 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,5 +16,8 @@ indent_size = 4 [*.snap] trim_trailing_whitespace = false +[crates/uv/tests/help.rs] +trim_trailing_whitespace = false + [*.md] max_line_length = 100 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 85e0faf5c..4e14d0f26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -173,11 +173,12 @@ jobs: - name: "Install Rust toolchain" run: rustup show + - uses: astral-sh/setup-uv@v3 + with: + version: "latest" + enable-cache: true - name: "Install required Python versions" - run: | - # astral-sh/setup-uv sets `UV_CACHE_DIR` which disrupts the help message check - curl -LsSf https://astral.sh/uv/install.sh | sh - uv python install + run: uv python install - name: "Install cargo nextest" uses: taiki-e/install-action@v2 @@ -221,11 +222,12 @@ jobs: - name: "Install Rust toolchain" run: rustup show + - uses: astral-sh/setup-uv@v3 + with: + version: "latest" + enable-cache: true - name: "Install required Python versions" - run: | - # astral-sh/setup-uv sets `UV_CACHE_DIR` which disrupts the help message check - curl -LsSf https://astral.sh/uv/install.sh | sh - uv python install + run: uv python install - name: "Install cargo nextest" uses: taiki-e/install-action@v2 diff --git a/crates/uv/tests/common/mod.rs b/crates/uv/tests/common/mod.rs index 10f7e2110..8549bff69 100644 --- a/crates/uv/tests/common/mod.rs +++ b/crates/uv/tests/common/mod.rs @@ -194,6 +194,20 @@ impl TestContext { self } + /// Ignore `UV_CACHE_DIR` env variable in tests. + #[must_use] + pub fn with_ignore_cache_dir(mut self) -> Self { + self.filters.push(( + r"\[env:[\n\s]* UV_CACHE_DIR=.+\]".to_string(), + "[env: UV_CACHE_DIR=]".to_string(), + )); + // When `--cache-dir` is followed with other options, + // remove it from the text. Since its presence is inconsistent. + self.filters + .push((r"--cache-dir <".to_string(), "<".to_string())); + self + } + /// Discover the path to the XDG state directory. We use this, rather than the OS-specific /// temporary directory, because on macOS (and Windows on GitHub Actions), they involve /// symlinks. (On macOS, the temporary directory is, like `/var/...`, which resolves to diff --git a/crates/uv/tests/help.rs b/crates/uv/tests/help.rs index e5d229dd5..39c930e75 100644 --- a/crates/uv/tests/help.rs +++ b/crates/uv/tests/help.rs @@ -4,7 +4,7 @@ mod common; #[test] fn help() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); // The `uv help` command should show the long help message uv_snapshot!(context.filters(), context.help(), @r###" @@ -74,7 +74,8 @@ fn help() { #[test] fn help_flag() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); + uv_snapshot!(context.filters(), context.command().arg("--help"), @r###" success: true exit_code: 0 @@ -140,7 +141,8 @@ fn help_flag() { #[test] fn help_short_flag() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); + uv_snapshot!(context.filters(), context.command().arg("-h"), @r###" success: true exit_code: 0 @@ -206,7 +208,7 @@ fn help_short_flag() { #[test] fn help_subcommand() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); uv_snapshot!(context.filters(), context.help().arg("python"), @r###" success: true @@ -394,7 +396,7 @@ fn help_subcommand() { #[test] fn help_subsubcommand() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); uv_snapshot!(context.filters(), context.help().arg("python").arg("install"), @r###" success: true @@ -562,7 +564,7 @@ fn help_subsubcommand() { #[test] fn help_flag_subcommand() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); uv_snapshot!(context.filters(), context.command().arg("python").arg("--help"), @r###" success: true @@ -618,7 +620,7 @@ fn help_flag_subcommand() { #[test] fn help_flag_subsubcommand() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); uv_snapshot!(context.filters(), context.command().arg("python").arg("install").arg("--help"), @r###" success: true @@ -747,7 +749,7 @@ fn help_unknown_subsubcommand() { #[test] fn help_with_global_option() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); uv_snapshot!(context.filters(), context.help().arg("--no-cache"), @r###" success: true @@ -849,7 +851,7 @@ fn help_with_version() { #[test] fn help_with_no_pager() { - let context = TestContext::new_with_versions(&[]); + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); // We can't really test whether the --no-pager option works with a snapshot test. // It's still nice to have a test for the option to confirm the option exists. diff --git a/crates/uv/tests/pip_uninstall.rs b/crates/uv/tests/pip_uninstall.rs index 8ee88af9c..ad5cd0e31 100644 --- a/crates/uv/tests/pip_uninstall.rs +++ b/crates/uv/tests/pip_uninstall.rs @@ -12,13 +12,10 @@ use crate::common::{get_bin, venv_to_interpreter, TestContext}; mod common; #[test] -fn no_arguments() -> Result<()> { - let temp_dir = assert_fs::TempDir::new()?; +fn no_arguments() { + let context = TestContext::new_with_versions(&[]).with_ignore_cache_dir(); - uv_snapshot!(Command::new(get_bin()) - .arg("pip") - .arg("uninstall") - .current_dir(&temp_dir), @r###" + uv_snapshot!(context.filters(), context.pip_uninstall(), @r###" success: false exit_code: 2 ----- stdout ----- @@ -32,8 +29,6 @@ fn no_arguments() -> Result<()> { For more information, try '--help'. "### ); - - Ok(()) } #[test]