test(scripts): keep git fixtures off the caller's GIT_* variables
A git hook exports GIT_DIR (and often GIT_INDEX_FILE). A fixture that passes the caller's environment to git would run its init, add and commit against this repository instead of its temporary one once the suite runs as a hook. tests/scripts/git_env.py gives each fixture a copy of the environment without any GIT_* key and with no user or system config. The bot pull request gate's repository tests also isolate os.environ, because the gate runs git in-process. Refs: #187 Assisted-by: Claude Code
This commit is contained in:
1 parent
d6037df6e4
commit
6c80ab7772
5 files changed
+29
-17
No files matched your search
@@ -13,6 +13,7 @@ from unittest import mock
|
||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
import check_bot_pr_diff as cbd
|
||||
import git_env
|
||||
from test_check_uv_lock import APM_NEW, APM_OLD, CLICK_OLD, UV_LOCK, uv_files
|
||||
|
||||
D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64
|
||||
@@ -51,11 +52,7 @@ def run(cwd, *cmd):
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env={
|
||||
**os.environ,
|
||||
"GIT_CONFIG_GLOBAL": "/dev/null",
|
||||
"GIT_CONFIG_SYSTEM": "/dev/null",
|
||||
},
|
||||
env=git_env.isolated(),
|
||||
)
|
||||
|
||||
|
||||
@@ -475,6 +472,10 @@ class RuleTest(unittest.TestCase):
|
||||
|
||||
class RepoTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# The gate runs git in-process, in the fixture's directory.
|
||||
patcher = mock.patch.dict(os.environ, git_env.isolated(), clear=True)
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.repo = pathlib.Path(self.tmp.name)
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
@@ -579,7 +580,7 @@ class RepoTest(unittest.TestCase):
|
||||
def test_empty_head_ref_on_a_pull_request_fails_closed(self):
|
||||
self.write("roles/x/main.yml", "a\n")
|
||||
self.commit("anything")
|
||||
env = {**os.environ, "EVENT_NAME": "pull_request"}
|
||||
env = git_env.isolated(EVENT_NAME="pull_request")
|
||||
proc = subprocess.run(
|
||||
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"],
|
||||
cwd=self.repo,
|
||||
|
||||
Reference in new issue
Block a user