diff --git a/pyproject.toml b/pyproject.toml index 8b66c97..1586a3a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,7 @@ cookiecutter = "^2.6.0" semver = "^3.0.2" typer = {extras = ["all"], version = "^0.12.3"} packaging = "^24.0" +gitpython = "3.1.43" [tool.poetry.group.dev.dependencies] pytest = "^8.1.1" diff --git a/tests/utils/test_console.py b/tests/utils/test_console.py new file mode 100644 index 0000000..d83fea1 --- /dev/null +++ b/tests/utils/test_console.py @@ -0,0 +1,25 @@ +import pytest +from unittest.mock import patch + +from cookieplone.utils import console + + +@patch("cookieplone.utils.console.print") +def test_print_plone_banner(mock_print): + console.print_plone_banner() + mock_print.assert_called_once_with(console.BANNER, "bold", "blue") + + +@pytest.mark.parametrize( + "func,msg,style,color", + [ + [console.info, "foo", "bold", "white"], + [console.success, "foo", "bold", "green"], + [console.error, "foo", "bold", "red"], + [console.warning, "foo", "bold", "yellow"], + ], +) +@patch("cookieplone.utils.console.print") +def test_prints(mock_print, func, msg, style, color): + func(msg) + mock_print.assert_called_once_with(msg, style, color) diff --git a/tests/utils/test_containers.py b/tests/utils/test_containers.py new file mode 100644 index 0000000..59f5d62 --- /dev/null +++ b/tests/utils/test_containers.py @@ -0,0 +1,16 @@ +import pytest + +from cookieplone.utils import containers + + +@pytest.mark.parametrize( + "registry,expected", + [ + ["docker_hub", ""], + ["github", "ghcr.io/"], + ["gitlab", "registry.gitlab.com/"], + ["bitbucket", ""], + ], +) +def test_image_prefix(registry, expected): + assert containers.image_prefix(registry) == expected diff --git a/tests/utils/test_files.py b/tests/utils/test_files.py new file mode 100644 index 0000000..d41fa89 --- /dev/null +++ b/tests/utils/test_files.py @@ -0,0 +1,44 @@ +import pytest + +from cookieplone.utils import files + + +@pytest.fixture +def tmp_files(tmp_path): + folder1 = tmp_path / "test" / ".github" + folder1.mkdir(parents=True) + + file1 = tmp_path / "test" / "test_file.txt" + file1.touch() + + return file1 + + +def test_remove_files(tmp_path, tmp_files): + files_to_remove = tmp_files + folders_to_remove = tmp_files.parent + func = files.remove_files + + assert files_to_remove.exists() + func(tmp_path, [files_to_remove]) + assert not files_to_remove.exists() + + assert folders_to_remove.exists() + func(tmp_path, [folders_to_remove]) + assert not folders_to_remove.exists() + + +def test_remove_files_nonexistent_file(tmp_path): + files_to_remove = ["nonexistent_file.txt"] + base_path = tmp_path + assert files.remove_files(base_path, files_to_remove) == None + + +def test_remove_gha(tmp_files): + func = files.remove_gha + base_path = tmp_files.parent + + gha = base_path / ".github" + assert gha.exists() + func(base_path) + assert not gha.exists() diff --git a/tests/utils/test_git.py b/tests/utils/test_git.py new file mode 100644 index 0000000..86bd419 --- /dev/null +++ b/tests/utils/test_git.py @@ -0,0 +1,47 @@ +import pytest + +from git import Repo, Commit + +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) + 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_initialize_repository(tmp_path): + repo = git.initialize_repository(tmp_path) + assert isinstance(repo, Repo) + + +def test_get_last_commit(tmp_repo): + commit = git.get_last_commit(tmp_repo) + assert isinstance(commit, Commit) + assert commit.summary == "test commit" + + +def test_get_last_commit_invalid(tmp_path): + assert git.get_last_commit(tmp_path) is None diff --git a/tests/utils/test_internal.py b/tests/utils/test_internal.py new file mode 100644 index 0000000..ffd3a6c --- /dev/null +++ b/tests/utils/test_internal.py @@ -0,0 +1,19 @@ +import sys +from pathlib import Path + +import pytest + +from cookiecutter import __version__ as __cookiecutter_version__ +from cookieplone import __version__ +from cookieplone.utils import internal + + +def test_version_info(): + result = internal.version_info() + location = Path(__file__).parent.parent.parent / "cookieplone" + expected = ( + f"Cookieplone {__version__} from {location} " + f"(Cookiecutter {__cookiecutter_version__}, " + f"Python {sys.version})" + ) + assert result == expected