Add test context utility for standardized filtering of counts (#4568)

There are cases where these counts simply don't matter and we're
manually tweaking them to deal with Windows.
This commit is contained in:
Zanie Blue 2024-06-27 06:48:19 -04:00 committed by GitHub
parent 8a046313b1
commit 857b3cc777
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 97 additions and 121 deletions

View file

@ -90,6 +90,36 @@ impl TestContext {
new new
} }
/// Add extra standard filtering for messages like "Resolved 10 packages" which
/// can differ between platforms.
///
/// In some cases, these counts are helpful for the snapshot and should not be filtered.
#[must_use]
pub fn with_filtered_counts(mut self) -> Self {
for verb in &[
"Resolved",
"Prepared",
"Installed",
"Uninstalled",
"Audited",
] {
self.filters.push((
format!("{verb} \\d+ packages?"),
format!("{verb} [N] packages"),
));
}
self
}
/// Add extra standard filtering for executable suffixes on the current platform e.g.
/// drops `.exe` on Windows.
#[must_use]
pub fn with_filtered_exe_suffix(mut self) -> Self {
self.filters
.push((std::env::consts::EXE_SUFFIX.to_string(), String::new()));
self
}
/// Create a new test context with multiple Python versions. /// Create a new test context with multiple Python versions.
/// ///
/// Does not create a virtual environment by default, but the first Python version /// Does not create a virtual environment by default, but the first Python version
@ -661,7 +691,6 @@ pub fn python_toolchains_for_versions(
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum WindowsFilters { pub enum WindowsFilters {
CachedPlatform,
Platform, Platform,
Universal, Universal,
} }
@ -742,10 +771,6 @@ pub fn run_and_format<T: AsRef<str>>(
WindowsFilters::Platform => { WindowsFilters::Platform => {
["Resolved", "Prepared", "Installed", "Uninstalled"].iter() ["Resolved", "Prepared", "Installed", "Uninstalled"].iter()
} }
// When cached, "Prepared" should not change.
WindowsFilters::CachedPlatform => {
["Resolved", "Installed", "Uninstalled"].iter()
}
WindowsFilters::Universal => { WindowsFilters::Universal => {
["Prepared", "Installed", "Uninstalled"].iter() ["Prepared", "Installed", "Uninstalled"].iter()
} }
@ -839,12 +864,6 @@ macro_rules! uv_snapshot {
::insta::assert_snapshot!(snapshot, @$snapshot); ::insta::assert_snapshot!(snapshot, @$snapshot);
output output
}}; }};
($filters:expr, cached_windows_filters=true, $spawnable:expr, @$snapshot:literal) => {{
// Take a reference for backwards compatibility with the vec-expecting insta filters.
let (snapshot, output) = $crate::common::run_and_format($spawnable, &$filters, function_name!(), Some($crate::common::WindowsFilters::CachedPlatform));
::insta::assert_snapshot!(snapshot, @$snapshot);
output
}};
} }
/// <https://stackoverflow.com/a/31749071/3549270> /// <https://stackoverflow.com/a/31749071/3549270>

View file

@ -526,17 +526,8 @@ fn respect_installed_and_reinstall() -> Result<()> {
let requirements_txt = context.temp_dir.child("requirements.txt"); let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("Flask==2.3.3")?; requirements_txt.write_str("Flask==2.3.3")?;
let filters = if cfg!(windows) { let context = context.with_filtered_counts();
// Remove the colorama count on windows uv_snapshot!(context.filters(), context.pip_install()
context
.filters()
.into_iter()
.chain([("Resolved 8 packages", "Resolved 7 packages")])
.collect()
} else {
context.filters()
};
uv_snapshot!(filters, context.pip_install()
.arg("-r") .arg("-r")
.arg("requirements.txt") .arg("requirements.txt")
.arg("--strict"), @r###" .arg("--strict"), @r###"
@ -545,10 +536,10 @@ fn respect_installed_and_reinstall() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Resolved 7 packages in [TIME] Resolved [N] packages in [TIME]
Prepared 1 package in [TIME] Prepared [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- flask==2.3.2 - flask==2.3.2
+ flask==2.3.3 + flask==2.3.3
"### "###
@ -558,7 +549,7 @@ fn respect_installed_and_reinstall() -> Result<()> {
let requirements_txt = context.temp_dir.child("requirements.txt"); let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("Flask")?; requirements_txt.write_str("Flask")?;
uv_snapshot!(filters, context.pip_install() uv_snapshot!(context.filters(), context.pip_install()
.arg("-r") .arg("-r")
.arg("requirements.txt") .arg("requirements.txt")
.arg("--reinstall-package") .arg("--reinstall-package")
@ -569,10 +560,10 @@ fn respect_installed_and_reinstall() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Resolved 7 packages in [TIME] Resolved [N] packages in [TIME]
Prepared 1 package in [TIME] Prepared [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- flask==2.3.3 - flask==2.3.3
+ flask==3.0.2 + flask==3.0.2
"### "###
@ -582,7 +573,7 @@ fn respect_installed_and_reinstall() -> Result<()> {
let requirements_txt = context.temp_dir.child("requirements.txt"); let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("Flask")?; requirements_txt.write_str("Flask")?;
uv_snapshot!(filters, context.pip_install() uv_snapshot!(context.filters(), context.pip_install()
.arg("-r") .arg("-r")
.arg("requirements.txt") .arg("requirements.txt")
.arg("--reinstall-package") .arg("--reinstall-package")
@ -593,9 +584,9 @@ fn respect_installed_and_reinstall() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Resolved 7 packages in [TIME] Resolved [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- flask==3.0.2 - flask==3.0.2
+ flask==3.0.2 + flask==3.0.2
"### "###
@ -895,27 +886,19 @@ fn install_editable_and_registry() {
"### "###
); );
let filters: Vec<_> = context let context = context.with_filtered_counts();
.filters()
.into_iter()
.chain([
// Remove colorama
("Resolved 7 packages", "Resolved 6 packages"),
])
.collect();
// Re-install Black at a specific version. This should replace the editable version. // Re-install Black at a specific version. This should replace the editable version.
uv_snapshot!(filters, context.pip_install() uv_snapshot!(context.filters(), context.pip_install()
.arg("black==23.10.0"), @r###" .arg("black==23.10.0"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Resolved 6 packages in [TIME] Resolved [N] packages in [TIME]
Prepared 1 package in [TIME] Prepared [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- black==0.1.0 (from file://[WORKSPACE]/scripts/packages/black_editable) - black==0.1.0 (from file://[WORKSPACE]/scripts/packages/black_editable)
+ black==23.10.0 + black==23.10.0
"### "###
@ -1770,17 +1753,7 @@ fn reinstall_no_binary() {
context.assert_command("import anyio").success(); context.assert_command("import anyio").success();
// With `--reinstall`, `--no-binary` should have an affect // With `--reinstall`, `--no-binary` should have an affect
let filters = if cfg!(windows) { let context = context.with_filtered_counts();
// Remove the colorama count on windows
context
.filters()
.into_iter()
.chain([("Resolved 8 packages", "Resolved 7 packages")])
.collect()
} else {
context.filters()
};
let mut command = context.pip_install(); let mut command = context.pip_install();
command command
.arg("anyio") .arg("anyio")
@ -1789,15 +1762,15 @@ fn reinstall_no_binary() {
.arg("--reinstall-package") .arg("--reinstall-package")
.arg("anyio") .arg("anyio")
.arg("--strict"); .arg("--strict");
uv_snapshot!(filters, command, @r###" uv_snapshot!(context.filters(), command, @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Resolved 3 packages in [TIME] Resolved [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- anyio==4.3.0 - anyio==4.3.0
+ anyio==4.3.0 + anyio==4.3.0
"### "###

View file

@ -15,7 +15,7 @@ mod common;
/// Test installing a tool with `uv tool install` /// Test installing a tool with `uv tool install`
#[test] #[test]
fn tool_install() { fn tool_install() {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12").with_filtered_counts();
let tool_dir = context.temp_dir.child("tools"); let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin"); let bin_dir = context.temp_dir.child("bin");
@ -31,9 +31,9 @@ fn tool_install() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved 6 packages in [TIME] Resolved [N] packages in [TIME]
Prepared 6 packages in [TIME] Prepared [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0
@ -91,7 +91,7 @@ fn tool_install() {
"###); "###);
// Install another tool // Install another tool
uv_snapshot!(context.filters(), cached_windows_filters=true, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("flask") .arg("flask")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###" .env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
@ -102,9 +102,9 @@ fn tool_install() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved 7 packages in [TIME] Resolved [N] packages in [TIME]
Prepared 6 packages in [TIME] Prepared [N] packages in [TIME]
Installed 7 packages in [TIME] Installed [N] packages in [TIME]
+ blinker==1.7.0 + blinker==1.7.0
+ click==8.1.7 + click==8.1.7
+ flask==3.0.2 + flask==3.0.2
@ -161,19 +161,12 @@ fn tool_install() {
/// Test installing and reinstalling an already installed tool /// Test installing and reinstalling an already installed tool
#[test] #[test]
fn tool_install_already_installed() { fn tool_install_already_installed() {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12").with_filtered_counts();
let tool_dir = context.temp_dir.child("tools"); let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin"); let bin_dir = context.temp_dir.child("bin");
// Drop resolved counts, they differ on Windows and are not relevant here
let filters = context
.filters()
.into_iter()
.chain([("Resolved [0-9] packages", "Resolved [COUNT] packages")])
.collect::<Vec<_>>();
// Install `black` // Install `black`
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###" .env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
@ -184,9 +177,9 @@ fn tool_install_already_installed() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Prepared 6 packages in [TIME] Prepared [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0
@ -234,7 +227,7 @@ fn tool_install_already_installed() {
}); });
// Install `black` again // Install `black` again
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###" .env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
@ -264,7 +257,7 @@ fn tool_install_already_installed() {
// Install `black` again with the `--reinstall` flag // Install `black` again with the `--reinstall` flag
// We should recreate the entire environment and reinstall the entry points // We should recreate the entire environment and reinstall the entry points
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.arg("--reinstall") .arg("--reinstall")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
@ -276,8 +269,8 @@ fn tool_install_already_installed() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0
@ -288,7 +281,7 @@ fn tool_install_already_installed() {
// Install `black` again with `--reinstall-package` for `black` // Install `black` again with `--reinstall-package` for `black`
// We should reinstall `black` in the environment and reinstall the entry points // We should reinstall `black` in the environment and reinstall the entry points
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.arg("--reinstall-package") .arg("--reinstall-package")
.arg("black") .arg("black")
@ -301,16 +294,16 @@ fn tool_install_already_installed() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- black==24.3.0 - black==24.3.0
+ black==24.3.0 + black==24.3.0
"###); "###);
// Install `black` again with `--reinstall-package` for a dependency // Install `black` again with `--reinstall-package` for a dependency
// We should reinstall `click` in the environment but not reinstall the entry points // We should reinstall `click` in the environment but not reinstall the entry points
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.arg("--reinstall-package") .arg("--reinstall-package")
.arg("click") .arg("click")
@ -322,9 +315,9 @@ fn tool_install_already_installed() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Uninstalled 1 package in [TIME] Uninstalled [N] packages in [TIME]
Installed 1 package in [TIME] Installed [N] packages in [TIME]
- click==8.1.7 - click==8.1.7
+ click==8.1.7 + click==8.1.7
Updated environment for tool `black` Updated environment for tool `black`
@ -334,26 +327,17 @@ fn tool_install_already_installed() {
/// Test installing a tool when its entry point already exists /// Test installing a tool when its entry point already exists
#[test] #[test]
fn tool_install_entry_point_exists() { fn tool_install_entry_point_exists() {
let context = TestContext::new("3.12"); let context = TestContext::new("3.12")
.with_filtered_counts()
.with_filtered_exe_suffix();
let tool_dir = context.temp_dir.child("tools"); let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin"); let bin_dir = context.temp_dir.child("bin");
let executable = bin_dir.child(format!("black{}", std::env::consts::EXE_SUFFIX)); let executable = bin_dir.child(format!("black{}", std::env::consts::EXE_SUFFIX));
executable.touch().unwrap(); executable.touch().unwrap();
// Drop executable suffixes for cross-platform snapshots
// Drop resolved counts, they differ on Windows and are not relevant here
let filters = context
.filters()
.into_iter()
.chain([
(std::env::consts::EXE_SUFFIX, ""),
("Resolved [0-9] packages", "Resolved [COUNT] packages"),
])
.collect::<Vec<_>>();
// Attempt to install `black` // Attempt to install `black`
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###" .env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
@ -363,9 +347,9 @@ fn tool_install_entry_point_exists() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Prepared 6 packages in [TIME] Prepared [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0
@ -391,7 +375,7 @@ fn tool_install_entry_point_exists() {
// Attempt to install `black` with the `--reinstall` flag // Attempt to install `black` with the `--reinstall` flag
// Should have no effect // Should have no effect
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.arg("--reinstall") .arg("--reinstall")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
@ -402,8 +386,8 @@ fn tool_install_entry_point_exists() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0
@ -432,7 +416,7 @@ fn tool_install_entry_point_exists() {
.child(format!("blackd{}", std::env::consts::EXE_SUFFIX)) .child(format!("blackd{}", std::env::consts::EXE_SUFFIX))
.touch() .touch()
.unwrap(); .unwrap();
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###" .env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
@ -442,8 +426,8 @@ fn tool_install_entry_point_exists() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0
@ -454,7 +438,7 @@ fn tool_install_entry_point_exists() {
"###); "###);
// Install `black` with `--force` // Install `black` with `--force`
uv_snapshot!(filters, context.tool_install() uv_snapshot!(context.filters(), context.tool_install()
.arg("black") .arg("black")
.arg("--force") .arg("--force")
.env("UV_TOOL_DIR", tool_dir.as_os_str()) .env("UV_TOOL_DIR", tool_dir.as_os_str())
@ -466,8 +450,8 @@ fn tool_install_entry_point_exists() {
----- stderr ----- ----- stderr -----
warning: `uv tool install` is experimental and may change without warning. warning: `uv tool install` is experimental and may change without warning.
Resolved [COUNT] packages in [TIME] Resolved [N] packages in [TIME]
Installed 6 packages in [TIME] Installed [N] packages in [TIME]
+ black==24.3.0 + black==24.3.0
+ click==8.1.7 + click==8.1.7
+ mypy-extensions==1.0.0 + mypy-extensions==1.0.0