Skip to content

Commit

Permalink
Add test for nonchronological commits
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Dec 17, 2020
1 parent 8ba855e commit cd3d823
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
7 changes: 6 additions & 1 deletion dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ class Vcs(Enum):


def _run_cmd(
command: str, codes: Sequence[int] = (0,), where: Path = None, shell: bool = False
command: str,
codes: Sequence[int] = (0,),
where: Path = None,
shell: bool = False,
env: dict = None,
) -> Tuple[int, str]:
result = subprocess.run(
shlex.split(command),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=str(where) if where is not None else None,
shell=shell,
env=env,
)
output = result.stdout.decode().strip()
if codes and result.returncode not in codes:
Expand Down
41 changes: 39 additions & 2 deletions tests/integration/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def chdir(where: Path) -> Iterator[None]:


def make_run_callback(where: Path) -> Callable:
def inner(command, expected_code: int = 0):
_, out = _run_cmd(command, where=where, codes=[expected_code])
def inner(command, expected_code: int = 0, env: dict = None):
_, out = _run_cmd(command, where=where, codes=[expected_code], env=env)
return out

return inner
Expand Down Expand Up @@ -218,6 +218,43 @@ def test__version__from_git__with_mixed_tags(tmp_path) -> None:
assert from_vcs(latest_tag=True) == Version("0.3.0", commit="abc", dirty=False)


@pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
def test__version__from_git__with_nonchronological_commits(tmp_path) -> None:
vcs = tmp_path / "dunamai-git-nonchronological"
vcs.mkdir()
run = make_run_callback(vcs)
from_vcs = make_from_callback(Version.from_git)

with chdir(vcs):
run("git init")
(vcs / "foo.txt").write_text("hi")
run("git add .")
run(
'git commit -m "Initial commit"',
env={
"GIT_COMMITTER_DATE": "2000-01-02T01:00:00",
"GIT_AUTHOR_DATE": "2000-01-02T01:00:00",
**os.environ,
},
)

run("git tag v0.1.0")
(vcs / "foo.txt").write_text("hi 2")
run("git add .")
avoid_identical_ref_timestamps()
run(
'git commit -m "Second"',
env={
"GIT_COMMITTER_DATE": "2000-01-01T01:00:00",
"GIT_AUTHOR_DATE": "2000-01-01T01:00:00",
**os.environ,
},
)

run("git tag v0.2.0")
assert from_vcs() == Version("0.2.0", commit="abc", dirty=False)


@pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
def test__version__not_a_repository(tmp_path) -> None:
vcs = tmp_path / "dunamai-not-a-repo"
Expand Down

0 comments on commit cd3d823

Please sign in to comment.