From 28b058391927a0107e6b74512db9ca3c85933ea6 Mon Sep 17 00:00:00 2001 From: Davy Peter Braun <543614+dheavy@users.noreply.github.com> Date: Mon, 1 Apr 2024 16:46:33 +0200 Subject: [PATCH 1/2] Fix pytest call from pre-commit hook --- .pre-commit-config.yaml | 2 +- CONTRIBUTING.md | 10 ++++++---- run_pytest.py | 36 ++++++++++++++++++++++++++++++++++++ software/tests/__init__.py | 0 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 run_pytest.py create mode 100644 software/tests/__init__.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0d70cf2b..913aeb4d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: hooks: - id: pytest name: pytest - entry: pytest software/tests + entry: python run_pytest.py language: system types: [python] pass_filenames: false diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0f011a65..c6aa693c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,14 +55,16 @@ Then run `poetry install` again. If this doesn't work, please join our [Discord ## Code Formatting and Linting -Our project uses `black` for code formatting and `isort` for import sorting. To ensure consistency across contributions, please adhere to the following guidelines: +Our project uses `ruff` for code formatting and `isort` for import sorting. To ensure consistency across contributions, please adhere to the following guidelines: 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 # /!\ You MUST do it the virtual environment of your project + poetry add --dev pre-commit # Install pre-commit as a dev dependency pre-commit install ``` @@ -73,7 +75,7 @@ Our project uses `black` for code formatting and `isort` for import sorting. To If you choose not to use the pre-commit hooks, you can manually format your code using: ```bash - black . + ruff . isort . ``` diff --git a/run_pytest.py b/run_pytest.py new file mode 100644 index 00000000..1a9ce25c --- /dev/null +++ b/run_pytest.py @@ -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() diff --git a/software/tests/__init__.py b/software/tests/__init__.py new file mode 100644 index 00000000..e69de29b From 130c0ac95fb985d465dced52a34c6c7b05ba9599 Mon Sep 17 00:00:00 2001 From: Davy Peter Braun <543614+dheavy@users.noreply.github.com> Date: Sun, 7 Apr 2024 15:13:03 +0200 Subject: [PATCH 2/2] Change linter, ruff to black --- .pre-commit-config.yaml | 9 ++++----- CONTRIBUTING.md | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 913aeb4d..54b74a1f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,9 @@ repos: - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.2.2" + - repo: https://github.com/psf/black + rev: 24.3.0 # Use the latest revision of Black hooks: - - id: ruff - args: ["--fix"] - - id: ruff-format + - id: black + language_version: python3 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.5.0 hooks: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c6aa693c..61c11716 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,7 +55,7 @@ Then run `poetry install` again. If this doesn't work, please join our [Discord ## Code Formatting and Linting -Our project uses `ruff` for code formatting and `isort` for import sorting. To ensure consistency across contributions, please adhere to the following guidelines: +Our project uses `black` for code formatting and `isort` for import sorting. To ensure consistency across contributions, please adhere to the following guidelines: 1. **Install Pre-commit Hooks**: @@ -63,7 +63,7 @@ Our project uses `ruff` for code formatting and `isort` for import sorting. To e ```bash cd software # Change into `software` directory if not there already. - poetry shell # /!\ You MUST do it the virtual environment of your project + 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 ``` @@ -75,7 +75,7 @@ Our project uses `ruff` for code formatting and `isort` for import sorting. To e If you choose not to use the pre-commit hooks, you can manually format your code using: ```bash - ruff . + black . isort . ```