Skip to content

Commit

Permalink
Merge pull request #425 from lincc-frameworks/issue/382/pre-commit-wo…
Browse files Browse the repository at this point in the history
…rkflow-validation

Add validation of Github workflows to pre-commit
  • Loading branch information
camposandro authored Feb 14, 2024
2 parents 6f69d3d + cb49ce4 commit 9c136cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
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)

0 comments on commit 9c136cd

Please sign in to comment.