Skip to content

Commit

Permalink
BUG: Fixed version discovery in submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
pastewka committed Jul 27, 2024
1 parent 0e93307 commit 5ea127a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
----------------

Expand Down
12 changes: 8 additions & 4 deletions DiscoverVersion/Discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
43 changes: 39 additions & 4 deletions test/test_version_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 5ea127a

Please sign in to comment.