diff --git a/test/fixtures/project_a/README.md b/test/fixtures/project_a/README.md new file mode 100644 index 0000000..6d069c5 --- /dev/null +++ b/test/fixtures/project_a/README.md @@ -0,0 +1,3 @@ +# PyCalVer README Fixture + +Current Version: v201612.0123-alpha diff --git a/test/fixtures/project_a/pycalver.toml b/test/fixtures/project_a/pycalver.toml new file mode 100644 index 0000000..141c633 --- /dev/null +++ b/test/fixtures/project_a/pycalver.toml @@ -0,0 +1,10 @@ +[pycalver] +current_version = "{initial_version}" +commit = true +tag = true + +[pycalver.file_patterns] +"README.md" = [ + "{version}", + "{pep440_version}", +] diff --git a/test/fixtures/project_a/setup.py b/test/fixtures/project_a/setup.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_config.html b/test/test_config.html new file mode 100644 index 0000000..7eda8f3 --- /dev/null +++ b/test/test_config.html @@ -0,0 +1,1200 @@ +test_config

import io

+

from pycalver import config

+

from . import util as test_util

+

PYCALVER_TOML_FIXTURE = “”” +[pycalver] +current_version = “v201808.0123-dev” +commit = true +tag = true +push = true +[pycalver.file_patterns] +“setup.py” = [ + “{version}”, + “{pep440_version}”, +] +“pycalver.toml” = [ + ‘current_version = “{version}”’, +] +“”“

+

SETUP_CFG_FIXTURE = “”” +[pycalver] +current_version = “v201808.0456-dev” +commit = True +tag = True +push = True

+

[pycalver:file:setup.py] +patterns = + {version} + {pep440_version} +[pycalver:file:setup.cfg] +patterns = + current_version = “{version}” +“”“

+

def mk_buf(text): + buf = io.StringIO() + buf.write(text) + buf.seek(0) + return buf

+

def test_parse_toml(): + buf = mk_buf(PYCALVER_TOML_FIXTURE)

+
raw_cfg = config._parse_toml(buf)
+cfg     = config._parse_config(raw_cfg)
+
+assert cfg.current_version == "v201808.0123-dev"
+assert cfg.commit is True
+assert cfg.tag    is True
+assert cfg.push   is True
+
+assert "pycalver.toml" in cfg.file_patterns
+assert cfg.file_patterns["setup.py"     ] == config.DEFAULT_PATTERNS
+assert cfg.file_patterns["pycalver.toml"] == ['current_version = "{version}"']
+
+ + +

def test_parse_cfg(): + buf = mk_buf(SETUP_CFG_FIXTURE)

+
raw_cfg = config._parse_cfg(buf)
+cfg     = config._parse_config(raw_cfg)
+
+assert cfg.current_version == "v201808.0456-dev"
+assert cfg.commit is True
+assert cfg.tag    is True
+assert cfg.push   is True
+
+assert "setup.cfg" in cfg.file_patterns
+assert cfg.file_patterns["setup.py" ] == config.DEFAULT_PATTERNS
+assert cfg.file_patterns["setup.cfg"] == ['current_version = "{version}"']
+
+ + +

def test_parse_default_config(): + project_path = test_util.FIXTURES_DIR / “project_a” + ctx = config.ProjectContext(project_path, project_path /) + buf = mk_buf(“\n”.join(config.default_config_lines(ctx)))

+
cfg = config._parse_buffer(buf)
+
+assert cfg
+assert cfg.current_version.endswith(".0001-dev")
+assert cfg.tag
+assert cfg.commit
+
+# NOTE (mb 2018-11-10): These refer to the actual files
+#   of the pycalver project. A different project might
+#   have README.rst for example.
+assert "setup.py" in cfg.file_patterns
+assert "setup.cfg" in cfg.file_patterns
+assert "README.md" in cfg.file_patterns
+
+ + +

def test_parse_cfg_file(tmpdir): + setup_path = tmpdir.mkdir(“minimal”).join(“setup.cfg”) + setup_path.write(SETUP_CFG_FIXTURE)

+
cfg = config.parse(str(setup_path))
+
+assert cfg
+assert cfg.current_version == "v201808.0001-dev"
+assert not cfg.tag
+assert not cfg.commit
+
+assert cfg.file_patterns == {"setup.cfg": ["current_version = {version}"]}
+
+ + +

def test_parse_config_missing(tmpdir): + cfg = config.parse(“does_not_exist/setup.cfg”) + assert cfg is None

+
setup_path = tmpdir.mkdir("fail").join("setup.cfg")
+
+cfg = config.parse(str(setup_path))
+assert cfg is None
+
+ + +

def test_parse_empty_config(tmpdir): + setup_path = tmpdir.mkdir(“fail”).join(“setup.cfg”) + setup_path.write(“”)

+
cfg = config.parse(str(setup_path))
+assert cfg is None
+
+ + +

def test_parse_missing_version(tmpdir): + setup_path = tmpdir.mkdir(“fail”).join(“setup.cfg”) + setup_path.write( + “\n”.join( + ( + “[pycalver]”, + # f”current_version = v201808.0001-dev”, + “commit = False”, + ) + ) + )

+
cfg = config.parse(str(setup_path))
+assert cfg is None
+
+ + +

def test_parse_invalid_version(tmpdir): + setup_path = tmpdir.mkdir(“fail”).join(“setup.cfg”) + setup_path.write(“\n”.join((“[pycalver]”, “current_version = 0.1.0”, “commit = False”)))

+
cfg = config.parse(str(setup_path))
+assert cfg is None
+
\ No newline at end of file diff --git a/test/test_config.py b/test/test_config.py index 078dbf3..48a59fe 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -2,45 +2,127 @@ import io from pycalver import config +from . import util as test_util -def test_parse_default_config(): + +PYCALVER_TOML_FIXTURE = """ +[pycalver] +current_version = "v201808.0123-dev" +commit = true +tag = true +push = true + +[pycalver.file_patterns] +"setup.py" = [ + "{version}", + "{pep440_version}", +] +"pycalver.toml" = [ + 'current_version = "{version}"', +] +""" + + +SETUP_CFG_FIXTURE = """ +[pycalver] +current_version = "v201808.0456-dev" +commit = True +tag = True +push = True + +[pycalver:file_patterns] +setup.py = + {version} + {pep440_version} +setup.cfg = + current_version = "{version}" +""" + + +def mk_buf(text): buf = io.StringIO() - for line in config.default_config_lines(): - buf.write(line + "\n") - + buf.write(text) buf.seek(0) - cfg = config._parse_buffer(buf) + return buf + + +def test_parse_toml(): + buf = mk_buf(PYCALVER_TOML_FIXTURE) + + raw_cfg = config._parse_toml(buf) + cfg = config._parse_config(raw_cfg) + + assert cfg.current_version == "v201808.0123-dev" + assert cfg.commit is True + assert cfg.tag is True + assert cfg.push is True + + assert "pycalver.toml" in cfg.file_patterns + assert cfg.file_patterns["setup.py" ] == ["{version}", "{pep440_version}"] + assert cfg.file_patterns["pycalver.toml"] == ['current_version = "{version}"'] + + +def test_parse_cfg(): + buf = mk_buf(SETUP_CFG_FIXTURE) + + raw_cfg = config._parse_cfg(buf) + cfg = config._parse_config(raw_cfg) + + assert cfg.current_version == "v201808.0456-dev" + assert cfg.commit is True + assert cfg.tag is True + assert cfg.push is True + + assert "setup.cfg" in cfg.file_patterns + assert cfg.file_patterns["setup.py" ] == ["{version}", "{pep440_version}"] + assert cfg.file_patterns["setup.cfg"] == ['current_version = "{version}"'] + + +def test_parse_default_toml(): + project_path = test_util.FIXTURES_DIR / "project_a" + config_path = test_util.FIXTURES_DIR / "project_a" / "pycalver.toml" + + ctx = config.ProjectContext(project_path, config_path, "toml", None) + buf = mk_buf(config.default_config(ctx)) + + raw_cfg = config._parse_toml(buf) + cfg = config._parse_config(raw_cfg) assert cfg assert cfg.current_version.endswith(".0001-dev") - assert cfg.tag - assert cfg.commit + assert cfg.commit is True + assert cfg.tag is True + assert cfg.push is True - # NOTE (mb 2018-11-10): These refer to the actual files - # of the pycalver project. A different project would - # have README.rst for example. To fully test this - # we might create temporary projects. assert "setup.py" in cfg.file_patterns - assert "setup.cfg" in cfg.file_patterns assert "README.md" in cfg.file_patterns + assert "pycalver.toml" in cfg.file_patterns -def test_parse(tmpdir): +def test_parse_default_cfg(): + project_path = test_util.FIXTURES_DIR / "project_a" + config_path = test_util.FIXTURES_DIR / "project_a" / "setup.cfg" + + ctx = config.ProjectContext(project_path, config_path, "cfg", None) + buf = mk_buf(config.default_config(ctx)) + + raw_cfg = config._parse_cfg(buf) + cfg = config._parse_config(raw_cfg) + + assert cfg + assert cfg.current_version.endswith(".0001-dev") + assert cfg.commit is True + assert cfg.tag is True + assert cfg.push is True + + assert "setup.py" in cfg.file_patterns + assert "README.md" in cfg.file_patterns + assert "setup.cfg" in cfg.file_patterns + + +def test_parse_cfg_file(tmpdir): setup_path = tmpdir.mkdir("minimal").join("setup.cfg") - setup_path.write( - "\n".join( - ( - "[pycalver]", - "current_version = v201808.0001-dev", - "commit = False", - "tag = False", - "", - "[pycalver:file:setup.cfg]", - "patterns = ", - " current_version = {version}", - ) - ) - ) + setup_path.write(SETUP_CFG_FIXTURE) cfg = config.parse(str(setup_path)) diff --git a/test/test_parse.py b/test/test_parse.py index db00bb6..96f35ac 100644 --- a/test/test_parse.py +++ b/test/test_parse.py @@ -75,7 +75,7 @@ def test_re_pattern_parts(): def test_parse_version_info(): version_str = "v201712.0001-alpha" - version_nfo = parse.VersionInfo.parse(version_str) + version_nfo = parse.parse_version_info(version_str) assert version_nfo.pep440_version == "201712.1a0" assert version_nfo.version == "v201712.0001-alpha" @@ -86,7 +86,7 @@ def test_parse_version_info(): assert version_nfo.release == "-alpha" version_str = "v201712.0001" - version_nfo = parse.VersionInfo.parse(version_str) + version_nfo = parse.parse_version_info(version_str) assert version_nfo.pep440_version == "201712.1" assert version_nfo.version == "v201712.0001" @@ -111,7 +111,7 @@ def test_default_parse_patterns(): lines = SETUP_PY_FIXTURE.splitlines() patterns = ["{version}", "{pep440_version}"] - matches = list(parse.PatternMatch.iter_matches(lines, patterns)) + matches = list(parse.iter_matches(lines, patterns)) assert len(matches) == 2 assert matches[0].lineno == 3 @@ -129,7 +129,7 @@ def test_explicit_parse_patterns(): patterns = ["__version__ = '{version}'", "version='{pep440_version}'"] - matches = list(parse.PatternMatch.iter_matches(lines, patterns)) + matches = list(parse.iter_matches(lines, patterns)) assert len(matches) == 2 assert matches[0].lineno == 3 @@ -156,7 +156,7 @@ def test_badge_parse_patterns(): patterns = ["badge/CalVer-{calver}{build}-{release}-blue.svg", ":alt: CalVer {version}"] - matches = list(parse.PatternMatch.iter_matches(lines, patterns)) + matches = list(parse.iter_matches(lines, patterns)) assert len(matches) == 2 assert matches[0].lineno == 3 diff --git a/test/util.py b/test/util.py new file mode 100644 index 0000000..c921aa1 --- /dev/null +++ b/test/util.py @@ -0,0 +1,64 @@ +import shlex +import shutil +import tempfile +import pathlib2 as pl +import subprocess as sp + + +class Shell: + def __init__(self, cwd): + self.cwd = cwd + + def __call__(self, cmd): + if isinstance(cmd, str): + cmd_parts = shlex.split(cmd) + else: + cmd_parts = cmd + output = sp.check_output(cmd_parts, cwd=self.cwd) + return output.decode("utf-8") + + +_MODULE_PATH = pl.Path(__file__) +FIXTURES_DIR = _MODULE_PATH.parent / "fixtures" + + +class Project: + def __init__(self, project_prefix="a"): + if not project_prefix.endswith("_"): + project_prefix += "_" + + tmpdir = pl.Path(tempfile.mkdtemp(prefix="pytest_")) + self.tmpdir = tmpdir + + _project_dir = tmpdir / "pycalver_project" + _project_dir.mkdir() + + for fpath in FIXTURES_DIR.glob(project_prefix + "*"): + fname = fpath.name[len(project_prefix) :] + shutil.copy(fpath, _project_dir / fname) + + self.dir = _project_dir + + def __enter__(self): + return self + + def __exit__(self, *exc): + shutil.rmtree(str(self.tmpdir)) + return False + + def sh(self, cmd): + shell = Shell(str(self.dir)) + return shell(cmd) + + def git_init(self): + self.sh("git init") + self.sh("git add pycalver.toml") + self.sh("git add README.md") + self.sh("git commit -m 'initial commit'") + + def hg_init(self): + self.sh = Shell(str(self.dir)) + self.sh("hg init") + self.sh("hg add pycalver.toml") + self.sh("hg add README.md") + self.sh("hg commit -m 'initial commit'")