Skip to main content

uv_test/
lib.rs

1// The `unreachable_pub` is to silence false positives in RustRover.
2#![allow(dead_code, unreachable_pub)]
3
4use std::borrow::BorrowMut;
5use std::ffi::OsString;
6use std::io::Write as _;
7use std::iter::Iterator;
8use std::path::{Path, PathBuf};
9use std::process::{Command, ExitStatus, Output, Stdio};
10use std::str::FromStr;
11use std::{env, io};
12use uv_preview::Preview;
13use uv_python::downloads::ManagedPythonDownloadList;
14
15use assert_cmd::assert::{Assert, OutputAssertExt};
16use assert_fs::assert::PathAssert;
17use assert_fs::fixture::{
18    ChildPath, FileWriteStr, PathChild, PathCopy, PathCreateDir, SymlinkToFile,
19};
20use base64::{Engine, prelude::BASE64_STANDARD as base64};
21use futures::StreamExt;
22use indoc::{formatdoc, indoc};
23use itertools::Itertools;
24use predicates::prelude::predicate;
25use regex::Regex;
26use tokio::io::AsyncWriteExt;
27
28use uv_cache::{Cache, CacheBucket};
29use uv_fs::Simplified;
30use uv_python::managed::ManagedPythonInstallations;
31use uv_python::{
32    EnvironmentPreference, PythonInstallation, PythonPreference, PythonRequest, PythonVersion,
33};
34use uv_static::EnvVars;
35
36// Exclude any packages uploaded after this date.
37static EXCLUDE_NEWER: &str = "2024-03-25T00:00:00Z";
38
39pub const PACKSE_VERSION: &str = "0.3.59";
40pub const DEFAULT_PYTHON_VERSION: &str = "3.12";
41
42// The expected latest patch version for each Python minor version.
43pub const LATEST_PYTHON_3_15: &str = "3.15.0a6";
44pub const LATEST_PYTHON_3_14: &str = "3.14.3";
45pub const LATEST_PYTHON_3_13: &str = "3.13.12";
46pub const LATEST_PYTHON_3_12: &str = "3.12.13";
47pub const LATEST_PYTHON_3_11: &str = "3.11.15";
48pub const LATEST_PYTHON_3_10: &str = "3.10.20";
49
50/// Using a find links url allows using `--index-url` instead of `--extra-index-url` in tests
51/// to prevent dependency confusion attacks against our test suite.
52pub fn build_vendor_links_url() -> String {
53    env::var(EnvVars::UV_TEST_PACKSE_INDEX)
54        .map(|url| format!("{}/vendor/", url.trim_end_matches('/')))
55        .ok()
56        .unwrap_or(format!(
57            "https://astral-sh.github.io/packse/{PACKSE_VERSION}/vendor/"
58        ))
59}
60
61pub fn packse_index_url() -> String {
62    env::var(EnvVars::UV_TEST_PACKSE_INDEX)
63        .map(|url| format!("{}/simple-html/", url.trim_end_matches('/')))
64        .ok()
65        .unwrap_or(format!(
66            "https://astral-sh.github.io/packse/{PACKSE_VERSION}/simple-html/"
67        ))
68}
69
70/// Create a new [`TestContext`] with the given Python version.
71///
72/// Creates a virtual environment for the test.
73///
74/// This macro captures the uv binary path at compile time using `env!("CARGO_BIN_EXE_uv")`,
75/// which is only available in the test crate.
76#[macro_export]
77macro_rules! test_context {
78    ($python_version:expr) => {
79        $crate::TestContext::new_with_bin(
80            $python_version,
81            std::path::PathBuf::from(env!("CARGO_BIN_EXE_uv")),
82        )
83    };
84}
85
86/// Create a new [`TestContext`] with zero or more Python versions.
87///
88/// Unlike [`test_context!`], this does not create a virtual environment.
89///
90/// This macro captures the uv binary path at compile time using `env!("CARGO_BIN_EXE_uv")`,
91/// which is only available in the test crate.
92#[macro_export]
93macro_rules! test_context_with_versions {
94    ($python_versions:expr) => {
95        $crate::TestContext::new_with_versions_and_bin(
96            $python_versions,
97            std::path::PathBuf::from(env!("CARGO_BIN_EXE_uv")),
98        )
99    };
100}
101
102/// Return the path to the uv binary.
103///
104/// This macro captures the uv binary path at compile time using `env!("CARGO_BIN_EXE_uv")`,
105/// which is only available in the test crate.
106#[macro_export]
107macro_rules! get_bin {
108    () => {
109        std::path::PathBuf::from(env!("CARGO_BIN_EXE_uv"))
110    };
111}
112
113#[doc(hidden)] // Macro and test context only, don't use directly.
114pub const INSTA_FILTERS: &[(&str, &str)] = &[
115    (r"--cache-dir [^\s]+", "--cache-dir [CACHE_DIR]"),
116    // Operation times
117    (r"(\s|\()(\d+m )?(\d+\.)?\d+(ms|s)", "$1[TIME]"),
118    // File sizes
119    (r"(\s|\()(\d+\.)?\d+([KM]i)?B", "$1[SIZE]"),
120    // Timestamps
121    (r"tv_sec: \d+", "tv_sec: [TIME]"),
122    (r"tv_nsec: \d+", "tv_nsec: [TIME]"),
123    // Rewrite Windows output to Unix output
124    (r"\\([\w\d]|\.)", "/$1"),
125    (r"uv\.exe", "uv"),
126    // uv version display
127    (
128        r"uv(-.*)? \d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?(\+\d+)?( \([^)]*\))?",
129        r"uv [VERSION] ([COMMIT] DATE)",
130    ),
131    // Trim end-of-line whitespaces, to allow removing them on save.
132    (r"([^\s])[ \t]+(\r?\n)", "$1$2"),
133];
134
135/// Create a context for tests which simplifies shared behavior across tests.
136///
137/// * Set the current directory to a temporary directory (`temp_dir`).
138/// * Set the cache dir to a different temporary directory (`cache_dir`).
139/// * Set a cutoff for versions used in the resolution so the snapshots don't change after a new release.
140/// * Set the venv to a fresh `.venv` in `temp_dir`
141pub struct TestContext {
142    pub root: ChildPath,
143    pub temp_dir: ChildPath,
144    pub cache_dir: ChildPath,
145    pub python_dir: ChildPath,
146    pub home_dir: ChildPath,
147    pub user_config_dir: ChildPath,
148    pub bin_dir: ChildPath,
149    pub venv: ChildPath,
150    pub workspace_root: PathBuf,
151
152    /// The Python version used for the virtual environment, if any.
153    pub python_version: Option<PythonVersion>,
154
155    /// All the Python versions available during this test context.
156    pub python_versions: Vec<(PythonVersion, PathBuf)>,
157
158    /// Path to the uv binary.
159    uv_bin: PathBuf,
160
161    /// Standard filters for this test context.
162    filters: Vec<(String, String)>,
163
164    /// Extra environment variables to apply to all commands.
165    extra_env: Vec<(OsString, OsString)>,
166
167    #[allow(dead_code)]
168    _root: tempfile::TempDir,
169
170    /// Extra temporary directories whose lifetimes are tied to this context (e.g., directories
171    /// on alternate filesystems created by [`TestContext::with_cache_on_cow_fs`]).
172    #[allow(dead_code)]
173    _extra_tempdirs: Vec<tempfile::TempDir>,
174}
175
176impl TestContext {
177    /// Create a new test context with a virtual environment and explicit uv binary path.
178    ///
179    /// This is called by the `test_context!` macro.
180    pub fn new_with_bin(python_version: &str, uv_bin: PathBuf) -> Self {
181        let new = Self::new_with_versions_and_bin(&[python_version], uv_bin);
182        new.create_venv();
183        new
184    }
185
186    /// Set the "exclude newer" timestamp for all commands in this context.
187    #[must_use]
188    pub fn with_exclude_newer(mut self, exclude_newer: &str) -> Self {
189        self.extra_env
190            .push((EnvVars::UV_EXCLUDE_NEWER.into(), exclude_newer.into()));
191        self
192    }
193
194    /// Set the "http timeout" for all commands in this context.
195    #[must_use]
196    pub fn with_http_timeout(mut self, http_timeout: &str) -> Self {
197        self.extra_env
198            .push((EnvVars::UV_HTTP_TIMEOUT.into(), http_timeout.into()));
199        self
200    }
201
202    /// Set the "concurrent installs" for all commands in this context.
203    #[must_use]
204    pub fn with_concurrent_installs(mut self, concurrent_installs: &str) -> Self {
205        self.extra_env.push((
206            EnvVars::UV_CONCURRENT_INSTALLS.into(),
207            concurrent_installs.into(),
208        ));
209        self
210    }
211
212    /// Add extra standard filtering for messages like "Resolved 10 packages" which
213    /// can differ between platforms.
214    ///
215    /// In some cases, these counts are helpful for the snapshot and should not be filtered.
216    #[must_use]
217    pub fn with_filtered_counts(mut self) -> Self {
218        for verb in &[
219            "Resolved",
220            "Prepared",
221            "Installed",
222            "Uninstalled",
223            "Audited",
224        ] {
225            self.filters.push((
226                format!("{verb} \\d+ packages?"),
227                format!("{verb} [N] packages"),
228            ));
229        }
230        self.filters.push((
231            "Removed \\d+ files?".to_string(),
232            "Removed [N] files".to_string(),
233        ));
234        self
235    }
236
237    /// Add extra filtering for cache size output
238    #[must_use]
239    pub fn with_filtered_cache_size(mut self) -> Self {
240        // Filter raw byte counts (numbers on their own line)
241        self.filters
242            .push((r"(?m)^\d+\n".to_string(), "[SIZE]\n".to_string()));
243        // Filter human-readable sizes (e.g., "384.2 KiB")
244        self.filters.push((
245            r"(?m)^\d+(\.\d+)? [KMGT]i?B\n".to_string(),
246            "[SIZE]\n".to_string(),
247        ));
248        self
249    }
250
251    /// Add extra standard filtering for Windows-compatible missing file errors.
252    #[must_use]
253    pub fn with_filtered_missing_file_error(mut self) -> Self {
254        // The exact message string depends on the system language, so we remove it.
255        // We want to only remove the phrase after `Caused by:`
256        self.filters.push((
257            r"[^:\n]* \(os error 2\)".to_string(),
258            " [OS ERROR 2]".to_string(),
259        ));
260        // Replace the Windows "The system cannot find the path specified. (os error 3)"
261        // with the Unix "No such file or directory (os error 2)"
262        // and mask the language-dependent message.
263        self.filters.push((
264            r"[^:\n]* \(os error 3\)".to_string(),
265            " [OS ERROR 2]".to_string(),
266        ));
267        self
268    }
269
270    /// Add extra standard filtering for executable suffixes on the current platform e.g.
271    /// drops `.exe` on Windows.
272    #[must_use]
273    pub fn with_filtered_exe_suffix(mut self) -> Self {
274        self.filters
275            .push((regex::escape(env::consts::EXE_SUFFIX), String::new()));
276        self
277    }
278
279    /// Add extra standard filtering for Python interpreter sources
280    #[must_use]
281    pub fn with_filtered_python_sources(mut self) -> Self {
282        self.filters.push((
283            "virtual environments, managed installations, or search path".to_string(),
284            "[PYTHON SOURCES]".to_string(),
285        ));
286        self.filters.push((
287            "virtual environments, managed installations, search path, or registry".to_string(),
288            "[PYTHON SOURCES]".to_string(),
289        ));
290        self.filters.push((
291            "virtual environments, search path, or registry".to_string(),
292            "[PYTHON SOURCES]".to_string(),
293        ));
294        self.filters.push((
295            "virtual environments, registry, or search path".to_string(),
296            "[PYTHON SOURCES]".to_string(),
297        ));
298        self.filters.push((
299            "virtual environments or search path".to_string(),
300            "[PYTHON SOURCES]".to_string(),
301        ));
302        self.filters.push((
303            "managed installations or search path".to_string(),
304            "[PYTHON SOURCES]".to_string(),
305        ));
306        self.filters.push((
307            "managed installations, search path, or registry".to_string(),
308            "[PYTHON SOURCES]".to_string(),
309        ));
310        self.filters.push((
311            "search path or registry".to_string(),
312            "[PYTHON SOURCES]".to_string(),
313        ));
314        self.filters.push((
315            "registry or search path".to_string(),
316            "[PYTHON SOURCES]".to_string(),
317        ));
318        self.filters
319            .push(("search path".to_string(), "[PYTHON SOURCES]".to_string()));
320        self
321    }
322
323    /// Add extra standard filtering for Python executable names, e.g., stripping version number
324    /// and `.exe` suffixes.
325    #[must_use]
326    pub fn with_filtered_python_names(mut self) -> Self {
327        for name in ["python", "pypy"] {
328            // Note we strip version numbers from the executable names because, e.g., on Windows
329            // `python.exe` is the equivalent to a Unix `python3.12`.`
330            let suffix = if cfg!(windows) {
331                // On Windows, we'll require a `.exe` suffix for disambiguation
332                // We'll also strip version numbers if present, which is not common for `python.exe`
333                // but can occur for, e.g., `pypy3.12.exe`
334                let exe_suffix = regex::escape(env::consts::EXE_SUFFIX);
335                format!(r"(\d\.\d+|\d)?{exe_suffix}")
336            } else {
337                // On Unix, we'll strip version numbers
338                if name == "python" {
339                    // We can't require them in this case since `/python` is common
340                    r"(\d\.\d+|\d)?(t|d|td)?".to_string()
341                } else {
342                    // However, for other names we'll require them to avoid over-matching
343                    r"(\d\.\d+|\d)(t|d|td)?".to_string()
344                }
345            };
346
347            self.filters.push((
348                // We use a leading path separator to help disambiguate cases where the name is not
349                // used in a path.
350                format!(r"[\\/]{name}{suffix}"),
351                format!("/[{}]", name.to_uppercase()),
352            ));
353        }
354
355        self
356    }
357
358    /// Add extra standard filtering for venv executable directories on the current platform e.g.
359    /// `Scripts` on Windows and `bin` on Unix.
360    #[must_use]
361    pub fn with_filtered_virtualenv_bin(mut self) -> Self {
362        self.filters.push((
363            format!(
364                r"[\\/]{}[\\/]",
365                venv_bin_path(PathBuf::new()).to_string_lossy()
366            ),
367            "/[BIN]/".to_string(),
368        ));
369        self.filters.push((
370            format!(r"[\\/]{}", venv_bin_path(PathBuf::new()).to_string_lossy()),
371            "/[BIN]".to_string(),
372        ));
373        self
374    }
375
376    /// Add extra standard filtering for Python installation `bin/` directories, which are not
377    /// present on Windows but are on Unix. See [`TestContext::with_filtered_virtualenv_bin`] for
378    /// the virtual environment equivalent.
379    #[must_use]
380    pub fn with_filtered_python_install_bin(mut self) -> Self {
381        // We don't want to eagerly match paths that aren't actually Python executables, so we
382        // do our best to detect that case
383        let suffix = if cfg!(windows) {
384            let exe_suffix = regex::escape(env::consts::EXE_SUFFIX);
385            // On Windows, we usually don't have a version attached but we might, e.g., for pypy3.12
386            format!(r"(\d\.\d+|\d)?{exe_suffix}")
387        } else {
388            // On Unix, we'll require a version to be attached to avoid over-matching
389            r"\d\.\d+|\d".to_string()
390        };
391
392        if cfg!(unix) {
393            self.filters.push((
394                format!(r"[\\/]bin/python({suffix})"),
395                "/[INSTALL-BIN]/python$1".to_string(),
396            ));
397            self.filters.push((
398                format!(r"[\\/]bin/pypy({suffix})"),
399                "/[INSTALL-BIN]/pypy$1".to_string(),
400            ));
401        } else {
402            self.filters.push((
403                format!(r"[\\/]python({suffix})"),
404                "/[INSTALL-BIN]/python$1".to_string(),
405            ));
406            self.filters.push((
407                format!(r"[\\/]pypy({suffix})"),
408                "/[INSTALL-BIN]/pypy$1".to_string(),
409            ));
410        }
411        self
412    }
413
414    /// Filtering for various keys in a `pyvenv.cfg` file that will vary
415    /// depending on the specific machine used:
416    /// - `home = foo/bar/baz/python3.X.X/bin`
417    /// - `uv = X.Y.Z`
418    /// - `extends-environment = <path/to/parent/venv>`
419    #[must_use]
420    pub fn with_pyvenv_cfg_filters(mut self) -> Self {
421        let added_filters = [
422            (r"home = .+".to_string(), "home = [PYTHON_HOME]".to_string()),
423            (
424                r"uv = \d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?(\+\d+)?".to_string(),
425                "uv = [UV_VERSION]".to_string(),
426            ),
427            (
428                r"extends-environment = .+".to_string(),
429                "extends-environment = [PARENT_VENV]".to_string(),
430            ),
431        ];
432        for filter in added_filters {
433            self.filters.insert(0, filter);
434        }
435        self
436    }
437
438    /// Add extra filtering for ` -> <PATH>` symlink display for Python versions in the test
439    /// context, e.g., for use in `uv python list`.
440    #[must_use]
441    pub fn with_filtered_python_symlinks(mut self) -> Self {
442        for (version, executable) in &self.python_versions {
443            if fs_err::symlink_metadata(executable).unwrap().is_symlink() {
444                self.filters.extend(
445                    Self::path_patterns(executable.read_link().unwrap())
446                        .into_iter()
447                        .map(|pattern| (format! {" -> {pattern}"}, String::new())),
448                );
449            }
450            // Drop links that are byproducts of the test context too
451            self.filters.push((
452                regex::escape(&format!(" -> [PYTHON-{version}]")),
453                String::new(),
454            ));
455        }
456        self
457    }
458
459    /// Add extra standard filtering for a given path.
460    #[must_use]
461    pub fn with_filtered_path(mut self, path: &Path, name: &str) -> Self {
462        // Note this is sloppy, ideally we wouldn't push to the front of the `Vec` but we need
463        // this to come in front of other filters or we can transform the path (e.g., with `[TMP]`)
464        // before we reach this filter.
465        for pattern in Self::path_patterns(path)
466            .into_iter()
467            .map(|pattern| (pattern, format!("[{name}]/")))
468        {
469            self.filters.insert(0, pattern);
470        }
471        self
472    }
473
474    /// Adds a filter that specifically ignores the link mode warning.
475    ///
476    /// This occurs in some cases and can be used on an ad hoc basis to squash
477    /// the warning in the snapshots. This is useful because the warning does
478    /// not consistently appear. It is dependent on the environment. (For
479    /// example, sometimes it's dependent on whether `/tmp` and `~/.local` live
480    /// on the same file system.)
481    #[inline]
482    #[must_use]
483    pub fn with_filtered_link_mode_warning(mut self) -> Self {
484        let pattern = "warning: Failed to hardlink files; .*\n.*\n.*\n";
485        self.filters.push((pattern.to_string(), String::new()));
486        self
487    }
488
489    /// Adds a filter for platform-specific errors when a file is not executable.
490    #[inline]
491    #[must_use]
492    pub fn with_filtered_not_executable(mut self) -> Self {
493        let pattern = if cfg!(unix) {
494            r"Permission denied \(os error 13\)"
495        } else {
496            r"\%1 is not a valid Win32 application. \(os error 193\)"
497        };
498        self.filters
499            .push((pattern.to_string(), "[PERMISSION DENIED]".to_string()));
500        self
501    }
502
503    /// Adds a filter that ignores platform information in a Python installation key.
504    #[must_use]
505    pub fn with_filtered_python_keys(mut self) -> Self {
506        // Filter platform keys
507        let platform_re = r"(?x)
508  (                         # We capture the group before the platform
509    (?:cpython|pypy|graalpy)# Python implementation
510    -
511    \d+\.\d+                # Major and minor version
512    (?:                     # The patch version is handled separately
513      \.
514      (?:
515        \[X\]               # A previously filtered patch version [X]
516        |                   # OR
517        \[LATEST\]          # A previously filtered latest patch version [LATEST]
518        |                   # OR
519        \d+                 # An actual patch version
520      )
521    )?                      # (we allow the patch version to be missing entirely, e.g., in a request)
522    (?:(?:a|b|rc)[0-9]+)?   # Pre-release version component, e.g., `a6` or `rc2`
523    (?:[td])?               # A short variant, such as `t` (for freethreaded) or `d` (for debug)
524    (?:(\+[a-z]+)+)?        # A long variant, such as `+freethreaded` or `+freethreaded+debug`
525  )
526  -
527  [a-z0-9]+                 # Operating system (e.g., 'macos')
528  -
529  [a-z0-9_]+                # Architecture (e.g., 'aarch64')
530  -
531  [a-z]+                    # Libc (e.g., 'none')
532";
533        self.filters
534            .push((platform_re.to_string(), "$1-[PLATFORM]".to_string()));
535        self
536    }
537
538    /// Adds a filter that replaces the latest Python patch versions with `[LATEST]` placeholder.
539    #[must_use]
540    pub fn with_filtered_latest_python_versions(mut self) -> Self {
541        // Filter the latest patch versions with [LATEST] placeholder
542        // The order matters - we want to match the full version first
543        for (minor, patch) in [
544            ("3.15", LATEST_PYTHON_3_15.strip_prefix("3.15.").unwrap()),
545            ("3.14", LATEST_PYTHON_3_14.strip_prefix("3.14.").unwrap()),
546            ("3.13", LATEST_PYTHON_3_13.strip_prefix("3.13.").unwrap()),
547            ("3.12", LATEST_PYTHON_3_12.strip_prefix("3.12.").unwrap()),
548            ("3.11", LATEST_PYTHON_3_11.strip_prefix("3.11.").unwrap()),
549            ("3.10", LATEST_PYTHON_3_10.strip_prefix("3.10.").unwrap()),
550        ] {
551            // Match the full version in various contexts (cpython-X.Y.Z, Python X.Y.Z, etc.)
552            let pattern = format!(r"(\b){minor}\.{patch}(\b)");
553            let replacement = format!("${{1}}{minor}.[LATEST]${{2}}");
554            self.filters.push((pattern, replacement));
555        }
556        self
557    }
558
559    /// Add a filter that ignores temporary directory in path.
560    #[must_use]
561    pub fn with_filtered_windows_temp_dir(mut self) -> Self {
562        let pattern = regex::escape(
563            &self
564                .temp_dir
565                .simplified_display()
566                .to_string()
567                .replace('/', "\\"),
568        );
569        self.filters.push((pattern, "[TEMP_DIR]".to_string()));
570        self
571    }
572
573    /// Add a filter for (bytecode) compilation file counts
574    #[must_use]
575    pub fn with_filtered_compiled_file_count(mut self) -> Self {
576        self.filters.push((
577            r"compiled \d+ files".to_string(),
578            "compiled [COUNT] files".to_string(),
579        ));
580        self
581    }
582
583    /// Adds filters for non-deterministic `CycloneDX` data
584    #[must_use]
585    pub fn with_cyclonedx_filters(mut self) -> Self {
586        self.filters.push((
587            r"urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}".to_string(),
588            "[SERIAL_NUMBER]".to_string(),
589        ));
590        self.filters.push((
591            r#""timestamp": "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+Z""#
592                .to_string(),
593            r#""timestamp": "[TIMESTAMP]""#.to_string(),
594        ));
595        self.filters.push((
596            r#""name": "uv",\s*"version": "\d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?(\+\d+)?""#
597                .to_string(),
598            r#""name": "uv",
599        "version": "[VERSION]""#
600                .to_string(),
601        ));
602        self
603    }
604
605    /// Add a filter that collapses duplicate whitespace.
606    #[must_use]
607    pub fn with_collapsed_whitespace(mut self) -> Self {
608        self.filters.push((r"[ \t]+".to_string(), " ".to_string()));
609        self
610    }
611
612    /// Use a shared global cache for Python downloads.
613    #[must_use]
614    pub fn with_python_download_cache(mut self) -> Self {
615        self.extra_env.push((
616            EnvVars::UV_PYTHON_CACHE_DIR.into(),
617            // Respect `UV_PYTHON_CACHE_DIR` if set, or use the default cache directory
618            env::var_os(EnvVars::UV_PYTHON_CACHE_DIR).unwrap_or_else(|| {
619                uv_cache::Cache::from_settings(false, None)
620                    .unwrap()
621                    .bucket(CacheBucket::Python)
622                    .into()
623            }),
624        ));
625        self
626    }
627
628    #[must_use]
629    pub fn with_empty_python_install_mirror(mut self) -> Self {
630        self.extra_env.push((
631            EnvVars::UV_PYTHON_INSTALL_MIRROR.into(),
632            String::new().into(),
633        ));
634        self
635    }
636
637    /// Add extra directories and configuration for managed Python installations.
638    #[must_use]
639    pub fn with_managed_python_dirs(mut self) -> Self {
640        let managed = self.temp_dir.join("managed");
641
642        self.extra_env.push((
643            EnvVars::UV_PYTHON_BIN_DIR.into(),
644            self.bin_dir.as_os_str().to_owned(),
645        ));
646        self.extra_env
647            .push((EnvVars::UV_PYTHON_INSTALL_DIR.into(), managed.into()));
648        self.extra_env
649            .push((EnvVars::UV_PYTHON_DOWNLOADS.into(), "automatic".into()));
650
651        self
652    }
653
654    #[must_use]
655    pub fn with_versions_as_managed(mut self, versions: &[&str]) -> Self {
656        self.extra_env.push((
657            EnvVars::UV_INTERNAL__TEST_PYTHON_MANAGED.into(),
658            versions.iter().join(" ").into(),
659        ));
660
661        self
662    }
663
664    /// Add a custom filter to the `TestContext`.
665    #[must_use]
666    pub fn with_filter(mut self, filter: (impl Into<String>, impl Into<String>)) -> Self {
667        self.filters.push((filter.0.into(), filter.1.into()));
668        self
669    }
670
671    // Unsets the git credential helper using temp home gitconfig
672    #[must_use]
673    pub fn with_unset_git_credential_helper(self) -> Self {
674        let git_config = self.home_dir.child(".gitconfig");
675        git_config
676            .write_str(indoc! {r"
677                [credential]
678                    helper =
679            "})
680            .expect("Failed to unset git credential helper");
681
682        self
683    }
684
685    /// Clear filters on `TestContext`.
686    #[must_use]
687    pub fn clear_filters(mut self) -> Self {
688        self.filters.clear();
689        self
690    }
691
692    /// Use a cache directory on the filesystem specified by
693    /// [`EnvVars::UV_INTERNAL__TEST_COW_FS`].
694    ///
695    /// Returns `Ok(None)` if the environment variable is not set.
696    pub fn with_cache_on_cow_fs(self) -> anyhow::Result<Option<Self>> {
697        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_COW_FS).ok() else {
698            return Ok(None);
699        };
700        self.with_cache_on_fs(&dir, "COW_FS").map(Some)
701    }
702
703    /// Use a cache directory on the filesystem specified by
704    /// [`EnvVars::UV_INTERNAL__TEST_ALT_FS`].
705    ///
706    /// Returns `Ok(None)` if the environment variable is not set.
707    pub fn with_cache_on_alt_fs(self) -> anyhow::Result<Option<Self>> {
708        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_ALT_FS).ok() else {
709            return Ok(None);
710        };
711        self.with_cache_on_fs(&dir, "ALT_FS").map(Some)
712    }
713
714    /// Use a cache directory on the filesystem specified by
715    /// [`EnvVars::UV_INTERNAL__TEST_LOWLINKS_FS`].
716    ///
717    /// Returns `Ok(None)` if the environment variable is not set.
718    pub fn with_cache_on_lowlinks_fs(self) -> anyhow::Result<Option<Self>> {
719        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_LOWLINKS_FS).ok() else {
720            return Ok(None);
721        };
722        self.with_cache_on_fs(&dir, "LOWLINKS_FS").map(Some)
723    }
724
725    /// Use a cache directory on the filesystem specified by
726    /// [`EnvVars::UV_INTERNAL__TEST_NOCOW_FS`].
727    ///
728    /// Returns `Ok(None)` if the environment variable is not set.
729    pub fn with_cache_on_nocow_fs(self) -> anyhow::Result<Option<Self>> {
730        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_NOCOW_FS).ok() else {
731            return Ok(None);
732        };
733        self.with_cache_on_fs(&dir, "NOCOW_FS").map(Some)
734    }
735
736    /// Use a working directory on the filesystem specified by
737    /// [`EnvVars::UV_INTERNAL__TEST_COW_FS`].
738    ///
739    /// Returns `Ok(None)` if the environment variable is not set.
740    ///
741    /// Note a virtual environment is not created automatically.
742    pub fn with_working_dir_on_cow_fs(self) -> anyhow::Result<Option<Self>> {
743        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_COW_FS).ok() else {
744            return Ok(None);
745        };
746        self.with_working_dir_on_fs(&dir, "COW_FS").map(Some)
747    }
748
749    /// Use a working directory on the filesystem specified by
750    /// [`EnvVars::UV_INTERNAL__TEST_ALT_FS`].
751    ///
752    /// Returns `Ok(None)` if the environment variable is not set.
753    ///
754    /// Note a virtual environment is not created automatically.
755    pub fn with_working_dir_on_alt_fs(self) -> anyhow::Result<Option<Self>> {
756        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_ALT_FS).ok() else {
757            return Ok(None);
758        };
759        self.with_working_dir_on_fs(&dir, "ALT_FS").map(Some)
760    }
761
762    /// Use a working directory on the filesystem specified by
763    /// [`EnvVars::UV_INTERNAL__TEST_NOCOW_FS`].
764    ///
765    /// Returns `Ok(None)` if the environment variable is not set.
766    ///
767    /// Note a virtual environment is not created automatically.
768    pub fn with_working_dir_on_nocow_fs(self) -> anyhow::Result<Option<Self>> {
769        let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_NOCOW_FS).ok() else {
770            return Ok(None);
771        };
772        self.with_working_dir_on_fs(&dir, "NOCOW_FS").map(Some)
773    }
774
775    fn with_cache_on_fs(mut self, dir: &str, name: &str) -> anyhow::Result<Self> {
776        fs_err::create_dir_all(dir)?;
777        let tmp = tempfile::TempDir::new_in(dir)?;
778        self.cache_dir = ChildPath::new(tmp.path()).child("cache");
779        fs_err::create_dir_all(&self.cache_dir)?;
780        let replacement = format!("[{name}]/[CACHE_DIR]/");
781        self.filters.extend(
782            Self::path_patterns(&self.cache_dir)
783                .into_iter()
784                .map(|pattern| (pattern, replacement.clone())),
785        );
786        self._extra_tempdirs.push(tmp);
787        Ok(self)
788    }
789
790    fn with_working_dir_on_fs(mut self, dir: &str, name: &str) -> anyhow::Result<Self> {
791        fs_err::create_dir_all(dir)?;
792        let tmp = tempfile::TempDir::new_in(dir)?;
793        self.temp_dir = ChildPath::new(tmp.path()).child("temp");
794        fs_err::create_dir_all(&self.temp_dir)?;
795        // Place the venv inside temp_dir (matching the default TestContext layout)
796        // so that `context.venv()` creates it at the same path that `VIRTUAL_ENV` points to.
797        let canonical_temp_dir = self.temp_dir.canonicalize()?;
798        self.venv = ChildPath::new(canonical_temp_dir.join(".venv"));
799        let temp_replacement = format!("[{name}]/[TEMP_DIR]/");
800        self.filters.extend(
801            Self::path_patterns(&self.temp_dir)
802                .into_iter()
803                .map(|pattern| (pattern, temp_replacement.clone())),
804        );
805        let venv_replacement = format!("[{name}]/[VENV]/");
806        self.filters.extend(
807            Self::path_patterns(&self.venv)
808                .into_iter()
809                .map(|pattern| (pattern, venv_replacement.clone())),
810        );
811        self._extra_tempdirs.push(tmp);
812        Ok(self)
813    }
814
815    /// Default to the canonicalized path to the temp directory. We need to do this because on
816    /// macOS (and Windows on GitHub Actions) the standard temp dir is a symlink. (On macOS, the
817    /// temporary directory is, like `/var/...`, which resolves to `/private/var/...`.)
818    ///
819    /// It turns out that, at least on macOS, if we pass a symlink as `current_dir`, it gets
820    /// _immediately_ resolved (such that if you call `current_dir` in the running `Command`, it
821    /// returns resolved symlink). This breaks some snapshot tests, since we _don't_ want to
822    /// resolve symlinks for user-provided paths.
823    pub fn test_bucket_dir() -> PathBuf {
824        std::env::temp_dir()
825            .simple_canonicalize()
826            .expect("failed to canonicalize temp dir")
827            .join("uv")
828            .join("tests")
829    }
830
831    /// Create a new test context with multiple Python versions and explicit uv binary path.
832    ///
833    /// Does not create a virtual environment by default, but the first Python version
834    /// can be used to create a virtual environment with [`TestContext::create_venv`].
835    ///
836    /// This is called by the `test_context_with_versions!` macro.
837    pub fn new_with_versions_and_bin(python_versions: &[&str], uv_bin: PathBuf) -> Self {
838        let bucket = Self::test_bucket_dir();
839        fs_err::create_dir_all(&bucket).expect("Failed to create test bucket");
840
841        let root = tempfile::TempDir::new_in(bucket).expect("Failed to create test root directory");
842
843        // Create a `.git` directory to isolate tests that search for git boundaries from the state
844        // of the file system
845        fs_err::create_dir_all(root.path().join(".git"))
846            .expect("Failed to create `.git` placeholder in test root directory");
847
848        let temp_dir = ChildPath::new(root.path()).child("temp");
849        fs_err::create_dir_all(&temp_dir).expect("Failed to create test working directory");
850
851        let cache_dir = ChildPath::new(root.path()).child("cache");
852        fs_err::create_dir_all(&cache_dir).expect("Failed to create test cache directory");
853
854        let python_dir = ChildPath::new(root.path()).child("python");
855        fs_err::create_dir_all(&python_dir).expect("Failed to create test Python directory");
856
857        let bin_dir = ChildPath::new(root.path()).child("bin");
858        fs_err::create_dir_all(&bin_dir).expect("Failed to create test bin directory");
859
860        // When the `git` feature is disabled, enforce that the test suite does not use `git`
861        if cfg!(not(feature = "git")) {
862            Self::disallow_git_cli(&bin_dir).expect("Failed to setup disallowed `git` command");
863        }
864
865        let home_dir = ChildPath::new(root.path()).child("home");
866        fs_err::create_dir_all(&home_dir).expect("Failed to create test home directory");
867
868        let user_config_dir = if cfg!(windows) {
869            ChildPath::new(home_dir.path())
870        } else {
871            ChildPath::new(home_dir.path()).child(".config")
872        };
873
874        // Canonicalize the temp dir for consistent snapshot behavior
875        let canonical_temp_dir = temp_dir.canonicalize().unwrap();
876        let venv = ChildPath::new(canonical_temp_dir.join(".venv"));
877
878        let python_version = python_versions
879            .first()
880            .map(|version| PythonVersion::from_str(version).unwrap());
881
882        let site_packages = python_version
883            .as_ref()
884            .map(|version| site_packages_path(&venv, &format!("python{version}")));
885
886        // The workspace root directory is not available without walking up the tree
887        // https://github.com/rust-lang/cargo/issues/3946
888        let workspace_root = Path::new(&env::var(EnvVars::CARGO_MANIFEST_DIR).unwrap())
889            .parent()
890            .expect("CARGO_MANIFEST_DIR should be nested in workspace")
891            .parent()
892            .expect("CARGO_MANIFEST_DIR should be doubly nested in workspace")
893            .to_path_buf();
894
895        let download_list = ManagedPythonDownloadList::new_only_embedded().unwrap();
896
897        let python_versions: Vec<_> = python_versions
898            .iter()
899            .map(|version| PythonVersion::from_str(version).unwrap())
900            .zip(
901                python_installations_for_versions(&temp_dir, python_versions, &download_list)
902                    .expect("Failed to find test Python versions"),
903            )
904            .collect();
905
906        // Construct directories for each Python executable on Unix where the executable names
907        // need to be normalized
908        if cfg!(unix) {
909            for (version, executable) in &python_versions {
910                let parent = python_dir.child(version.to_string());
911                parent.create_dir_all().unwrap();
912                parent.child("python3").symlink_to_file(executable).unwrap();
913            }
914        }
915
916        let mut filters = Vec::new();
917
918        filters.extend(
919            Self::path_patterns(&uv_bin)
920                .into_iter()
921                .map(|pattern| (pattern, "[UV]".to_string())),
922        );
923
924        // Exclude `link-mode` on Windows since we set it in the remote test suite
925        if cfg!(windows) {
926            filters.push((" --link-mode <LINK_MODE>".to_string(), String::new()));
927            filters.push((r#"link-mode = "copy"\n"#.to_string(), String::new()));
928            // Unix uses "exit status", Windows uses "exit code"
929            filters.push((r"exit code: ".to_string(), "exit status: ".to_string()));
930        }
931
932        for (version, executable) in &python_versions {
933            // Add filtering for the interpreter path
934            filters.extend(
935                Self::path_patterns(executable)
936                    .into_iter()
937                    .map(|pattern| (pattern, format!("[PYTHON-{version}]"))),
938            );
939
940            // And for the symlink we created in the test the Python path
941            filters.extend(
942                Self::path_patterns(python_dir.join(version.to_string()))
943                    .into_iter()
944                    .map(|pattern| {
945                        (
946                            format!("{pattern}[a-zA-Z0-9]*"),
947                            format!("[PYTHON-{version}]"),
948                        )
949                    }),
950            );
951
952            // Add Python patch version filtering unless explicitly requested to ensure
953            // snapshots are patch version agnostic when it is not a part of the test.
954            if version.patch().is_none() {
955                filters.push((
956                    format!(r"({})\.\d+", regex::escape(version.to_string().as_str())),
957                    "$1.[X]".to_string(),
958                ));
959            }
960        }
961
962        filters.extend(
963            Self::path_patterns(&bin_dir)
964                .into_iter()
965                .map(|pattern| (pattern, "[BIN]/".to_string())),
966        );
967        filters.extend(
968            Self::path_patterns(&cache_dir)
969                .into_iter()
970                .map(|pattern| (pattern, "[CACHE_DIR]/".to_string())),
971        );
972        if let Some(ref site_packages) = site_packages {
973            filters.extend(
974                Self::path_patterns(site_packages)
975                    .into_iter()
976                    .map(|pattern| (pattern, "[SITE_PACKAGES]/".to_string())),
977            );
978        }
979        filters.extend(
980            Self::path_patterns(&venv)
981                .into_iter()
982                .map(|pattern| (pattern, "[VENV]/".to_string())),
983        );
984
985        // Account for [`Simplified::user_display`] which is relative to the command working directory
986        if let Some(site_packages) = site_packages {
987            filters.push((
988                Self::path_pattern(
989                    site_packages
990                        .strip_prefix(&canonical_temp_dir)
991                        .expect("The test site-packages directory is always in the tempdir"),
992                ),
993                "[SITE_PACKAGES]/".to_string(),
994            ));
995        }
996
997        // Filter Python library path differences between Windows and Unix
998        filters.push((
999            r"[\\/]lib[\\/]python\d+\.\d+[\\/]".to_string(),
1000            "/[PYTHON-LIB]/".to_string(),
1001        ));
1002        filters.push((r"[\\/]Lib[\\/]".to_string(), "/[PYTHON-LIB]/".to_string()));
1003
1004        filters.extend(
1005            Self::path_patterns(&temp_dir)
1006                .into_iter()
1007                .map(|pattern| (pattern, "[TEMP_DIR]/".to_string())),
1008        );
1009        filters.extend(
1010            Self::path_patterns(&python_dir)
1011                .into_iter()
1012                .map(|pattern| (pattern, "[PYTHON_DIR]/".to_string())),
1013        );
1014        let mut uv_user_config_dir = PathBuf::from(user_config_dir.path());
1015        uv_user_config_dir.push("uv");
1016        filters.extend(
1017            Self::path_patterns(&uv_user_config_dir)
1018                .into_iter()
1019                .map(|pattern| (pattern, "[UV_USER_CONFIG_DIR]/".to_string())),
1020        );
1021        filters.extend(
1022            Self::path_patterns(&user_config_dir)
1023                .into_iter()
1024                .map(|pattern| (pattern, "[USER_CONFIG_DIR]/".to_string())),
1025        );
1026        filters.extend(
1027            Self::path_patterns(&home_dir)
1028                .into_iter()
1029                .map(|pattern| (pattern, "[HOME]/".to_string())),
1030        );
1031        filters.extend(
1032            Self::path_patterns(&workspace_root)
1033                .into_iter()
1034                .map(|pattern| (pattern, "[WORKSPACE]/".to_string())),
1035        );
1036
1037        // Make virtual environment activation cross-platform and shell-agnostic
1038        filters.push((
1039            r"Activate with: (.*)\\Scripts\\activate".to_string(),
1040            "Activate with: source $1/[BIN]/activate".to_string(),
1041        ));
1042        filters.push((
1043            r"Activate with: Scripts\\activate".to_string(),
1044            "Activate with: source [BIN]/activate".to_string(),
1045        ));
1046        filters.push((
1047            r"Activate with: source (.*/|)bin/activate(?:\.\w+)?".to_string(),
1048            "Activate with: source $1[BIN]/activate".to_string(),
1049        ));
1050
1051        // Filter non-deterministic temporary directory names
1052        // Note we apply this _after_ all the full paths to avoid breaking their matching
1053        filters.push((r"(\\|\/)\.tmp.*(\\|\/)".to_string(), "/[TMP]/".to_string()));
1054
1055        // Account for platform prefix differences `file://` (Unix) vs `file:///` (Windows)
1056        filters.push((r"file:///".to_string(), "file://".to_string()));
1057
1058        // Destroy any remaining UNC prefixes (Windows only)
1059        filters.push((r"\\\\\?\\".to_string(), String::new()));
1060
1061        // Remove the version from the packse url in lockfile snapshots. This avoids having a huge
1062        // diff any time we upgrade packse
1063        filters.push((
1064            format!("https://astral-sh.github.io/packse/{PACKSE_VERSION}"),
1065            "https://astral-sh.github.io/packse/PACKSE_VERSION".to_string(),
1066        ));
1067        // Developer convenience
1068        if let Ok(packse_test_index) = env::var(EnvVars::UV_TEST_PACKSE_INDEX) {
1069            filters.push((
1070                packse_test_index.trim_end_matches('/').to_string(),
1071                "https://astral-sh.github.io/packse/PACKSE_VERSION".to_string(),
1072            ));
1073        }
1074        // For wiremock tests
1075        filters.push((r"127\.0\.0\.1:\d*".to_string(), "[LOCALHOST]".to_string()));
1076        // Avoid breaking the tests when bumping the uv version
1077        filters.push((
1078            format!(
1079                r#"requires = \["uv_build>={},<[0-9.]+"\]"#,
1080                uv_version::version()
1081            ),
1082            r#"requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]"#.to_string(),
1083        ));
1084        // Filter script environment hashes
1085        filters.push((
1086            r"environments-v(\d+)[\\/](\w+)-[a-z0-9]+".to_string(),
1087            "environments-v$1/$2-[HASH]".to_string(),
1088        ));
1089        // Filter archive hashes
1090        filters.push((
1091            r"archive-v(\d+)[\\/][A-Za-z0-9\-\_]+".to_string(),
1092            "archive-v$1/[HASH]".to_string(),
1093        ));
1094
1095        Self {
1096            root: ChildPath::new(root.path()),
1097            temp_dir,
1098            cache_dir,
1099            python_dir,
1100            home_dir,
1101            user_config_dir,
1102            bin_dir,
1103            venv,
1104            workspace_root,
1105            python_version,
1106            python_versions,
1107            uv_bin,
1108            filters,
1109            extra_env: vec![],
1110            _root: root,
1111            _extra_tempdirs: vec![],
1112        }
1113    }
1114
1115    /// Create a uv command for testing.
1116    pub fn command(&self) -> Command {
1117        let mut command = self.new_command();
1118        self.add_shared_options(&mut command, true);
1119        command
1120    }
1121
1122    pub fn disallow_git_cli(bin_dir: &Path) -> std::io::Result<()> {
1123        let contents = r"#!/bin/sh
1124    echo 'error: `git` operations are not allowed — are you missing a cfg for the `git` feature?' >&2
1125    exit 127";
1126        let git = bin_dir.join(format!("git{}", env::consts::EXE_SUFFIX));
1127        fs_err::write(&git, contents)?;
1128
1129        #[cfg(unix)]
1130        {
1131            use std::os::unix::fs::PermissionsExt;
1132            let mut perms = fs_err::metadata(&git)?.permissions();
1133            perms.set_mode(0o755);
1134            fs_err::set_permissions(&git, perms)?;
1135        }
1136
1137        Ok(())
1138    }
1139
1140    /// Setup Git LFS Filters
1141    ///
1142    /// You can find the default filters in <https://github.com/git-lfs/git-lfs/blob/v3.7.1/lfs/attribute.go#L66-L71>
1143    /// We set required to true to get a full stacktrace when these commands fail.
1144    #[must_use]
1145    pub fn with_git_lfs_config(mut self) -> Self {
1146        let git_lfs_config = self.root.child(".gitconfig");
1147        git_lfs_config
1148            .write_str(indoc! {r#"
1149                [filter "lfs"]
1150                    clean = git-lfs clean -- %f
1151                    smudge = git-lfs smudge -- %f
1152                    process = git-lfs filter-process
1153                    required = true
1154            "#})
1155            .expect("Failed to setup `git-lfs` filters");
1156
1157        // Its possible your system config can cause conflicts with the Git LFS tests.
1158        // In such cases, add self.extra_env.push(("GIT_CONFIG_NOSYSTEM".into(), "1".into()));
1159        self.extra_env.push((
1160            EnvVars::GIT_CONFIG_GLOBAL.into(),
1161            git_lfs_config.as_os_str().into(),
1162        ));
1163        self
1164    }
1165
1166    /// Shared behaviour for almost all test commands.
1167    ///
1168    /// * Use a temporary cache directory
1169    /// * Use a temporary virtual environment with the Python version of [`Self`]
1170    /// * Don't wrap text output based on the terminal we're in, the test output doesn't get printed
1171    ///   but snapshotted to a string.
1172    /// * Use a fake `HOME` to avoid accidentally changing the developer's machine.
1173    /// * Hide other Pythons with `UV_PYTHON_INSTALL_DIR` and installed interpreters with
1174    ///   `UV_TEST_PYTHON_PATH` and an active venv (if applicable) by removing `VIRTUAL_ENV`.
1175    /// * Increase the stack size to avoid stack overflows on windows due to large async functions.
1176    pub fn add_shared_options(&self, command: &mut Command, activate_venv: bool) {
1177        self.add_shared_args(command);
1178        self.add_shared_env(command, activate_venv);
1179    }
1180
1181    /// Only the arguments of [`TestContext::add_shared_options`].
1182    pub fn add_shared_args(&self, command: &mut Command) {
1183        command.arg("--cache-dir").arg(self.cache_dir.path());
1184    }
1185
1186    /// Only the environment variables of [`TestContext::add_shared_options`].
1187    pub fn add_shared_env(&self, command: &mut Command, activate_venv: bool) {
1188        // Push the test context bin to the front of the PATH
1189        let path = env::join_paths(std::iter::once(self.bin_dir.to_path_buf()).chain(
1190            env::split_paths(&env::var(EnvVars::PATH).unwrap_or_default()),
1191        ))
1192        .unwrap();
1193
1194        // Ensure the tests aren't sensitive to the running user's shell without forcing
1195        // `bash` on Windows
1196        if cfg!(not(windows)) {
1197            command.env(EnvVars::SHELL, "bash");
1198        }
1199
1200        command
1201            // When running the tests in a venv, ignore that venv, otherwise we'll capture warnings.
1202            .env_remove(EnvVars::VIRTUAL_ENV)
1203            // Disable wrapping of uv output for readability / determinism in snapshots.
1204            .env(EnvVars::UV_NO_WRAP, "1")
1205            // While we disable wrapping in uv above, invoked tools may still wrap their output so
1206            // we set a fixed `COLUMNS` value for isolation from terminal width.
1207            .env(EnvVars::COLUMNS, "100")
1208            .env(EnvVars::PATH, path)
1209            .env(EnvVars::HOME, self.home_dir.as_os_str())
1210            .env(EnvVars::APPDATA, self.home_dir.as_os_str())
1211            .env(EnvVars::USERPROFILE, self.home_dir.as_os_str())
1212            .env(
1213                EnvVars::XDG_CONFIG_DIRS,
1214                self.home_dir.join("config").as_os_str(),
1215            )
1216            .env(
1217                EnvVars::XDG_DATA_HOME,
1218                self.home_dir.join("data").as_os_str(),
1219            )
1220            .env(EnvVars::UV_PYTHON_INSTALL_DIR, "")
1221            // Installations are not allowed by default; see `Self::with_managed_python_dirs`
1222            .env(EnvVars::UV_PYTHON_DOWNLOADS, "never")
1223            .env(EnvVars::UV_TEST_PYTHON_PATH, self.python_path())
1224            // Lock to a point in time view of the world
1225            .env(EnvVars::UV_EXCLUDE_NEWER, EXCLUDE_NEWER)
1226            .env(EnvVars::UV_TEST_CURRENT_TIMESTAMP, EXCLUDE_NEWER)
1227            // When installations are allowed, we don't want to write to global state, like the
1228            // Windows registry
1229            .env(EnvVars::UV_PYTHON_INSTALL_REGISTRY, "0")
1230            // Since downloads, fetches and builds run in parallel, their message output order is
1231            // non-deterministic, so can't capture them in test output.
1232            .env(EnvVars::UV_TEST_NO_CLI_PROGRESS, "1")
1233            // I believe the intent of all tests is that they are run outside the
1234            // context of an existing git repository. And when they aren't, state
1235            // from the parent git repository can bleed into the behavior of `uv
1236            // init` in a way that makes it difficult to test consistently. By
1237            // setting GIT_CEILING_DIRECTORIES, we specifically prevent git from
1238            // climbing up past the root of our test directory to look for any
1239            // other git repos.
1240            //
1241            // If one wants to write a test specifically targeting uv within a
1242            // pre-existing git repository, then the test should make the parent
1243            // git repo explicitly. The GIT_CEILING_DIRECTORIES here shouldn't
1244            // impact it, since it only prevents git from discovering repositories
1245            // at or above the root.
1246            .env(EnvVars::GIT_CEILING_DIRECTORIES, self.root.path())
1247            .current_dir(self.temp_dir.path());
1248
1249        for (key, value) in &self.extra_env {
1250            command.env(key, value);
1251        }
1252
1253        if activate_venv {
1254            command.env(EnvVars::VIRTUAL_ENV, self.venv.as_os_str());
1255        }
1256
1257        if cfg!(unix) {
1258            // Avoid locale issues in tests
1259            command.env(EnvVars::LC_ALL, "C");
1260        }
1261    }
1262
1263    /// Create a `pip compile` command for testing.
1264    pub fn pip_compile(&self) -> Command {
1265        let mut command = self.new_command();
1266        command.arg("pip").arg("compile");
1267        self.add_shared_options(&mut command, true);
1268        command
1269    }
1270
1271    /// Create a `pip compile` command for testing.
1272    pub fn pip_sync(&self) -> Command {
1273        let mut command = self.new_command();
1274        command.arg("pip").arg("sync");
1275        self.add_shared_options(&mut command, true);
1276        command
1277    }
1278
1279    pub fn pip_show(&self) -> Command {
1280        let mut command = self.new_command();
1281        command.arg("pip").arg("show");
1282        self.add_shared_options(&mut command, true);
1283        command
1284    }
1285
1286    /// Create a `pip freeze` command with options shared across scenarios.
1287    pub fn pip_freeze(&self) -> Command {
1288        let mut command = self.new_command();
1289        command.arg("pip").arg("freeze");
1290        self.add_shared_options(&mut command, true);
1291        command
1292    }
1293
1294    /// Create a `pip check` command with options shared across scenarios.
1295    pub fn pip_check(&self) -> Command {
1296        let mut command = self.new_command();
1297        command.arg("pip").arg("check");
1298        self.add_shared_options(&mut command, true);
1299        command
1300    }
1301
1302    pub fn pip_list(&self) -> Command {
1303        let mut command = self.new_command();
1304        command.arg("pip").arg("list");
1305        self.add_shared_options(&mut command, true);
1306        command
1307    }
1308
1309    /// Create a `uv venv` command
1310    pub fn venv(&self) -> Command {
1311        let mut command = self.new_command();
1312        command.arg("venv");
1313        self.add_shared_options(&mut command, false);
1314        command
1315    }
1316
1317    /// Create a `pip install` command with options shared across scenarios.
1318    pub fn pip_install(&self) -> Command {
1319        let mut command = self.new_command();
1320        command.arg("pip").arg("install");
1321        self.add_shared_options(&mut command, true);
1322        command
1323    }
1324
1325    /// Create a `pip uninstall` command with options shared across scenarios.
1326    pub fn pip_uninstall(&self) -> Command {
1327        let mut command = self.new_command();
1328        command.arg("pip").arg("uninstall");
1329        self.add_shared_options(&mut command, true);
1330        command
1331    }
1332
1333    /// Create a `pip tree` command for testing.
1334    pub fn pip_tree(&self) -> Command {
1335        let mut command = self.new_command();
1336        command.arg("pip").arg("tree");
1337        self.add_shared_options(&mut command, true);
1338        command
1339    }
1340
1341    /// Create a `pip debug` command for testing.
1342    pub fn pip_debug(&self) -> Command {
1343        let mut command = self.new_command();
1344        command.arg("pip").arg("debug");
1345        self.add_shared_options(&mut command, true);
1346        command
1347    }
1348
1349    /// Create a `uv help` command with options shared across scenarios.
1350    pub fn help(&self) -> Command {
1351        let mut command = self.new_command();
1352        command.arg("help");
1353        self.add_shared_env(&mut command, false);
1354        command
1355    }
1356
1357    /// Create a `uv init` command with options shared across scenarios and
1358    /// isolated from any git repository that may exist in a parent directory.
1359    pub fn init(&self) -> Command {
1360        let mut command = self.new_command();
1361        command.arg("init");
1362        self.add_shared_options(&mut command, false);
1363        command
1364    }
1365
1366    /// Create a `uv sync` command with options shared across scenarios.
1367    pub fn sync(&self) -> Command {
1368        let mut command = self.new_command();
1369        command.arg("sync");
1370        self.add_shared_options(&mut command, false);
1371        command
1372    }
1373
1374    /// Create a `uv lock` command with options shared across scenarios.
1375    pub fn lock(&self) -> Command {
1376        let mut command = self.new_command();
1377        command.arg("lock");
1378        self.add_shared_options(&mut command, false);
1379        command
1380    }
1381
1382    /// Create a `uv workspace metadata` command with options shared across scenarios.
1383    pub fn workspace_metadata(&self) -> Command {
1384        let mut command = self.new_command();
1385        command.arg("workspace").arg("metadata");
1386        self.add_shared_options(&mut command, false);
1387        command
1388    }
1389
1390    /// Create a `uv workspace dir` command with options shared across scenarios.
1391    pub fn workspace_dir(&self) -> Command {
1392        let mut command = self.new_command();
1393        command.arg("workspace").arg("dir");
1394        self.add_shared_options(&mut command, false);
1395        command
1396    }
1397
1398    /// Create a `uv workspace list` command with options shared across scenarios.
1399    pub fn workspace_list(&self) -> Command {
1400        let mut command = self.new_command();
1401        command.arg("workspace").arg("list");
1402        self.add_shared_options(&mut command, false);
1403        command
1404    }
1405
1406    /// Create a `uv export` command with options shared across scenarios.
1407    pub fn export(&self) -> Command {
1408        let mut command = self.new_command();
1409        command.arg("export");
1410        self.add_shared_options(&mut command, false);
1411        command
1412    }
1413
1414    /// Create a `uv format` command with options shared across scenarios.
1415    pub fn format(&self) -> Command {
1416        let mut command = self.new_command();
1417        command.arg("format");
1418        self.add_shared_options(&mut command, false);
1419        // Override to a more recent date for ruff version resolution
1420        command.env(EnvVars::UV_EXCLUDE_NEWER, "2026-02-15T00:00:00Z");
1421        command
1422    }
1423
1424    /// Create a `uv build` command with options shared across scenarios.
1425    pub fn build(&self) -> Command {
1426        let mut command = self.new_command();
1427        command.arg("build");
1428        self.add_shared_options(&mut command, false);
1429        command
1430    }
1431
1432    pub fn version(&self) -> Command {
1433        let mut command = self.new_command();
1434        command.arg("version");
1435        self.add_shared_options(&mut command, false);
1436        command
1437    }
1438
1439    pub fn self_version(&self) -> Command {
1440        let mut command = self.new_command();
1441        command.arg("self").arg("version");
1442        self.add_shared_options(&mut command, false);
1443        command
1444    }
1445
1446    pub fn self_update(&self) -> Command {
1447        let mut command = self.new_command();
1448        command.arg("self").arg("update");
1449        self.add_shared_options(&mut command, false);
1450        command
1451    }
1452
1453    /// Create a `uv publish` command with options shared across scenarios.
1454    pub fn publish(&self) -> Command {
1455        let mut command = self.new_command();
1456        command.arg("publish");
1457        self.add_shared_options(&mut command, false);
1458        command
1459    }
1460
1461    /// Create a `uv python find` command with options shared across scenarios.
1462    pub fn python_find(&self) -> Command {
1463        let mut command = self.new_command();
1464        command
1465            .arg("python")
1466            .arg("find")
1467            .env(EnvVars::UV_PREVIEW, "1")
1468            .env(EnvVars::UV_PYTHON_INSTALL_DIR, "");
1469        self.add_shared_options(&mut command, false);
1470        command
1471    }
1472
1473    /// Create a `uv python list` command with options shared across scenarios.
1474    pub fn python_list(&self) -> Command {
1475        let mut command = self.new_command();
1476        command
1477            .arg("python")
1478            .arg("list")
1479            .env(EnvVars::UV_PYTHON_INSTALL_DIR, "");
1480        self.add_shared_options(&mut command, false);
1481        command
1482    }
1483
1484    /// Create a `uv python install` command with options shared across scenarios.
1485    pub fn python_install(&self) -> Command {
1486        let mut command = self.new_command();
1487        command.arg("python").arg("install");
1488        self.add_shared_options(&mut command, true);
1489        command
1490    }
1491
1492    /// Create a `uv python uninstall` command with options shared across scenarios.
1493    pub fn python_uninstall(&self) -> Command {
1494        let mut command = self.new_command();
1495        command.arg("python").arg("uninstall");
1496        self.add_shared_options(&mut command, true);
1497        command
1498    }
1499
1500    /// Create a `uv python upgrade` command with options shared across scenarios.
1501    pub fn python_upgrade(&self) -> Command {
1502        let mut command = self.new_command();
1503        command.arg("python").arg("upgrade");
1504        self.add_shared_options(&mut command, true);
1505        command
1506    }
1507
1508    /// Create a `uv python pin` command with options shared across scenarios.
1509    pub fn python_pin(&self) -> Command {
1510        let mut command = self.new_command();
1511        command.arg("python").arg("pin");
1512        self.add_shared_options(&mut command, true);
1513        command
1514    }
1515
1516    /// Create a `uv python dir` command with options shared across scenarios.
1517    pub fn python_dir(&self) -> Command {
1518        let mut command = self.new_command();
1519        command.arg("python").arg("dir");
1520        self.add_shared_options(&mut command, true);
1521        command
1522    }
1523
1524    /// Create a `uv run` command with options shared across scenarios.
1525    pub fn run(&self) -> Command {
1526        let mut command = self.new_command();
1527        command.arg("run").env(EnvVars::UV_SHOW_RESOLUTION, "1");
1528        self.add_shared_options(&mut command, true);
1529        command
1530    }
1531
1532    /// Create a `uv tool run` command with options shared across scenarios.
1533    pub fn tool_run(&self) -> Command {
1534        let mut command = self.new_command();
1535        command
1536            .arg("tool")
1537            .arg("run")
1538            .env(EnvVars::UV_SHOW_RESOLUTION, "1");
1539        self.add_shared_options(&mut command, false);
1540        command
1541    }
1542
1543    /// Create a `uv upgrade run` command with options shared across scenarios.
1544    pub fn tool_upgrade(&self) -> Command {
1545        let mut command = self.new_command();
1546        command.arg("tool").arg("upgrade");
1547        self.add_shared_options(&mut command, false);
1548        command
1549    }
1550
1551    /// Create a `uv tool install` command with options shared across scenarios.
1552    pub fn tool_install(&self) -> Command {
1553        let mut command = self.new_command();
1554        command.arg("tool").arg("install");
1555        self.add_shared_options(&mut command, false);
1556        command
1557    }
1558
1559    /// Create a `uv tool list` command with options shared across scenarios.
1560    pub fn tool_list(&self) -> Command {
1561        let mut command = self.new_command();
1562        command.arg("tool").arg("list");
1563        self.add_shared_options(&mut command, false);
1564        command
1565    }
1566
1567    /// Create a `uv tool dir` command with options shared across scenarios.
1568    pub fn tool_dir(&self) -> Command {
1569        let mut command = self.new_command();
1570        command.arg("tool").arg("dir");
1571        self.add_shared_options(&mut command, false);
1572        command
1573    }
1574
1575    /// Create a `uv tool uninstall` command with options shared across scenarios.
1576    pub fn tool_uninstall(&self) -> Command {
1577        let mut command = self.new_command();
1578        command.arg("tool").arg("uninstall");
1579        self.add_shared_options(&mut command, false);
1580        command
1581    }
1582
1583    /// Create a `uv add` command for the given requirements.
1584    pub fn add(&self) -> Command {
1585        let mut command = self.new_command();
1586        command.arg("add");
1587        self.add_shared_options(&mut command, false);
1588        command
1589    }
1590
1591    /// Create a `uv remove` command for the given requirements.
1592    pub fn remove(&self) -> Command {
1593        let mut command = self.new_command();
1594        command.arg("remove");
1595        self.add_shared_options(&mut command, false);
1596        command
1597    }
1598
1599    /// Create a `uv tree` command with options shared across scenarios.
1600    pub fn tree(&self) -> Command {
1601        let mut command = self.new_command();
1602        command.arg("tree");
1603        self.add_shared_options(&mut command, false);
1604        command
1605    }
1606
1607    /// Create a `uv cache clean` command.
1608    pub fn clean(&self) -> Command {
1609        let mut command = self.new_command();
1610        command.arg("cache").arg("clean");
1611        self.add_shared_options(&mut command, false);
1612        command
1613    }
1614
1615    /// Create a `uv cache prune` command.
1616    pub fn prune(&self) -> Command {
1617        let mut command = self.new_command();
1618        command.arg("cache").arg("prune");
1619        self.add_shared_options(&mut command, false);
1620        command
1621    }
1622
1623    /// Create a `uv cache size` command.
1624    pub fn cache_size(&self) -> Command {
1625        let mut command = self.new_command();
1626        command.arg("cache").arg("size");
1627        self.add_shared_options(&mut command, false);
1628        command
1629    }
1630
1631    /// Create a `uv build_backend` command.
1632    ///
1633    /// Note that this command is hidden and only invoking it through a build frontend is supported.
1634    pub fn build_backend(&self) -> Command {
1635        let mut command = self.new_command();
1636        command.arg("build-backend");
1637        self.add_shared_options(&mut command, false);
1638        command
1639    }
1640
1641    /// The path to the Python interpreter in the venv.
1642    ///
1643    /// Don't use this for `Command::new`, use `Self::python_command` instead.
1644    pub fn interpreter(&self) -> PathBuf {
1645        let venv = &self.venv;
1646        if cfg!(unix) {
1647            venv.join("bin").join("python")
1648        } else if cfg!(windows) {
1649            venv.join("Scripts").join("python.exe")
1650        } else {
1651            unimplemented!("Only Windows and Unix are supported")
1652        }
1653    }
1654
1655    pub fn python_command(&self) -> Command {
1656        let mut interpreter = self.interpreter();
1657
1658        // If there's not a virtual environment, use the first Python interpreter in the context
1659        if !interpreter.exists() {
1660            interpreter.clone_from(
1661                &self
1662                    .python_versions
1663                    .first()
1664                    .expect("At least one Python version is required")
1665                    .1,
1666            );
1667        }
1668
1669        let mut command = Self::new_command_with(&interpreter);
1670        command
1671            // Our tests change files in <1s, so we must disable CPython bytecode caching or we'll get stale files
1672            // https://github.com/python/cpython/issues/75953
1673            .arg("-B")
1674            // Python on windows
1675            .env(EnvVars::PYTHONUTF8, "1");
1676
1677        self.add_shared_env(&mut command, false);
1678
1679        command
1680    }
1681
1682    /// Create a `uv auth login` command.
1683    pub fn auth_login(&self) -> Command {
1684        let mut command = self.new_command();
1685        command.arg("auth").arg("login");
1686        self.add_shared_options(&mut command, false);
1687        command
1688    }
1689
1690    /// Create a `uv auth logout` command.
1691    pub fn auth_logout(&self) -> Command {
1692        let mut command = self.new_command();
1693        command.arg("auth").arg("logout");
1694        self.add_shared_options(&mut command, false);
1695        command
1696    }
1697
1698    /// Create a `uv auth helper --protocol bazel get` command.
1699    pub fn auth_helper(&self) -> Command {
1700        let mut command = self.new_command();
1701        command.arg("auth").arg("helper");
1702        self.add_shared_options(&mut command, false);
1703        command
1704    }
1705
1706    /// Create a `uv auth token` command.
1707    pub fn auth_token(&self) -> Command {
1708        let mut command = self.new_command();
1709        command.arg("auth").arg("token");
1710        self.add_shared_options(&mut command, false);
1711        command
1712    }
1713
1714    /// Set `HOME` to the real home directory.
1715    ///
1716    /// We need this for testing commands which use the macOS keychain.
1717    #[must_use]
1718    pub fn with_real_home(mut self) -> Self {
1719        if let Some(home) = env::var_os(EnvVars::HOME) {
1720            self.extra_env
1721                .push((EnvVars::HOME.to_string().into(), home));
1722        }
1723        // Use the test's isolated config directory to avoid reading user
1724        // configuration files (like `.python-version`) that could interfere with tests.
1725        self.extra_env.push((
1726            EnvVars::XDG_CONFIG_HOME.into(),
1727            self.user_config_dir.as_os_str().into(),
1728        ));
1729        self
1730    }
1731
1732    /// Run the given python code and check whether it succeeds.
1733    pub fn assert_command(&self, command: &str) -> Assert {
1734        self.python_command()
1735            .arg("-c")
1736            .arg(command)
1737            .current_dir(&self.temp_dir)
1738            .assert()
1739    }
1740
1741    /// Run the given python file and check whether it succeeds.
1742    pub fn assert_file(&self, file: impl AsRef<Path>) -> Assert {
1743        self.python_command()
1744            .arg(file.as_ref())
1745            .current_dir(&self.temp_dir)
1746            .assert()
1747    }
1748
1749    /// Assert a package is installed with the given version.
1750    pub fn assert_installed(&self, package: &'static str, version: &'static str) {
1751        self.assert_command(
1752            format!("import {package} as package; print(package.__version__, end='')").as_str(),
1753        )
1754        .success()
1755        .stdout(version);
1756    }
1757
1758    /// Assert a package is not installed.
1759    pub fn assert_not_installed(&self, package: &'static str) {
1760        self.assert_command(format!("import {package}").as_str())
1761            .failure();
1762    }
1763
1764    /// Generate various escaped regex patterns for the given path.
1765    pub fn path_patterns(path: impl AsRef<Path>) -> Vec<String> {
1766        let mut patterns = Vec::new();
1767
1768        // We can only canonicalize paths that exist already
1769        if path.as_ref().exists() {
1770            patterns.push(Self::path_pattern(
1771                path.as_ref()
1772                    .canonicalize()
1773                    .expect("Failed to create canonical path"),
1774            ));
1775        }
1776
1777        // Include a non-canonicalized version
1778        patterns.push(Self::path_pattern(path));
1779
1780        patterns
1781    }
1782
1783    /// Generate an escaped regex pattern for the given path.
1784    fn path_pattern(path: impl AsRef<Path>) -> String {
1785        format!(
1786            // Trim the trailing separator for cross-platform directories filters
1787            r"{}\\?/?",
1788            regex::escape(&path.as_ref().simplified_display().to_string())
1789                // Make separators platform agnostic because on Windows we will display
1790                // paths with Unix-style separators sometimes
1791                .replace(r"\\", r"(\\|\/)")
1792        )
1793    }
1794
1795    pub fn python_path(&self) -> OsString {
1796        if cfg!(unix) {
1797            // On Unix, we needed to normalize the Python executable names to `python3` for the tests
1798            env::join_paths(
1799                self.python_versions
1800                    .iter()
1801                    .map(|(version, _)| self.python_dir.join(version.to_string())),
1802            )
1803            .unwrap()
1804        } else {
1805            // On Windows, just join the parent directories of the executables
1806            env::join_paths(
1807                self.python_versions
1808                    .iter()
1809                    .map(|(_, executable)| executable.parent().unwrap().to_path_buf()),
1810            )
1811            .unwrap()
1812        }
1813    }
1814
1815    /// Standard snapshot filters _plus_ those for this test context.
1816    pub fn filters(&self) -> Vec<(&str, &str)> {
1817        // Put test context snapshots before the default filters
1818        // This ensures we don't replace other patterns inside paths from the test context first
1819        self.filters
1820            .iter()
1821            .map(|(p, r)| (p.as_str(), r.as_str()))
1822            .chain(INSTA_FILTERS.iter().copied())
1823            .collect()
1824    }
1825
1826    /// Only the filters added to this test context.
1827    pub fn filters_without_standard_filters(&self) -> Vec<(&str, &str)> {
1828        self.filters
1829            .iter()
1830            .map(|(p, r)| (p.as_str(), r.as_str()))
1831            .collect()
1832    }
1833
1834    /// For when we add pypy to the test suite.
1835    #[allow(clippy::unused_self)]
1836    pub fn python_kind(&self) -> &'static str {
1837        "python"
1838    }
1839
1840    /// Returns the site-packages folder inside the venv.
1841    pub fn site_packages(&self) -> PathBuf {
1842        site_packages_path(
1843            &self.venv,
1844            &format!(
1845                "{}{}",
1846                self.python_kind(),
1847                self.python_version.as_ref().expect(
1848                    "A Python version must be provided to retrieve the test site packages path"
1849                )
1850            ),
1851        )
1852    }
1853
1854    /// Reset the virtual environment in the test context.
1855    pub fn reset_venv(&self) {
1856        self.create_venv();
1857    }
1858
1859    /// Create a new virtual environment named `.venv` in the test context.
1860    fn create_venv(&self) {
1861        let executable = get_python(
1862            self.python_version
1863                .as_ref()
1864                .expect("A Python version must be provided to create a test virtual environment"),
1865        );
1866        create_venv_from_executable(&self.venv, &self.cache_dir, &executable, &self.uv_bin);
1867    }
1868
1869    /// Copies the files from the ecosystem project given into this text
1870    /// context.
1871    ///
1872    /// This will almost always write at least a `pyproject.toml` into this
1873    /// test context.
1874    ///
1875    /// The given name should correspond to the name of a sub-directory (not a
1876    /// path to it) in the `test/ecosystem` directory.
1877    ///
1878    /// This panics (fails the current test) for any failure.
1879    pub fn copy_ecosystem_project(&self, name: &str) {
1880        let project_dir = PathBuf::from(format!("../../test/ecosystem/{name}"));
1881        self.temp_dir.copy_from(project_dir, &["*"]).unwrap();
1882        // If there is a (gitignore) lockfile, remove it.
1883        if let Err(err) = fs_err::remove_file(self.temp_dir.join("uv.lock")) {
1884            assert_eq!(
1885                err.kind(),
1886                io::ErrorKind::NotFound,
1887                "Failed to remove uv.lock: {err}"
1888            );
1889        }
1890    }
1891
1892    /// Creates a way to compare the changes made to a lock file.
1893    ///
1894    /// This routine starts by copying (not moves) the generated lock file to
1895    /// memory. It then calls the given closure with this test context to get a
1896    /// `Command` and runs the command. The diff between the old lock file and
1897    /// the new one is then returned.
1898    ///
1899    /// This assumes that a lock has already been performed.
1900    pub fn diff_lock(&self, change: impl Fn(&Self) -> Command) -> String {
1901        static TRIM_TRAILING_WHITESPACE: std::sync::LazyLock<Regex> =
1902            std::sync::LazyLock::new(|| Regex::new(r"(?m)^\s+$").unwrap());
1903
1904        let lock_path = ChildPath::new(self.temp_dir.join("uv.lock"));
1905        let old_lock = fs_err::read_to_string(&lock_path).unwrap();
1906        let (snapshot, _, status) = run_and_format_with_status(
1907            change(self),
1908            self.filters(),
1909            "diff_lock",
1910            Some(WindowsFilters::Platform),
1911            None,
1912        );
1913        assert!(status.success(), "{snapshot}");
1914        let new_lock = fs_err::read_to_string(&lock_path).unwrap();
1915        diff_snapshot(&old_lock, &new_lock)
1916    }
1917
1918    /// Read a file in the temporary directory
1919    pub fn read(&self, file: impl AsRef<Path>) -> String {
1920        fs_err::read_to_string(self.temp_dir.join(&file))
1921            .unwrap_or_else(|_| panic!("Missing file: `{}`", file.user_display()))
1922    }
1923
1924    /// Creates a new `Command` that is intended to be suitable for use in
1925    /// all tests.
1926    fn new_command(&self) -> Command {
1927        Self::new_command_with(&self.uv_bin)
1928    }
1929
1930    /// Creates a new `Command` that is intended to be suitable for use in
1931    /// all tests, but with the given binary.
1932    ///
1933    /// Clears environment variables defined in [`EnvVars`] to avoid reading
1934    /// test host settings.
1935    fn new_command_with(bin: &Path) -> Command {
1936        let mut command = Command::new(bin);
1937
1938        let passthrough = [
1939            // For linux distributions
1940            EnvVars::PATH,
1941            // For debugging tests.
1942            EnvVars::RUST_LOG,
1943            EnvVars::RUST_BACKTRACE,
1944            // Windows System configuration.
1945            EnvVars::SYSTEMDRIVE,
1946            // Work around small default stack sizes and large futures in debug builds.
1947            EnvVars::RUST_MIN_STACK,
1948            EnvVars::UV_STACK_SIZE,
1949            // Allow running tests with custom network settings.
1950            EnvVars::ALL_PROXY,
1951            EnvVars::HTTPS_PROXY,
1952            EnvVars::HTTP_PROXY,
1953            EnvVars::NO_PROXY,
1954            EnvVars::SSL_CERT_DIR,
1955            EnvVars::SSL_CERT_FILE,
1956            EnvVars::UV_NATIVE_TLS,
1957        ];
1958
1959        for env_var in EnvVars::all_names()
1960            .iter()
1961            .filter(|name| !passthrough.contains(name))
1962        {
1963            command.env_remove(env_var);
1964        }
1965
1966        command
1967    }
1968}
1969
1970/// Creates a "unified" diff between the two line-oriented strings suitable
1971/// for snapshotting.
1972pub fn diff_snapshot(old: &str, new: &str) -> String {
1973    static TRIM_TRAILING_WHITESPACE: std::sync::LazyLock<Regex> =
1974        std::sync::LazyLock::new(|| Regex::new(r"(?m)^\s+$").unwrap());
1975
1976    let diff = similar::TextDiff::from_lines(old, new);
1977    let unified = diff
1978        .unified_diff()
1979        .context_radius(10)
1980        .header("old", "new")
1981        .to_string();
1982    // Not totally clear why, but some lines end up containing only
1983    // whitespace in the diff, even though they don't appear in the
1984    // original data. So just strip them here.
1985    TRIM_TRAILING_WHITESPACE
1986        .replace_all(&unified, "")
1987        .into_owned()
1988}
1989
1990pub fn site_packages_path(venv: &Path, python: &str) -> PathBuf {
1991    if cfg!(unix) {
1992        venv.join("lib").join(python).join("site-packages")
1993    } else if cfg!(windows) {
1994        venv.join("Lib").join("site-packages")
1995    } else {
1996        unimplemented!("Only Windows and Unix are supported")
1997    }
1998}
1999
2000pub fn venv_bin_path(venv: impl AsRef<Path>) -> PathBuf {
2001    if cfg!(unix) {
2002        venv.as_ref().join("bin")
2003    } else if cfg!(windows) {
2004        venv.as_ref().join("Scripts")
2005    } else {
2006        unimplemented!("Only Windows and Unix are supported")
2007    }
2008}
2009
2010/// Get the path to the python interpreter for a specific python version.
2011pub fn get_python(version: &PythonVersion) -> PathBuf {
2012    ManagedPythonInstallations::from_settings(None)
2013        .map(|installed_pythons| {
2014            installed_pythons
2015                .find_version(version)
2016                .expect("Tests are run on a supported platform")
2017                .next()
2018                .as_ref()
2019                .map(|python| python.executable(false))
2020        })
2021        // We'll search for the request Python on the PATH if not found in the python versions
2022        // We hack this into a `PathBuf` to satisfy the compiler but it's just a string
2023        .unwrap_or_default()
2024        .unwrap_or(PathBuf::from(version.to_string()))
2025}
2026
2027/// Create a virtual environment at the given path.
2028pub fn create_venv_from_executable<P: AsRef<Path>>(
2029    path: P,
2030    cache_dir: &ChildPath,
2031    python: &Path,
2032    uv_bin: &Path,
2033) {
2034    TestContext::new_command_with(uv_bin)
2035        .arg("venv")
2036        .arg(path.as_ref().as_os_str())
2037        .arg("--clear")
2038        .arg("--cache-dir")
2039        .arg(cache_dir.path())
2040        .arg("--python")
2041        .arg(python)
2042        .current_dir(path.as_ref().parent().unwrap())
2043        .assert()
2044        .success();
2045    ChildPath::new(path.as_ref()).assert(predicate::path::is_dir());
2046}
2047
2048/// Create a `PATH` with the requested Python versions available in order.
2049///
2050/// Generally this should be used with `UV_TEST_PYTHON_PATH`.
2051pub fn python_path_with_versions(
2052    temp_dir: &ChildPath,
2053    python_versions: &[&str],
2054) -> anyhow::Result<OsString> {
2055    let download_list = ManagedPythonDownloadList::new_only_embedded().unwrap();
2056    Ok(env::join_paths(
2057        python_installations_for_versions(temp_dir, python_versions, &download_list)?
2058            .into_iter()
2059            .map(|path| path.parent().unwrap().to_path_buf()),
2060    )?)
2061}
2062
2063/// Returns a list of Python executables for the given versions.
2064///
2065/// Generally this should be used with `UV_TEST_PYTHON_PATH`.
2066pub fn python_installations_for_versions(
2067    temp_dir: &ChildPath,
2068    python_versions: &[&str],
2069    download_list: &ManagedPythonDownloadList,
2070) -> anyhow::Result<Vec<PathBuf>> {
2071    let cache = Cache::from_path(temp_dir.child("cache").to_path_buf())
2072        .init_no_wait()?
2073        .expect("No cache contention when setting up Python in tests");
2074    let selected_pythons = python_versions
2075        .iter()
2076        .map(|python_version| {
2077            if let Ok(python) = PythonInstallation::find(
2078                &PythonRequest::parse(python_version),
2079                EnvironmentPreference::OnlySystem,
2080                PythonPreference::Managed,
2081                download_list,
2082                &cache,
2083                Preview::default(),
2084            ) {
2085                python.into_interpreter().sys_executable().to_owned()
2086            } else {
2087                panic!("Could not find Python {python_version} for test\nTry `cargo run python install` first, or refer to CONTRIBUTING.md");
2088            }
2089        })
2090        .collect::<Vec<_>>();
2091
2092    assert!(
2093        python_versions.is_empty() || !selected_pythons.is_empty(),
2094        "Failed to fulfill requested test Python versions: {selected_pythons:?}"
2095    );
2096
2097    Ok(selected_pythons)
2098}
2099
2100#[derive(Debug, Copy, Clone)]
2101pub enum WindowsFilters {
2102    Platform,
2103    Universal,
2104}
2105
2106/// Helper method to apply filters to a string. Useful when `!uv_snapshot` cannot be used.
2107pub fn apply_filters<T: AsRef<str>>(mut snapshot: String, filters: impl AsRef<[(T, T)]>) -> String {
2108    for (matcher, replacement) in filters.as_ref() {
2109        // TODO(konstin): Cache regex compilation
2110        let re = Regex::new(matcher.as_ref()).expect("Do you need to regex::escape your filter?");
2111        if re.is_match(&snapshot) {
2112            snapshot = re.replace_all(&snapshot, replacement.as_ref()).to_string();
2113        }
2114    }
2115    snapshot
2116}
2117
2118/// Execute the command and format its output status, stdout and stderr into a snapshot string.
2119///
2120/// This function is derived from `insta_cmd`s `spawn_with_info`.
2121pub fn run_and_format<T: AsRef<str>>(
2122    command: impl BorrowMut<Command>,
2123    filters: impl AsRef<[(T, T)]>,
2124    function_name: &str,
2125    windows_filters: Option<WindowsFilters>,
2126    input: Option<&str>,
2127) -> (String, Output) {
2128    let (snapshot, output, _) =
2129        run_and_format_with_status(command, filters, function_name, windows_filters, input);
2130    (snapshot, output)
2131}
2132
2133/// Execute the command and format its output status, stdout and stderr into a snapshot string.
2134///
2135/// This function is derived from `insta_cmd`s `spawn_with_info`.
2136#[expect(clippy::print_stderr)]
2137pub fn run_and_format_with_status<T: AsRef<str>>(
2138    mut command: impl BorrowMut<Command>,
2139    filters: impl AsRef<[(T, T)]>,
2140    function_name: &str,
2141    windows_filters: Option<WindowsFilters>,
2142    input: Option<&str>,
2143) -> (String, Output, ExitStatus) {
2144    let program = command
2145        .borrow_mut()
2146        .get_program()
2147        .to_string_lossy()
2148        .to_string();
2149
2150    // Support profiling test run commands with traces.
2151    if let Ok(root) = env::var(EnvVars::TRACING_DURATIONS_TEST_ROOT) {
2152        // We only want to fail if the variable is set at runtime.
2153        #[allow(clippy::assertions_on_constants)]
2154        {
2155            assert!(
2156                cfg!(feature = "tracing-durations-export"),
2157                "You need to enable the tracing-durations-export feature to use `TRACING_DURATIONS_TEST_ROOT`"
2158            );
2159        }
2160        command.borrow_mut().env(
2161            EnvVars::TRACING_DURATIONS_FILE,
2162            Path::new(&root).join(function_name).with_extension("jsonl"),
2163        );
2164    }
2165
2166    let output = if let Some(input) = input {
2167        let mut child = command
2168            .borrow_mut()
2169            .stdin(Stdio::piped())
2170            .stdout(Stdio::piped())
2171            .stderr(Stdio::piped())
2172            .spawn()
2173            .unwrap_or_else(|err| panic!("Failed to spawn {program}: {err}"));
2174        child
2175            .stdin
2176            .as_mut()
2177            .expect("Failed to open stdin")
2178            .write_all(input.as_bytes())
2179            .expect("Failed to write to stdin");
2180
2181        child
2182            .wait_with_output()
2183            .unwrap_or_else(|err| panic!("Failed to read output from {program}: {err}"))
2184    } else {
2185        command
2186            .borrow_mut()
2187            .output()
2188            .unwrap_or_else(|err| panic!("Failed to spawn {program}: {err}"))
2189    };
2190
2191    eprintln!("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Unfiltered output ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
2192    eprintln!(
2193        "----- stdout -----\n{}\n----- stderr -----\n{}",
2194        String::from_utf8_lossy(&output.stdout),
2195        String::from_utf8_lossy(&output.stderr),
2196    );
2197    eprintln!("────────────────────────────────────────────────────────────────────────────────\n");
2198
2199    let mut snapshot = apply_filters(
2200        format!(
2201            "success: {:?}\nexit_code: {}\n----- stdout -----\n{}\n----- stderr -----\n{}",
2202            output.status.success(),
2203            output.status.code().unwrap_or(!0),
2204            String::from_utf8_lossy(&output.stdout),
2205            String::from_utf8_lossy(&output.stderr),
2206        ),
2207        filters,
2208    );
2209
2210    // This is a heuristic filter meant to try and make *most* of our tests
2211    // pass whether it's on Windows or Unix. In particular, there are some very
2212    // common Windows-only dependencies that, when removed from a resolution,
2213    // cause the set of dependencies to be the same across platforms.
2214    if cfg!(windows) {
2215        if let Some(windows_filters) = windows_filters {
2216            // The optional leading +/-/~ is for install logs, the optional next line is for lockfiles
2217            let windows_only_deps = [
2218                (r"( ?[-+~] ?)?colorama==\d+(\.\d+)+( [\\]\n\s+--hash=.*)?\n(\s+# via .*\n)?"),
2219                (r"( ?[-+~] ?)?colorama==\d+(\.\d+)+(\s+[-+~]?\s+# via .*)?\n"),
2220                (r"( ?[-+~] ?)?tzdata==\d+(\.\d+)+( [\\]\n\s+--hash=.*)?\n(\s+# via .*\n)?"),
2221                (r"( ?[-+~] ?)?tzdata==\d+(\.\d+)+(\s+[-+~]?\s+# via .*)?\n"),
2222            ];
2223            let mut removed_packages = 0;
2224            for windows_only_dep in windows_only_deps {
2225                // TODO(konstin): Cache regex compilation
2226                let re = Regex::new(windows_only_dep).unwrap();
2227                if re.is_match(&snapshot) {
2228                    snapshot = re.replace(&snapshot, "").to_string();
2229                    removed_packages += 1;
2230                }
2231            }
2232            if removed_packages > 0 {
2233                for i in 1..20 {
2234                    for verb in match windows_filters {
2235                        WindowsFilters::Platform => [
2236                            "Resolved",
2237                            "Prepared",
2238                            "Installed",
2239                            "Audited",
2240                            "Uninstalled",
2241                        ]
2242                        .iter(),
2243                        WindowsFilters::Universal => {
2244                            ["Prepared", "Installed", "Audited", "Uninstalled"].iter()
2245                        }
2246                    } {
2247                        snapshot = snapshot.replace(
2248                            &format!("{verb} {} packages", i + removed_packages),
2249                            &format!("{verb} {} package{}", i, if i > 1 { "s" } else { "" }),
2250                        );
2251                    }
2252                }
2253            }
2254        }
2255    }
2256
2257    let status = output.status;
2258    (snapshot, output, status)
2259}
2260
2261/// Recursively copy a directory and its contents, skipping gitignored files.
2262pub fn copy_dir_ignore(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> {
2263    for entry in ignore::Walk::new(&src) {
2264        let entry = entry?;
2265        let relative = entry.path().strip_prefix(&src)?;
2266        let ty = entry.file_type().unwrap();
2267        if ty.is_dir() {
2268            fs_err::create_dir(dst.as_ref().join(relative))?;
2269        } else {
2270            fs_err::copy(entry.path(), dst.as_ref().join(relative))?;
2271        }
2272    }
2273    Ok(())
2274}
2275
2276/// Create a stub package `name` in `dir` with the given `pyproject.toml` body.
2277pub fn make_project(dir: &Path, name: &str, body: &str) -> anyhow::Result<()> {
2278    let pyproject_toml = formatdoc! {r#"
2279        [project]
2280        name = "{name}"
2281        version = "0.1.0"
2282        requires-python = ">=3.11,<3.13"
2283        {body}
2284
2285        [build-system]
2286        requires = ["uv_build>=0.9.0,<10000"]
2287        build-backend = "uv_build"
2288        "#
2289    };
2290    fs_err::create_dir_all(dir)?;
2291    fs_err::write(dir.join("pyproject.toml"), pyproject_toml)?;
2292    fs_err::create_dir_all(dir.join("src").join(name))?;
2293    fs_err::write(dir.join("src").join(name).join("__init__.py"), "")?;
2294    Ok(())
2295}
2296
2297// This is a fine-grained token that only has read-only access to the `uv-private-pypackage` repository
2298pub const READ_ONLY_GITHUB_TOKEN: &[&str] = &[
2299    "Z2l0aHViCg==",
2300    "cGF0Cg==",
2301    "MTFBQlVDUjZBMERMUTQ3aVphN3hPdV9qQmhTMkZUeHZ4ZE13OHczakxuZndsV2ZlZjc2cE53eHBWS2tiRUFwdnpmUk8zV0dDSUhicDFsT01aago=",
2302];
2303
2304// This is a fine-grained token that only has read-only access to the `uv-private-pypackage-2` repository
2305#[cfg(not(windows))]
2306pub const READ_ONLY_GITHUB_TOKEN_2: &[&str] = &[
2307    "Z2l0aHViCg==",
2308    "cGF0Cg==",
2309    "MTFBQlVDUjZBMDJTOFYwMTM4YmQ0bV9uTXpueWhxZDBrcllROTQ5SERTeTI0dENKZ2lmdzIybDFSR2s1SE04QW8xTUVYQ1I0Q1YxYUdPRGpvZQo=",
2310];
2311
2312pub const READ_ONLY_GITHUB_SSH_DEPLOY_KEY: &str = "LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNBeTF1SnNZK1JXcWp1NkdIY3Z6a3AwS21yWDEwdmo3RUZqTkpNTkRqSGZPZ0FBQUpqWUpwVnAyQ2FWCmFRQUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDQXkxdUpzWStSV3FqdTZHSGN2emtwMEttclgxMHZqN0VGak5KTU5EakhmT2cKQUFBRUMwbzBnd1BxbGl6TFBJOEFXWDVaS2dVZHJyQ2ptMDhIQm9FenB4VDg3MXBqTFc0bXhqNUZhcU83b1lkeS9PU25RcQphdGZYUytQc1FXTTBrdzBPTWQ4NkFBQUFFR3R2Ym5OMGFVQmhjM1J5WVd3dWMyZ0JBZ01FQlE9PQotLS0tLUVORCBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0K";
2313
2314/// Decode a split, base64 encoded authentication token.
2315/// We split and encode the token to bypass revoke by GitHub's secret scanning
2316pub fn decode_token(content: &[&str]) -> String {
2317    content
2318        .iter()
2319        .map(|part| base64.decode(part).unwrap())
2320        .map(|decoded| {
2321            std::str::from_utf8(decoded.as_slice())
2322                .unwrap()
2323                .trim_end()
2324                .to_string()
2325        })
2326        .join("_")
2327}
2328
2329/// Simulates `reqwest::blocking::get` but returns bytes directly, and disables
2330/// certificate verification, passing through the `BaseClient`
2331#[tokio::main(flavor = "current_thread")]
2332pub async fn download_to_disk(url: &str, path: &Path) {
2333    let trusted_hosts: Vec<_> = env::var(EnvVars::UV_INSECURE_HOST)
2334        .unwrap_or_default()
2335        .split(' ')
2336        .map(|h| uv_configuration::TrustedHost::from_str(h).unwrap())
2337        .collect();
2338
2339    let client = uv_client::BaseClientBuilder::default()
2340        .allow_insecure_host(trusted_hosts)
2341        .build();
2342    let url = url.parse().unwrap();
2343    let response = client
2344        .for_host(&url)
2345        .get(reqwest::Url::from(url))
2346        .send()
2347        .await
2348        .unwrap();
2349
2350    let mut file = fs_err::tokio::File::create(path).await.unwrap();
2351    let mut stream = response.bytes_stream();
2352    while let Some(chunk) = stream.next().await {
2353        file.write_all(&chunk.unwrap()).await.unwrap();
2354    }
2355    file.sync_all().await.unwrap();
2356}
2357
2358/// A guard that sets a directory to read-only and restores original permissions when dropped.
2359///
2360/// This is useful for tests that need to make a directory read-only and ensure
2361/// the permissions are restored even if the test panics.
2362#[cfg(unix)]
2363pub struct ReadOnlyDirectoryGuard {
2364    path: PathBuf,
2365    original_mode: u32,
2366}
2367
2368#[cfg(unix)]
2369impl ReadOnlyDirectoryGuard {
2370    /// Sets the directory to read-only (removes write permission) and returns a guard
2371    /// that will restore the original permissions when dropped.
2372    pub fn new(path: impl Into<PathBuf>) -> std::io::Result<Self> {
2373        use std::os::unix::fs::PermissionsExt;
2374        let path = path.into();
2375        let metadata = fs_err::metadata(&path)?;
2376        let original_mode = metadata.permissions().mode();
2377        // Remove write permissions (keep read and execute)
2378        let readonly_mode = original_mode & !0o222;
2379        fs_err::set_permissions(&path, std::fs::Permissions::from_mode(readonly_mode))?;
2380        Ok(Self {
2381            path,
2382            original_mode,
2383        })
2384    }
2385}
2386
2387#[cfg(unix)]
2388impl Drop for ReadOnlyDirectoryGuard {
2389    fn drop(&mut self) {
2390        use std::os::unix::fs::PermissionsExt;
2391        let _ = fs_err::set_permissions(
2392            &self.path,
2393            std::fs::Permissions::from_mode(self.original_mode),
2394        );
2395    }
2396}
2397
2398/// Utility macro to return the name of the current function.
2399///
2400/// https://stackoverflow.com/a/40234666/3549270
2401#[doc(hidden)]
2402#[macro_export]
2403macro_rules! function_name {
2404    () => {{
2405        fn f() {}
2406        fn type_name_of_val<T>(_: T) -> &'static str {
2407            std::any::type_name::<T>()
2408        }
2409        let mut name = type_name_of_val(f).strip_suffix("::f").unwrap_or("");
2410        while let Some(rest) = name.strip_suffix("::{{closure}}") {
2411            name = rest;
2412        }
2413        name
2414    }};
2415}
2416
2417/// Run [`assert_cmd_snapshot!`], with default filters or with custom filters.
2418///
2419/// By default, the filters will search for the generally windows-only deps colorama and tzdata,
2420/// filter them out and decrease the package counts by one for each match.
2421#[macro_export]
2422macro_rules! uv_snapshot {
2423    ($spawnable:expr, @$snapshot:literal) => {{
2424        uv_snapshot!($crate::INSTA_FILTERS.to_vec(), $spawnable, @$snapshot)
2425    }};
2426    ($filters:expr, $spawnable:expr, @$snapshot:literal) => {{
2427        // Take a reference for backwards compatibility with the vec-expecting insta filters.
2428        let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), Some($crate::WindowsFilters::Platform), None);
2429        ::insta::assert_snapshot!(snapshot, @$snapshot);
2430        output
2431    }};
2432    ($filters:expr, $spawnable:expr, input=$input:expr, @$snapshot:literal) => {{
2433        // Take a reference for backwards compatibility with the vec-expecting insta filters.
2434        let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), Some($crate::WindowsFilters::Platform), Some($input));
2435        ::insta::assert_snapshot!(snapshot, @$snapshot);
2436        output
2437    }};
2438    ($filters:expr, windows_filters=false, $spawnable:expr, @$snapshot:literal) => {{
2439        // Take a reference for backwards compatibility with the vec-expecting insta filters.
2440        let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), None, None);
2441        ::insta::assert_snapshot!(snapshot, @$snapshot);
2442        output
2443    }};
2444    ($filters:expr, universal_windows_filters=true, $spawnable:expr, @$snapshot:literal) => {{
2445        // Take a reference for backwards compatibility with the vec-expecting insta filters.
2446        let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), Some($crate::WindowsFilters::Universal), None);
2447        ::insta::assert_snapshot!(snapshot, @$snapshot);
2448        output
2449    }};
2450}