Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__get_version__defaults(config):
v, s = plugin._get_version(config)
assert v == Version.from_git()
assert s == Version.from_git().serialize()
return inner
def make_from_callback(function: Callable, mock_commit: Optional[str] = "abc") -> Callable:
def inner(*args, **kwargs):
version = function(*args, **kwargs)
if version.commit and mock_commit:
version.commit = mock_commit
return version
return inner
from_any_vcs = make_from_callback(Version.from_any_vcs)
from_any_vcs_unmocked = make_from_callback(Version.from_any_vcs, mock_commit=None)
from_explicit_vcs = make_from_callback(Version.from_vcs)
def test__version__init() -> None:
v = Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True)
assert v.base == "1"
assert v.stage == "a"
assert v.revision == 2
assert v.distance == 3
assert v.commit == "abc"
assert v.dirty
def test__version__str() -> None:
v = Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True)
assert str(v) == v.serialize()
return inner
def make_from_callback(function: Callable, mock_commit: Optional[str] = "abc") -> Callable:
def inner(*args, **kwargs):
version = function(*args, **kwargs)
if version.commit and mock_commit:
version.commit = mock_commit
return version
return inner
from_any_vcs = make_from_callback(Version.from_any_vcs)
from_any_vcs_unmocked = make_from_callback(Version.from_any_vcs, mock_commit=None)
from_explicit_vcs = make_from_callback(Version.from_vcs)
@pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
def test__version__from_git(tmp_path) -> None:
vcs = tmp_path / "dunamai-git"
vcs.mkdir()
run = make_run_callback(vcs)
from_vcs = make_from_callback(Version.from_git)
with chdir(vcs):
run("git init")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=True)
(vcs / "foo.txt").write_text("hi")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=True)
def test__get_version__defaults(config):
v, s = plugin._get_version(config)
assert v == Version.from_git()
assert s == Version.from_git().serialize()
def test__version__from_git(tmp_path) -> None:
vcs = tmp_path / "dunamai-git"
vcs.mkdir()
run = make_run_callback(vcs)
from_vcs = make_from_callback(Version.from_git)
with chdir(vcs):
run("git init")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=True)
(vcs / "foo.txt").write_text("hi")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=True)
run("git add .")
run('git commit -m "Initial commit"')
assert from_vcs() == Version("0.0.0", distance=1, commit="abc", dirty=False)
# Additional one-off check not in other VCS integration tests:
# when the only tag in the repository does not match the pattern.
run("git tag other")
with pytest.raises(ValueError):
return out
return inner
def make_from_callback(function: Callable, mock_commit: Optional[str] = "abc") -> Callable:
def inner(*args, **kwargs):
version = function(*args, **kwargs)
if version.commit and mock_commit:
version.commit = mock_commit
return version
return inner
from_any_vcs = make_from_callback(Version.from_any_vcs)
from_any_vcs_unmocked = make_from_callback(Version.from_any_vcs, mock_commit=None)
from_explicit_vcs = make_from_callback(Version.from_vcs)
def test__version__init() -> None:
v = Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True)
assert v.base == "1"
assert v.stage == "a"
assert v.revision == 2
assert v.distance == 3
assert v.commit == "abc"
assert v.dirty
def test__version__str() -> None:
v = Version("1", stage=("a", 2), distance=3, commit="abc", dirty=True)
return out
return inner
def make_from_callback(function: Callable, mock_commit: Optional[str] = "abc") -> Callable:
def inner(*args, **kwargs):
version = function(*args, **kwargs)
if version.commit and mock_commit:
version.commit = mock_commit
return version
return inner
from_any_vcs = make_from_callback(Version.from_any_vcs)
from_any_vcs_unmocked = make_from_callback(Version.from_any_vcs, mock_commit=None)
from_explicit_vcs = make_from_callback(Version.from_vcs)
@pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")
def test__version__from_git(tmp_path) -> None:
vcs = tmp_path / "dunamai-git"
vcs.mkdir()
run = make_run_callback(vcs)
from_vcs = make_from_callback(Version.from_git)
with chdir(vcs):
run("git init")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=True)
(vcs / "foo.txt").write_text("hi")
def test__version__from_darcs(tmp_path) -> None:
vcs = tmp_path / "dunamai-darcs"
vcs.mkdir()
run = make_run_callback(vcs)
from_vcs = make_from_callback(Version.from_darcs)
with chdir(vcs):
run("darcs init")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=False)
(vcs / "foo.txt").write_text("hi")
assert from_vcs() == Version("0.0.0", distance=0, commit=None, dirty=True)
run("darcs add foo.txt")
run('darcs record -am "Initial commit"')
assert from_vcs() == Version("0.0.0", distance=1, commit="abc", dirty=False)
run("darcs tag v0.1.0")
assert from_vcs() == Version("0.1.0", commit="abc", dirty=False)
assert from_vcs(latest_tag=True) == Version("0.1.0", commit="abc", dirty=False)
assert run("dunamai from darcs") == "0.1.0"
assert run("dunamai from any") == "0.1.0"
(vcs / "foo.txt").write_text("bye")
assert from_vcs() == Version("0.1.0", commit="abc", dirty=True)
assert run("dunamai from any") == "0.1.0"
(vcs / "foo.txt").write_text("bye")
assert from_vcs() == Version("0.1.0", commit="abc", dirty=True)
run('darcs record -am "Second"')
assert from_vcs() == Version("0.1.0", distance=1, commit="abc", dirty=False)
assert from_any_vcs() == Version("0.1.0", distance=1, commit="abc", dirty=False)
run("darcs tag unmatched")
assert from_vcs() == Version("0.1.0", distance=2, commit="abc", dirty=False)
with pytest.raises(ValueError):
from_vcs(latest_tag=True)
run("darcs tag v0.2.0")
assert from_vcs() == Version("0.2.0", commit="abc", dirty=False)
run("darcs obliterate --all --last 3")
assert from_vcs() == Version("0.1.0", commit="abc", dirty=False)
def test__version__serialize__pvp_with_metadata() -> None:
style = Style.Pvp
assert Version("0.1.0").serialize(style=style) == "0.1.0"
assert Version("0.1.0").serialize(metadata=True, style=style) == "0.1.0"
assert Version("0.1.0").serialize(metadata=False, style=style) == "0.1.0"
assert Version("0.1.0", stage=("a", 1), commit="abc").serialize(style=style) == "0.1.0-a-1"
assert (
Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=True, style=style)
== "0.1.0-a-1-abc"
)
assert (
Version("0.1.0", stage=("a", 1), commit="abc").serialize(metadata=False, style=style)
== "0.1.0-a-1"
)
assert Version("0.1.0", distance=1, commit="abc").serialize(style=style) == "0.1.0-post-1-abc"
assert (
Version("0.1.0", distance=1, commit="abc").serialize(metadata=True, style=style)
== "0.1.0-post-1-abc"
)