Skip to content

Commit

Permalink
Added tests for utils (#7)
Browse files Browse the repository at this point in the history
* added test for files

* added test for git

* added function alias

* added tests for console

* added tests for containers

* added tests for internal
  • Loading branch information
erikriver authored Apr 24, 2024
1 parent de1aaa5 commit d2f91ed
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions tests/utils/test_console.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions tests/utils/test_containers.py
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions tests/utils/test_files.py
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 47 additions & 0 deletions tests/utils/test_git.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/utils/test_internal.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d2f91ed

Please sign in to comment.