Skip to content

Commit

Permalink
githooks: Replace pre-commit tool with simple shell script
Browse files Browse the repository at this point in the history
It's basically impossible to run mypy with pre-commit because it will
ignore the repo config and check all files changed the commit. The
dbusmock templates are special and mypy fails on them so we use the mypy
config to skip them.

This introduces a new script which checks the python tests and installs
all the required tools into a python virtual environment. It also
changes to pre-commit hook to call the script if any of the files in
tests changed and uses git stash push/pop to get the test a clean state
of the repo.
  • Loading branch information
swick committed Jan 17, 2025
1 parent 6c39299 commit 768e94c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
22 changes: 22 additions & 0 deletions .githooks/check-tests-python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail

PYTHON=${PYTHON:-python}

HERE="$(cd "$(dirname "$0")" && pwd)"
VENV="${HERE}/.venv"

[ -d $VENV ] || $PYTHON -m venv $VENV
source $VENV/bin/activate


$PYTHON -m ruff --version >/dev/null || $PYTHON -m pip install ruff
$PYTHON -m mypy --version >/dev/null || $PYTHON -m pip install mypy
$PYTHON -m pytest --version >/dev/null || $PYTHON -m pip install pytest

cd tests

# FIXME run all of them and then exit with correct code
python3 -m ruff check .
python3 -m ruff format --check .
mypy .
43 changes: 27 additions & 16 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
#!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03

# start templated
INSTALL_PYTHON=/usr/bin/python3
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated
set -euo pipefail

HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")
CHECK_TEST_PYTHON="${HERE}/check-tests-python.sh"

path_unchanged()
{
! git diff --cached --name-only | grep $1 >/dev/null
}

check()
{
if [ -z ${PRE_COMMIT_STASH+x} ]; then
PRE_COMMIT_STASH=1

clean_up ()
{
git stash pop
}

if ! git diff -s --exit-code; then
git stash push --keep-index
trap clean_up EXIT
fi
fi

$@
}

if [ -x "$INSTALL_PYTHON" ]; then
exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
elif command -v pre-commit > /dev/null; then
exec pre-commit "${ARGS[@]}"
else
echo '`pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2
exit 1
fi
path_unchanged ^tests/ || check $CHECK_TEST_PYTHON
3 changes: 0 additions & 3 deletions .github/workflows/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,5 @@ RUN apt install -y --no-install-recommends python3-pip
RUN pip install --user --break-system-packages furo">=2024.04.27" \
sphinx-copybutton sphinxext-opengraph matplotlib

# Install pre-commit
RUN pip install --user --break-system-packages pre-commit

# Install qdbusxml2cpp
RUN apt install -y --no-install-recommends qt6-base-dev-tools
6 changes: 2 additions & 4 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ jobs:
- name: Check out xdg-desktop-portal
uses: actions/checkout@v4

- name: Run pre-commit hooks
- name: Run checks
run: |
export PYTHONPATH="/root/.local/lib/python$(python3 -c 'import sys; print("{}.{}".format(*sys.version_info))')/site-packages:$PYTHONPATH"
export PATH="/root/.local/bin:$PATH"
pre-commit run --show-diff-on-failure --color=always --all-files
.githooks/check-tests-python.sh
- name: Check POTFILES.in
run: .github/workflows/check-potfiles.sh
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/container.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
IMAGE_TAG: 20241211-1
IMAGE_TAG: 2025-01-17.0

on:
workflow_call:
Expand Down
10 changes: 0 additions & 10 deletions .pre-commit-config.yaml

This file was deleted.

0 comments on commit 768e94c

Please sign in to comment.