From 6c80ab77725cc9fbf7cb540461161b23cf6fc00e Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 6 Dec 2026 19:10:56 +0000 Subject: [PATCH] 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 --- tests/scripts/git_env.py | 12 ++++++++++++ tests/scripts/test_check_bot_pr_diff.py | 13 +++++++------ tests/scripts/test_check_image_versions.py | 8 ++------ tests/scripts/test_check_scenarios_load_checkout.py | 7 ++++++- tests/scripts/test_select_molecule_scenarios.py | 6 ++---- 5 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 tests/scripts/git_env.py diff --git a/tests/scripts/git_env.py b/tests/scripts/git_env.py new file mode 100644 index 0000000..fce8bb7 --- /dev/null +++ b/tests/scripts/git_env.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: Defame1297 and contributors +# SPDX-License-Identifier: GPL-3.0-or-later +"""The environment for a test's git fixture: no caller GIT_* and no user or system config.""" + +import os + + +def isolated(**extra): + """A copy of os.environ for git: a hook exports GIT_DIR, which would point it at the real repo.""" + env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} + env.update(GIT_CONFIG_GLOBAL="/dev/null", GIT_CONFIG_NOSYSTEM="1", **extra) + return env diff --git a/tests/scripts/test_check_bot_pr_diff.py b/tests/scripts/test_check_bot_pr_diff.py index e8ee19c..40404d7 100644 --- a/tests/scripts/test_check_bot_pr_diff.py +++ b/tests/scripts/test_check_bot_pr_diff.py @@ -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, diff --git a/tests/scripts/test_check_image_versions.py b/tests/scripts/test_check_image_versions.py index afe3dc1..814f2bc 100644 --- a/tests/scripts/test_check_image_versions.py +++ b/tests/scripts/test_check_image_versions.py @@ -2,7 +2,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later """Run with: python3 -m unittest discover -s tests/scripts""" -import os import pathlib import subprocess import sys @@ -12,6 +11,7 @@ import unittest SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts" sys.path.insert(0, str(SCRIPTS)) import check_image_versions as civ +import git_env DIGEST = "sha256:" + "a" * 64 JOB = f"ubuntu:24.04@{DIGEST}" @@ -67,11 +67,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(), ) diff --git a/tests/scripts/test_check_scenarios_load_checkout.py b/tests/scripts/test_check_scenarios_load_checkout.py index 03f2f05..7045bb4 100644 --- a/tests/scripts/test_check_scenarios_load_checkout.py +++ b/tests/scripts/test_check_scenarios_load_checkout.py @@ -14,6 +14,7 @@ import sys import tempfile import unittest +import git_env import yaml REPO = pathlib.Path(__file__).resolve().parents[2] @@ -44,7 +45,11 @@ class CheckScenariosLoadCheckoutTest(unittest.TestCase): self.checkout = collection(self.ac_root) (self.checkout / "scripts").mkdir() # molecule's CLI finds the base config from the repository root, as in the real checkout - subprocess.run(["git", "init", "-q", str(self.checkout)], check=True) + subprocess.run( + ["git", "init", "-q", str(self.checkout)], + check=True, + env=git_env.isolated(), + ) self.base_config = self.checkout / ".config" / "molecule" / "config.yml" self.base_config.parent.mkdir(parents=True) self.base_config.write_text("prerun: false\n") diff --git a/tests/scripts/test_select_molecule_scenarios.py b/tests/scripts/test_select_molecule_scenarios.py index e49ac30..8dfb30c 100644 --- a/tests/scripts/test_select_molecule_scenarios.py +++ b/tests/scripts/test_select_molecule_scenarios.py @@ -17,6 +17,7 @@ import yaml REPO = pathlib.Path(__file__).resolve().parents[2] sys.path.insert(0, str(REPO / "scripts")) +import git_env import select_molecule_scenarios as sel ROLE_KEYS = { @@ -409,9 +410,6 @@ class MainTest(unittest.TestCase): (d / "molecule.yml").write_text("---\n") def git(self, root, *args): - # No GIT_* from the caller (a hook exports GIT_DIR), no user or system config. - env = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")} - env.update(GIT_CONFIG_GLOBAL="/dev/null", GIT_CONFIG_NOSYSTEM="1") subprocess.run( [ "git", @@ -427,7 +425,7 @@ class MainTest(unittest.TestCase): ], check=True, capture_output=True, - env=env, + env=git_env.isolated(), ) def topic_repo(self, root, files):