Skip to content

Commit

Permalink
Add features to Git repository test fixture
Browse files Browse the repository at this point in the history
- get commit hash at HEAD
- create a branch
- refactor shell command invocation helpers
  • Loading branch information
akaihola committed Nov 1, 2020
1 parent 5fd3c13 commit a8df754
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/darker/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Configuration and fixtures for the Pytest based test suite"""

import sys
import types
from pathlib import Path
from subprocess import check_call
from typing import Dict, Optional
from unittest.mock import patch
Expand All @@ -8,6 +11,8 @@
from black import find_project_root
from py.path import local as LocalPath

from darker.git import _git_check_output_lines


@pytest.fixture
def without_isort():
Expand All @@ -25,6 +30,16 @@ class GitRepoFixture:
def __init__(self, root: LocalPath):
self.root = root

@classmethod
def create_repository(cls, root: LocalPath) -> "GitRepoFixture":
"""Fixture method for creating a Git repository in the given directory"""
check_call(["git", "init"], cwd=root)
return cls(root)

def _run(self, *args: str) -> None:
"""Helper method to run a Git command line in the repository root"""
check_call(["git"] + list(args), cwd=self.root)

def add(
self, paths_and_contents: Dict[str, Optional[str]], commit: str = None
) -> Dict[str, LocalPath]:
Expand All @@ -44,21 +59,29 @@ def add(
for relative_path, content in paths_and_contents.items():
path = absolute_paths[relative_path]
if content is None:
check_call(["git", "rm", "--", relative_path], cwd=self.root)
self._run("rm", "--", relative_path)
else:
path.write(content, ensure=True)
check_call(["git", "add", "--", relative_path], cwd=self.root)
self._run("add", "--", relative_path)
if commit:
check_call(["git", "commit", "-m", commit], cwd=self.root)
self._run("commit", "-m", commit)
return absolute_paths

def get_hash(self) -> str:
"""Return the commit hash at HEAD in the Git repository"""
return _git_check_output_lines(["git", "rev-parse", "HEAD"], Path(self.root))[0]

def create_branch(self, new_branch: str, start_point: str) -> None:
"""Fixture method to create and check out new branch at given starting point"""
self._run("checkout", "-b", new_branch, start_point)


@pytest.fixture
def git_repo(tmpdir, monkeypatch):
"""Create a temporary Git repository and change current working directory into it"""
check_call(["git", "init"], cwd=tmpdir)
repository = GitRepoFixture.create_repository(tmpdir)
monkeypatch.chdir(tmpdir)
return GitRepoFixture(tmpdir)
return repository


@pytest.fixture
Expand Down

0 comments on commit a8df754

Please sign in to comment.