Skip to content

Commit

Permalink
Expose git_repo and hg_repo as fixtures and add commit_tree method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco authored Aug 6, 2024
2 parents b10534e + 9e64abe commit 40042da
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 40 deletions.
53 changes: 15 additions & 38 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
import pathlib

import pytest

import jaraco.path
from jaraco import vcs


@pytest.fixture(autouse=True)
def _isolate_home(tmp_home_dir):
"""Isolate the tests from a developer's VCS config."""


def _ensure_present(mgr):
try:
mgr.version()
except Exception:
pytest.skip()


@pytest.fixture
def temp_work_dir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return tmp_path
rev1 = dict(
bar=dict(
baz="",
),
)


source_tree = dict(
rev2 = dict(
bar=dict(
baz="",
baz="content",
),
)


@pytest.fixture
def hg_repo(temp_work_dir):
repo = vcs.Mercurial()
_ensure_present(repo)
repo._invoke('init', '.')
jaraco.path.build(source_tree)
repo._invoke('addremove')
repo._invoke('ci', '-m', 'committed')
pathlib.Path('bar/baz').write_text('content', encoding='utf-8')
repo._invoke('ci', '-m', 'added content')
def hg_repo(hg_repo):
repo = hg_repo
repo.commit_tree(rev1, 'committed')
repo.commit_tree(rev2, 'added content')
return repo


@pytest.fixture
def git_repo(temp_work_dir):
repo = vcs.Git()
_ensure_present(repo)
repo._invoke('init')
repo._invoke('config', 'user.email', 'vip@example.com')
repo._invoke('config', 'user.name', 'Important User')
jaraco.path.build(source_tree)
repo._invoke('add', '.')
repo._invoke('commit', '-m', 'committed')
pathlib.Path('bar/baz').write_text('content', encoding='utf-8')
repo._invoke('commit', '-am', 'added content')
def git_repo(git_repo):
repo = git_repo
repo.commit_tree(rev1, 'committed')
repo.commit_tree(rev2, 'added content')
return repo
6 changes: 6 additions & 0 deletions jaraco/vcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ def age(self):
Return the age of the repo.
"""
raise NotImplementedError()

def commit_tree(self, spec, msg="committed"):
"""
Apply the tree in spec and commit.
"""
raise NotImplementedError()
17 changes: 16 additions & 1 deletion jaraco/vcs/cmd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import abc
import collections
import itertools
import operator
Expand All @@ -6,13 +7,17 @@
import subprocess

import dateutil.parser
import jaraco.path
from tempora import utc


TaggedRevision = collections.namedtuple('TaggedRevision', 'tag revision')


class Command:
class Command(metaclass=abc.ABCMeta):
@abc.abstractmethod
def _invoke(self, *args): ...

def is_valid(self):
try:
# Check if both command and repo are valid
Expand Down Expand Up @@ -150,6 +155,11 @@ def sub_paths(self):
def _get_timestamp_str(self, rev):
return self._invoke('log', '-l', '1', '--template', '{date|isodate}', '-r', rev)

def commit_tree(self, spec, message: str = 'committed'):
jaraco.path.build(spec)
self._invoke('addremove')
self._invoke('commit', '-m', message)


class Git(Command):
exe = 'git'
Expand Down Expand Up @@ -225,3 +235,8 @@ def age(self):
proc.wait()
proc.stdout.close()
return utc.now() - dateutil.parser.parse(first_line)

def commit_tree(self, spec, message: str = 'committed'):
jaraco.path.build(spec)
self._invoke('add', '.')
self._invoke('commit', '-m', message)
34 changes: 34 additions & 0 deletions jaraco/vcs/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from .. import vcs


def _ensure_present(repo):
try:
repo.version()
except Exception:
pytest.skip()


@pytest.fixture
def temp_work_dir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return tmp_path


@pytest.fixture
def hg_repo(temp_work_dir):
repo = vcs.Mercurial()
_ensure_present(repo)
repo._invoke('init', '.')
return repo


@pytest.fixture
def git_repo(temp_work_dir):
repo = vcs.Git()
_ensure_present(repo)
repo._invoke('init')
repo._invoke('config', 'user.email', 'vip@example.com')
repo._invoke('config', 'user.name', 'Important User')
return repo
1 change: 1 addition & 0 deletions newsfragments/36.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Presented hg_repo and git_repo as fixtures.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies = [
"jaraco.versioning",
"python-dateutil",
"tempora",
"jaraco.path",
]
dynamic = ["version"]

Expand All @@ -43,7 +44,6 @@ test = [

# local
"pygments",
"jaraco.path",
"types-python-dateutil",
"pytest-home",
]
Expand All @@ -59,3 +59,6 @@ doc = [
]

[tool.setuptools_scm]

[project.entry-points]
pytest11 = {"jaraco.vcs.fixtures" = "jaraco.vcs.fixtures"}

0 comments on commit 40042da

Please sign in to comment.