2020-09-17 23:38:58 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import unicode_literals
|
2020-07-19 14:38:57 +00:00
|
|
|
|
2019-02-21 17:06:47 +01:00
|
|
|
import copy
|
2020-09-06 20:20:36 +00:00
|
|
|
from test import util
|
2019-02-21 17:06:47 +01:00
|
|
|
|
2019-02-21 15:41:06 +01:00
|
|
|
from pycalver import config
|
2020-09-19 22:35:48 +00:00
|
|
|
from pycalver import rewrite
|
|
|
|
|
from pycalver import v1rewrite
|
|
|
|
|
from pycalver import v1version
|
|
|
|
|
from pycalver import v2rewrite
|
|
|
|
|
from pycalver import v2version
|
2019-02-21 15:41:06 +01:00
|
|
|
|
2020-09-17 23:38:58 +00:00
|
|
|
# pylint:disable=protected-access ; allowed for test code
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 22:16:16 +01:00
|
|
|
REWRITE_FIXTURE = """
|
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
__version__ = "v201809.0002-beta"
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2018-11-04 21:11:42 +01:00
|
|
|
def test_rewrite_lines():
|
2018-12-22 09:49:27 +01:00
|
|
|
old_lines = REWRITE_FIXTURE.splitlines()
|
2019-01-06 14:38:20 +01:00
|
|
|
patterns = ['__version__ = "{pycalver}"']
|
2020-09-06 20:20:36 +00:00
|
|
|
new_vinfo = v1version.parse_version_info("v201911.0003")
|
|
|
|
|
new_lines = v1rewrite.rewrite_lines(patterns, new_vinfo, old_lines)
|
2018-12-22 09:49:27 +01:00
|
|
|
|
|
|
|
|
assert len(new_lines) == len(old_lines)
|
|
|
|
|
assert "v201911.0003" not in "\n".join(old_lines)
|
|
|
|
|
assert "v201911.0003" in "\n".join(new_lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_rewrite_final():
|
|
|
|
|
# Patterns written with {release_tag} placeholder preserve
|
|
|
|
|
# the release tag even if the new version is -final
|
|
|
|
|
|
|
|
|
|
old_lines = REWRITE_FIXTURE.splitlines()
|
|
|
|
|
patterns = ['__version__ = "v{year}{month}.{build_no}-{release_tag}"']
|
2020-09-06 20:20:36 +00:00
|
|
|
new_vinfo = v1version.parse_version_info("v201911.0003")
|
|
|
|
|
new_lines = v1rewrite.rewrite_lines(patterns, new_vinfo, old_lines)
|
2018-11-04 21:11:42 +01:00
|
|
|
|
|
|
|
|
assert len(new_lines) == len(old_lines)
|
2018-12-22 09:49:27 +01:00
|
|
|
assert "v201911.0003" not in "\n".join(old_lines)
|
|
|
|
|
assert "None" not in "\n".join(new_lines)
|
|
|
|
|
assert "v201911.0003-final" in "\n".join(new_lines)
|
2019-02-21 15:41:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_iter_file_paths():
|
|
|
|
|
with util.Project(project="a") as project:
|
|
|
|
|
ctx = config.init_project_ctx(project.dir)
|
|
|
|
|
cfg = config.parse(ctx)
|
|
|
|
|
assert cfg
|
|
|
|
|
|
2020-09-19 22:35:48 +00:00
|
|
|
_paths_and_patterns = rewrite.iter_path_patterns_items(cfg.file_patterns)
|
2020-09-06 20:20:36 +00:00
|
|
|
file_paths = {str(file_path) for file_path, patterns in _paths_and_patterns}
|
2019-02-21 15:41:06 +01:00
|
|
|
|
2019-02-22 22:47:44 +01:00
|
|
|
assert file_paths == {"pycalver.toml", "README.md"}
|
2019-02-21 15:41:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_iter_file_globs():
|
|
|
|
|
with util.Project(project="b") as project:
|
|
|
|
|
ctx = config.init_project_ctx(project.dir)
|
|
|
|
|
cfg = config.parse(ctx)
|
|
|
|
|
assert cfg
|
|
|
|
|
|
2020-09-19 22:35:48 +00:00
|
|
|
_paths_and_patterns = rewrite.iter_path_patterns_items(cfg.file_patterns)
|
2020-09-06 20:20:36 +00:00
|
|
|
file_paths = {str(file_path) for file_path, patterns in _paths_and_patterns}
|
2019-02-21 15:41:06 +01:00
|
|
|
|
|
|
|
|
assert file_paths == {
|
|
|
|
|
"setup.cfg",
|
|
|
|
|
"setup.py",
|
|
|
|
|
"README.rst",
|
|
|
|
|
"src/module_v1/__init__.py",
|
|
|
|
|
"src/module_v2/__init__.py",
|
|
|
|
|
}
|
2019-02-21 17:06:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_error_bad_path():
|
|
|
|
|
with util.Project(project="b") as project:
|
|
|
|
|
ctx = config.init_project_ctx(project.dir)
|
|
|
|
|
cfg = config.parse(ctx)
|
|
|
|
|
assert cfg
|
|
|
|
|
|
|
|
|
|
(project.dir / "setup.py").unlink()
|
|
|
|
|
try:
|
2020-09-19 22:35:48 +00:00
|
|
|
list(rewrite.iter_path_patterns_items(cfg.file_patterns))
|
2019-03-29 02:32:29 +01:00
|
|
|
assert False, "expected IOError"
|
|
|
|
|
except IOError as ex:
|
2019-02-21 17:06:47 +01:00
|
|
|
assert "setup.py" in str(ex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_error_bad_pattern():
|
|
|
|
|
with util.Project(project="b") as project:
|
|
|
|
|
ctx = config.init_project_ctx(project.dir)
|
|
|
|
|
cfg = config.parse(ctx)
|
|
|
|
|
assert cfg
|
|
|
|
|
|
|
|
|
|
patterns = copy.deepcopy(cfg.file_patterns)
|
|
|
|
|
patterns["setup.py"] = patterns["setup.py"][0] + "invalid"
|
|
|
|
|
|
|
|
|
|
try:
|
2020-09-19 22:35:48 +00:00
|
|
|
old_vinfo = v1version.parse_version_info("v201808.0233")
|
2020-09-06 20:20:36 +00:00
|
|
|
new_vinfo = v1version.parse_version_info("v201809.1234")
|
2020-09-19 22:35:48 +00:00
|
|
|
list(v1rewrite.diff(old_vinfo, new_vinfo, patterns))
|
|
|
|
|
assert False, "expected rewrite.NoPatternMatch"
|
|
|
|
|
except rewrite.NoPatternMatch as ex:
|
2019-02-21 17:06:47 +01:00
|
|
|
assert "setup.py" in str(ex)
|
2019-07-09 08:21:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONAL_RELEASE_FIXTURE = """
|
|
|
|
|
# SPDX-License-Identifier: BSD
|
|
|
|
|
__version__ = "2018.0002-beta"
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2020-09-06 20:20:36 +00:00
|
|
|
def test_v1_optional_release():
|
2019-07-09 08:21:57 +02:00
|
|
|
old_lines = OPTIONAL_RELEASE_FIXTURE.splitlines()
|
|
|
|
|
pattern = "{year}.{build_no}{release}"
|
|
|
|
|
patterns = ['__version__ = "{year}.{build_no}{release}"']
|
|
|
|
|
|
2020-09-06 20:20:36 +00:00
|
|
|
new_vinfo = v1version.parse_version_info("2019.0003", pattern)
|
|
|
|
|
new_lines = v1rewrite.rewrite_lines(patterns, new_vinfo, old_lines)
|
2019-07-09 08:21:57 +02:00
|
|
|
|
|
|
|
|
assert len(new_lines) == len(old_lines)
|
|
|
|
|
assert "2019.0003" not in "\n".join(old_lines)
|
|
|
|
|
new_text = "\n".join(new_lines)
|
|
|
|
|
assert "2019.0003" in new_text
|
|
|
|
|
|
2020-09-06 20:20:36 +00:00
|
|
|
new_vinfo = v1version.parse_version_info("2019.0004-beta", pattern)
|
|
|
|
|
new_lines = v1rewrite.rewrite_lines(patterns, new_vinfo, old_lines)
|
2019-07-09 08:21:57 +02:00
|
|
|
|
|
|
|
|
# make sure optional release tag is added back on
|
|
|
|
|
assert len(new_lines) == len(old_lines)
|
|
|
|
|
assert "2019.0004-beta" not in "\n".join(old_lines)
|
|
|
|
|
assert "2019.0004-beta" in "\n".join(new_lines)
|
2020-09-18 17:02:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_v2_optional_release():
|
|
|
|
|
old_lines = OPTIONAL_RELEASE_FIXTURE.splitlines()
|
|
|
|
|
pattern = "YYYY.BUILD[-TAG]"
|
|
|
|
|
patterns = ['__version__ = "YYYY.BUILD[-TAG]"']
|
|
|
|
|
|
|
|
|
|
new_vinfo = v2version.parse_version_info("2019.0003", pattern)
|
|
|
|
|
new_lines = v2rewrite.rewrite_lines(patterns, new_vinfo, old_lines)
|
|
|
|
|
|
|
|
|
|
assert len(new_lines) == len(old_lines)
|
|
|
|
|
assert "2019.0003" not in "\n".join(old_lines)
|
|
|
|
|
new_text = "\n".join(new_lines)
|
|
|
|
|
assert "2019.0003" in new_text
|
|
|
|
|
assert '__version__ = "2019.0003"' in new_text
|
|
|
|
|
|
|
|
|
|
new_vinfo = v2version.parse_version_info("2019.0004-beta", pattern)
|
|
|
|
|
new_lines = v2rewrite.rewrite_lines(patterns, new_vinfo, old_lines)
|
|
|
|
|
|
|
|
|
|
# make sure optional release tag is added back on
|
|
|
|
|
assert len(new_lines) == len(old_lines)
|
|
|
|
|
assert "2019.0004-beta" not in "\n".join(old_lines)
|
|
|
|
|
assert "2019.0004-beta" in "\n".join(new_lines)
|