Skip to content

Commit

Permalink
isolate git config in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry committed Mar 26, 2024
1 parent 59b7af0 commit ea7f0c7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/check-test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ jobs:

- run: pip install .[dev]
- uses: pre-commit/action@v3.0.1
- name: Run tests
run: |
git config --global user.email "olivaw@iterative.ai"
git config --global user.name "Olivaw"
pytest
env:
GITHUB_MATRIX_OS: ${{ matrix.os }}
GITHUB_MATRIX_PYTHON: ${{ matrix.python }}
- run: pytest

- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v4
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Source = "https://github.com/iterative/gto"
[project.optional-dependencies]
tests = [
"freezegun",
"pygit2",
"pytest",
"pytest-cov",
"pytest-mock",
Expand Down Expand Up @@ -104,7 +105,7 @@ extend-select = ["I", "B", "C4", "C90", "T10", "Q"]
known-first-party = ["gto", "tests"]

[tool.pylint.master]
extension-pkg-whitelist = ["pydantic", "pytest-lazy-fixture", "pytest"]
extension-pkg-whitelist = ["pydantic", "pytest-lazy-fixture", "pygit2", "pytest"]
load-plugins= ["pylint.extensions.no_self_use"]

[tool.pylint.message_control]
Expand Down
32 changes: 31 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# pylint: disable=redefined-outer-name
import os
import sys
from time import sleep
from typing import Tuple
from typing import Iterator, Tuple

import pygit2
import pytest
from click.testing import Result
from pytest_test_utils import TmpDir
Expand All @@ -27,6 +29,34 @@ def runner() -> Runner:
return Runner()


@pytest.fixture(scope="session", autouse=True)
def isolate(tmp_path_factory: pytest.TempPathFactory) -> Iterator[None]:
path = tmp_path_factory.mktemp("mock")
home_dir = path / "home"
home_dir.mkdir()

monkeypatch = pytest.MonkeyPatch()
if sys.platform == "win32":
home_drive, home_path = os.path.splitdrive(home_dir)
monkeypatch.setenv("USERPROFILE", str(home_dir))
monkeypatch.setenv("HOMEDRIVE", home_drive)
monkeypatch.setenv("HOMEPATH", home_path)
else:
monkeypatch.setenv("HOME", str(home_dir))

monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
contents = b"""
[user]
name=GTO Tester
email=gtotester@example.com
"""
(home_dir / ".gitconfig").write_bytes(contents)
pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = str(home_dir)

yield
monkeypatch.undo()


@pytest.fixture
def scm(tmp_dir: TmpDir) -> Git:
scm_instance = Git.init(tmp_dir)
Expand Down

0 comments on commit ea7f0c7

Please sign in to comment.