Skip to content

Commit

Permalink
change the order of extracting a version
Browse files Browse the repository at this point in the history
pyproject.toml is the first - then installed package
  • Loading branch information
rabbit72 committed May 10, 2021
1 parent b4471b3 commit b70d323
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions single_source/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def get_version(
if not target_path.is_file():
target_path /= pyproject_name

version: Optional[str] = _get_version_from_metadata(package_name)
version: Optional[str] = _get_version_from_path(target_path, version_regex)
if version is None:
version = _get_version_from_path(target_path, version_regex)
version = _get_version_from_metadata(package_name)

if not version and fail:
raise VersionNotFoundError(
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
import toml

TEST_VERSION_STRINGS = [
("version = '2.0.1-alpha.0' ", "2.0.1-alpha.0"),
Expand Down Expand Up @@ -30,6 +31,12 @@ def correct_pyproject_path() -> Path:
return Path(__file__).parent / "data" / "pyproject.toml"


@pytest.fixture
def version_from_pyproject(correct_pyproject_path: Path) -> str:
pyproject = toml.load(str(correct_pyproject_path))
return pyproject["tool"]["poetry"]["version"].strip()


@pytest.fixture
def correct_setuppy_path() -> Path:
return Path(__file__).parent / "data" / "setup.py"
21 changes: 15 additions & 6 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from pathlib import Path

import pytest
import toml

from single_source.version import (
VERSION_REGEX,
Expand Down Expand Up @@ -30,12 +30,11 @@ def test_get_version_from_installed_package(mocker):
assert version_from_metadata == expected_version


def test_get_version_from_pyproject(correct_pyproject_path):
pyproject = toml.load(str(correct_pyproject_path))

def test_get_version_from_pyproject(
correct_pyproject_path: Path, version_from_pyproject: str
):
version = _get_version_from_path(correct_pyproject_path, VERSION_REGEX)
cleaned_version = pyproject["tool"]["poetry"]["version"].strip()
assert version == cleaned_version
assert version == version_from_pyproject


def test_get_version_raise_exception(non_existing_package_name, bad_pyproject_path):
Expand Down Expand Up @@ -64,3 +63,13 @@ def test_get_version_from_setuppy(correct_setuppy_path):

version = _get_version_from_path(correct_setuppy_path, VERSION_REGEX)
assert version == expected_version


def test_get_version_pyproject_first_priority_than_from_installed_package(
correct_pyproject_path: Path, version_from_pyproject: str
):
package_name = "dummy_name"
fail = True

version = get_version(package_name, correct_pyproject_path.parent, fail=fail)
assert version == version_from_pyproject

0 comments on commit b70d323

Please sign in to comment.