From 84545c6e93c6338faf9414be44148ea298a39934 Mon Sep 17 00:00:00 2001 From: Florian Maas Date: Mon, 15 Jul 2024 13:07:33 +0200 Subject: [PATCH] improve tests --- tests/test_cookiecutter.py | 26 +++++++++++-------------- tests/utils.py | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/test_cookiecutter.py b/tests/test_cookiecutter.py index 5085c2c..6d037ab 100644 --- a/tests/test_cookiecutter.py +++ b/tests/test_cookiecutter.py @@ -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): @@ -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)): @@ -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") @@ -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") @@ -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" ) @@ -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") @@ -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" ) @@ -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") @@ -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") diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..88dc3e1 --- /dev/null +++ b/tests/utils.py @@ -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