Repository navigation
test: clear inherited GIT_* env once per test file so real-Git suites cannot write into an outer repo - #35859
Conversation
… cannot write into an outer repo
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 9 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
[Low impact] The change is limited to the shared unit-test setup. The PR appears safe to merge, with a non-blocking request for a direct regression test. Findings
SummaryClears nine inherited Git environment variables before unit test files load.
Reviews (1) · Last reviewed commit: "test: clear only repo-locating Git env s..." · Reviewed by Greptile |
| for (const name of [ | ||
| 'GIT_DIR', | ||
| 'GIT_WORK_TREE', | ||
| 'GIT_INDEX_FILE', | ||
| 'GIT_COMMON_DIR', | ||
| 'GIT_OBJECT_DIRECTORY', | ||
| 'GIT_ALTERNATE_OBJECT_DIRECTORIES', | ||
| 'GIT_NAMESPACE', | ||
| 'GIT_PREFIX', | ||
| 'GIT_QUARANTINE_PATH' | ||
| ]) { | ||
| delete process.env[name] |
There was a problem hiding this comment.
Cleanup lacks regression coverage
The new shared cleanup has no direct automated test. Existing Git tests can pass in a clean environment even if this setup stops running, leaving the protection against changing a developer’s repository untested.
Add a test that starts the runner with inherited repository pointers, checks they are gone before the test module loads, and confirms that GIT_EXEC_PATH and values set in beforeAll survive.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
ELI5
Many unit tests create a throwaway Git repository and run real
gitin it, and Orca's own Git code that they exercise runsgittoo. When the test runner is started from inside a Git hook, Git puts variables likeGIT_DIRandGIT_INDEX_FILEin the environment. Every one of those commands then quietly works on the outer repository instead of the throwaway one, so commits, branches and worktrees land in the developer's real repo. This clears those inherited variables once for every test file.What Changed
config/scripts/vitest-inherited-git-env-setup.ts, registered inUNIT_SETUP_FILESandconfig/tsconfig.node.json. When each test file loads, it deletes the inherited variables that tell git which repository to work on:GIT_DIR,GIT_WORK_TREE,GIT_INDEX_FILE,GIT_COMMON_DIR,GIT_OBJECT_DIRECTORY,GIT_ALTERNATE_OBJECT_DIRECTORIES,GIT_NAMESPACE,GIT_PREFIXandGIT_QUARANTINE_PATH.GIT_EXEC_PATHand theGIT_CONFIG_*variables are deliberately kept. The Git compatibility CI lane setsGIT_EXEC_PATHto point the bundled git 2.25.5 binary at its helper programs, and that lane uses this vitest config.vitest-caller-identity-env-setup.ts, which clears inherited agent identity.GIT_*on purpose are unaffected. The clearing happens before the test file's own module code,beforeAll,beforeEachand test bodies run, so their values are set afterwards and kept.Why
#35853 fixed this in one suite after it was reproduced: running
runtime-failed-local-worktree-removal.test.tswithGIT_DIRpointing at another repo passed, yet wrote the test's commit, branch and worktree into that repo. Other suites that run real Git have the same exposure, so doing it once in setup covers all of them.Clearing at file load, rather than in a global
beforeEach, matters: abeforeEachwould also wipeGIT_*values that a suite sets inbeforeAllor at module scope.Linked Issue
N/A (maintainer PR; no existing issue).
Visual Proof
N/A: test infrastructure only.
Testing
GIT_DIR,GIT_WORK_TREE,GIT_INDEX_FILEandGIT_PREFIXset to point at an outer repo, I ran 8 real-Git suites: remove-worktree, worktree-removal-real-git, merge-tree-object-quarantine-real-git, local-pty-provider-spawn-env, runtime-failed-local-worktree-removal, worktree-shared-directories, worktree-safety-real-git and git-binary-compatibility. 113 passed, 39 skipped, and the outer repo stayed empty (0 commits, 0 worktrees). An earlier revision of this PR, which cleared everyGIT_*variable, passed 144 tests in 9 suites the same way.config/scripts/ci-unit-selection.test.mjspasses (27).pnpm tcandpnpm run check:code-quality:changedpass. CI runs the full suite.Review
Agent skill upstream boundary
Notes
Applies to the unit projects (node, bun, node-runtime) that use
UNIT_SETUP_FILES. Cross-platform: an environment-variable delete only.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)