From f58f189a1f515400ebec3333b1581ebcf2426000 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Tue, 8 Jun 2021 15:23:10 +0200 Subject: [PATCH] Allow deriving when distname and module name mismatch (#13) --- get_version.py | 23 +++++++++++++---------- tests/test_packages.py | 24 ++++++++---------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/get_version.py b/get_version.py index abe1c87..083a593 100644 --- a/get_version.py +++ b/get_version.py @@ -16,7 +16,6 @@ RE_PEP440_VERSION = re.compile( r""" -v? (?:(?P[0-9]+)!)? (?P[0-9]+(?:\.[0-9]+)*) (?: @@ -51,28 +50,32 @@ def working_dir(dir_: Optional[os.PathLike] = None): os.chdir(curdir) -def get_version_from_dirname(name: str, parent: Path) -> Optional[str]: +def get_version_from_dirname(parent: Path) -> Optional[str]: """Extracted sdist""" parent = parent.resolve() - logger.info(f"dirname: Trying to get version of {name} from dirname {parent}") + logger.info(f"dirname: Trying to get version from dirname {parent}") - name_re = name.replace("_", "[_-]") - re_dirname = re.compile(f"{name_re}-{RE_PEP440_VERSION.pattern}$", re.VERBOSE) - if not re_dirname.match(parent.name): + re_dirname = re.compile( + f"[A-Za-z]+(?:[_-][A-Za-z]+)*-(?P{RE_PEP440_VERSION.pattern})$", + re.VERBOSE, + ) + match = re_dirname.match(parent.name) + if not match: logger.info( - f"dirname: Failed; Directory name {parent.name!r} does not contain a valid version" + f"dirname: Failed; Directory name {parent.name!r} " + "does not contain a valid version" ) return None logger.info("dirname: Succeeded") - return parent.name[len(name) + 1 :] + return match["version"] def dunamai_get_from_vcs(dir_: Path): from dunamai import Version with working_dir(dir_): - return Version.from_any_vcs(f"(?x){RE_PEP440_VERSION.pattern}") + return Version.from_any_vcs(f"(?x)v?{RE_PEP440_VERSION.pattern}") def get_version_from_vcs(parent: Path) -> Optional[str]: @@ -158,7 +161,7 @@ def get_version(package: Union[Path, str]) -> str: parent = parent.parent version = ( - get_version_from_dirname(name, parent) + get_version_from_dirname(parent) or get_version_from_vcs(parent) or get_version_from_metadata(name, parent) ) diff --git a/tests/test_packages.py b/tests/test_packages.py index 0e341f2..5ac8123 100644 --- a/tests/test_packages.py +++ b/tests/test_packages.py @@ -11,16 +11,6 @@ TempTreeCB = Callable[[Desc], Path] -mock_git_describe = """\ -#!/usr/bin/env python3 -import sys -if "--show-toplevel" in sys.argv: - print("{}") -else: - print("v0.1.2-3-gfefe123-dirty") -""" - - @pytest.fixture(params=[True, False], ids=["src", "plain"]) def has_src(request) -> bool: return request.param @@ -70,26 +60,28 @@ def add_and_commit(msg: str): assert f"{version}.post1.dev0+{hash}.dirty" == v -def test_dir(temp_tree: TempTreeCB, has_src): - dirname = "dir_mod-0.1.3+dirty" +@pytest.mark.parametrize("version", ["0.1.3+dirty", "1.2.post29.dev0+41ced3e.dirty"]) +@pytest.mark.parametrize("distname", ["dir_mod", "dir-mod", "mod"]) +def test_dir(temp_tree: TempTreeCB, has_src, version, distname): content = {"dir_mod.py": "print('hi!')\n"} if has_src: content = dict(src=content) + dirname = f"{distname}-{version}" spec = {dirname: content} with temp_tree(spec) as package: - v = get_version.get_version_from_dirname("dir_mod", package / dirname) - assert "0.1.3+dirty" == v + v = get_version.get_version_from_dirname(package / dirname) + assert version == v parent = (package / dirname / "src") if has_src else (package / dirname) v = get_version.get_version(parent / "dir_mod.py") - assert "0.1.3+dirty" == v + assert version == v def test_dir_dash(temp_tree: TempTreeCB): dirname = "dir-two-0.1" spec = {dirname: {"dir_two.py": "print('hi!')\n"}} with temp_tree(spec) as package: - v = get_version.get_version_from_dirname("dir-two", package / dirname) + v = get_version.get_version_from_dirname(package / dirname) assert "0.1" == v v = get_version.get_version(package / dirname / "dir_two.py")