Consolidate environment hash filtering (#14864)

This commit is contained in:
Zanie Blue 2025-07-24 07:35:45 -05:00 committed by GitHub
parent 1150de3fc5
commit 23ed31b94d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 114 deletions

View File

@ -706,6 +706,11 @@ impl TestContext {
), ),
r#"requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]"#.to_string(), r#"requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]"#.to_string(),
)); ));
// Filter script environment hashes
filters.push((
r"environments-v(\d+)[\\/](\w+)-[a-z0-9]+".to_string(),
"environments-v$1/$2-[HASH]".to_string(),
));
Self { Self {
root: ChildPath::new(root.path()), root: ChildPath::new(root.path()),

View File

@ -837,16 +837,7 @@ fn python_find_script() {
.with_filtered_python_names() .with_filtered_python_names()
.with_filtered_exe_suffix(); .with_filtered_exe_suffix();
let filters = context uv_snapshot!(context.filters(), context.init().arg("--script").arg("foo.py"), @r###"
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/[\w-]+",
"environments-v2/[HASHEDNAME]",
)])
.collect::<Vec<_>>();
uv_snapshot!(filters, context.init().arg("--script").arg("foo.py"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -855,22 +846,22 @@ fn python_find_script() {
Initialized script at `foo.py` Initialized script at `foo.py`
"###); "###);
uv_snapshot!(filters, context.sync().arg("--script").arg("foo.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("foo.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Creating script environment at: [CACHE_DIR]/environments-v2/[HASHEDNAME] Creating script environment at: [CACHE_DIR]/environments-v2/foo-[HASH]
Resolved in [TIME] Resolved in [TIME]
Audited in [TIME] Audited in [TIME]
"); ");
uv_snapshot!(filters, context.python_find().arg("--script").arg("foo.py"), @r" uv_snapshot!(context.filters(), context.python_find().arg("--script").arg("foo.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
[CACHE_DIR]/environments-v2/[HASHEDNAME]/[BIN]/[PYTHON] [CACHE_DIR]/environments-v2/foo-[HASH]/[BIN]/[PYTHON]
----- stderr ----- ----- stderr -----
"); ");
@ -936,15 +927,6 @@ fn python_find_script_no_such_version() {
.with_filtered_python_names() .with_filtered_python_names()
.with_filtered_exe_suffix() .with_filtered_exe_suffix()
.with_filtered_python_sources(); .with_filtered_python_sources();
let filters = context
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/[\w-]+",
"environments-v2/[HASHEDNAME]",
)])
.collect::<Vec<_>>();
let script = context.temp_dir.child("foo.py"); let script = context.temp_dir.child("foo.py");
script script
.write_str(indoc! {r#" .write_str(indoc! {r#"
@ -955,13 +937,13 @@ fn python_find_script_no_such_version() {
"#}) "#})
.unwrap(); .unwrap();
uv_snapshot!(filters, context.sync().arg("--script").arg("foo.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("foo.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
Creating script environment at: [CACHE_DIR]/environments-v2/[HASHEDNAME] Creating script environment at: [CACHE_DIR]/environments-v2/foo-[HASH]
Resolved in [TIME] Resolved in [TIME]
Audited in [TIME] Audited in [TIME]
"); ");
@ -975,7 +957,7 @@ fn python_find_script_no_such_version() {
"#}) "#})
.unwrap(); .unwrap();
uv_snapshot!(filters, context.python_find().arg("--script").arg("foo.py"), @r" uv_snapshot!(context.filters(), context.python_find().arg("--script").arg("foo.py"), @r"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----

View File

@ -4031,17 +4031,8 @@ fn run_active_script_environment() -> Result<()> {
"# "#
})?; })?;
let filters = context
.filters()
.into_iter()
.chain(vec![(
r"environments-v1/main-\w+",
"environments-v1/main-[HASH]",
)])
.collect::<Vec<_>>();
// Running `uv run --script` with `VIRTUAL_ENV` should _not_ warn. // Running `uv run --script` with `VIRTUAL_ENV` should _not_ warn.
uv_snapshot!(&filters, context.run() uv_snapshot!(context.filters(), context.run()
.arg("--script") .arg("--script")
.arg("main.py") .arg("main.py")
.env(EnvVars::VIRTUAL_ENV, "foo"), @r###" .env(EnvVars::VIRTUAL_ENV, "foo"), @r###"
@ -4058,7 +4049,7 @@ fn run_active_script_environment() -> Result<()> {
"###); "###);
// Using `--no-active` should also _not_ warn. // Using `--no-active` should also _not_ warn.
uv_snapshot!(&filters, context.run() uv_snapshot!(context.filters(), context.run()
.arg("--no-active") .arg("--no-active")
.arg("--script") .arg("--script")
.arg("main.py") .arg("main.py")
@ -4077,7 +4068,7 @@ fn run_active_script_environment() -> Result<()> {
.assert(predicate::path::missing()); .assert(predicate::path::missing());
// Using `--active` should create the environment // Using `--active` should create the environment
uv_snapshot!(&filters, context.run() uv_snapshot!(context.filters(), context.run()
.arg("--active") .arg("--active")
.arg("--script") .arg("--script")
.arg("main.py") .arg("main.py")
@ -4099,7 +4090,7 @@ fn run_active_script_environment() -> Result<()> {
.assert(predicate::path::is_dir()); .assert(predicate::path::is_dir());
// Requesting a different Python version should invalidate the environment // Requesting a different Python version should invalidate the environment
uv_snapshot!(&filters, context.run() uv_snapshot!(context.filters(), context.run()
.arg("--active") .arg("--active")
.arg("-p").arg("3.12") .arg("-p").arg("3.12")
.arg("--script") .arg("--script")

View File

@ -4911,17 +4911,8 @@ fn sync_active_script_environment() -> Result<()> {
"# "#
})?; })?;
let filters = context
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/script-[a-z0-9]+",
"environments-v2/script-[HASH]",
)])
.collect::<Vec<_>>();
// Running `uv sync --script` with `VIRTUAL_ENV` should warn // Running `uv sync --script` with `VIRTUAL_ENV` should warn
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -4943,7 +4934,7 @@ fn sync_active_script_environment() -> Result<()> {
.assert(predicate::path::missing()); .assert(predicate::path::missing());
// Using `--active` should create the environment // Using `--active` should create the environment
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -4963,7 +4954,7 @@ fn sync_active_script_environment() -> Result<()> {
.assert(predicate::path::is_dir()); .assert(predicate::path::is_dir());
// A subsequent sync will re-use the environment // A subsequent sync will re-use the environment
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -4975,7 +4966,7 @@ fn sync_active_script_environment() -> Result<()> {
"); ");
// Requesting another Python version will invalidate the environment // Requesting another Python version will invalidate the environment
uv_snapshot!(&filters, context.sync() uv_snapshot!(context.filters(), context.sync()
.arg("--script") .arg("--script")
.arg("script.py") .arg("script.py")
.env(EnvVars::VIRTUAL_ENV, "foo") .env(EnvVars::VIRTUAL_ENV, "foo")
@ -5017,17 +5008,8 @@ fn sync_active_script_environment_json() -> Result<()> {
"# "#
})?; })?;
let filters = context
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/script-[a-z0-9]+",
"environments-v2/script-[HASH]",
)])
.collect::<Vec<_>>();
// Running `uv sync --script` with `VIRTUAL_ENV` should warn // Running `uv sync --script` with `VIRTUAL_ENV` should warn
uv_snapshot!(&filters, context.sync() uv_snapshot!(context.filters(), context.sync()
.arg("--script").arg("script.py") .arg("--script").arg("script.py")
.arg("--output-format").arg("json") .arg("--output-format").arg("json")
.env(EnvVars::VIRTUAL_ENV, "foo"), @r#" .env(EnvVars::VIRTUAL_ENV, "foo"), @r#"
@ -5073,7 +5055,7 @@ fn sync_active_script_environment_json() -> Result<()> {
.assert(predicate::path::missing()); .assert(predicate::path::missing());
// Using `--active` should create the environment // Using `--active` should create the environment
uv_snapshot!(&filters, context.sync() uv_snapshot!(context.filters(), context.sync()
.arg("--script").arg("script.py") .arg("--script").arg("script.py")
.arg("--output-format").arg("json") .arg("--output-format").arg("json")
.env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r#" .env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r#"
@ -5117,7 +5099,7 @@ fn sync_active_script_environment_json() -> Result<()> {
.assert(predicate::path::is_dir()); .assert(predicate::path::is_dir());
// A subsequent sync will re-use the environment // A subsequent sync will re-use the environment
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").env(EnvVars::VIRTUAL_ENV, "foo").arg("--active"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -5129,7 +5111,7 @@ fn sync_active_script_environment_json() -> Result<()> {
"); ");
// Requesting another Python version will invalidate the environment // Requesting another Python version will invalidate the environment
uv_snapshot!(&filters, context.sync() uv_snapshot!(context.filters(), context.sync()
.arg("--script").arg("script.py") .arg("--script").arg("script.py")
.arg("--output-format").arg("json") .arg("--output-format").arg("json")
.env(EnvVars::VIRTUAL_ENV, "foo") .env(EnvVars::VIRTUAL_ENV, "foo")
@ -9015,16 +8997,7 @@ fn sync_script() -> Result<()> {
"# "#
})?; })?;
let filters = context uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/script-\w+",
"environments-v2/script-[HASH]",
)])
.collect::<Vec<_>>();
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9056,7 +9029,7 @@ fn sync_script() -> Result<()> {
"# "#
})?; })?;
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9082,7 +9055,7 @@ fn sync_script() -> Result<()> {
"# "#
})?; })?;
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9107,7 +9080,7 @@ fn sync_script() -> Result<()> {
"# "#
})?; })?;
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9125,7 +9098,7 @@ fn sync_script() -> Result<()> {
"); ");
// `--locked` and `--frozen` should fail with helpful error messages. // `--locked` and `--frozen` should fail with helpful error messages.
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").arg("--locked"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").arg("--locked"), @r"
success: false success: false
exit_code: 2 exit_code: 2
----- stdout ----- ----- stdout -----
@ -9135,7 +9108,7 @@ fn sync_script() -> Result<()> {
error: `uv sync --locked` requires a script lockfile; run `uv lock --script script.py` to lock the script error: `uv sync --locked` requires a script lockfile; run `uv lock --script script.py` to lock the script
"); ");
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").arg("--frozen"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").arg("--frozen"), @r"
success: false success: false
exit_code: 2 exit_code: 2
----- stdout ----- ----- stdout -----
@ -9165,17 +9138,8 @@ fn sync_locked_script() -> Result<()> {
"# "#
})?; })?;
let filters = context
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/script-\w+",
"environments-v2/script-[HASH]",
)])
.collect::<Vec<_>>();
// Lock the script. // Lock the script.
uv_snapshot!(&filters, context.lock().arg("--script").arg("script.py"), @r###" uv_snapshot!(context.filters(), context.lock().arg("--script").arg("script.py"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9235,7 +9199,7 @@ fn sync_locked_script() -> Result<()> {
); );
}); });
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9265,7 +9229,7 @@ fn sync_locked_script() -> Result<()> {
})?; })?;
// Re-run with `--locked`. // Re-run with `--locked`.
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").arg("--locked"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").arg("--locked"), @r"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
@ -9276,7 +9240,7 @@ fn sync_locked_script() -> Result<()> {
The lockfile at `uv.lock` needs to be updated, but `--locked` was provided. To update the lockfile, run `uv lock`. The lockfile at `uv.lock` needs to be updated, but `--locked` was provided. To update the lockfile, run `uv lock`.
"); ");
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9367,7 +9331,7 @@ fn sync_locked_script() -> Result<()> {
})?; })?;
// Re-run with `--locked`. // Re-run with `--locked`.
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py").arg("--locked"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py").arg("--locked"), @r"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----
@ -9379,7 +9343,7 @@ fn sync_locked_script() -> Result<()> {
The lockfile at `uv.lock` needs to be updated, but `--locked` was provided. To update the lockfile, run `uv lock`. The lockfile at `uv.lock` needs to be updated, but `--locked` was provided. To update the lockfile, run `uv lock`.
"); ");
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9424,16 +9388,7 @@ fn sync_script_with_compatible_build_constraints() -> Result<()> {
"# "#
})?; })?;
let filters = context uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/script-\w+",
"environments-v2/script-[HASH]",
)])
.collect::<Vec<_>>();
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -9459,14 +9414,6 @@ fn sync_script_with_incompatible_build_constraints() -> Result<()> {
let context = TestContext::new("3.9"); let context = TestContext::new("3.9");
let test_script = context.temp_dir.child("script.py"); let test_script = context.temp_dir.child("script.py");
let filters = context
.filters()
.into_iter()
.chain(vec![(
r"environments-v2/script-\w+",
"environments-v2/script-[HASH]",
)])
.collect::<Vec<_>>();
// Incompatible build constraints. // Incompatible build constraints.
test_script.write_str(indoc! { r#" test_script.write_str(indoc! { r#"
@ -9485,7 +9432,7 @@ fn sync_script_with_incompatible_build_constraints() -> Result<()> {
"# "#
})?; })?;
uv_snapshot!(&filters, context.sync().arg("--script").arg("script.py"), @r" uv_snapshot!(context.filters(), context.sync().arg("--script").arg("script.py"), @r"
success: false success: false
exit_code: 1 exit_code: 1
----- stdout ----- ----- stdout -----