Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation of Github workflows to pre-commit #425

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions python-project-template/.pre-commit-config.yaml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 34 additions & 3 deletions tests/test_package_creation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import pytest_copie
import subprocess


Expand All @@ -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
Expand All @@ -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,
)

Expand All @@ -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.
Expand Down Expand Up @@ -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)