Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed Apr 26, 2024
1 parent f1bd2ca commit 90780a7
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cookieplone/utils/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def check_docker_version(min_version: str) -> str:
version = _parse_docker_version(raw_version)
return (
""
if version >= settings.MIN_DOCKER_VERSION
else f"Docker version is not supported: Got {raw_version}"
if version >= min_version
else f"Docker version is not supported: Got {version}"
)


Expand Down
2 changes: 2 additions & 0 deletions cookieplone/utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def initialize_repository(path: Path) -> Repo:
if not check_path_is_repository(path):
repo = Repo.init(path)
repo.git.add(path)
else:
repo = Repo(path)
return repo


Expand Down
3 changes: 1 addition & 2 deletions cookieplone/utils/sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def run_sanity_checks(checks: list[data.SanityCheck]) -> data.SanityCheckResults
global_status = True
results = []
for check in checks:
status = False
name = check.name
func = check.func
args = check.args
Expand All @@ -19,8 +20,6 @@ def run_sanity_checks(checks: list[data.SanityCheck]) -> data.SanityCheckResults
message = "✓"
elif level == "warning":
status = True
elif level == "error":
status = False
global_status = global_status and status
results.append(data.SanityCheckResult(name, status, message))
global_message = (
Expand Down
15 changes: 7 additions & 8 deletions cookieplone/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ def validate_npm_package_name(value: str) -> str:

def validate_plone_version(value: str) -> str:
"""Validate Plone Version."""
status = False
version = _version_from_str(value)
if version:
status = version >= _version_from_str(settings.PLONE_MIN_VERSION)
status = bool(version) and (
version >= _version_from_str(settings.PLONE_MIN_VERSION)
)
return "" if status else f"{value} is not a valid Plone version."


def validate_volto_version(value: str) -> str:
"""Validate Volto Version."""
status = False
version = _version_from_str(value)
if version:
status = version >= _version_from_str(settings.VOLTO_MIN_VERSION)
status = bool(version) and (
version >= _version_from_str(settings.VOLTO_MIN_VERSION)
)
return "" if status else f"{value} is not a valid Volto version."


Expand All @@ -114,6 +114,7 @@ def run_context_validations(
continue
validations.append(data.ItemValidator(key, func, "error"))
for validation in validations:
status = False
key = validation.key
func = validation.func
value = context.get(key, "")
Expand All @@ -124,8 +125,6 @@ def run_context_validations(
message = "✓"
elif level == "warning":
status = True
elif level == "error":
status = False
global_status = global_status and status
results.append(data.ItemValidatorResult(key, status, message))
global_message = (
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ tests = ["tests", "*/cookieplone/tests"]

[tool.coverage.report]
skip_empty = true
show_missing = true
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import random
import string

import pytest
from git import Repo


@pytest.fixture()
def tmp_repo(tmp_path):
repo = Repo.init(tmp_path)
repo.index.add(tmp_path)
repo.index.commit("test commit")

return tmp_path


@pytest.fixture()
def no_repo(tmp_path):
sub_path = "".join(random.choice(string.ascii_lowercase) for _ in range(20)) # noQA:S311
path = tmp_path / sub_path
path.mkdir(parents=True)
return path
133 changes: 133 additions & 0 deletions tests/utils/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import sys

import pytest

from cookieplone.utils import commands


@pytest.fixture
def mock_get_command_version(monkeypatch):
def func(raw_version):
def patch(cmd: str):
return raw_version

monkeypatch.setattr(commands, "_get_command_version", patch)

return func


@pytest.mark.parametrize(
"value,expected",
[
["v20.11.1", "20"],
["v22.11.1", "22"],
["v22.11", ""],
["v22", ""],
["22.11.1", ""],
["22", ""],
["foo", ""],
],
)
def test_parse_node_major_version(value: str, expected: str):
func = commands._parse_node_major_version
assert func(value) == expected


@pytest.mark.parametrize(
"value,expected",
[
["Docker version 25.0.3, build 4debf41", "25.0"],
["Docker version 25.0.3", "25.0"],
[" Docker version 25.0.3", "25.0"],
[" Docker version 25.0.3 ", "25.0"],
["25.0.3", ""],
["25.0", ""],
["25", ""],
["foo", ""],
],
)
def test_parse_docker_version(value: str, expected: str):
func = commands._parse_docker_version
assert func(value) == expected


@pytest.mark.parametrize(
"cli,expected",
[
["git", ""],
["python", ""],
["kowabunga0123", "Command kowabunga0123 is not available."],
],
)
def test_check_command_is_available(cli: str, expected: str):
func = commands.check_command_is_available
assert func(cli) == expected


@pytest.mark.parametrize(
"versions,expected",
[
[[], ""],
[
[
"3.10",
"3.11",
"3.12",
"3.13",
],
"",
],
[
[
"1.5",
"2.4",
],
f"Python version is not supported: Got {sys.version}",
],
],
)
def test_check_python_version(versions: list[str], expected: str):
func = commands.check_python_version
assert func(versions) == expected


@pytest.mark.parametrize(
"raw_version,min_version,expected",
[
["", "", "Docker not found."],
["", "20.04", "Docker not found."],
["Docker version 25.0.3, build 4debf41", "20.4", ""],
[
"Docker version 25.0.3, build 4debf41",
"26.4",
"Docker version is not supported: Got 25.0",
],
],
)
def test_check_docker_version(
mock_get_command_version, raw_version: str, min_version: str, expected: str
):
mock_get_command_version(raw_version)
func = commands.check_docker_version
assert func(min_version) == expected


@pytest.mark.parametrize(
"raw_version,versions,expected",
[
["", [], "NodeJS not found."],
["", ["16", "17", "18", "19", "20"], "NodeJS not found."],
["v20.11.1", ["16", "17", "18", "19", "20"], ""],
[
"v22.11.1",
["16", "17", "18", "19", "20"],
"Node version is not supported: Got v22.11.1",
],
],
)
def test_check_node_version(
mock_get_command_version, raw_version: str, versions: list[str], expected: str
):
mock_get_command_version(raw_version)
func = commands.check_node_version
assert func(versions) == expected
31 changes: 13 additions & 18 deletions tests/utils/test_git.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import pytest
from git import Commit, Repo

from cookieplone.utils import git


@pytest.fixture
def tmp_repo(tmp_path):
repo = Repo.init(tmp_path)
repo.index.add(tmp_path)
repo.index.commit("test commit")

return tmp_path


def test_repo_from_path(tmp_repo):
repo = git.repo_from_path(tmp_repo)
assert repo == Repo(tmp_repo)


def test_repo_from_path_invalid(tmp_path):
repo = git.repo_from_path(tmp_path)
def test_repo_from_path_invalid(no_repo):
repo = git.repo_from_path(no_repo)
assert repo is None


def test_check_path_is_repository(tmp_repo):
assert git.check_path_is_repository(tmp_repo)


def test_check_path_is_repository_invalid(tmp_path):
assert not git.check_path_is_repository(tmp_path)
def test_check_path_is_repository_invalid(no_repo):
assert not git.check_path_is_repository(no_repo)


def test_initialize_repository_existing_repo(tmp_repo):
repo = git.initialize_repository(tmp_repo)
assert isinstance(repo, Repo)


def test_initialize_repository(tmp_path):
repo = git.initialize_repository(tmp_path)
def test_initialize_repository_new_repo(no_repo):
repo = git.initialize_repository(no_repo)
assert isinstance(repo, Repo)


Expand All @@ -42,5 +37,5 @@ def test_get_last_commit(tmp_repo):
assert commit.summary == "test commit"


def test_get_last_commit_invalid(tmp_path):
assert git.get_last_commit(tmp_path) is None
def test_get_last_commit_invalid(no_repo):
assert git.get_last_commit(no_repo) is None
15 changes: 15 additions & 0 deletions tests/utils/test_internal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import sys
from pathlib import Path

Expand All @@ -16,3 +17,17 @@ def test_version_info():
f"Python {sys.version})"
)
assert result == expected


def test_signature_md_without_commit(no_repo):
result = internal.signature_md(no_repo)
assert isinstance(result, str)
assert result.startswith(f"Generated using [Cookieplone ({__version__})]")
assert "[cookiecutter-plone]" in result


def test_signature_md_with_commit(tmp_repo):
result = internal.signature_md(tmp_repo)
assert isinstance(result, str)
assert result.startswith(f"Generated using [Cookieplone ({__version__})]")
assert re.search(r"\[cookiecutter-plone \([a-f0-9]{7}\)]\([^\)]*\)", result)
34 changes: 34 additions & 0 deletions tests/utils/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
["foo", "", "foo should be provided"],
["", "", " should be provided"],
["foo", "not empty", ""],
["foo", 0, ""],
["foo", 0.0, ""],
["foo", [0], ""],
["foo", [], "foo should be provided"],
],
)
def test_validate_not_empty(key: str, value: str, expected: str):
Expand Down Expand Up @@ -130,6 +134,7 @@ def test_validate_npm_package_name(value: str, expected: str):
@pytest.fixture
def context():
return {
"__foo": "",
"addon_name": "volto-code-block",
"npm_package_name": "@plonegovbr/volto-code-block",
"another": "",
Expand All @@ -144,6 +149,7 @@ def my_validators():
return [
data.ItemValidator("addon_name", validators.validate_volto_addon_name),
data.ItemValidator("npm_package_name", validators.validate_npm_package_name),
data.ItemValidator("another", validators.validate_plone_version, "warning"),
]


Expand All @@ -160,3 +166,31 @@ def test_run_context_validations(
"""Test run_context_validations function."""
result = validators.run_context_validations(context, my_validators, allow_empty)
assert result.status is expected


@pytest.mark.parametrize(
"version,expected",
(
("5.2.99", "5.2.99 is not a valid Plone version."),
("6.0.1", ""),
("6.1.0a3", ""),
("7.0.0", ""),
),
)
def test_validate_plone_version(version: str, expected: str):
func = validators.validate_plone_version
assert func(version) == expected


@pytest.mark.parametrize(
"version,expected",
(
("14.0.0", "14.0.0 is not a valid Volto version."),
("18.0.0-alpha.21", ""),
("17.0.0", ""),
("16.15.1", ""),
),
)
def test_validate_volto_version(version: str, expected: str):
func = validators.validate_volto_version
assert func(version) == expected

0 comments on commit 90780a7

Please sign in to comment.