Repository navigation
Build: share one test env guard in core (#1889) - #1891
Mikola Lysenko (mikolalysenko) wants to merge 3 commits into
Conversation
Assisted-by: Claude Code:claude-opus-5-5
Tests that change process env each wrote their own restore-on-drop
guard: 11 EnvGuard copies plus the with_env / with_home_env closures
and api::client's OrgResolutionEnvGuard. The copies drifted. Most
saved the previous value as a String, so a non-UTF-8 value was lost,
and the two closure helpers never restored env when the body panicked,
which leaked into the next #[serial] test.
Add crate::test_env with EnvGuard::{set, unset, many} and with_env.
It saves OsString values, restores newest-first and is panic-safe.
Every core unit test now uses it. The three integration tests that
had their own copy compile the same file in via #[path]. Tests only;
no production code changes.
Assisted-by: Claude Code:claude-opus-5-5
- EnvGuard::many records each saved value before changing it, so a panic partway through still restores the vars already changed. - Move the guard's self-tests into test_env_tests.rs, so the integration tests that #[path]-include test_env.rs don't run them again. - Put SOCKET_PROXY_URL under the guard in the org-resolution proxy test instead of restoring it by hand after the asserts. - Pass paths straight to EnvGuard::set instead of to_str().unwrap(). Assisted-by: Claude Code:claude-opus-5-5
|
BugBot review Generated by Claude Code |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit c34e51c. Configure here.
|
[agent] This failure is not from this PR:
No fix exists yet. It looks like the macOS runner's npm changed which bins get their shebang rewritten (#1804 territory). I'm leaving it to the nightly/janitor lane rather than widening this PR. No other macOS leg in the dispatch run has failed so far. Update: Generated by Claude Code |
|
[burn-down agent] Labeled Ready for review at c34e51c.
Generated by Claude Code |
Final review briefWhat it does: It replaces 11 hand-rolled test Risk: low. The change is test-only: both new modules are behind Look here
Verified: I read the full diff, and Changes I made: none. Open questions (non-blocking): the guard can't force a test to use Auto-merge is armed, so approving sends this straight to the merge queue. Generated by Claude Code |
LLM Description written by Claude Code:claude-opus-5-5
Fixes #1889
Root cause
Every core test that changes process env wrote its own restore-on-drop guard: 11
struct EnvGuardcopies, thewith_env/with_home_envclosure helpers andapi::client'sOrgResolutionEnvGuard. The copies had drifted, and two of the drifts were real defects:String(env::var(..).ok()). A non-UTF-8 value was therefore removed instead of restored.with_env(utils/socket_cli_config.rs) andwith_home_env(utils/fs.rs) restored env only after the closure returned. A failing assertion inside them leakedHOME,USERPROFILE,XDG_DATA_HOMEorSOCKET_NO_CONFIGinto the next#[serial]test.Change
#[cfg(test)] crate::test_env, next totest_rng:EnvGuard::set(key, impl AsRef<OsStr>),EnvGuard::unset(key)andEnvGuard::many(pairs), where aNonevalue removes the var.with_env(pairs, f).OsStringand restores newest-first, so a key listed twice ends at its original value.many.struct EnvGuardcopy in coresrc/now uses it.with_envandwith_home_envare thin wrappers or re-exports.OrgResolutionEnvGuardbecomesisolate_org_resolution_env(), which returns anEnvGuard. The proxy test's hand-writtenSOCKET_PROXY_URLsave/restore moved under the guard.upstream_restore_golden,crawler_deno_e2e,crawler_npm_e2e) compile the same file in with#[path = "../src/test_env.rs"].grep -rn "struct EnvGuard" crates/socket-patch-corenow finds exactly one definition.src/test_env_tests.rs, so they run once (lib) and not again in each binary that includes it.#[serial].Net diff: about -270 lines.
Review findings I didn't fix (from
/code-review high)set_var/remove_varsave-restore in core tests (update/{state,swap,release,mod}.rs,policy/tests.rs,telemetry.rs,vendor/registry_fetch.rs, the token-veto tests inapi/client.rsand others, about 150 call sites in 20+ files): out of scope. Share one scoped env-var test guard: EnvGuard is copy-pasted 11 times across core unit and integration tests #1889 is about the guard copies. Moving every rawset_varonto the guard is a larger follow-up, which is now easy because the guard exists. Some of those tests also lack#[serial](api/client.rstest_get_api_client_from_env_no_token), and that belongs in the same follow-up.#[path]include stanza appears 3 times instead of living intests/common/mod.rs: two of the three binaries don't includecommon, and including it would pull its helpers into binaries that don't use them. One 4-line stanza that points at the single definition replaces three 25-line copies.CI cost
No workflow change. A lean PR run is unchanged: ~28 Linux jobs, about 8 min. Because one edited test is
cfg(target_os = "macos")(npm_crawlervolta/fnm), I dispatched one macOS-only run on this branch (os=macos,hosted_e2e=skip): https://github.com/SocketDev/socket-patch/actions/runs/41020866014Test evidence
cargo fmt --all -- --check: clean.cargo clippy --workspace --all-features -- -D warnings: clean. With--all-targets, the lint set is the same as onorigin/main; the review commit fixed the newneedless_borrows/unnecessary_to_ownedhits.cargo test -p socket-patch-core --all-features --lib: 6997 passed, 4 failed. The same 4 fail on unmodifiedorigin/mainin this sandbox, because it runs as root and root ignores the read-only dirs they rely on (same as Build: share one test RNG across randomized tests (#1868) #1882):copy_tree::relax_loop_must_not_traverse_symlinked_root,vlt_heal::an_unremovable_hidden_lock_keeps_every_store_entry,pypi_poetry::wire_write_failure_maps_error_and_leaves_lock_untouched,pypi_requirements::wire_failure_rolls_back_already_written_files.cargo test -p socket-patch-core --all-features --test upstream_restore_golden --test crawler_deno_e2e --test crawler_npm_e2e: 53 + 123 + 13 passed.test_env_tests): restore after set, unset and a repeated key;with_envrestores after a panic (the oldwith_env/with_home_envfailed this); a non-UTF-8 previous value survives (the oldStringcopies lost it).x86_64-apple-darwin/ windows locally becauseringneeds a C cross toolchain. That's why the macOS dispatch above exists.https://claude.ai/code/session_011f9aePwCD7qBYF59E3rCfT
Generated by Claude Code