From a89eb50b5c46a3980a548a0265490c8d48023514 Mon Sep 17 00:00:00 2001 From: Stefan Hackmann Date: Fri, 16 Oct 2026 08:43:38 +0000 Subject: [PATCH] fix: Isolate tests from the calling git process's environment --- tests/conftest.py | 15 +++++++++++++++ tests/test_git_environment.py | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/test_git_environment.py diff --git a/tests/conftest.py b/tests/conftest.py index b296564..e9b3ceb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,12 +18,27 @@ from collections.abc import Callable import json +import os from pathlib import Path from typing import Any import pytest _FIXTURE_DIR = Path(__file__).resolve().parent / "fixtures" +GIT_ENVIRONMENT_PREFIX = "GIT_" + + +def pytest_configure(config: pytest.Config) -> None: + """Remove every ``GIT_*`` variable before any fixture or xdist worker starts. + + A hook run by ``git commit -a`` exports an absolute ``GIT_INDEX_FILE`` that + points at this repository's ``index.lock``; a test that runs git in a + temporary repository would otherwise read and write that index instead. + """ + for name in [ + name for name in os.environ if name.startswith(GIT_ENVIRONMENT_PREFIX) + ]: + del os.environ[name] def _load_json_fixture(name: str) -> dict[str, Any]: diff --git a/tests/test_git_environment.py b/tests/test_git_environment.py new file mode 100644 index 0000000..302f8cd --- /dev/null +++ b/tests/test_git_environment.py @@ -0,0 +1,24 @@ +# Copyright 2026 DataRobot, Inc. and its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests run free of the environment a calling git process exports.""" + +from __future__ import annotations + +import os + + +def test_tests_see_no_git_environment_variables() -> None: + """A hook run by ``git commit -a`` exports ``GIT_INDEX_FILE``; tests never see it.""" + assert [name for name in os.environ if name.startswith("GIT_")] == []