Skip to content

Commit

Permalink
Ignore UV_CACHE_DIR in help tests (#7895)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Oct 4, 2024
1 parent ad638d7 commit ff1a896
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 10 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions crates/uv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
Expand Down
20 changes: 11 additions & 9 deletions crates/uv/tests/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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###"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 3 additions & 8 deletions crates/uv/tests/pip_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----
Expand All @@ -32,8 +29,6 @@ fn no_arguments() -> Result<()> {
For more information, try '--help'.
"###
);

Ok(())
}

#[test]
Expand Down

0 comments on commit ff1a896

Please sign in to comment.