From b2bd2a02fe7637232ef8422c93b5fe51d22ab9b3 Mon Sep 17 00:00:00 2001 From: Sandro Campos Date: Tue, 13 Feb 2024 17:56:08 -0500 Subject: [PATCH 1/2] Add Github workflows validation to pre-commit --- python-project-template/.pre-commit-config.yaml.jinja | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python-project-template/.pre-commit-config.yaml.jinja b/python-project-template/.pre-commit-config.yaml.jinja index fdc2069..3751f3e 100644 --- a/python-project-template/.pre-commit-config.yaml.jinja +++ b/python-project-template/.pre-commit-config.yaml.jinja @@ -37,6 +37,12 @@ repos: - id: validate-pyproject name: Validate pyproject.toml description: Verify that pyproject.toml adheres to the established schema. + # Verify that GitHub workflows are well formed + - repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.28.0 + hooks: + - id: check-github-workflows + args: ["--verbose"] {%- if 'isort' in enforce_style %} # Automatically sort the imports used in .py files - repo: https://github.com/pycqa/isort From cb49ce488d273a90c9d21ec66986386b82a49955 Mon Sep 17 00:00:00 2001 From: Sandro Campos Date: Wed, 14 Feb 2024 14:53:13 -0500 Subject: [PATCH 2/2] Add unit test for workflow validation --- tests/test_package_creation.py | 37 +++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/tests/test_package_creation.py b/tests/test_package_creation.py index 4ca2c8c..80cc11c 100644 --- a/tests/test_package_creation.py +++ b/tests/test_package_creation.py @@ -1,5 +1,4 @@ import pytest -import pytest_copie import subprocess @@ -19,7 +18,8 @@ def black_runs_successfully(result): """Test to ensure that the black linter runs successfully on the project""" # run black with `--check` to look for lint errors, but don't fix them. black_results = subprocess.run( - ["python", "-m", "black", "--check", (result.project_dir / "src")], cwd=result.project_dir + ["python", "-m", "black", "--check", (result.project_dir / "src")], + cwd=result.project_dir, ) return black_results.returncode == 0 @@ -29,7 +29,14 @@ def pylint_runs_successfully(result): """Test to ensure that the pylint linter runs successfully on the project""" # run pylint to ensure that the hydrated files are linted correctly pylint_results = subprocess.run( - ["python", "-m", "pylint", "--recursive=y", "--rcfile=./src/.pylintrc", (result.project_dir / "src")], + [ + "python", + "-m", + "pylint", + "--recursive=y", + "--rcfile=./src/.pylintrc", + (result.project_dir / "src"), + ], cwd=result.project_dir, ) @@ -48,6 +55,19 @@ def unit_tests_in_project_run_successfully(result, package_name="example_package return pytest_results.returncode == 0 +def github_workflows_are_valid(result): + """Test to ensure that the GitHub workflows are valid""" + workflows_results = subprocess.run( + ["pre-commit", "run", "check-github-workflows"], cwd=result.project_dir + ) + return workflows_results.returncode == 0 + + +def initialize_git_project(result): + """Initializes local git repository (required to run pre-commit)""" + subprocess.call(["git", "init", "."], cwd=result.project_dir) + + def test_all_defaults(copie): """Test that the default values are used when no arguments are given. Ensure that the project is created and that the basic files exist. @@ -155,3 +175,14 @@ def test_smoke_test_notification(copie): assert successfully_created_project(result) assert directory_structure_is_correct(result) assert black_runs_successfully(result) + + +def test_github_workflows_schema(copie): + """Confirm the current GitHub workflows have valid schemas.""" + extra_answers = { + "include_benchmarks": True, + "include_docs": True, + } + result = copie.copy(extra_answers=extra_answers) + initialize_git_project(result) + assert github_workflows_are_valid(result)