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

Fix pytest call from pre-commit hook, change linter to black #213

Merged
merged 3 commits into from
Apr 8, 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
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ repos:
hooks:
- id: black
language_version: python3
args: ["software/"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand All @@ -19,7 +18,7 @@ repos:
hooks:
- id: pytest
name: pytest
entry: pytest software/tests
entry: python run_pytest.py
language: system
types: [python]
pass_filenames: false
Expand Down
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ Our project uses `black` for code formatting and `isort` for import sorting. To

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

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

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

Expand Down
36 changes: 36 additions & 0 deletions run_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import subprocess
import sys
import ctypes
import os


def main():
"""Run pytest in the software directory.

This script is intended to be used as a pre-commit hook to run the tests from the root of the repository.
"""

# Additional setup for Windows (10 at least) to prevent issues with Unicode characters in the console.
# see https://www.reddit.com/r/learnpython/comments/350c8c/unicode_python_3_and_the_windows_console/
if sys.platform.startswith("win"):
# Force UTF-8 encoding in Python
os.environ["PYTHONUTF8"] = "1"

# Change Windows console code page to UTF-8
ctypes.windll.kernel32.SetConsoleCP(65001)
ctypes.windll.kernel32.SetConsoleOutputCP(65001)

# Define the target directory relative to this script location.
target_directory = os.path.join(os.path.dirname(__file__), "software")

os.chdir(target_directory)

# Run pytest with any additional arguments passed to this script.
result = subprocess.run(["pytest"] + sys.argv[1:])

# Exit with pytest's exit code to reflect the test outcome in the pre-commit hook.
sys.exit(result.returncode)


if __name__ == "__main__":
main()
Empty file added software/tests/__init__.py
Empty file.