Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isolate git config in tests #449

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]
- name: Run tests
run: |
git config --global user.email "[email protected]"
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 protected]
"""
(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