Skip to content

Commit

Permalink
Set protocol.file.allow only in tests that need it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliahKagan authored Sep 6, 2023
1 parent 92d9ae2 commit 8c77e95
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/cygwin-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion test/test_submodule.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"""

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 8c77e95

Please sign in to comment.