Skip to content

Commit

Permalink
Allow deriving when distname and module name mismatch (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jun 8, 2021
1 parent f59a425 commit f58f189
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
23 changes: 13 additions & 10 deletions get_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

RE_PEP440_VERSION = re.compile(
r"""
v?
(?:(?P<epoch>[0-9]+)!)?
(?P<base>[0-9]+(?:\.[0-9]+)*)
(?:
Expand Down Expand Up @@ -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<version>{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]:
Expand Down Expand Up @@ -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)
)
Expand Down
24 changes: 8 additions & 16 deletions tests/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f58f189

Please sign in to comment.