From 4f594cd2cbf68caabb7d3f22397104f5aa1b49b7 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Wed, 6 Sep 2023 18:59:12 -0400 Subject: [PATCH] Set protocol.file.allow only in tests that need it Instead of setting environment variables just on CI and for the the entire pytest command, this has the two test cases that need protocol.file.allow to be set to "always" (instead of "user") set them, via a shared fixture, just while those tests are running. Both on CI and for local test runs, this makes it no longer necessary to set this in a global configuration or through environment variables, reducing the setup needed to run the tests. --- .github/workflows/cygwin-test.yml | 4 ---- .github/workflows/pythonpackage.yml | 4 ---- test/test_submodule.py | 22 +++++++++++++++++++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.github/workflows/cygwin-test.yml b/.github/workflows/cygwin-test.yml index b612ca94a..808dc5608 100644 --- a/.github/workflows/cygwin-test.yml +++ b/.github/workflows/cygwin-test.yml @@ -46,8 +46,4 @@ jobs: shell: bash.exe -eo pipefail -o igncr "{0}" run: | /usr/bin/python -m pytest - env: - GIT_CONFIG_COUNT: "1" - GIT_CONFIG_KEY_0: protocol.file.allow - GIT_CONFIG_VALUE_0: always continue-on-error: false diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 207714810..a6af507d1 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -56,10 +56,6 @@ jobs: run: | set -x pytest - env: - GIT_CONFIG_COUNT: "1" - GIT_CONFIG_KEY_0: protocol.file.allow - GIT_CONFIG_VALUE_0: always continue-on-error: false - name: Documentation diff --git a/test/test_submodule.py b/test/test_submodule.py index 982226411..a039faaf3 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php +import contextlib import os import shutil import tempfile from pathlib import Path import sys -from unittest import skipIf +from unittest import mock, skipIf import pytest @@ -31,6 +32,23 @@ import os.path as osp +@contextlib.contextmanager +def _allow_file_protocol(): + """Temporarily set protocol.file.allow to always, using environment variables.""" + pair_index = int(os.getenv("GIT_CONFIG_COUNT", "0")) + + # This is recomputed each time the context is entered, for compatibility with + # existing GIT_CONFIG_* environment variables, even if changed in this process. + patcher = mock.patch.dict(os.environ, { + "GIT_CONFIG_COUNT": str(pair_index + 1), + f"GIT_CONFIG_KEY_{pair_index}": "protocol.file.allow", + f"GIT_CONFIG_VALUE_{pair_index}": "always", + }) + + with patcher: + yield + + class TestRootProgress(RootUpdateProgress): """Just prints messages, for now without checking the correctness of the states""" @@ -709,6 +727,7 @@ def test_add_empty_repo(self, rwdir): # end for each checkout mode @with_rw_directory + @_allow_file_protocol() def test_list_only_valid_submodules(self, rwdir): repo_path = osp.join(rwdir, "parent") repo = git.Repo.init(repo_path) @@ -737,6 +756,7 @@ def test_list_only_valid_submodules(self, rwdir): """, ) @with_rw_directory + @_allow_file_protocol() def test_git_submodules_and_add_sm_with_new_commit(self, rwdir): parent = git.Repo.init(osp.join(rwdir, "parent")) parent.git.submodule("add", self._small_repo_url(), "module")