From 5ea127aebc2165cdac8b263dcd45619f4170c0fa Mon Sep 17 00:00:00 2001 From: Lars Pastewka Date: Sat, 27 Jul 2024 10:13:31 +0200 Subject: [PATCH] BUG: Fixed version discovery in submodules --- CHANGELOG.md | 5 ++++ DiscoverVersion/Discovery.py | 12 ++++++---- test/test_version_discovery.py | 43 ++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5290f9d..883aa08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ This file lists all changes to the code +v0.2.3 (27Jul24) +---------------- + +* BUG: Fixed version discovery in submodules + v0.2.2 (25Jul24) ---------------- diff --git a/DiscoverVersion/Discovery.py b/DiscoverVersion/Discovery.py index 9a44f70..dc09b76 100644 --- a/DiscoverVersion/Discovery.py +++ b/DiscoverVersion/Discovery.py @@ -42,11 +42,11 @@ def get_version_from_git(dirname): if shutil.which('git') is None: raise CannotDiscoverVersion('git executable does not exist.') - path = Path(os.path.abspath('.')) - while path.parent != path and not path.joinpath('.git').is_dir(): + path = Path(os.path.abspath(dirname)) + while path.parent != path and not path.joinpath('.git').exists(): path = path.parent - if not path.joinpath('.git').is_dir(): + if not path.joinpath('.git').exists(): raise CannotDiscoverVersion('.git directory does not exist.') try: @@ -132,7 +132,11 @@ def get_version(package_name, file_name, use_git=True, use_importlib=True, use_p if discovered_version is None and use_git: tried += ', git' try: - discovered_version = get_version_from_git(os.path.dirname(file_name)) + if os.path.isdir(file_name): + dirname = file_name + else: + dirname = os.path.dirname(file_name) + discovered_version = get_version_from_git(dirname) except CannotDiscoverVersion: discovered_version = None diff --git a/test/test_version_discovery.py b/test/test_version_discovery.py index 4cc3030..ead639b 100644 --- a/test/test_version_discovery.py +++ b/test/test_version_discovery.py @@ -26,17 +26,52 @@ Tests the hello world function """ +import os +import subprocess +from tempfile import TemporaryDirectory + from DiscoverVersion import __version__, get_version -_current_version = '0.2.2' +_current_version = "0.2.2" def test_version_discovery(): assert __version__.startswith(_current_version) -def test_version_from_submodule(): - assert get_version('DiscoverVersion', __file__).startswith(_current_version) +def test_version_from_nested_module(): + assert get_version("DiscoverVersion", __file__).startswith(_current_version) + + assert get_version("DiscoverVersion", __file__, use_git=False).startswith( + _current_version + ) + - assert get_version('DiscoverVersion', __file__, use_git=False).startswith(_current_version) +def test_version_from_git_submodule(): + with TemporaryDirectory() as tmpdir: + # Create git repository + os.mkdir(f"{tmpdir}/my-git-repository") + r = subprocess.run( + ["git", "init"], cwd=f"{tmpdir}/my-git-repository", stdout=subprocess.PIPE + ) + assert r.returncode == 0 + # Create and add git submodule + assert r.returncode == 0 + r = subprocess.run( + [ + "git", + "submodule", + "add", + "https://github.com/IMTEK-Simulation/DiscoverVersion.git", + ], + cwd=f"{tmpdir}/my-git-repository", + stdout=subprocess.PIPE, + ) + assert r.returncode == 0 + assert get_version( + "DiscoverVersion", + f"{tmpdir}/my-git-repository/DiscoverVersion", + use_importlib=False, + use_pkginfo=False, + ).startswith(_current_version)