Skip to content

Commit 4f594cd

Browse files
authored
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.
1 parent 92d9ae2 commit 4f594cd

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

Diff for: .github/workflows/cygwin-test.yml

-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,4 @@ jobs:
4646
shell: bash.exe -eo pipefail -o igncr "{0}"
4747
run: |
4848
/usr/bin/python -m pytest
49-
env:
50-
GIT_CONFIG_COUNT: "1"
51-
GIT_CONFIG_KEY_0: protocol.file.allow
52-
GIT_CONFIG_VALUE_0: always
5349
continue-on-error: false

Diff for: .github/workflows/pythonpackage.yml

-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ jobs:
5656
run: |
5757
set -x
5858
pytest
59-
env:
60-
GIT_CONFIG_COUNT: "1"
61-
GIT_CONFIG_KEY_0: protocol.file.allow
62-
GIT_CONFIG_VALUE_0: always
6359
continue-on-error: false
6460

6561
- name: Documentation

Diff for: test/test_submodule.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# -*- coding: utf-8 -*-
22
# This module is part of GitPython and is released under
33
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
4+
import contextlib
45
import os
56
import shutil
67
import tempfile
78
from pathlib import Path
89
import sys
9-
from unittest import skipIf
10+
from unittest import mock, skipIf
1011

1112
import pytest
1213

@@ -31,6 +32,23 @@
3132
import os.path as osp
3233

3334

35+
@contextlib.contextmanager
36+
def _allow_file_protocol():
37+
"""Temporarily set protocol.file.allow to always, using environment variables."""
38+
pair_index = int(os.getenv("GIT_CONFIG_COUNT", "0"))
39+
40+
# This is recomputed each time the context is entered, for compatibility with
41+
# existing GIT_CONFIG_* environment variables, even if changed in this process.
42+
patcher = mock.patch.dict(os.environ, {
43+
"GIT_CONFIG_COUNT": str(pair_index + 1),
44+
f"GIT_CONFIG_KEY_{pair_index}": "protocol.file.allow",
45+
f"GIT_CONFIG_VALUE_{pair_index}": "always",
46+
})
47+
48+
with patcher:
49+
yield
50+
51+
3452
class TestRootProgress(RootUpdateProgress):
3553
"""Just prints messages, for now without checking the correctness of the states"""
3654

@@ -709,6 +727,7 @@ def test_add_empty_repo(self, rwdir):
709727
# end for each checkout mode
710728

711729
@with_rw_directory
730+
@_allow_file_protocol()
712731
def test_list_only_valid_submodules(self, rwdir):
713732
repo_path = osp.join(rwdir, "parent")
714733
repo = git.Repo.init(repo_path)
@@ -737,6 +756,7 @@ def test_list_only_valid_submodules(self, rwdir):
737756
""",
738757
)
739758
@with_rw_directory
759+
@_allow_file_protocol()
740760
def test_git_submodules_and_add_sm_with_new_commit(self, rwdir):
741761
parent = git.Repo.init(osp.join(rwdir, "parent"))
742762
parent.git.submodule("add", self._small_repo_url(), "module")

0 commit comments

Comments
 (0)