Skip to content

Commit

Permalink
Fix version regex for multi-line version files (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpieters authored Jul 31, 2022
1 parent 72c6c5c commit b0fcc7f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
11 changes: 5 additions & 6 deletions get_releasenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ def check_changes_version(
VERSION_RE = re.compile(
"^{version} *= *{spec}".format(
version="(?:__version__|version)",
spec=r"""(['"])([^\1]+)\1""",
)
spec=r"""(["'])((?:(?!\1).)*)\1""",
),
re.MULTILINE,
)


Expand All @@ -193,17 +194,15 @@ def find_version(ctx: Context, version_file: str, version: str) -> str:
)
return version
txt = ctx.read_file(version_file)
match = VERSION_RE.match(txt)
if match:
if match := VERSION_RE.search(txt):
ret = match.group(2)
if ctx.dist and ret != ctx.dist.version:
raise ValueError(
f"version {ret} from file {version_file} "
f"mismatches with autodetected {ctx.dist.version}"
)
return ret
else:
raise ValueError(f"Unable to determine version in file '{version_file}'")
raise ValueError(f"Unable to determine version in file '{version_file}'")


def check_fix_issue(fix_issue_regex: str, fix_issue_repl: str) -> None:
Expand Down
50 changes: 42 additions & 8 deletions tests/test_find_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

from get_releasenote import Context, DistInfo, analyze_dists, find_version

VERSION_FILE_TMPL = """\
from ._inner import Foo, Bar, spam_ham
{version_line}
__all__ = ("Foo", "Bar", "spam_ham")
"""


@pytest.fixture
def root() -> pathlib.Path:
Expand All @@ -28,7 +36,9 @@ def test_find_version_autodetected(ctx: Context) -> None:

def test_find_version_ambigous(ctx: Context) -> None:
with pytest.raises(ValueError, match="ambiguous"):
(ctx.root / "file.py").write_text("__version__='1.2.3'")
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="__version__='1.2.3'")
)
find_version(ctx, "file.py", "3.4.5")


Expand All @@ -43,36 +53,60 @@ def test_find_version_explicit(ctx: Context) -> None:

def test_find_version_file_mismatch_with_autodetected(ctx: Context) -> None:
with pytest.raises(ValueError, match="mismatches with autodetected "):
(ctx.root / "file.py").write_text("__version__='1.2.3'")
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="__version__='1.2.3'")
)
find_version(ctx, "file.py", "")


def test_find___version___no_spaces(ctx: Context) -> None:
(ctx.root / "file.py").write_text("__version__='0.0.7'")
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="__version__='0.0.7'")
)
assert "0.0.7" == find_version(ctx, "file.py", "")


def test_find___version___from_file_single_quotes(ctx: Context) -> None:
(ctx.root / "file.py").write_text("__version__ = '0.0.7'")
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="__version__ = '0.0.7'")
)
assert "0.0.7" == find_version(ctx, "file.py", "")


def test_find___version___from_file_double_quotes(ctx: Context) -> None:
(ctx.root / "file.py").write_text('__version__ = "0.0.7"')
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line='__version__ = "0.0.7"')
)
assert "0.0.7" == find_version(ctx, "file.py", "")


def test_find_version_from_file_single_quotes(ctx: Context) -> None:
(ctx.root / "file.py").write_text("version = '0.0.7'")
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="version = '0.0.7'")
)
assert "0.0.7" == find_version(ctx, "file.py", "")


def test_find_version_from_file_double_quotes(ctx: Context) -> None:
(ctx.root / "file.py").write_text('version = "0.0.7"')
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="version = '0.0.7'")
)
assert "0.0.7" == find_version(ctx, "file.py", "")


def test_find_version_not_found(ctx: Context) -> None:
(ctx.root / "file.py").write_text("not_a_version = '0.0.7'")
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(version_line="not_a_version = '0.0.7'")
)
with pytest.raises(ValueError, match="Unable to determine version"):
find_version(ctx, "file.py", "")


@pytest.mark.parametrize("quote", ["'", '"'])
def test_find_version_with_quote_in_comment(ctx: Context, quote: str) -> None:
(ctx.root / "file.py").write_text(
VERSION_FILE_TMPL.format(
version_line="version = '0.0.7' # don't forget to update!"
).replace("'", quote)
)
assert "0.0.7" == find_version(ctx, "file.py", "")

0 comments on commit b0fcc7f

Please sign in to comment.