Prevent MagicMock from leaking into global gitconfig in tests#83
Merged
Conversation
orchestrator.main() called `git config --global user.name/.email` with
values pulled off the `settings` object. Tests that patched `get_settings`
with a MagicMock left `effective_git_name` and `git_email` as auto-generated
MagicMock attributes — truthy, so the guard passed — and subprocess
stringified them ("MagicMock/mock.effective_git_name/<id>") into the
developer's real ~/.gitconfig, polluting subsequent commit authorship.
- Extract git identity setup into `_configure_global_git_identity` so tests
patch a single symbol.
- Add isinstance(str) guard so a non-string identity fails loudly instead
of corrupting global config.
- Fix existing tests: set string identities on the mock settings helper
and patch `_configure_global_git_identity` in all `main()` tests.
Five TestMain tests duplicated the same ~10-line patch stack. Collapse into a single `_patched_main` helper that uses ExitStack to apply all common patches and yields the mocks dict for per-test assertions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
orchestrator.main()rangit config --global user.name/.emailwithsettings.effective_git_name/settings.git_email. Tests patchedget_settingswith a bareMagicMock, leaving those as auto-attributes — truthy → guard passed → subprocess stringified the mock and wroteMagicMock/mock.effective_git_name/<id>into the developer's real~/.gitconfig, polluting commit authorship across all repos._configure_global_git_identityso tests can patch a single symbol.isinstance(str)guard so a non-string identity fails loudly instead of mutating global config._mock_settingshelper and patch_configure_global_git_identityin allmain()tests.Test plan
pytest tests/test_orchestrator.py— 28 passed~/.gitconfigunchanged after full suite run