Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpgmaas committed Jul 15, 2024
1 parent f568120 commit 84545c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
26 changes: 11 additions & 15 deletions tests/test_cookiecutter.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import os
import shlex
import subprocess
from contextlib import contextmanager


@contextmanager
def run_within_dir(path: str):
oldpwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(oldpwd)


def file_contains_text(file: str, text: str) -> bool:
with open(file) as f:
return f.read().find(text) != -1
from tests.utils import file_contains_text, is_valid_yaml, run_within_dir


def test_bake_project(cookies):
Expand All @@ -37,6 +23,7 @@ def test_using_pytest(cookies, tmp_path):
assert result.exception is None
assert result.project_path.name == "example-project"
assert result.project_path.is_dir()
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "main.yml")

# Install the poetry environment and run the tests.
with run_within_dir(str(result.project_path)):
Expand Down Expand Up @@ -66,6 +53,7 @@ def test_cicd_contains_artifactory_secrets(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"publish_to": "artifactory"})
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "on-release-main.yml")
for text in ["ARTIFACTORY_URL", "ARTIFACTORY_USERNAME", "ARTIFACTORY_PASSWORD"]:
assert file_contains_text(f"{result.project_path}/.github/workflows/on-release-main.yml", text)
assert file_contains_text(f"{result.project_path}/Makefile", "build-and-publish")
Expand All @@ -75,6 +63,7 @@ def test_cicd_contains_pypi_secrets(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"publish_to": "pypi"})
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "on-release-main.yml")
assert file_contains_text(f"{result.project_path}/.github/workflows/on-release-main.yml", "PYPI_TOKEN")
assert file_contains_text(f"{result.project_path}/Makefile", "build-and-publish")

Expand All @@ -83,6 +72,7 @@ def test_dont_publish(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"publish_to": "none"})
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "on-release-main.yml")
assert not file_contains_text(
f"{result.project_path}/.github/workflows/on-release-main.yml", "make build-and-publish"
)
Expand All @@ -92,6 +82,8 @@ def test_mkdocs(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"mkdocs": "y"})
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "main.yml")
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "on-release-main.yml")
assert file_contains_text(f"{result.project_path}/.github/workflows/on-release-main.yml", "mkdocs gh-deploy")
assert file_contains_text(f"{result.project_path}/Makefile", "docs:")
assert os.path.isdir(f"{result.project_path}/docs")
Expand All @@ -101,6 +93,8 @@ def test_not_mkdocs(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"mkdocs": "n"})
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "main.yml")
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "on-release-main.yml")
assert not file_contains_text(
f"{result.project_path}/.github/workflows/on-release-main.yml", "mkdocs gh-deploy"
)
Expand Down Expand Up @@ -134,6 +128,7 @@ def test_codecov(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake()
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "main.yml")
assert os.path.isfile(f"{result.project_path}/codecov.yaml")
assert os.path.isfile(f"{result.project_path}/.github/workflows/validate-codecov-config.yml")

Expand All @@ -142,6 +137,7 @@ def test_not_codecov(cookies, tmp_path):
with run_within_dir(tmp_path):
result = cookies.bake(extra_context={"codecov": "n"})
assert result.exit_code == 0
assert is_valid_yaml(result.project_path / ".github" / "workflows" / "main.yml")
assert not os.path.isfile(f"{result.project_path}/codecov.yaml")
assert not os.path.isfile(f"{result.project_path}/.github/workflows/validate-codecov-config.yml")

Expand Down
40 changes: 40 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from contextlib import contextmanager
from pathlib import Path

import yaml


def is_valid_yaml(path: str | Path):
path = Path(path)

if not path.is_file():
print(f"File does not exist: {path}")
return False

try:
with path.open("r") as file:
yaml.safe_load(file)
except yaml.YAMLError as e:
print(f"Invalid YAML file: {path} - Error: {e}")
return False
except OSError as e:
print(f"Error reading file: {path} - Error: {e}")
return False

return True


@contextmanager
def run_within_dir(path: str):
oldpwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(oldpwd)


def file_contains_text(file: str, text: str) -> bool:
with open(file) as f:
return f.read().find(text) != -1

0 comments on commit 84545c6

Please sign in to comment.