1#![allow(dead_code, unreachable_pub)]
3
4use std::borrow::BorrowMut;
5use std::ffi::OsString;
6use std::io::Write as _;
7use std::iter::Iterator;
8use std::path::{Path, PathBuf};
9use std::process::{Command, ExitStatus, Output, Stdio};
10use std::str::FromStr;
11use std::{env, io};
12use uv_preview::Preview;
13use uv_python::downloads::ManagedPythonDownloadList;
14
15use assert_cmd::assert::{Assert, OutputAssertExt};
16use assert_fs::assert::PathAssert;
17use assert_fs::fixture::{
18 ChildPath, FileWriteStr, PathChild, PathCopy, PathCreateDir, SymlinkToFile,
19};
20use base64::{Engine, prelude::BASE64_STANDARD as base64};
21use futures::StreamExt;
22use indoc::{formatdoc, indoc};
23use itertools::Itertools;
24use predicates::prelude::predicate;
25use regex::Regex;
26use tokio::io::AsyncWriteExt;
27
28use uv_cache::{Cache, CacheBucket};
29use uv_fs::Simplified;
30use uv_python::managed::ManagedPythonInstallations;
31use uv_python::{
32 EnvironmentPreference, PythonInstallation, PythonPreference, PythonRequest, PythonVersion,
33};
34use uv_static::EnvVars;
35
36static TEST_TIMESTAMP: &str = "2024-03-25T00:00:00Z";
38
39pub const PACKSE_VERSION: &str = "0.3.59";
40pub const DEFAULT_PYTHON_VERSION: &str = "3.12";
41
42pub const LATEST_PYTHON_3_15: &str = "3.15.0b1";
44pub const LATEST_PYTHON_3_14: &str = "3.14.4";
45pub const LATEST_PYTHON_3_13: &str = "3.13.13";
46pub const LATEST_PYTHON_3_12: &str = "3.12.13";
47pub const LATEST_PYTHON_3_11: &str = "3.11.15";
48pub const LATEST_PYTHON_3_10: &str = "3.10.20";
49
50pub fn build_vendor_links_url() -> String {
53 env::var(EnvVars::UV_TEST_PACKSE_INDEX)
54 .map(|url| format!("{}/vendor/", url.trim_end_matches('/')))
55 .ok()
56 .unwrap_or(format!(
57 "https://astral-sh.github.io/packse/{PACKSE_VERSION}/vendor/"
58 ))
59}
60
61pub fn packse_index_url() -> String {
62 env::var(EnvVars::UV_TEST_PACKSE_INDEX)
63 .map(|url| format!("{}/simple-html/", url.trim_end_matches('/')))
64 .ok()
65 .unwrap_or(format!(
66 "https://astral-sh.github.io/packse/{PACKSE_VERSION}/simple-html/"
67 ))
68}
69
70#[macro_export]
77macro_rules! test_context {
78 ($python_version:expr) => {
79 $crate::TestContext::new_with_bin(
80 $python_version,
81 std::path::PathBuf::from(env!("CARGO_BIN_EXE_uv")),
82 )
83 };
84}
85
86#[macro_export]
93macro_rules! test_context_with_versions {
94 ($python_versions:expr) => {
95 $crate::TestContext::new_with_versions_and_bin(
96 $python_versions,
97 std::path::PathBuf::from(env!("CARGO_BIN_EXE_uv")),
98 )
99 };
100}
101
102#[macro_export]
107macro_rules! get_bin {
108 () => {
109 std::path::PathBuf::from(env!("CARGO_BIN_EXE_uv"))
110 };
111}
112
113#[doc(hidden)] pub const INSTA_FILTERS: &[(&str, &str)] = &[
115 (r"--cache-dir [^\s]+", "--cache-dir [CACHE_DIR]"),
116 (r"(\s|\()(\d+m )?(\d+\.)?\d+(ms|s)", "$1[TIME]"),
118 (r"(\s|\()(\d+\.)?\d+([KM]i)?B", "$1[SIZE]"),
120 (r"tv_sec: \d+", "tv_sec: [TIME]"),
122 (r"tv_nsec: \d+", "tv_nsec: [TIME]"),
123 (r"\\([\w\d]|\.)", "/$1"),
125 (r"uv\.exe", "uv"),
126 (
128 r"uv(-.*)? \d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?(\+\d+)?( \([^)]*\))?",
129 r"uv [VERSION] ([COMMIT] DATE)",
130 ),
131 (r"([^\s])[ \t]+(\r?\n)", "$1$2"),
133 (r"DEBUG Loaded \d+ certificate\(s\) from [^\n]+\n", ""),
135];
136
137pub struct TestContext {
144 pub root: ChildPath,
145 pub temp_dir: ChildPath,
146 pub cache_dir: ChildPath,
147 pub python_dir: ChildPath,
148 pub home_dir: ChildPath,
149 pub user_config_dir: ChildPath,
150 pub bin_dir: ChildPath,
151 pub venv: ChildPath,
152 pub workspace_root: PathBuf,
153
154 pub python_version: Option<PythonVersion>,
156
157 pub python_versions: Vec<(PythonVersion, PathBuf)>,
159
160 uv_bin: PathBuf,
162
163 filters: Vec<(String, String)>,
165
166 extra_env: Vec<(OsString, OsString)>,
168
169 #[allow(dead_code)]
170 _root: tempfile::TempDir,
171
172 #[allow(dead_code)]
175 _extra_tempdirs: Vec<tempfile::TempDir>,
176}
177
178impl TestContext {
179 pub fn new_with_bin(python_version: &str, uv_bin: PathBuf) -> Self {
183 let new = Self::new_with_versions_and_bin(&[python_version], uv_bin);
184 new.create_venv();
185 new
186 }
187
188 #[must_use]
190 pub fn with_exclude_newer(mut self, exclude_newer: &str) -> Self {
191 self.extra_env
192 .push((EnvVars::UV_EXCLUDE_NEWER.into(), exclude_newer.into()));
193 self
194 }
195
196 #[must_use]
198 pub fn with_http_timeout(mut self, http_timeout: &str) -> Self {
199 self.extra_env
200 .push((EnvVars::UV_HTTP_TIMEOUT.into(), http_timeout.into()));
201 self
202 }
203
204 #[must_use]
206 pub fn with_concurrent_installs(mut self, concurrent_installs: &str) -> Self {
207 self.extra_env.push((
208 EnvVars::UV_CONCURRENT_INSTALLS.into(),
209 concurrent_installs.into(),
210 ));
211 self
212 }
213
214 #[must_use]
219 pub fn with_filtered_counts(mut self) -> Self {
220 for verb in &[
221 "Resolved",
222 "Prepared",
223 "Installed",
224 "Uninstalled",
225 "Checked",
226 ] {
227 self.filters.push((
228 format!("{verb} \\d+ packages?"),
229 format!("{verb} [N] packages"),
230 ));
231 }
232 self.filters.push((
233 "Removed \\d+ files?".to_string(),
234 "Removed [N] files".to_string(),
235 ));
236 self
237 }
238
239 #[must_use]
241 pub fn with_filtered_cache_size(mut self) -> Self {
242 self.filters
244 .push((r"(?m)^\d+\n".to_string(), "[SIZE]\n".to_string()));
245 self.filters.push((
247 r"(?m)^\d+(\.\d+)? [KMGT]i?B\n".to_string(),
248 "[SIZE]\n".to_string(),
249 ));
250 self
251 }
252
253 #[must_use]
255 pub fn with_filtered_missing_file_error(mut self) -> Self {
256 self.filters.push((
259 r"[^:\n]* \(os error 2\)".to_string(),
260 " [OS ERROR 2]".to_string(),
261 ));
262 self.filters.push((
266 r"[^:\n]* \(os error 3\)".to_string(),
267 " [OS ERROR 2]".to_string(),
268 ));
269 self
270 }
271
272 #[must_use]
275 pub fn with_filtered_exe_suffix(mut self) -> Self {
276 self.filters
277 .push((regex::escape(env::consts::EXE_SUFFIX), String::new()));
278 self
279 }
280
281 #[must_use]
283 pub fn with_filtered_python_sources(mut self) -> Self {
284 self.filters.push((
285 "virtual environments, managed installations, or search path".to_string(),
286 "[PYTHON SOURCES]".to_string(),
287 ));
288 self.filters.push((
289 "virtual environments, managed installations, search path, or registry".to_string(),
290 "[PYTHON SOURCES]".to_string(),
291 ));
292 self.filters.push((
293 "virtual environments, search path, or registry".to_string(),
294 "[PYTHON SOURCES]".to_string(),
295 ));
296 self.filters.push((
297 "virtual environments, registry, or search path".to_string(),
298 "[PYTHON SOURCES]".to_string(),
299 ));
300 self.filters.push((
301 "virtual environments or search path".to_string(),
302 "[PYTHON SOURCES]".to_string(),
303 ));
304 self.filters.push((
305 "managed installations or search path".to_string(),
306 "[PYTHON SOURCES]".to_string(),
307 ));
308 self.filters.push((
309 "managed installations, search path, or registry".to_string(),
310 "[PYTHON SOURCES]".to_string(),
311 ));
312 self.filters.push((
313 "search path or registry".to_string(),
314 "[PYTHON SOURCES]".to_string(),
315 ));
316 self.filters.push((
317 "registry or search path".to_string(),
318 "[PYTHON SOURCES]".to_string(),
319 ));
320 self.filters
321 .push(("search path".to_string(), "[PYTHON SOURCES]".to_string()));
322 self
323 }
324
325 #[must_use]
328 pub fn with_filtered_python_names(mut self) -> Self {
329 for name in ["python", "pypy"] {
330 let suffix = if cfg!(windows) {
333 let exe_suffix = regex::escape(env::consts::EXE_SUFFIX);
337 format!(r"(\d\.\d+|\d)?{exe_suffix}")
338 } else {
339 if name == "python" {
341 r"(\d\.\d+|\d)?(t|d|td)?".to_string()
343 } else {
344 r"(\d\.\d+|\d)(t|d|td)?".to_string()
346 }
347 };
348
349 self.filters.push((
350 format!(r"[\\/]{name}{suffix}"),
353 format!("/[{}]", name.to_uppercase()),
354 ));
355 }
356
357 self
358 }
359
360 #[must_use]
363 pub fn with_filtered_virtualenv_bin(mut self) -> Self {
364 self.filters.push((
365 format!(
366 r"[\\/]{}[\\/]",
367 venv_bin_path(PathBuf::new()).to_string_lossy()
368 ),
369 "/[BIN]/".to_string(),
370 ));
371 self.filters.push((
372 format!(r"[\\/]{}", venv_bin_path(PathBuf::new()).to_string_lossy()),
373 "/[BIN]".to_string(),
374 ));
375 self
376 }
377
378 #[must_use]
382 pub fn with_filtered_python_install_bin(mut self) -> Self {
383 let suffix = if cfg!(windows) {
386 let exe_suffix = regex::escape(env::consts::EXE_SUFFIX);
387 format!(r"(\d\.\d+|\d)?{exe_suffix}")
389 } else {
390 r"\d\.\d+|\d".to_string()
392 };
393
394 if cfg!(unix) {
395 self.filters.push((
396 format!(r"[\\/]bin/python({suffix})"),
397 "/[INSTALL-BIN]/python$1".to_string(),
398 ));
399 self.filters.push((
400 format!(r"[\\/]bin/pypy({suffix})"),
401 "/[INSTALL-BIN]/pypy$1".to_string(),
402 ));
403 } else {
404 self.filters.push((
405 format!(r"[\\/]python({suffix})"),
406 "/[INSTALL-BIN]/python$1".to_string(),
407 ));
408 self.filters.push((
409 format!(r"[\\/]pypy({suffix})"),
410 "/[INSTALL-BIN]/pypy$1".to_string(),
411 ));
412 }
413 self
414 }
415
416 #[must_use]
422 pub fn with_pyvenv_cfg_filters(mut self) -> Self {
423 let added_filters = [
424 (r"home = .+".to_string(), "home = [PYTHON_HOME]".to_string()),
425 (
426 r"uv = \d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?(\+\d+)?".to_string(),
427 "uv = [UV_VERSION]".to_string(),
428 ),
429 (
430 r"extends-environment = .+".to_string(),
431 "extends-environment = [PARENT_VENV]".to_string(),
432 ),
433 ];
434 for filter in added_filters {
435 self.filters.insert(0, filter);
436 }
437 self
438 }
439
440 #[must_use]
443 pub fn with_filtered_python_symlinks(mut self) -> Self {
444 for (version, executable) in &self.python_versions {
445 if fs_err::symlink_metadata(executable).unwrap().is_symlink() {
446 self.filters.extend(
447 Self::path_patterns(executable.read_link().unwrap())
448 .into_iter()
449 .map(|pattern| (format! {" -> {pattern}"}, String::new())),
450 );
451 }
452 self.filters.push((
454 regex::escape(&format!(" -> [PYTHON-{version}]")),
455 String::new(),
456 ));
457 }
458 self
459 }
460
461 #[must_use]
463 pub fn with_filtered_path(mut self, path: &Path, name: &str) -> Self {
464 for pattern in Self::path_patterns(path)
468 .into_iter()
469 .map(|pattern| (pattern, format!("[{name}]/")))
470 {
471 self.filters.insert(0, pattern);
472 }
473 self
474 }
475
476 #[inline]
484 #[must_use]
485 pub fn with_filtered_link_mode_warning(mut self) -> Self {
486 let pattern = "warning: Failed to hardlink files; .*\n.*\n.*\n";
487 self.filters.push((pattern.to_string(), String::new()));
488 self
489 }
490
491 #[inline]
493 #[must_use]
494 pub fn with_filtered_not_executable(mut self) -> Self {
495 let pattern = if cfg!(unix) {
496 r"Permission denied \(os error 13\)"
497 } else {
498 r"\%1 is not a valid Win32 application. \(os error 193\)"
499 };
500 self.filters
501 .push((pattern.to_string(), "[PERMISSION DENIED]".to_string()));
502 self
503 }
504
505 #[must_use]
507 pub fn with_filtered_python_keys(mut self) -> Self {
508 let platform_re = r"(?x)
510 ( # We capture the group before the platform
511 (?:cpython|pypy|graalpy)# Python implementation
512 -
513 \d+\.\d+ # Major and minor version
514 (?: # The patch version is handled separately
515 \.
516 (?:
517 \[X\] # A previously filtered patch version [X]
518 | # OR
519 \[LATEST\] # A previously filtered latest patch version [LATEST]
520 | # OR
521 \d+ # An actual patch version
522 )
523 )? # (we allow the patch version to be missing entirely, e.g., in a request)
524 (?:(?:a|b|rc)[0-9]+)? # Pre-release version component, e.g., `a6` or `rc2`
525 (?:[td])? # A short variant, such as `t` (for freethreaded) or `d` (for debug)
526 (?:(\+[a-z]+)+)? # A long variant, such as `+freethreaded` or `+freethreaded+debug`
527 )
528 -
529 [a-z0-9]+ # Operating system (e.g., 'macos')
530 -
531 [a-z0-9_]+ # Architecture (e.g., 'aarch64')
532 -
533 [a-z]+ # Libc (e.g., 'none')
534";
535 self.filters
536 .push((platform_re.to_string(), "$1-[PLATFORM]".to_string()));
537 self
538 }
539
540 #[must_use]
542 pub fn with_filtered_latest_python_versions(mut self) -> Self {
543 for (minor, patch) in [
546 ("3.15", LATEST_PYTHON_3_15.strip_prefix("3.15.").unwrap()),
547 ("3.14", LATEST_PYTHON_3_14.strip_prefix("3.14.").unwrap()),
548 ("3.13", LATEST_PYTHON_3_13.strip_prefix("3.13.").unwrap()),
549 ("3.12", LATEST_PYTHON_3_12.strip_prefix("3.12.").unwrap()),
550 ("3.11", LATEST_PYTHON_3_11.strip_prefix("3.11.").unwrap()),
551 ("3.10", LATEST_PYTHON_3_10.strip_prefix("3.10.").unwrap()),
552 ] {
553 let pattern = format!(r"(\b){minor}\.{patch}(\b)");
555 let replacement = format!("${{1}}{minor}.[LATEST]${{2}}");
556 self.filters.push((pattern, replacement));
557 }
558 self
559 }
560
561 #[must_use]
563 pub fn with_filtered_windows_temp_dir(mut self) -> Self {
564 let pattern = regex::escape(
565 &self
566 .temp_dir
567 .simplified_display()
568 .to_string()
569 .replace('/', "\\"),
570 );
571 self.filters.push((pattern, "[TEMP_DIR]".to_string()));
572 self
573 }
574
575 #[must_use]
577 pub fn with_filtered_compiled_file_count(mut self) -> Self {
578 self.filters.push((
579 r"compiled \d+ files".to_string(),
580 "compiled [COUNT] files".to_string(),
581 ));
582 self
583 }
584
585 #[must_use]
587 pub fn with_filtered_current_version(mut self) -> Self {
588 self.filters.push((
589 regex::escape(&format!("v{}", env!("CARGO_PKG_VERSION"))),
590 "v[CURRENT_VERSION]".to_string(),
591 ));
592 self
593 }
594
595 #[must_use]
597 pub fn with_cyclonedx_filters(mut self) -> Self {
598 self.filters.push((
599 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(),
600 "[SERIAL_NUMBER]".to_string(),
601 ));
602 self.filters.push((
603 r#""timestamp": "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+Z""#
604 .to_string(),
605 r#""timestamp": "[TIMESTAMP]""#.to_string(),
606 ));
607 self.filters.push((
608 r#""name": "uv",\s*"version": "\d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?(\+\d+)?""#
609 .to_string(),
610 r#""name": "uv",
611 "version": "[VERSION]""#
612 .to_string(),
613 ));
614 self
615 }
616
617 #[must_use]
619 pub fn with_collapsed_whitespace(mut self) -> Self {
620 self.filters.push((r"[ \t]+".to_string(), " ".to_string()));
621 self
622 }
623
624 #[must_use]
626 pub fn with_python_download_cache(mut self) -> Self {
627 self.extra_env.push((
628 EnvVars::UV_PYTHON_CACHE_DIR.into(),
629 env::var_os(EnvVars::UV_PYTHON_CACHE_DIR).unwrap_or_else(|| {
631 uv_cache::Cache::from_settings(false, None)
632 .unwrap()
633 .bucket(CacheBucket::Python)
634 .into()
635 }),
636 ));
637 self
638 }
639
640 #[must_use]
641 pub fn with_empty_python_install_mirror(mut self) -> Self {
642 self.extra_env.push((
643 EnvVars::UV_PYTHON_INSTALL_MIRROR.into(),
644 String::new().into(),
645 ));
646 self
647 }
648
649 #[must_use]
651 pub fn with_managed_python_dirs(mut self) -> Self {
652 let managed = self.temp_dir.join("managed");
653
654 self.extra_env.push((
655 EnvVars::UV_PYTHON_BIN_DIR.into(),
656 self.bin_dir.as_os_str().to_owned(),
657 ));
658 self.extra_env
659 .push((EnvVars::UV_PYTHON_INSTALL_DIR.into(), managed.into()));
660 self.extra_env
661 .push((EnvVars::UV_PYTHON_DOWNLOADS.into(), "automatic".into()));
662
663 self
664 }
665
666 #[must_use]
667 pub fn with_versions_as_managed(mut self, versions: &[&str]) -> Self {
668 self.extra_env.push((
669 EnvVars::UV_INTERNAL__TEST_PYTHON_MANAGED.into(),
670 versions.iter().join(" ").into(),
671 ));
672
673 self
674 }
675
676 #[must_use]
678 pub fn with_filter(mut self, filter: (impl Into<String>, impl Into<String>)) -> Self {
679 self.filters.push((filter.0.into(), filter.1.into()));
680 self
681 }
682
683 #[must_use]
685 pub fn with_unset_git_credential_helper(self) -> Self {
686 let git_config = self.home_dir.child(".gitconfig");
687 git_config
688 .write_str(indoc! {r"
689 [credential]
690 helper =
691 "})
692 .expect("Failed to unset git credential helper");
693
694 self
695 }
696
697 #[must_use]
699 pub fn clear_filters(mut self) -> Self {
700 self.filters.clear();
701 self
702 }
703
704 pub fn with_cache_on_cow_fs(self) -> anyhow::Result<Option<Self>> {
709 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_COW_FS).ok() else {
710 return Ok(None);
711 };
712 self.with_cache_on_fs(&dir, "COW_FS").map(Some)
713 }
714
715 pub fn with_cache_on_alt_fs(self) -> anyhow::Result<Option<Self>> {
720 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_ALT_FS).ok() else {
721 return Ok(None);
722 };
723 self.with_cache_on_fs(&dir, "ALT_FS").map(Some)
724 }
725
726 pub fn with_cache_on_lowlinks_fs(self) -> anyhow::Result<Option<Self>> {
731 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_LOWLINKS_FS).ok() else {
732 return Ok(None);
733 };
734 self.with_cache_on_fs(&dir, "LOWLINKS_FS").map(Some)
735 }
736
737 pub fn with_cache_on_nocow_fs(self) -> anyhow::Result<Option<Self>> {
742 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_NOCOW_FS).ok() else {
743 return Ok(None);
744 };
745 self.with_cache_on_fs(&dir, "NOCOW_FS").map(Some)
746 }
747
748 pub fn with_working_dir_on_cow_fs(self) -> anyhow::Result<Option<Self>> {
755 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_COW_FS).ok() else {
756 return Ok(None);
757 };
758 self.with_working_dir_on_fs(&dir, "COW_FS").map(Some)
759 }
760
761 pub fn with_working_dir_on_alt_fs(self) -> anyhow::Result<Option<Self>> {
768 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_ALT_FS).ok() else {
769 return Ok(None);
770 };
771 self.with_working_dir_on_fs(&dir, "ALT_FS").map(Some)
772 }
773
774 pub fn with_working_dir_on_nocow_fs(self) -> anyhow::Result<Option<Self>> {
781 let Some(dir) = env::var(EnvVars::UV_INTERNAL__TEST_NOCOW_FS).ok() else {
782 return Ok(None);
783 };
784 self.with_working_dir_on_fs(&dir, "NOCOW_FS").map(Some)
785 }
786
787 fn with_cache_on_fs(mut self, dir: &str, name: &str) -> anyhow::Result<Self> {
788 fs_err::create_dir_all(dir)?;
789 let tmp = tempfile::TempDir::new_in(dir)?;
790 self.cache_dir = ChildPath::new(tmp.path()).child("cache");
791 fs_err::create_dir_all(&self.cache_dir)?;
792 let replacement = format!("[{name}]/[CACHE_DIR]/");
793 self.filters.extend(
794 Self::path_patterns(&self.cache_dir)
795 .into_iter()
796 .map(|pattern| (pattern, replacement.clone())),
797 );
798 self._extra_tempdirs.push(tmp);
799 Ok(self)
800 }
801
802 fn with_working_dir_on_fs(mut self, dir: &str, name: &str) -> anyhow::Result<Self> {
803 fs_err::create_dir_all(dir)?;
804 let tmp = tempfile::TempDir::new_in(dir)?;
805 self.temp_dir = ChildPath::new(tmp.path()).child("temp");
806 fs_err::create_dir_all(&self.temp_dir)?;
807 let canonical_temp_dir = self.temp_dir.canonicalize()?;
810 self.venv = ChildPath::new(canonical_temp_dir.join(".venv"));
811 let temp_replacement = format!("[{name}]/[TEMP_DIR]/");
812 self.filters.extend(
813 Self::path_patterns(&self.temp_dir)
814 .into_iter()
815 .map(|pattern| (pattern, temp_replacement.clone())),
816 );
817 let venv_replacement = format!("[{name}]/[VENV]/");
818 self.filters.extend(
819 Self::path_patterns(&self.venv)
820 .into_iter()
821 .map(|pattern| (pattern, venv_replacement.clone())),
822 );
823 self._extra_tempdirs.push(tmp);
824 Ok(self)
825 }
826
827 pub fn test_bucket_dir() -> PathBuf {
836 std::env::temp_dir()
837 .simple_canonicalize()
838 .expect("failed to canonicalize temp dir")
839 .join("uv")
840 .join("tests")
841 }
842
843 pub fn new_with_versions_and_bin(python_versions: &[&str], uv_bin: PathBuf) -> Self {
850 let bucket = Self::test_bucket_dir();
851 fs_err::create_dir_all(&bucket).expect("Failed to create test bucket");
852
853 let root = tempfile::TempDir::new_in(bucket).expect("Failed to create test root directory");
854
855 fs_err::create_dir_all(root.path().join(".git"))
858 .expect("Failed to create `.git` placeholder in test root directory");
859
860 let temp_dir = ChildPath::new(root.path()).child("temp");
861 fs_err::create_dir_all(&temp_dir).expect("Failed to create test working directory");
862
863 let cache_dir = ChildPath::new(root.path()).child("cache");
864 fs_err::create_dir_all(&cache_dir).expect("Failed to create test cache directory");
865
866 let python_dir = ChildPath::new(root.path()).child("python");
867 fs_err::create_dir_all(&python_dir).expect("Failed to create test Python directory");
868
869 let bin_dir = ChildPath::new(root.path()).child("bin");
870 fs_err::create_dir_all(&bin_dir).expect("Failed to create test bin directory");
871
872 if cfg!(not(feature = "git")) {
874 Self::disallow_git_cli(&bin_dir).expect("Failed to setup disallowed `git` command");
875 }
876
877 let home_dir = ChildPath::new(root.path()).child("home");
878 fs_err::create_dir_all(&home_dir).expect("Failed to create test home directory");
879
880 let user_config_dir = if cfg!(windows) {
881 ChildPath::new(home_dir.path())
882 } else {
883 ChildPath::new(home_dir.path()).child(".config")
884 };
885
886 let canonical_temp_dir = temp_dir.canonicalize().unwrap();
888 let venv = ChildPath::new(canonical_temp_dir.join(".venv"));
889
890 let python_version = python_versions
891 .first()
892 .map(|version| PythonVersion::from_str(version).unwrap());
893
894 let site_packages = python_version
895 .as_ref()
896 .map(|version| site_packages_path(&venv, &format!("python{version}")));
897
898 let workspace_root = Path::new(&env::var(EnvVars::CARGO_MANIFEST_DIR).unwrap())
901 .parent()
902 .expect("CARGO_MANIFEST_DIR should be nested in workspace")
903 .parent()
904 .expect("CARGO_MANIFEST_DIR should be doubly nested in workspace")
905 .to_path_buf();
906
907 let download_list = ManagedPythonDownloadList::new_only_embedded().unwrap();
908
909 let python_versions: Vec<_> = python_versions
910 .iter()
911 .map(|version| PythonVersion::from_str(version).unwrap())
912 .zip(
913 python_installations_for_versions(&temp_dir, python_versions, &download_list)
914 .expect("Failed to find test Python versions"),
915 )
916 .collect();
917
918 if cfg!(unix) {
921 for (version, executable) in &python_versions {
922 let parent = python_dir.child(version.to_string());
923 parent.create_dir_all().unwrap();
924 parent.child("python3").symlink_to_file(executable).unwrap();
925 }
926 }
927
928 let mut filters = Vec::new();
929
930 filters.extend(
931 Self::path_patterns(&uv_bin)
932 .into_iter()
933 .map(|pattern| (pattern, "[UV]".to_string())),
934 );
935
936 if cfg!(windows) {
938 filters.push((" --link-mode <LINK_MODE>".to_string(), String::new()));
939 filters.push((r#"link-mode = "copy"\n"#.to_string(), String::new()));
940 filters.push((r"exit code: ".to_string(), "exit status: ".to_string()));
942 }
943
944 for (version, executable) in &python_versions {
945 filters.extend(
947 Self::path_patterns(executable)
948 .into_iter()
949 .map(|pattern| (pattern, format!("[PYTHON-{version}]"))),
950 );
951
952 filters.extend(
954 Self::path_patterns(python_dir.join(version.to_string()))
955 .into_iter()
956 .map(|pattern| {
957 (
958 format!("{pattern}[a-zA-Z0-9]*"),
959 format!("[PYTHON-{version}]"),
960 )
961 }),
962 );
963
964 if version.patch().is_none() {
967 filters.push((
968 format!(r"({})\.\d+", regex::escape(version.to_string().as_str())),
969 "$1.[X]".to_string(),
970 ));
971 }
972 }
973
974 filters.extend(
975 Self::path_patterns(&bin_dir)
976 .into_iter()
977 .map(|pattern| (pattern, "[BIN]/".to_string())),
978 );
979 filters.extend(
980 Self::path_patterns(&cache_dir)
981 .into_iter()
982 .map(|pattern| (pattern, "[CACHE_DIR]/".to_string())),
983 );
984 if let Some(ref site_packages) = site_packages {
985 filters.extend(
986 Self::path_patterns(site_packages)
987 .into_iter()
988 .map(|pattern| (pattern, "[SITE_PACKAGES]/".to_string())),
989 );
990 }
991 filters.extend(
992 Self::path_patterns(&venv)
993 .into_iter()
994 .map(|pattern| (pattern, "[VENV]/".to_string())),
995 );
996
997 if let Some(site_packages) = site_packages {
999 filters.push((
1000 Self::path_pattern(
1001 site_packages
1002 .strip_prefix(&canonical_temp_dir)
1003 .expect("The test site-packages directory is always in the tempdir"),
1004 ),
1005 "[SITE_PACKAGES]/".to_string(),
1006 ));
1007 }
1008
1009 filters.push((
1011 r"[\\/]lib[\\/]python\d+\.\d+[\\/]".to_string(),
1012 "/[PYTHON-LIB]/".to_string(),
1013 ));
1014 filters.push((r"[\\/]Lib[\\/]".to_string(), "/[PYTHON-LIB]/".to_string()));
1015
1016 filters.extend(
1017 Self::path_patterns(&temp_dir)
1018 .into_iter()
1019 .map(|pattern| (pattern, "[TEMP_DIR]/".to_string())),
1020 );
1021 filters.extend(
1022 Self::path_patterns(&python_dir)
1023 .into_iter()
1024 .map(|pattern| (pattern, "[PYTHON_DIR]/".to_string())),
1025 );
1026 let mut uv_user_config_dir = PathBuf::from(user_config_dir.path());
1027 uv_user_config_dir.push("uv");
1028 filters.extend(
1029 Self::path_patterns(&uv_user_config_dir)
1030 .into_iter()
1031 .map(|pattern| (pattern, "[UV_USER_CONFIG_DIR]/".to_string())),
1032 );
1033 filters.extend(
1034 Self::path_patterns(&user_config_dir)
1035 .into_iter()
1036 .map(|pattern| (pattern, "[USER_CONFIG_DIR]/".to_string())),
1037 );
1038 filters.extend(
1039 Self::path_patterns(&home_dir)
1040 .into_iter()
1041 .map(|pattern| (pattern, "[HOME]/".to_string())),
1042 );
1043 filters.extend(
1044 Self::path_patterns(&workspace_root)
1045 .into_iter()
1046 .map(|pattern| (pattern, "[WORKSPACE]/".to_string())),
1047 );
1048
1049 filters.push((
1051 r"Activate with: (.*)\\Scripts\\activate".to_string(),
1052 "Activate with: source $1/[BIN]/activate".to_string(),
1053 ));
1054 filters.push((
1055 r"Activate with: Scripts\\activate".to_string(),
1056 "Activate with: source [BIN]/activate".to_string(),
1057 ));
1058 filters.push((
1059 r"Activate with: source (.*/|)bin/activate(?:\.\w+)?".to_string(),
1060 "Activate with: source $1[BIN]/activate".to_string(),
1061 ));
1062
1063 filters.push((r"(\\|\/)\.tmp.*(\\|\/)".to_string(), "/[TMP]/".to_string()));
1066
1067 filters.push((r"file:///".to_string(), "file://".to_string()));
1069
1070 filters.push((r"\\\\\?\\".to_string(), String::new()));
1072
1073 filters.push((
1076 format!("https://astral-sh.github.io/packse/{PACKSE_VERSION}"),
1077 "https://astral-sh.github.io/packse/PACKSE_VERSION".to_string(),
1078 ));
1079 if let Ok(packse_test_index) = env::var(EnvVars::UV_TEST_PACKSE_INDEX) {
1081 filters.push((
1082 packse_test_index.trim_end_matches('/').to_string(),
1083 "https://astral-sh.github.io/packse/PACKSE_VERSION".to_string(),
1084 ));
1085 }
1086 filters.push((r"127\.0\.0\.1:\d*".to_string(), "[LOCALHOST]".to_string()));
1088 filters.push((
1090 format!(
1091 r#"requires = \["uv_build>={},<[0-9.]+"\]"#,
1092 uv_version::version()
1093 ),
1094 r#"requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]"#.to_string(),
1095 ));
1096 filters.push((
1098 r"environments-v(\d+)[\\/](\w+)-[a-z0-9]+".to_string(),
1099 "environments-v$1/$2-[HASH]".to_string(),
1100 ));
1101 filters.push((
1103 r"archive-v(\d+)[\\/][A-Za-z0-9\-\_]+".to_string(),
1104 "archive-v$1/[HASH]".to_string(),
1105 ));
1106
1107 Self {
1108 root: ChildPath::new(root.path()),
1109 temp_dir,
1110 cache_dir,
1111 python_dir,
1112 home_dir,
1113 user_config_dir,
1114 bin_dir,
1115 venv,
1116 workspace_root,
1117 python_version,
1118 python_versions,
1119 uv_bin,
1120 filters,
1121 extra_env: vec![],
1122 _root: root,
1123 _extra_tempdirs: vec![],
1124 }
1125 }
1126
1127 pub fn command(&self) -> Command {
1129 let mut command = self.new_command();
1130 self.add_shared_options(&mut command, true);
1131 command
1132 }
1133
1134 pub fn disallow_git_cli(bin_dir: &Path) -> std::io::Result<()> {
1135 let contents = r"#!/bin/sh
1136 echo 'error: `git` operations are not allowed — are you missing a cfg for the `git` feature?' >&2
1137 exit 127";
1138 let git = bin_dir.join(format!("git{}", env::consts::EXE_SUFFIX));
1139 fs_err::write(&git, contents)?;
1140
1141 #[cfg(unix)]
1142 {
1143 use std::os::unix::fs::PermissionsExt;
1144 let mut perms = fs_err::metadata(&git)?.permissions();
1145 perms.set_mode(0o755);
1146 fs_err::set_permissions(&git, perms)?;
1147 }
1148
1149 Ok(())
1150 }
1151
1152 #[must_use]
1157 pub fn with_git_lfs_config(mut self) -> Self {
1158 let git_lfs_config = self.root.child(".gitconfig");
1159 git_lfs_config
1160 .write_str(indoc! {r#"
1161 [filter "lfs"]
1162 clean = git-lfs clean -- %f
1163 smudge = git-lfs smudge -- %f
1164 process = git-lfs filter-process
1165 required = true
1166 "#})
1167 .expect("Failed to setup `git-lfs` filters");
1168
1169 self.extra_env.push((
1172 EnvVars::GIT_CONFIG_GLOBAL.into(),
1173 git_lfs_config.as_os_str().into(),
1174 ));
1175 self
1176 }
1177
1178 pub fn add_shared_options(&self, command: &mut Command, activate_venv: bool) {
1189 self.add_shared_args(command);
1190 self.add_shared_env(command, activate_venv);
1191 }
1192
1193 pub fn add_shared_args(&self, command: &mut Command) {
1195 command.arg("--cache-dir").arg(self.cache_dir.path());
1196 }
1197
1198 pub fn add_shared_env(&self, command: &mut Command, activate_venv: bool) {
1200 let path = env::join_paths(std::iter::once(self.bin_dir.to_path_buf()).chain(
1202 env::split_paths(&env::var(EnvVars::PATH).unwrap_or_default()),
1203 ))
1204 .unwrap();
1205
1206 if cfg!(not(windows)) {
1209 command.env(EnvVars::SHELL, "bash");
1210 }
1211
1212 command
1213 .env_remove(EnvVars::VIRTUAL_ENV)
1215 .env(EnvVars::UV_NO_WRAP, "1")
1217 .env(EnvVars::COLUMNS, "100")
1220 .env(EnvVars::PATH, path)
1221 .env(EnvVars::HOME, self.home_dir.as_os_str())
1222 .env(EnvVars::APPDATA, self.home_dir.as_os_str())
1223 .env(EnvVars::USERPROFILE, self.home_dir.as_os_str())
1224 .env(
1225 EnvVars::XDG_CONFIG_DIRS,
1226 self.home_dir.join("config").as_os_str(),
1227 )
1228 .env(
1229 EnvVars::XDG_DATA_HOME,
1230 self.home_dir.join("data").as_os_str(),
1231 )
1232 .env(EnvVars::UV_PYTHON_INSTALL_DIR, "")
1233 .env(EnvVars::UV_PYTHON_DOWNLOADS, "never")
1235 .env(EnvVars::UV_PYTHON_SEARCH_PATH, self.python_path())
1236 .env(EnvVars::UV_EXCLUDE_NEWER, TEST_TIMESTAMP)
1237 .env(EnvVars::UV_TEST_CURRENT_TIMESTAMP, TEST_TIMESTAMP)
1238 .env(EnvVars::UV_TEST_AVAILABLE_VERSION_CUTOFF, TEST_TIMESTAMP)
1239 .env(EnvVars::UV_PYTHON_NO_REGISTRY, "1")
1242 .env(EnvVars::UV_PYTHON_INSTALL_REGISTRY, "0")
1243 .env(EnvVars::UV_TEST_NO_CLI_PROGRESS, "1")
1246 .env(EnvVars::GIT_CEILING_DIRECTORIES, self.root.path())
1260 .current_dir(self.temp_dir.path());
1261
1262 for (key, value) in &self.extra_env {
1263 command.env(key, value);
1264 }
1265
1266 if activate_venv {
1267 command.env(EnvVars::VIRTUAL_ENV, self.venv.as_os_str());
1268 }
1269
1270 if cfg!(unix) {
1271 command.env(EnvVars::LC_ALL, "C");
1273 }
1274 }
1275
1276 pub fn pip_compile(&self) -> Command {
1278 let mut command = self.new_command();
1279 command.arg("pip").arg("compile");
1280 self.add_shared_options(&mut command, true);
1281 command
1282 }
1283
1284 pub fn pip_sync(&self) -> Command {
1286 let mut command = self.new_command();
1287 command.arg("pip").arg("sync");
1288 self.add_shared_options(&mut command, true);
1289 command
1290 }
1291
1292 pub fn pip_show(&self) -> Command {
1293 let mut command = self.new_command();
1294 command.arg("pip").arg("show");
1295 self.add_shared_options(&mut command, true);
1296 command
1297 }
1298
1299 pub fn pip_freeze(&self) -> Command {
1301 let mut command = self.new_command();
1302 command.arg("pip").arg("freeze");
1303 self.add_shared_options(&mut command, true);
1304 command
1305 }
1306
1307 pub fn pip_check(&self) -> Command {
1309 let mut command = self.new_command();
1310 command.arg("pip").arg("check");
1311 self.add_shared_options(&mut command, true);
1312 command
1313 }
1314
1315 pub fn pip_list(&self) -> Command {
1316 let mut command = self.new_command();
1317 command.arg("pip").arg("list");
1318 self.add_shared_options(&mut command, true);
1319 command
1320 }
1321
1322 pub fn venv(&self) -> Command {
1324 let mut command = self.new_command();
1325 command.arg("venv");
1326 self.add_shared_options(&mut command, false);
1327 command
1328 }
1329
1330 pub fn pip_install(&self) -> Command {
1332 let mut command = self.new_command();
1333 command.arg("pip").arg("install");
1334 self.add_shared_options(&mut command, true);
1335 command
1336 }
1337
1338 pub fn pip_uninstall(&self) -> Command {
1340 let mut command = self.new_command();
1341 command.arg("pip").arg("uninstall");
1342 self.add_shared_options(&mut command, true);
1343 command
1344 }
1345
1346 pub fn pip_tree(&self) -> Command {
1348 let mut command = self.new_command();
1349 command.arg("pip").arg("tree");
1350 self.add_shared_options(&mut command, true);
1351 command
1352 }
1353
1354 pub fn pip_debug(&self) -> Command {
1356 let mut command = self.new_command();
1357 command.arg("pip").arg("debug");
1358 self.add_shared_options(&mut command, true);
1359 command
1360 }
1361
1362 pub fn help(&self) -> Command {
1364 let mut command = self.new_command();
1365 command.arg("help");
1366 self.add_shared_env(&mut command, false);
1367 command
1368 }
1369
1370 pub fn init(&self) -> Command {
1373 let mut command = self.new_command();
1374 command.arg("init");
1375 self.add_shared_options(&mut command, false);
1376 command
1377 }
1378
1379 pub fn sync(&self) -> Command {
1381 let mut command = self.new_command();
1382 command.arg("sync");
1383 self.add_shared_options(&mut command, false);
1384 command
1385 }
1386
1387 pub fn lock(&self) -> Command {
1389 let mut command = self.new_command();
1390 command.arg("lock");
1391 self.add_shared_options(&mut command, false);
1392 command
1393 }
1394
1395 pub fn audit(&self) -> Command {
1397 let mut command = self.new_command();
1398 command.arg("audit");
1399 self.add_shared_options(&mut command, false);
1400 command
1401 }
1402
1403 pub fn workspace_metadata(&self) -> Command {
1405 let mut command = self.new_command();
1406 command.arg("workspace").arg("metadata");
1407 self.add_shared_options(&mut command, false);
1408 command
1409 }
1410
1411 pub fn workspace_dir(&self) -> Command {
1413 let mut command = self.new_command();
1414 command.arg("workspace").arg("dir");
1415 self.add_shared_options(&mut command, false);
1416 command
1417 }
1418
1419 pub fn workspace_list(&self) -> Command {
1421 let mut command = self.new_command();
1422 command.arg("workspace").arg("list");
1423 self.add_shared_options(&mut command, false);
1424 command
1425 }
1426
1427 pub fn export(&self) -> Command {
1429 let mut command = self.new_command();
1430 command.arg("export");
1431 self.add_shared_options(&mut command, false);
1432 command
1433 }
1434
1435 pub fn format(&self) -> Command {
1437 let mut command = self.new_command();
1438 command.arg("format");
1439 self.add_shared_options(&mut command, false);
1440 command.env(EnvVars::UV_EXCLUDE_NEWER, "2026-02-15T00:00:00Z");
1442 command
1443 }
1444
1445 pub fn build(&self) -> Command {
1447 let mut command = self.new_command();
1448 command.arg("build");
1449 self.add_shared_options(&mut command, false);
1450 command
1451 }
1452
1453 pub fn version(&self) -> Command {
1454 let mut command = self.new_command();
1455 command.arg("version");
1456 self.add_shared_options(&mut command, false);
1457 command
1458 }
1459
1460 pub fn self_version(&self) -> Command {
1461 let mut command = self.new_command();
1462 command.arg("self").arg("version");
1463 self.add_shared_options(&mut command, false);
1464 command
1465 }
1466
1467 pub fn self_update(&self) -> Command {
1468 let mut command = self.new_command();
1469 command.arg("self").arg("update");
1470 self.add_shared_options(&mut command, false);
1471 command
1472 }
1473
1474 pub fn publish(&self) -> Command {
1476 let mut command = self.new_command();
1477 command.arg("publish");
1478 self.add_shared_options(&mut command, false);
1479 command
1480 }
1481
1482 pub fn python_find(&self) -> Command {
1484 let mut command = self.new_command();
1485 command
1486 .arg("python")
1487 .arg("find")
1488 .env(EnvVars::UV_PREVIEW, "1")
1489 .env(EnvVars::UV_PYTHON_INSTALL_DIR, "");
1490 self.add_shared_options(&mut command, false);
1491 command
1492 }
1493
1494 pub fn python_list(&self) -> Command {
1496 let mut command = self.new_command();
1497 command
1498 .arg("python")
1499 .arg("list")
1500 .env(EnvVars::UV_PYTHON_INSTALL_DIR, "");
1501 self.add_shared_options(&mut command, false);
1502 command
1503 }
1504
1505 pub fn python_install(&self) -> Command {
1507 let mut command = self.new_command();
1508 command.arg("python").arg("install");
1509 self.add_shared_options(&mut command, true);
1510 command
1511 }
1512
1513 pub fn python_uninstall(&self) -> Command {
1515 let mut command = self.new_command();
1516 command.arg("python").arg("uninstall");
1517 self.add_shared_options(&mut command, true);
1518 command
1519 }
1520
1521 pub fn python_upgrade(&self) -> Command {
1523 let mut command = self.new_command();
1524 command.arg("python").arg("upgrade");
1525 self.add_shared_options(&mut command, true);
1526 command
1527 }
1528
1529 pub fn python_pin(&self) -> Command {
1531 let mut command = self.new_command();
1532 command.arg("python").arg("pin");
1533 self.add_shared_options(&mut command, true);
1534 command
1535 }
1536
1537 pub fn python_dir(&self) -> Command {
1539 let mut command = self.new_command();
1540 command.arg("python").arg("dir");
1541 self.add_shared_options(&mut command, true);
1542 command
1543 }
1544
1545 pub fn run(&self) -> Command {
1547 let mut command = self.new_command();
1548 command.arg("run").env(EnvVars::UV_SHOW_RESOLUTION, "1");
1549 self.add_shared_options(&mut command, true);
1550 command
1551 }
1552
1553 pub fn tool_run(&self) -> Command {
1555 let mut command = self.new_command();
1556 command
1557 .arg("tool")
1558 .arg("run")
1559 .env(EnvVars::UV_SHOW_RESOLUTION, "1");
1560 self.add_shared_options(&mut command, false);
1561 command
1562 }
1563
1564 pub fn tool_upgrade(&self) -> Command {
1566 let mut command = self.new_command();
1567 command.arg("tool").arg("upgrade");
1568 self.add_shared_options(&mut command, false);
1569 command
1570 }
1571
1572 pub fn tool_install(&self) -> Command {
1574 let mut command = self.new_command();
1575 command.arg("tool").arg("install");
1576 self.add_shared_options(&mut command, false);
1577 command
1578 }
1579
1580 pub fn tool_list(&self) -> Command {
1582 let mut command = self.new_command();
1583 command.arg("tool").arg("list");
1584 self.add_shared_options(&mut command, false);
1585 command
1586 }
1587
1588 pub fn tool_dir(&self) -> Command {
1590 let mut command = self.new_command();
1591 command.arg("tool").arg("dir");
1592 self.add_shared_options(&mut command, false);
1593 command
1594 }
1595
1596 pub fn tool_uninstall(&self) -> Command {
1598 let mut command = self.new_command();
1599 command.arg("tool").arg("uninstall");
1600 self.add_shared_options(&mut command, false);
1601 command
1602 }
1603
1604 pub fn add(&self) -> Command {
1606 let mut command = self.new_command();
1607 command.arg("add");
1608 self.add_shared_options(&mut command, false);
1609 command
1610 }
1611
1612 pub fn remove(&self) -> Command {
1614 let mut command = self.new_command();
1615 command.arg("remove");
1616 self.add_shared_options(&mut command, false);
1617 command
1618 }
1619
1620 pub fn tree(&self) -> Command {
1622 let mut command = self.new_command();
1623 command.arg("tree");
1624 self.add_shared_options(&mut command, false);
1625 command
1626 }
1627
1628 pub fn clean(&self) -> Command {
1630 let mut command = self.new_command();
1631 command.arg("cache").arg("clean");
1632 self.add_shared_options(&mut command, false);
1633 command
1634 }
1635
1636 pub fn prune(&self) -> Command {
1638 let mut command = self.new_command();
1639 command.arg("cache").arg("prune");
1640 self.add_shared_options(&mut command, false);
1641 command
1642 }
1643
1644 pub fn cache_size(&self) -> Command {
1646 let mut command = self.new_command();
1647 command.arg("cache").arg("size");
1648 self.add_shared_options(&mut command, false);
1649 command
1650 }
1651
1652 pub fn build_backend(&self) -> Command {
1656 let mut command = self.new_command();
1657 command.arg("build-backend");
1658 self.add_shared_options(&mut command, false);
1659 command
1660 }
1661
1662 pub fn interpreter(&self) -> PathBuf {
1666 let venv = &self.venv;
1667 if cfg!(unix) {
1668 venv.join("bin").join("python")
1669 } else if cfg!(windows) {
1670 venv.join("Scripts").join("python.exe")
1671 } else {
1672 unimplemented!("Only Windows and Unix are supported")
1673 }
1674 }
1675
1676 pub fn python_command(&self) -> Command {
1677 let mut interpreter = self.interpreter();
1678
1679 if !interpreter.exists() {
1681 interpreter.clone_from(
1682 &self
1683 .python_versions
1684 .first()
1685 .expect("At least one Python version is required")
1686 .1,
1687 );
1688 }
1689
1690 let mut command = Self::new_command_with(&interpreter);
1691 command
1692 .arg("-B")
1695 .env(EnvVars::PYTHONUTF8, "1");
1697
1698 self.add_shared_env(&mut command, false);
1699
1700 command
1701 }
1702
1703 pub fn auth_login(&self) -> Command {
1705 let mut command = self.new_command();
1706 command.arg("auth").arg("login");
1707 self.add_shared_options(&mut command, false);
1708 command
1709 }
1710
1711 pub fn auth_logout(&self) -> Command {
1713 let mut command = self.new_command();
1714 command.arg("auth").arg("logout");
1715 self.add_shared_options(&mut command, false);
1716 command
1717 }
1718
1719 pub fn auth_helper(&self) -> Command {
1721 let mut command = self.new_command();
1722 command.arg("auth").arg("helper");
1723 self.add_shared_options(&mut command, false);
1724 command
1725 }
1726
1727 pub fn auth_token(&self) -> Command {
1729 let mut command = self.new_command();
1730 command.arg("auth").arg("token");
1731 self.add_shared_options(&mut command, false);
1732 command
1733 }
1734
1735 #[must_use]
1739 pub fn with_real_home(mut self) -> Self {
1740 if let Some(home) = env::var_os(EnvVars::HOME) {
1741 self.extra_env
1742 .push((EnvVars::HOME.to_string().into(), home));
1743 }
1744 self.extra_env.push((
1747 EnvVars::XDG_CONFIG_HOME.into(),
1748 self.user_config_dir.as_os_str().into(),
1749 ));
1750 self
1751 }
1752
1753 pub fn assert_command(&self, command: &str) -> Assert {
1755 self.python_command()
1756 .arg("-c")
1757 .arg(command)
1758 .current_dir(&self.temp_dir)
1759 .assert()
1760 }
1761
1762 pub fn assert_file(&self, file: impl AsRef<Path>) -> Assert {
1764 self.python_command()
1765 .arg(file.as_ref())
1766 .current_dir(&self.temp_dir)
1767 .assert()
1768 }
1769
1770 pub fn assert_installed(&self, package: &'static str, version: &'static str) {
1772 self.assert_command(
1773 format!("import {package} as package; print(package.__version__, end='')").as_str(),
1774 )
1775 .success()
1776 .stdout(version);
1777 }
1778
1779 pub fn assert_not_installed(&self, package: &'static str) {
1781 self.assert_command(format!("import {package}").as_str())
1782 .failure();
1783 }
1784
1785 pub fn path_patterns(path: impl AsRef<Path>) -> Vec<String> {
1787 let mut patterns = Vec::new();
1788
1789 if path.as_ref().exists() {
1791 patterns.push(Self::path_pattern(
1792 path.as_ref()
1793 .canonicalize()
1794 .expect("Failed to create canonical path"),
1795 ));
1796 }
1797
1798 patterns.push(Self::path_pattern(path));
1800
1801 patterns
1802 }
1803
1804 fn path_pattern(path: impl AsRef<Path>) -> String {
1806 format!(
1807 r"{}\\?/?",
1809 regex::escape(&path.as_ref().simplified_display().to_string())
1810 .replace(r"\\", r"(\\|\/)")
1813 )
1814 }
1815
1816 pub fn python_path(&self) -> OsString {
1817 if cfg!(unix) {
1818 env::join_paths(
1820 self.python_versions
1821 .iter()
1822 .map(|(version, _)| self.python_dir.join(version.to_string())),
1823 )
1824 .unwrap()
1825 } else {
1826 env::join_paths(
1828 self.python_versions
1829 .iter()
1830 .map(|(_, executable)| executable.parent().unwrap().to_path_buf()),
1831 )
1832 .unwrap()
1833 }
1834 }
1835
1836 pub fn filters(&self) -> Vec<(&str, &str)> {
1838 self.filters
1841 .iter()
1842 .map(|(p, r)| (p.as_str(), r.as_str()))
1843 .chain(INSTA_FILTERS.iter().copied())
1844 .collect()
1845 }
1846
1847 pub fn filters_without_standard_filters(&self) -> Vec<(&str, &str)> {
1849 self.filters
1850 .iter()
1851 .map(|(p, r)| (p.as_str(), r.as_str()))
1852 .collect()
1853 }
1854
1855 pub fn python_kind(&self) -> &'static str {
1857 "python"
1858 }
1859
1860 pub fn site_packages(&self) -> PathBuf {
1862 site_packages_path(
1863 &self.venv,
1864 &format!(
1865 "{}{}",
1866 self.python_kind(),
1867 self.python_version.as_ref().expect(
1868 "A Python version must be provided to retrieve the test site packages path"
1869 )
1870 ),
1871 )
1872 }
1873
1874 pub fn reset_venv(&self) {
1876 self.create_venv();
1877 }
1878
1879 fn create_venv(&self) {
1881 let executable = get_python(
1882 self.python_version
1883 .as_ref()
1884 .expect("A Python version must be provided to create a test virtual environment"),
1885 );
1886 create_venv_from_executable(&self.venv, &self.cache_dir, &executable, &self.uv_bin);
1887 }
1888
1889 pub fn copy_ecosystem_project(&self, name: &str) {
1900 let project_dir = PathBuf::from(format!("../../test/ecosystem/{name}"));
1901 self.temp_dir.copy_from(project_dir, &["*"]).unwrap();
1902 if let Err(err) = fs_err::remove_file(self.temp_dir.join("uv.lock")) {
1904 assert_eq!(
1905 err.kind(),
1906 io::ErrorKind::NotFound,
1907 "Failed to remove uv.lock: {err}"
1908 );
1909 }
1910 }
1911
1912 pub fn diff_lock(&self, change: impl Fn(&Self) -> Command) -> String {
1921 static TRIM_TRAILING_WHITESPACE: std::sync::LazyLock<Regex> =
1922 std::sync::LazyLock::new(|| Regex::new(r"(?m)^\s+$").unwrap());
1923
1924 let lock_path = ChildPath::new(self.temp_dir.join("uv.lock"));
1925 let old_lock = fs_err::read_to_string(&lock_path).unwrap();
1926 let (snapshot, _, status) = run_and_format_with_status(
1927 change(self),
1928 self.filters(),
1929 "diff_lock",
1930 Some(WindowsFilters::Platform),
1931 None,
1932 );
1933 assert!(status.success(), "{snapshot}");
1934 let new_lock = fs_err::read_to_string(&lock_path).unwrap();
1935 diff_snapshot(&old_lock, &new_lock)
1936 }
1937
1938 pub fn read(&self, file: impl AsRef<Path>) -> String {
1940 fs_err::read_to_string(self.temp_dir.join(&file))
1941 .unwrap_or_else(|_| panic!("Missing file: `{}`", file.user_display()))
1942 }
1943
1944 fn new_command(&self) -> Command {
1947 Self::new_command_with(&self.uv_bin)
1948 }
1949
1950 fn new_command_with(bin: &Path) -> Command {
1956 let mut command = Command::new(bin);
1957
1958 let passthrough = [
1959 EnvVars::PATH,
1961 EnvVars::RUST_LOG,
1963 EnvVars::RUST_BACKTRACE,
1964 EnvVars::SYSTEMDRIVE,
1966 EnvVars::RUST_MIN_STACK,
1968 EnvVars::UV_STACK_SIZE,
1969 EnvVars::ALL_PROXY,
1971 EnvVars::HTTPS_PROXY,
1972 EnvVars::HTTP_PROXY,
1973 EnvVars::NO_PROXY,
1974 EnvVars::SSL_CERT_DIR,
1975 EnvVars::SSL_CERT_FILE,
1976 EnvVars::UV_NATIVE_TLS,
1977 EnvVars::UV_SYSTEM_CERTS,
1978 ];
1979
1980 for env_var in EnvVars::all_names()
1981 .iter()
1982 .filter(|name| !passthrough.contains(name))
1983 {
1984 command.env_remove(env_var);
1985 }
1986
1987 command
1988 }
1989}
1990
1991pub fn diff_snapshot(old: &str, new: &str) -> String {
1994 static TRIM_TRAILING_WHITESPACE: std::sync::LazyLock<Regex> =
1995 std::sync::LazyLock::new(|| Regex::new(r"(?m)^\s+$").unwrap());
1996
1997 let diff = similar::TextDiff::from_lines(old, new);
1998 let unified = diff
1999 .unified_diff()
2000 .context_radius(10)
2001 .header("old", "new")
2002 .to_string();
2003 TRIM_TRAILING_WHITESPACE
2007 .replace_all(&unified, "")
2008 .into_owned()
2009}
2010
2011pub fn site_packages_path(venv: &Path, python: &str) -> PathBuf {
2012 if cfg!(unix) {
2013 venv.join("lib").join(python).join("site-packages")
2014 } else if cfg!(windows) {
2015 venv.join("Lib").join("site-packages")
2016 } else {
2017 unimplemented!("Only Windows and Unix are supported")
2018 }
2019}
2020
2021pub fn venv_bin_path(venv: impl AsRef<Path>) -> PathBuf {
2022 if cfg!(unix) {
2023 venv.as_ref().join("bin")
2024 } else if cfg!(windows) {
2025 venv.as_ref().join("Scripts")
2026 } else {
2027 unimplemented!("Only Windows and Unix are supported")
2028 }
2029}
2030
2031pub fn get_python(version: &PythonVersion) -> PathBuf {
2033 ManagedPythonInstallations::from_settings(None)
2034 .map(|installed_pythons| {
2035 installed_pythons
2036 .find_version(version)
2037 .expect("Tests are run on a supported platform")
2038 .next()
2039 .as_ref()
2040 .map(|python| python.executable(false))
2041 })
2042 .unwrap_or_default()
2045 .unwrap_or(PathBuf::from(version.to_string()))
2046}
2047
2048pub fn create_venv_from_executable<P: AsRef<Path>>(
2050 path: P,
2051 cache_dir: &ChildPath,
2052 python: &Path,
2053 uv_bin: &Path,
2054) {
2055 TestContext::new_command_with(uv_bin)
2056 .arg("venv")
2057 .arg(path.as_ref().as_os_str())
2058 .arg("--clear")
2059 .arg("--cache-dir")
2060 .arg(cache_dir.path())
2061 .arg("--python")
2062 .arg(python)
2063 .current_dir(path.as_ref().parent().unwrap())
2064 .assert()
2065 .success();
2066 ChildPath::new(path.as_ref()).assert(predicate::path::is_dir());
2067}
2068
2069pub fn python_path_with_versions(
2073 temp_dir: &ChildPath,
2074 python_versions: &[&str],
2075) -> anyhow::Result<OsString> {
2076 let download_list = ManagedPythonDownloadList::new_only_embedded().unwrap();
2077 Ok(env::join_paths(
2078 python_installations_for_versions(temp_dir, python_versions, &download_list)?
2079 .into_iter()
2080 .map(|path| path.parent().unwrap().to_path_buf()),
2081 )?)
2082}
2083
2084pub fn python_installations_for_versions(
2088 temp_dir: &ChildPath,
2089 python_versions: &[&str],
2090 download_list: &ManagedPythonDownloadList,
2091) -> anyhow::Result<Vec<PathBuf>> {
2092 let cache = Cache::from_path(temp_dir.child("cache").to_path_buf())
2093 .init_no_wait()?
2094 .expect("No cache contention when setting up Python in tests");
2095 let selected_pythons = python_versions
2096 .iter()
2097 .map(|python_version| {
2098 if let Ok(python) = PythonInstallation::find(
2099 &PythonRequest::parse(python_version),
2100 EnvironmentPreference::OnlySystem,
2101 PythonPreference::Managed,
2102 download_list,
2103 &cache,
2104 Preview::default(),
2105 ) {
2106 python.into_interpreter().sys_executable().to_owned()
2107 } else {
2108 panic!("Could not find Python {python_version} for test\nTry `cargo run python install` first, or refer to CONTRIBUTING.md");
2109 }
2110 })
2111 .collect::<Vec<_>>();
2112
2113 assert!(
2114 python_versions.is_empty() || !selected_pythons.is_empty(),
2115 "Failed to fulfill requested test Python versions: {selected_pythons:?}"
2116 );
2117
2118 Ok(selected_pythons)
2119}
2120
2121#[derive(Debug, Copy, Clone)]
2122pub enum WindowsFilters {
2123 Platform,
2124 Universal,
2125}
2126
2127pub fn apply_filters<T: AsRef<str>>(mut snapshot: String, filters: impl AsRef<[(T, T)]>) -> String {
2129 for (matcher, replacement) in filters.as_ref() {
2130 let re = Regex::new(matcher.as_ref()).expect("Do you need to regex::escape your filter?");
2132 if re.is_match(&snapshot) {
2133 snapshot = re.replace_all(&snapshot, replacement.as_ref()).to_string();
2134 }
2135 }
2136 snapshot
2137}
2138
2139pub fn run_and_format<T: AsRef<str>>(
2143 command: impl BorrowMut<Command>,
2144 filters: impl AsRef<[(T, T)]>,
2145 function_name: &str,
2146 windows_filters: Option<WindowsFilters>,
2147 input: Option<&str>,
2148) -> (String, Output) {
2149 let (snapshot, output, _) =
2150 run_and_format_with_status(command, filters, function_name, windows_filters, input);
2151 (snapshot, output)
2152}
2153
2154#[expect(clippy::print_stderr)]
2158pub fn run_and_format_with_status<T: AsRef<str>>(
2159 mut command: impl BorrowMut<Command>,
2160 filters: impl AsRef<[(T, T)]>,
2161 function_name: &str,
2162 windows_filters: Option<WindowsFilters>,
2163 input: Option<&str>,
2164) -> (String, Output, ExitStatus) {
2165 let program = command
2166 .borrow_mut()
2167 .get_program()
2168 .to_string_lossy()
2169 .to_string();
2170
2171 if let Ok(root) = env::var(EnvVars::TRACING_DURATIONS_TEST_ROOT) {
2173 #[expect(clippy::assertions_on_constants)]
2175 {
2176 assert!(
2177 cfg!(feature = "tracing-durations-export"),
2178 "You need to enable the tracing-durations-export feature to use `TRACING_DURATIONS_TEST_ROOT`"
2179 );
2180 }
2181 command.borrow_mut().env(
2182 EnvVars::TRACING_DURATIONS_FILE,
2183 Path::new(&root).join(function_name).with_extension("jsonl"),
2184 );
2185 }
2186
2187 let output = if let Some(input) = input {
2188 let mut child = command
2189 .borrow_mut()
2190 .stdin(Stdio::piped())
2191 .stdout(Stdio::piped())
2192 .stderr(Stdio::piped())
2193 .spawn()
2194 .unwrap_or_else(|err| panic!("Failed to spawn {program}: {err}"));
2195 child
2196 .stdin
2197 .as_mut()
2198 .expect("Failed to open stdin")
2199 .write_all(input.as_bytes())
2200 .expect("Failed to write to stdin");
2201
2202 child
2203 .wait_with_output()
2204 .unwrap_or_else(|err| panic!("Failed to read output from {program}: {err}"))
2205 } else {
2206 command
2207 .borrow_mut()
2208 .output()
2209 .unwrap_or_else(|err| panic!("Failed to spawn {program}: {err}"))
2210 };
2211
2212 eprintln!("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Unfiltered output ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
2213 eprintln!(
2214 "----- stdout -----\n{}\n----- stderr -----\n{}",
2215 String::from_utf8_lossy(&output.stdout),
2216 String::from_utf8_lossy(&output.stderr),
2217 );
2218 eprintln!("────────────────────────────────────────────────────────────────────────────────\n");
2219
2220 let mut snapshot = apply_filters(
2221 format!(
2222 "success: {:?}\nexit_code: {}\n----- stdout -----\n{}\n----- stderr -----\n{}",
2223 output.status.success(),
2224 output.status.code().unwrap_or(!0),
2225 String::from_utf8_lossy(&output.stdout),
2226 String::from_utf8_lossy(&output.stderr),
2227 ),
2228 filters,
2229 );
2230
2231 if cfg!(windows) {
2236 if let Some(windows_filters) = windows_filters {
2237 let windows_only_deps = [
2239 (r"( ?[-+~] ?)?colorama==\d+(\.\d+)+( [\\]\n\s+--hash=.*)?\n(\s+# via .*\n)?"),
2240 (r"( ?[-+~] ?)?colorama==\d+(\.\d+)+(\s+[-+~]?\s+# via .*)?\n"),
2241 (r"( ?[-+~] ?)?tzdata==\d+(\.\d+)+( [\\]\n\s+--hash=.*)?\n(\s+# via .*\n)?"),
2242 (r"( ?[-+~] ?)?tzdata==\d+(\.\d+)+(\s+[-+~]?\s+# via .*)?\n"),
2243 ];
2244 let mut removed_packages = 0;
2245 for windows_only_dep in windows_only_deps {
2246 let re = Regex::new(windows_only_dep).unwrap();
2248 if re.is_match(&snapshot) {
2249 snapshot = re.replace(&snapshot, "").to_string();
2250 removed_packages += 1;
2251 }
2252 }
2253 if removed_packages > 0 {
2254 for i in 1..20 {
2255 for verb in match windows_filters {
2256 WindowsFilters::Platform => [
2257 "Resolved",
2258 "Prepared",
2259 "Installed",
2260 "Checked",
2261 "Uninstalled",
2262 ]
2263 .iter(),
2264 WindowsFilters::Universal => {
2265 ["Prepared", "Installed", "Checked", "Uninstalled"].iter()
2266 }
2267 } {
2268 snapshot = snapshot.replace(
2269 &format!("{verb} {} packages", i + removed_packages),
2270 &format!("{verb} {} package{}", i, if i > 1 { "s" } else { "" }),
2271 );
2272 }
2273 }
2274 }
2275 }
2276 }
2277
2278 let status = output.status;
2279 (snapshot, output, status)
2280}
2281
2282pub fn copy_dir_ignore(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> {
2284 for entry in ignore::Walk::new(&src) {
2285 let entry = entry?;
2286 let relative = entry.path().strip_prefix(&src)?;
2287 let ty = entry.file_type().unwrap();
2288 if ty.is_dir() {
2289 fs_err::create_dir(dst.as_ref().join(relative))?;
2290 } else {
2291 fs_err::copy(entry.path(), dst.as_ref().join(relative))?;
2292 }
2293 }
2294 Ok(())
2295}
2296
2297pub fn make_project(dir: &Path, name: &str, body: &str) -> anyhow::Result<()> {
2299 let pyproject_toml = formatdoc! {r#"
2300 [project]
2301 name = "{name}"
2302 version = "0.1.0"
2303 requires-python = ">=3.11,<3.13"
2304 {body}
2305
2306 [build-system]
2307 requires = ["uv_build>=0.9.0,<10000"]
2308 build-backend = "uv_build"
2309 "#
2310 };
2311 fs_err::create_dir_all(dir)?;
2312 fs_err::write(dir.join("pyproject.toml"), pyproject_toml)?;
2313 fs_err::create_dir_all(dir.join("src").join(name))?;
2314 fs_err::write(dir.join("src").join(name).join("__init__.py"), "")?;
2315 Ok(())
2316}
2317
2318pub const READ_ONLY_GITHUB_TOKEN: &[&str] = &[
2320 "Z2l0aHViCg==",
2321 "cGF0Cg==",
2322 "MTFBQlVDUjZBMERMUTQ3aVphN3hPdV9qQmhTMkZUeHZ4ZE13OHczakxuZndsV2ZlZjc2cE53eHBWS2tiRUFwdnpmUk8zV0dDSUhicDFsT01aago=",
2323];
2324
2325#[cfg(not(windows))]
2327pub const READ_ONLY_GITHUB_TOKEN_2: &[&str] = &[
2328 "Z2l0aHViCg==",
2329 "cGF0Cg==",
2330 "MTFBQlVDUjZBMDJTOFYwMTM4YmQ0bV9uTXpueWhxZDBrcllROTQ5SERTeTI0dENKZ2lmdzIybDFSR2s1SE04QW8xTUVYQ1I0Q1YxYUdPRGpvZQo=",
2331];
2332
2333pub const READ_ONLY_GITHUB_SSH_DEPLOY_KEY: &str = "LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNBeTF1SnNZK1JXcWp1NkdIY3Z6a3AwS21yWDEwdmo3RUZqTkpNTkRqSGZPZ0FBQUpqWUpwVnAyQ2FWCmFRQUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDQXkxdUpzWStSV3FqdTZHSGN2emtwMEttclgxMHZqN0VGak5KTU5EakhmT2cKQUFBRUMwbzBnd1BxbGl6TFBJOEFXWDVaS2dVZHJyQ2ptMDhIQm9FenB4VDg3MXBqTFc0bXhqNUZhcU83b1lkeS9PU25RcQphdGZYUytQc1FXTTBrdzBPTWQ4NkFBQUFFR3R2Ym5OMGFVQmhjM1J5WVd3dWMyZ0JBZ01FQlE9PQotLS0tLUVORCBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0K";
2334
2335pub fn decode_token(content: &[&str]) -> String {
2338 content
2339 .iter()
2340 .map(|part| base64.decode(part).unwrap())
2341 .map(|decoded| {
2342 std::str::from_utf8(decoded.as_slice())
2343 .unwrap()
2344 .trim_end()
2345 .to_string()
2346 })
2347 .join("_")
2348}
2349
2350#[tokio::main(flavor = "current_thread")]
2353pub async fn download_to_disk(url: &str, path: &Path) {
2354 let trusted_hosts: Vec<_> = env::var(EnvVars::UV_INSECURE_HOST)
2355 .unwrap_or_default()
2356 .split(' ')
2357 .map(|h| uv_configuration::TrustedHost::from_str(h).unwrap())
2358 .collect();
2359
2360 let client = uv_client::BaseClientBuilder::default()
2361 .allow_insecure_host(trusted_hosts)
2362 .build()
2363 .expect("failed to build base client");
2364 let url = url.parse().unwrap();
2365 let response = client
2366 .for_host(&url)
2367 .get(reqwest::Url::from(url))
2368 .send()
2369 .await
2370 .unwrap();
2371
2372 let mut file = fs_err::tokio::File::create(path).await.unwrap();
2373 let mut stream = response.bytes_stream();
2374 while let Some(chunk) = stream.next().await {
2375 file.write_all(&chunk.unwrap()).await.unwrap();
2376 }
2377 file.sync_all().await.unwrap();
2378}
2379
2380#[cfg(unix)]
2385pub struct ReadOnlyDirectoryGuard {
2386 path: PathBuf,
2387 original_mode: u32,
2388}
2389
2390#[cfg(unix)]
2391impl ReadOnlyDirectoryGuard {
2392 pub fn new(path: impl Into<PathBuf>) -> std::io::Result<Self> {
2395 use std::os::unix::fs::PermissionsExt;
2396 let path = path.into();
2397 let metadata = fs_err::metadata(&path)?;
2398 let original_mode = metadata.permissions().mode();
2399 let readonly_mode = original_mode & !0o222;
2401 fs_err::set_permissions(&path, std::fs::Permissions::from_mode(readonly_mode))?;
2402 Ok(Self {
2403 path,
2404 original_mode,
2405 })
2406 }
2407}
2408
2409#[cfg(unix)]
2410impl Drop for ReadOnlyDirectoryGuard {
2411 fn drop(&mut self) {
2412 use std::os::unix::fs::PermissionsExt;
2413 let _ = fs_err::set_permissions(
2414 &self.path,
2415 std::fs::Permissions::from_mode(self.original_mode),
2416 );
2417 }
2418}
2419
2420#[doc(hidden)]
2424#[macro_export]
2425macro_rules! function_name {
2426 () => {{
2427 fn f() {}
2428 fn type_name_of_val<T>(_: T) -> &'static str {
2429 std::any::type_name::<T>()
2430 }
2431 let mut name = type_name_of_val(f).strip_suffix("::f").unwrap_or("");
2432 while let Some(rest) = name.strip_suffix("::{{closure}}") {
2433 name = rest;
2434 }
2435 name
2436 }};
2437}
2438
2439#[macro_export]
2444macro_rules! uv_snapshot {
2445 ($spawnable:expr, @$snapshot:literal) => {{
2446 uv_snapshot!($crate::INSTA_FILTERS.to_vec(), $spawnable, @$snapshot)
2447 }};
2448 ($filters:expr, $spawnable:expr, @$snapshot:literal) => {{
2449 let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), Some($crate::WindowsFilters::Platform), None);
2451 ::insta::assert_snapshot!(snapshot, @$snapshot);
2452 output
2453 }};
2454 ($filters:expr, $spawnable:expr, input=$input:expr, @$snapshot:literal) => {{
2455 let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), Some($crate::WindowsFilters::Platform), Some($input));
2457 ::insta::assert_snapshot!(snapshot, @$snapshot);
2458 output
2459 }};
2460 ($filters:expr, windows_filters=false, $spawnable:expr, @$snapshot:literal) => {{
2461 let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), None, None);
2463 ::insta::assert_snapshot!(snapshot, @$snapshot);
2464 output
2465 }};
2466 ($filters:expr, universal_windows_filters=true, $spawnable:expr, @$snapshot:literal) => {{
2467 let (snapshot, output) = $crate::run_and_format($spawnable, &$filters, $crate::function_name!(), Some($crate::WindowsFilters::Universal), None);
2469 ::insta::assert_snapshot!(snapshot, @$snapshot);
2470 output
2471 }};
2472}