Skip to content

Commit 48c5d89

Browse files
authored
Merge pull request #213 from dheavy/fix/precommit-tests
Fix pytest call from pre-commit hook, change linter to black
2 parents a40c804 + 0eca547 commit 48c5d89

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ repos:
44
hooks:
55
- id: black
66
language_version: python3
7-
args: ["software/"]
87
- repo: https://github.com/pre-commit/pre-commit-hooks
98
rev: v4.5.0
109
hooks:
@@ -19,7 +18,7 @@ repos:
1918
hooks:
2019
- id: pytest
2120
name: pytest
22-
entry: pytest software/tests
21+
entry: python run_pytest.py
2322
language: system
2423
types: [python]
2524
pass_filenames: false

CONTRIBUTING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ Our project uses `black` for code formatting and `isort` for import sorting. To
5959

6060
1. **Install Pre-commit Hooks**:
6161

62-
If you want to automatically format your code every time you make a commit, install the pre-commit hooks.
62+
To automatically format your code every time you make a commit, install the pre-commit hooks.
6363

6464
```bash
65-
pip install pre-commit
65+
cd software # Change into `software` directory if not there already.
66+
poetry shell # It's better to do it within the virtual environment of your project
67+
poetry add --dev pre-commit # Install pre-commit as a dev dependency
6668
pre-commit install
6769
```
6870

run_pytest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import subprocess
2+
import sys
3+
import ctypes
4+
import os
5+
6+
7+
def main():
8+
"""Run pytest in the software directory.
9+
10+
This script is intended to be used as a pre-commit hook to run the tests from the root of the repository.
11+
"""
12+
13+
# Additional setup for Windows (10 at least) to prevent issues with Unicode characters in the console.
14+
# see https://www.reddit.com/r/learnpython/comments/350c8c/unicode_python_3_and_the_windows_console/
15+
if sys.platform.startswith("win"):
16+
# Force UTF-8 encoding in Python
17+
os.environ["PYTHONUTF8"] = "1"
18+
19+
# Change Windows console code page to UTF-8
20+
ctypes.windll.kernel32.SetConsoleCP(65001)
21+
ctypes.windll.kernel32.SetConsoleOutputCP(65001)
22+
23+
# Define the target directory relative to this script location.
24+
target_directory = os.path.join(os.path.dirname(__file__), "software")
25+
26+
os.chdir(target_directory)
27+
28+
# Run pytest with any additional arguments passed to this script.
29+
result = subprocess.run(["pytest"] + sys.argv[1:])
30+
31+
# Exit with pytest's exit code to reflect the test outcome in the pre-commit hook.
32+
sys.exit(result.returncode)
33+
34+
35+
if __name__ == "__main__":
36+
main()

software/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)