diff --git a/dunamai/__init__.py b/dunamai/__init__.py index 091a3da..c9dd494 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -57,7 +57,11 @@ 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), @@ -65,6 +69,7 @@ def _run_cmd( 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: diff --git a/tests/integration/test_dunamai.py b/tests/integration/test_dunamai.py index 14259ac..e9904e2 100644 --- a/tests/integration/test_dunamai.py +++ b/tests/integration/test_dunamai.py @@ -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 @@ -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"