Skip to content

Commit 5fb640b

Browse files
authored
Add pre-commit config to primary PPT project. (#471)
* Add smoke test failure notification to lincc-dev channel. * Add pre-commit config to primary PPT project.
1 parent 1a4729b commit 5fb640b

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

.pre-commit-config.yaml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
repos:
2+
# Prevents committing directly branches named 'main' and 'master'.
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v4.4.0
5+
hooks:
6+
- id: no-commit-to-branch
7+
name: Prevent main branch commits
8+
description: Prevent the user from committing directly to the primary branch.
9+
- id: check-added-large-files
10+
name: Check for large files
11+
description: Prevent the user from committing very large files.
12+
args: ['--maxkb=500']
13+
# Verify that pyproject.toml is well formed
14+
- repo: https://github.com/abravalheri/validate-pyproject
15+
rev: v0.12.1
16+
hooks:
17+
- id: validate-pyproject
18+
name: Validate pyproject.toml
19+
description: Verify that pyproject.toml adheres to the established schema.
20+
# Verify that GitHub workflows are well formed
21+
- repo: https://github.com/python-jsonschema/check-jsonschema
22+
rev: 0.28.0
23+
hooks:
24+
- id: check-github-workflows
25+
args: ["--verbose"]
26+
# Automatically sort the imports used in .py files
27+
- repo: https://github.com/pycqa/isort
28+
rev: 5.12.0
29+
hooks:
30+
- id: isort
31+
name: Run isort
32+
description: Sort and organize imports in .py and .pyi files.
33+
types_or: [python, pyi]
34+
# Analyze the tests code style and report code that doesn't adhere.
35+
- repo: local
36+
hooks:
37+
- id: pylint
38+
name: pylint (python files in tests/ and benchmarks/)
39+
entry: pylint
40+
language: system
41+
types: [python]
42+
files: ^(tests|benchmarks)/
43+
args:
44+
[
45+
"-rn", # Only display messages
46+
"-sn", # Don't display the score
47+
]
48+
# Analyze the code style and report code that doesn't adhere.
49+
- repo: https://github.com/psf/black
50+
rev: 23.7.0
51+
hooks:
52+
- id: black-jupyter
53+
name: Format code using black
54+
types_or: [python, pyi, jupyter]
55+
# It is recommended to specify the latest version of Python
56+
# supported by your project here, or alternatively use
57+
# pre-commit's default_language_version, see
58+
# https://pre-commit.com/#top_level-default_language_version
59+
language_version: python3.10
60+
# Make sure Sphinx can build the documentation without issues.
61+
- repo: local
62+
hooks:
63+
- id: sphinx-build
64+
name: Build documentation with Sphinx
65+
entry: sphinx-build
66+
language: system
67+
always_run: true
68+
exclude_types: [file, symlink]
69+
args:
70+
[
71+
"-T", # Show full trace back on exception
72+
"-E", # Don't use saved env. always read all files.
73+
"-b", # Flag to select which builder to use
74+
"html", # Use the HTML builder
75+
"-d", # Flag for cached environment and doctrees
76+
"./docs/_build/doctrees", # directory
77+
"./docs", # Source directory of documents
78+
"./_readthedocs", # Output directory for rendered documents.
79+
]
80+
# Run unit tests, verify that they pass. Note that coverage is run against
81+
# the ./src directory here because that is what will be committed. In the
82+
# github workflow script, the coverage is run against the installed package
83+
# and uploaded to Codecov by calling pytest like so:
84+
# `python -m pytest --cov=<package_name> --cov-report=xml`
85+
- repo: local
86+
hooks:
87+
- id: pytest-check
88+
name: Run unit tests
89+
description: Run unit tests with pytest.
90+
entry: bash -c "if python -m pytest --co -qq; then python -m pytest --cov=./src --cov-report=html; fi"
91+
language: system
92+
pass_filenames: false
93+
always_run: true

tests/test_package_creation.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import pytest
1+
"""Verify package creation using `pytest-copie`"""
2+
23
import subprocess
34

45
import pytest
@@ -10,6 +11,8 @@ def successfully_created_project(result):
1011

1112

1213
def contains_required_files(result):
14+
"""Utility method to confirm that the required project files exist in the
15+
copier result."""
1316
required_files = [
1417
".copier-answers.yml",
1518
".git_archival.txt",
@@ -42,6 +45,7 @@ def black_runs_successfully(result):
4245
black_results = subprocess.run(
4346
["python", "-m", "black", "--check", (result.project_dir / "src")],
4447
cwd=result.project_dir,
48+
check=False,
4549
)
4650

4751
return black_results.returncode == 0
@@ -60,19 +64,20 @@ def pylint_runs_successfully(result):
6064
(result.project_dir / "src"),
6165
],
6266
cwd=result.project_dir,
67+
check=False,
6368
)
6469

6570
return pylint_results.returncode == 0
6671

6772

68-
def unit_tests_in_project_run_successfully(result, package_name="example_package"):
73+
def unit_tests_in_project_run_successfully(result):
6974
"""Test to ensure that the unit tests run successfully on the project
7075
7176
!!! NOTE - This doesn't currently work because we need to `pip install` the hydrated
7277
project before running the tests. And we don't have a way to create a temporary
7378
virtual environment for the project.
7479
"""
75-
pytest_results = subprocess.run(["python", "-m", "pytest"], cwd=result.project_dir)
80+
pytest_results = subprocess.run(["python", "-m", "pytest"], cwd=result.project_dir, check=False)
7681

7782
return pytest_results.returncode == 0
7883

@@ -106,7 +111,7 @@ def docs_build_successfully(result):
106111
def github_workflows_are_valid(result):
107112
"""Test to ensure that the GitHub workflows are valid"""
108113
workflows_results = subprocess.run(
109-
["pre-commit", "run", "check-github-workflows"], cwd=result.project_dir
114+
["pre-commit", "run", "check-github-workflows"], cwd=result.project_dir, check=False
110115
)
111116
return workflows_results.returncode == 0
112117

@@ -131,7 +136,7 @@ def test_all_defaults(copie):
131136

132137
# check to see if the README file was hydrated with copier answers.
133138
found_line = False
134-
with open(result.project_dir / "README.md") as f:
139+
with open(result.project_dir / "README.md", encoding="utf-8") as f:
135140
for line in f:
136141
if "example_project" in line:
137142
found_line = True
@@ -164,7 +169,7 @@ def test_use_black_and_no_example_modules(copie):
164169

165170
# check to see if the pyproject.toml file has the expected dependencies
166171
found_line = False
167-
with open(result.project_dir / "pyproject.toml") as f:
172+
with open(result.project_dir / "pyproject.toml", encoding="utf-8") as f:
168173
for line in f:
169174
if '"black", # Used for static linting of files' in line:
170175
found_line = True

0 commit comments

Comments
 (0)