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

Add GitRepoFixture helper methods needed by #80 #87

Merged
merged 1 commit into from
Nov 13, 2020
Merged
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
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