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
@@ -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
|
||||||
@@ -13,6 +13,7 @@ from unittest import mock
|
|||||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||||
sys.path.insert(0, str(SCRIPTS))
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
import check_bot_pr_diff as cbd
|
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
|
from test_check_uv_lock import APM_NEW, APM_OLD, CLICK_OLD, UV_LOCK, uv_files
|
||||||
|
|
||||||
D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64
|
D1, D2 = "sha256:" + "a" * 64, "sha256:" + "b" * 64
|
||||||
@@ -51,11 +52,7 @@ def run(cwd, *cmd):
|
|||||||
check=True,
|
check=True,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
env={
|
env=git_env.isolated(),
|
||||||
**os.environ,
|
|
||||||
"GIT_CONFIG_GLOBAL": "/dev/null",
|
|
||||||
"GIT_CONFIG_SYSTEM": "/dev/null",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -475,6 +472,10 @@ class RuleTest(unittest.TestCase):
|
|||||||
|
|
||||||
class RepoTest(unittest.TestCase):
|
class RepoTest(unittest.TestCase):
|
||||||
def setUp(self):
|
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.tmp = tempfile.TemporaryDirectory()
|
||||||
self.repo = pathlib.Path(self.tmp.name)
|
self.repo = pathlib.Path(self.tmp.name)
|
||||||
self.addCleanup(self.tmp.cleanup)
|
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):
|
def test_empty_head_ref_on_a_pull_request_fails_closed(self):
|
||||||
self.write("roles/x/main.yml", "a\n")
|
self.write("roles/x/main.yml", "a\n")
|
||||||
self.commit("anything")
|
self.commit("anything")
|
||||||
env = {**os.environ, "EVENT_NAME": "pull_request"}
|
env = git_env.isolated(EVENT_NAME="pull_request")
|
||||||
proc = subprocess.run(
|
proc = subprocess.run(
|
||||||
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"],
|
[sys.executable, str(SCRIPTS / "check_bot_pr_diff.py"), "", "main"],
|
||||||
cwd=self.repo,
|
cwd=self.repo,
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
||||||
|
|
||||||
import os
|
|
||||||
import pathlib
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
@@ -12,6 +11,7 @@ import unittest
|
|||||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||||
sys.path.insert(0, str(SCRIPTS))
|
sys.path.insert(0, str(SCRIPTS))
|
||||||
import check_image_versions as civ
|
import check_image_versions as civ
|
||||||
|
import git_env
|
||||||
|
|
||||||
DIGEST = "sha256:" + "a" * 64
|
DIGEST = "sha256:" + "a" * 64
|
||||||
JOB = f"ubuntu:24.04@{DIGEST}"
|
JOB = f"ubuntu:24.04@{DIGEST}"
|
||||||
@@ -67,11 +67,7 @@ def run(cwd, *cmd):
|
|||||||
check=True,
|
check=True,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
env={
|
env=git_env.isolated(),
|
||||||
**os.environ,
|
|
||||||
"GIT_CONFIG_GLOBAL": "/dev/null",
|
|
||||||
"GIT_CONFIG_SYSTEM": "/dev/null",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import sys
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import git_env
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
REPO = pathlib.Path(__file__).resolve().parents[2]
|
REPO = pathlib.Path(__file__).resolve().parents[2]
|
||||||
@@ -44,7 +45,11 @@ class CheckScenariosLoadCheckoutTest(unittest.TestCase):
|
|||||||
self.checkout = collection(self.ac_root)
|
self.checkout = collection(self.ac_root)
|
||||||
(self.checkout / "scripts").mkdir()
|
(self.checkout / "scripts").mkdir()
|
||||||
# molecule's CLI finds the base config from the repository root, as in the real checkout
|
# 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 = self.checkout / ".config" / "molecule" / "config.yml"
|
||||||
self.base_config.parent.mkdir(parents=True)
|
self.base_config.parent.mkdir(parents=True)
|
||||||
self.base_config.write_text("prerun: false\n")
|
self.base_config.write_text("prerun: false\n")
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import yaml
|
|||||||
|
|
||||||
REPO = pathlib.Path(__file__).resolve().parents[2]
|
REPO = pathlib.Path(__file__).resolve().parents[2]
|
||||||
sys.path.insert(0, str(REPO / "scripts"))
|
sys.path.insert(0, str(REPO / "scripts"))
|
||||||
|
import git_env
|
||||||
import select_molecule_scenarios as sel
|
import select_molecule_scenarios as sel
|
||||||
|
|
||||||
ROLE_KEYS = {
|
ROLE_KEYS = {
|
||||||
@@ -409,9 +410,6 @@ class MainTest(unittest.TestCase):
|
|||||||
(d / "molecule.yml").write_text("---\n")
|
(d / "molecule.yml").write_text("---\n")
|
||||||
|
|
||||||
def git(self, root, *args):
|
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(
|
subprocess.run(
|
||||||
[
|
[
|
||||||
"git",
|
"git",
|
||||||
@@ -427,7 +425,7 @@ class MainTest(unittest.TestCase):
|
|||||||
],
|
],
|
||||||
check=True,
|
check=True,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
env=env,
|
env=git_env.isolated(),
|
||||||
)
|
)
|
||||||
|
|
||||||
def topic_repo(self, root, files):
|
def topic_repo(self, root, files):
|
||||||
|
|||||||
Reference in new issue
Block a user