diff --git a/conftest.py b/conftest.py index db6264a..fe68ac5 100644 --- a/conftest.py +++ b/conftest.py @@ -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 diff --git a/jaraco/vcs/base.py b/jaraco/vcs/base.py index 1296f6c..bac42ea 100755 --- a/jaraco/vcs/base.py +++ b/jaraco/vcs/base.py @@ -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() diff --git a/jaraco/vcs/cmd.py b/jaraco/vcs/cmd.py index 6eb4626..4929ac4 100644 --- a/jaraco/vcs/cmd.py +++ b/jaraco/vcs/cmd.py @@ -1,3 +1,4 @@ +import abc import collections import itertools import operator @@ -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 @@ -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' @@ -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) diff --git a/jaraco/vcs/fixtures.py b/jaraco/vcs/fixtures.py new file mode 100644 index 0000000..f24479e --- /dev/null +++ b/jaraco/vcs/fixtures.py @@ -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 diff --git a/newsfragments/36.feature.rst b/newsfragments/36.feature.rst new file mode 100644 index 0000000..278cf0e --- /dev/null +++ b/newsfragments/36.feature.rst @@ -0,0 +1 @@ +Presented hg_repo and git_repo as fixtures. diff --git a/pyproject.toml b/pyproject.toml index 882e1d5..b11589e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ dependencies = [ "jaraco.versioning", "python-dateutil", "tempora", + "jaraco.path", ] dynamic = ["version"] @@ -43,7 +44,6 @@ test = [ # local "pygments", - "jaraco.path", "types-python-dateutil", "pytest-home", ] @@ -59,3 +59,6 @@ doc = [ ] [tool.setuptools_scm] + +[project.entry-points] +pytest11 = {"jaraco.vcs.fixtures" = "jaraco.vcs.fixtures"}