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