Skip to content

Commit

Permalink
Add test to ensure that we always create repo with branch master
Browse files Browse the repository at this point in the history
We also add a fixture which creates a predictable empty gitconfig for
the tests.
  • Loading branch information
simu committed Feb 9, 2024
1 parent b71a28e commit 26c9023
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""
from __future__ import annotations

import os

from pathlib import Path
from typing import Protocol

Expand All @@ -23,6 +25,23 @@ def __call__(self, args: list[str]) -> Result:
...


@pytest.fixture(autouse=True)
def gitconfig(tmp_path: Path) -> Path:
"""Ensure that tests have a predictable empty gitconfig.
We set autouse=True, so that the fixture is automatically used for all
tests. Tests that want to access the mock gitconfig can explicitly specify
the fixutre, so they get the path to the mock gitconfig.
"""
os.environ["GIT_CONFIG_NOSYSTEM"] = "true"
os.environ["HOME"] = str(tmp_path)
os.environ["XDG_CONFIG_HOME"] = str(tmp_path / ".config")
gitconfig = tmp_path / ".config" / "git" / "config"
os.makedirs(gitconfig.parent, exist_ok=True)

return gitconfig


@pytest.fixture
def cli_runner() -> RunnerFunc:
r = CliRunner()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_gitrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,14 @@ def test_gitrepo_is_ahead_of_remote_local_branch(tmp_path: Path):
# verify that our local branch is ahead of both local and remote tracking master
assert len(list(r.repo.iter_commits("master..local"))) == 1
assert len(list(r.repo.iter_commits("origin/master..local"))) == 1


def test_gitrepo_init_always_master(gitconfig: Path, tmp_path: Path):
cfg = git.config.GitConfigParser(file_or_files=gitconfig, read_only=False)
cfg.add_value("init", "defaultBranch", "main").write()

r = gitrepo.GitRepo(None, tmp_path / "repo.git")
r.commit("Initial commit")

assert len(r._repo.heads) == 1
assert r._repo.heads[0].name == "master"

0 comments on commit 26c9023

Please sign in to comment.