Skip to content

Commit

Permalink
fix: version guess from tags that don't start with v, but are still…
Browse files Browse the repository at this point in the history
… PEP440 compliant (#1048)

Without this change, Dunamai by default needs tags to be prefixed with a 'v'. ie: v0.1.2 v1.1.2 ...

Fixes #1042.

Co-authored-by: cclecle <[email protected]>
Co-authored-by: Jairo Llopis <[email protected]>
  • Loading branch information
3 people authored Oct 1, 2023
1 parent f8614fc commit e38caa2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 6 additions & 2 deletions copier/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,13 @@ def version(self) -> Optional[Version]:
return None
try:
with local.cwd(self.local_abspath):
# Leverage dunamai by default; usually it gets best results
# Leverage dunamai by default; usually it gets best results.
# `dunamai.Version.from_git` needs `Pattern.DefaultUnprefixed`
# to be PEP440 compliant on version reading
return Version(
dunamai.Version.from_git().serialize(style=dunamai.Style.Pep440)
dunamai.Version.from_git(
pattern=dunamai.Pattern.DefaultUnprefixed
).serialize(style=dunamai.Style.Pep440)
)
except ValueError:
# A fully descriptive commit can be easily detected converted into a
Expand Down
48 changes: 48 additions & 0 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
assert_file,
build_file_tree,
filecmp,
git_save,
render,
)

Expand Down Expand Up @@ -69,6 +70,53 @@ def test_copy_with_non_version_tags(tmp_path_factory: pytest.TempPathFactory) ->
)


@pytest.mark.parametrize(
"tag2, use_prereleases, expected_version",
[
("1.2.3.post1", True, "2"),
("1.2.4.dev1", True, "2"),
("1.2.3.post1", False, "2"),
("1.2.4.dev1", False, "1"),
],
)
@pytest.mark.parametrize("prefix", ["", "v"])
def test_tag_autodetect_v_prefix_optional(
tmp_path_factory: pytest.TempPathFactory,
tag2: str,
use_prereleases: bool,
expected_version,
prefix: str,
) -> None:
"""Tags with and without `v` prefix should work the same."""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "version.txt"): "1",
(
src / "{{_copier_conf.answers_file}}.jinja"
): "{{ _copier_answers|to_nice_yaml -}}",
}
)
# One stable tag
git_save(src, tag=f"{prefix}1.2.3")
# One pre or post release tag
(src / "version.txt").write_text("2")
git_save(src, tag=f"{prefix}{tag2}")
# One untagged change
(src / "version.txt").write_text("3")
git_save(src)
# Copy, leaving latest tag auto-detection
copier.run_copy(
str(src),
dst,
use_prereleases=use_prereleases,
defaults=True,
unsafe=True,
)
# Check it works as expected
assert (dst / "version.txt").read_text() == expected_version


def test_copy_with_non_version_tags_and_vcs_ref(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
Expand Down

0 comments on commit e38caa2

Please sign in to comment.