bumpver/test/test_config.py

384 lines
9.9 KiB
Python
Raw Normal View History

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
2018-09-04 09:56:53 +02:00
import io
2020-09-06 20:20:36 +00:00
from test import util
2018-09-04 09:56:53 +02:00
2020-10-14 22:17:18 +00:00
from pycalver2 import config
2018-09-03 22:23:51 +02:00
2020-09-17 23:38:58 +00:00
# pylint:disable=redefined-outer-name ; pytest fixtures
# pylint:disable=protected-access ; allowed for test code
2019-01-07 17:30:02 +01:00
PYCALVER_TOML_FIXTURE_1 = """
2018-12-05 09:24:42 +01:00
[pycalver]
2018-12-08 19:18:47 +01:00
current_version = "v201808.0123-alpha"
2018-12-05 09:24:42 +01:00
commit = true
tag = true
push = true
[pycalver.file_patterns]
2018-12-08 19:18:47 +01:00
"README.md" = [
2018-12-05 09:24:42 +01:00
"{version}",
"{pep440_version}",
]
"pycalver.toml" = [
'current_version = "{version}"',
]
"""
2019-01-07 17:30:02 +01:00
PYCALVER_TOML_FIXTURE_2 = """
[pycalver]
current_version = "1.2.3"
version_pattern = "{semver}"
[pycalver.file_patterns]
"README.md" = [
"{version}",
"{pep440_version}",
]
"pycalver.toml" = [
'current_version = "{version}"',
]
"""
2018-12-05 09:24:42 +01:00
SETUP_CFG_FIXTURE = """
[pycalver]
2018-12-08 19:18:47 +01:00
current_version = "v201808.0456-beta"
2018-12-05 09:24:42 +01:00
commit = True
tag = True
push = True
[pycalver:file_patterns]
setup.py =
{version}
{pep440_version}
setup.cfg =
current_version = "{version}"
"""
2020-09-18 19:52:40 +00:00
NEW_PATTERN_CFG_FIXTURE = """
[pycalver]
current_version = "v201808.1456-beta"
2020-10-14 22:17:18 +00:00
version_pattern = "vYYYY0M.BUILD[-TAG]"
2020-09-18 19:52:40 +00:00
commit_message = "bump version to {new_version}"
commit = True
tag = True
push = True
[pycalver:file_patterns]
setup.py =
{version}
{pep440_version}
setup.cfg =
current_version = "{version}"
src/project/*.py =
Copyright (c) 2018-YYYY
"""
2018-12-05 09:24:42 +01:00
def mk_buf(text):
buf = io.StringIO()
buf.write(text)
2018-09-04 09:56:53 +02:00
buf.seek(0)
2018-12-05 09:24:42 +01:00
return buf
2020-10-02 20:52:54 +00:00
def _parse_raw_patterns_by_filepath(cfg):
return {
filepath: [pattern.raw_pattern for pattern in patterns]
for filepath, patterns in cfg.file_patterns.items()
}
2019-01-07 17:30:02 +01:00
def test_parse_toml_1():
buf = mk_buf(PYCALVER_TOML_FIXTURE_1)
2018-12-05 09:24:42 +01:00
raw_cfg = config._parse_toml(buf)
cfg = config._parse_config(raw_cfg)
2018-12-08 19:18:47 +01:00
assert cfg.current_version == "v201808.0123-alpha"
2019-01-07 17:30:02 +01:00
assert cfg.version_pattern == "{pycalver}"
2018-12-05 09:24:42 +01:00
assert cfg.commit is True
assert cfg.tag is True
assert cfg.push is True
assert "pycalver.toml" in cfg.file_patterns
2020-10-02 20:52:54 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath["README.md" ] == ["{pycalver}", "{pep440_pycalver}"]
assert raw_patterns_by_filepath["pycalver.toml"] == ['current_version = "{pycalver}"']
2018-12-05 09:24:42 +01:00
2019-01-07 17:30:02 +01:00
def test_parse_toml_2():
buf = mk_buf(PYCALVER_TOML_FIXTURE_2)
raw_cfg = config._parse_toml(buf)
cfg = config._parse_config(raw_cfg)
assert cfg.current_version == "1.2.3"
assert cfg.version_pattern == "{semver}"
assert cfg.commit is False
assert cfg.tag is False
assert cfg.push is False
assert "pycalver.toml" in cfg.file_patterns
2020-10-02 20:52:54 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath["README.md" ] == ["{semver}", "{semver}"]
assert raw_patterns_by_filepath["pycalver.toml"] == ['current_version = "{semver}"']
2019-01-07 17:30:02 +01:00
2020-09-18 19:52:40 +00:00
def test_parse_v1_cfg():
2018-12-05 09:24:42 +01:00
buf = mk_buf(SETUP_CFG_FIXTURE)
raw_cfg = config._parse_cfg(buf)
cfg = config._parse_config(raw_cfg)
2018-12-08 19:18:47 +01:00
assert cfg.current_version == "v201808.0456-beta"
2018-12-05 09:24:42 +01:00
assert cfg.commit is True
assert cfg.tag is True
assert cfg.push is True
assert "setup.cfg" in cfg.file_patterns
2020-10-02 20:52:54 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath["setup.py" ] == ["{pycalver}", "{pep440_pycalver}"]
assert raw_patterns_by_filepath["setup.cfg"] == ['current_version = "{pycalver}"']
2018-12-05 09:24:42 +01:00
2020-09-18 19:52:40 +00:00
def test_parse_v2_cfg():
buf = mk_buf(NEW_PATTERN_CFG_FIXTURE)
raw_cfg = config._parse_cfg(buf)
cfg = config._parse_config(raw_cfg)
assert cfg.current_version == "v201808.1456-beta"
assert cfg.commit_message == "bump version to {new_version}"
assert cfg.commit is True
assert cfg.tag is True
assert cfg.push is True
assert "setup.py" in cfg.file_patterns
assert "setup.cfg" in cfg.file_patterns
2020-10-02 20:52:54 +00:00
2020-10-04 12:10:38 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath["setup.py"] == [
2020-10-14 22:17:18 +00:00
"vYYYY0M.BUILD[-TAG]",
2020-10-04 12:10:38 +00:00
"YYYY0M.BLD[PYTAGNUM]",
]
2020-10-14 22:17:18 +00:00
assert raw_patterns_by_filepath["setup.cfg"] == ['current_version = "vYYYY0M.BUILD[-TAG]"']
2020-10-04 12:10:38 +00:00
assert raw_patterns_by_filepath["src/project/*.py"] == ["Copyright (c) 2018-YYYY"]
2020-09-18 19:52:40 +00:00
2018-12-05 09:24:42 +01:00
def test_parse_default_toml():
2018-12-08 19:18:47 +01:00
project_path = util.FIXTURES_DIR / "project_a"
2018-12-05 09:24:42 +01:00
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(project_path)
default_toml = config.default_config(ctx)
2018-12-05 09:24:42 +01:00
2018-12-08 19:18:47 +01:00
buf = mk_buf(default_toml)
2018-12-05 09:24:42 +01:00
raw_cfg = config._parse_toml(buf)
cfg = config._parse_config(raw_cfg)
2018-09-04 09:56:53 +02:00
assert cfg
2018-12-08 19:18:47 +01:00
def test_parse_default_cfg():
project_path = util.FIXTURES_DIR / "project_b"
ctx = config.init_project_ctx(project_path)
default_cfg = config.default_config(ctx)
buf = mk_buf(default_cfg)
raw_cfg = config._parse_cfg(buf)
cfg = config._parse_config(raw_cfg)
assert cfg
def test_parse_project_toml():
2020-10-02 20:52:54 +00:00
project_path = util.FIXTURES_DIR / "project_a"
config_path = util.FIXTURES_DIR / "project_a" / "pycalver.toml"
config_rel_path = "pycalver.toml"
2018-12-08 19:18:47 +01:00
2020-07-19 14:38:57 +00:00
with config_path.open() as fobj:
config_data = fobj.read()
2018-12-08 19:18:47 +01:00
2020-10-14 22:17:18 +00:00
assert "v2017.0123-alpha" in config_data
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(project_path)
2020-10-02 20:52:54 +00:00
assert ctx == config.ProjectContext(project_path, config_path, config_rel_path, "toml", None)
2018-12-08 19:18:47 +01:00
cfg = config.parse(ctx)
assert cfg
2020-10-14 22:17:18 +00:00
assert cfg.current_version == "v2017.0123-alpha"
2018-12-05 09:24:42 +01:00
assert cfg.commit is True
assert cfg.tag is True
assert cfg.push is True
2018-09-04 09:56:53 +02:00
2018-12-08 19:18:47 +01:00
assert set(cfg.file_patterns.keys()) == {"pycalver.toml", "README.md"}
2018-12-05 09:24:42 +01:00
2018-12-08 19:18:47 +01:00
def test_parse_project_cfg():
2020-10-02 20:52:54 +00:00
project_path = util.FIXTURES_DIR / "project_b"
config_path = util.FIXTURES_DIR / "project_b" / "setup.cfg"
config_rel_path = "setup.cfg"
2018-12-05 09:24:42 +01:00
2020-07-19 14:38:57 +00:00
with config_path.open() as fobj:
config_data = fobj.read()
2018-12-05 09:24:42 +01:00
2018-12-08 19:18:47 +01:00
assert "v201307.0456-beta" in config_data
ctx = config.init_project_ctx(project_path)
2020-10-02 20:52:54 +00:00
assert ctx == config.ProjectContext(project_path, config_path, config_rel_path, 'cfg', None)
2018-12-08 19:18:47 +01:00
cfg = config.parse(ctx)
2018-12-05 09:24:42 +01:00
assert cfg
2018-12-08 19:18:47 +01:00
assert cfg.current_version == "v201307.0456-beta"
2018-12-05 09:24:42 +01:00
assert cfg.commit is True
assert cfg.tag is True
assert cfg.push is True
2019-02-21 15:41:06 +01:00
assert set(cfg.file_patterns.keys()) == {
"setup.py",
"README.rst",
"setup.cfg",
"src/module_v*/__init__.py",
}
2018-12-08 19:18:47 +01:00
def test_parse_toml_file(tmpdir):
project_path = tmpdir.mkdir("minimal")
setup_cfg = project_path.join("pycalver.toml")
2019-01-07 17:30:02 +01:00
setup_cfg.write(PYCALVER_TOML_FIXTURE_1)
2020-10-02 20:52:54 +00:00
setup_cfg_rel_path = "pycalver.toml"
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(project_path)
2020-10-02 20:52:54 +00:00
assert ctx == config.ProjectContext(project_path, setup_cfg, setup_cfg_rel_path, 'toml', None)
2018-12-08 19:18:47 +01:00
cfg = config.parse(ctx)
assert cfg
assert cfg.current_version == "v201808.0123-alpha"
assert cfg.tag is True
assert cfg.commit is True
assert cfg.push is True
2020-10-02 20:52:54 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath == {
"README.md" : ["{pycalver}", "{pep440_pycalver}"],
"pycalver.toml": ['current_version = "{pycalver}"'],
2018-12-08 19:18:47 +01:00
}
2018-09-04 09:56:53 +02:00
2020-07-19 14:38:57 +00:00
def test_parse_default_pattern():
2020-10-02 20:52:54 +00:00
project_path = util.FIXTURES_DIR / "project_c"
config_path = util.FIXTURES_DIR / "project_c" / "pyproject.toml"
config_rel_path = "pyproject.toml"
2019-03-29 21:24:47 +01:00
ctx = config.init_project_ctx(project_path)
2020-10-02 20:52:54 +00:00
assert ctx == config.ProjectContext(project_path, config_path, config_rel_path, "toml", None)
2019-03-29 21:24:47 +01:00
cfg = config.parse(ctx)
assert cfg
assert cfg.current_version == "v2017q1.54321"
assert cfg.commit is True
assert cfg.tag is True
assert cfg.push is True
2020-10-02 20:52:54 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath == {
2019-03-29 21:24:47 +01:00
"pyproject.toml": [r'current_version = "v{year}q{quarter}.{build_no}"']
}
2018-12-05 09:24:42 +01:00
def test_parse_cfg_file(tmpdir):
2018-12-08 19:18:47 +01:00
project_path = tmpdir.mkdir("minimal")
setup_cfg = project_path.join("setup.cfg")
setup_cfg.write(SETUP_CFG_FIXTURE)
2020-10-02 20:52:54 +00:00
setup_cfg_rel_path = "setup.cfg"
2018-09-04 09:56:53 +02:00
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(project_path)
2020-10-02 20:52:54 +00:00
assert ctx == config.ProjectContext(project_path, setup_cfg, setup_cfg_rel_path, 'cfg', None)
2018-12-08 19:18:47 +01:00
cfg = config.parse(ctx)
2018-09-04 09:56:53 +02:00
assert cfg
2018-12-08 19:18:47 +01:00
assert cfg.current_version == "v201808.0456-beta"
assert cfg.tag is True
assert cfg.commit is True
assert cfg.push is True
2018-09-04 09:56:53 +02:00
2020-10-02 20:52:54 +00:00
raw_patterns_by_filepath = _parse_raw_patterns_by_filepath(cfg)
assert raw_patterns_by_filepath == {
"setup.py" : ["{pycalver}", "{pep440_pycalver}"],
"setup.cfg": ['current_version = "{pycalver}"'],
2018-12-08 19:18:47 +01:00
}
2018-09-04 09:56:53 +02:00
def test_parse_config_missing(tmpdir):
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx("does_not_exist/setup.cfg")
assert ctx
cfg = config.parse(ctx)
2018-09-04 09:56:53 +02:00
assert cfg is None
setup_path = tmpdir.mkdir("fail").join("setup.cfg")
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(setup_path)
assert ctx
2018-09-04 09:56:53 +02:00
2018-12-08 19:18:47 +01:00
cfg = config.parse(ctx)
2018-09-04 09:56:53 +02:00
assert cfg is None
def test_parse_empty_config(tmpdir):
setup_path = tmpdir.mkdir("fail").join("setup.cfg")
setup_path.write("")
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(setup_path)
assert ctx
2018-09-04 09:56:53 +02:00
2018-12-08 19:18:47 +01:00
cfg = config.parse(ctx)
2018-09-04 09:56:53 +02:00
assert cfg is None
def test_parse_missing_version(tmpdir):
setup_path = tmpdir.mkdir("fail").join("setup.cfg")
2018-11-06 21:45:33 +01:00
setup_path.write(
"\n".join(
(
"[pycalver]",
2020-09-18 19:52:40 +00:00
# f"current_version = v201808.1001-dev",
2018-11-06 21:45:33 +01:00
"commit = False",
)
)
)
2018-09-04 09:56:53 +02:00
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(setup_path)
assert ctx
cfg = config.parse(ctx)
2018-09-04 09:56:53 +02:00
assert cfg is None
def test_parse_invalid_version(tmpdir):
setup_path = tmpdir.mkdir("fail").join("setup.cfg")
2018-11-06 21:45:33 +01:00
setup_path.write("\n".join(("[pycalver]", "current_version = 0.1.0", "commit = False")))
2018-09-04 09:56:53 +02:00
2018-12-08 19:18:47 +01:00
ctx = config.init_project_ctx(setup_path)
assert ctx
cfg = config.parse(ctx)
2018-09-04 09:56:53 +02:00
assert cfg is None