diff --git a/.github/workflows/test-docker-image-build.yml b/.github/workflows/test-docker-image-build.yml new file mode 100644 index 00000000..5ef380d1 --- /dev/null +++ b/.github/workflows/test-docker-image-build.yml @@ -0,0 +1,57 @@ +name: Docker Image Build for Tests + +on: + workflow_dispatch: + push: + paths: + - "tests/Dockerfile" + - "tests/run.sh" + - "tests/requirements.txt" + - ".github/workflows/test-docker-image-build.yml" + +env: + IMAGE_NAME: "pitop/pt-miniscreen-test-runner" + PLATFORMS: "linux/amd64" + +jobs: + build-push-docker-hub: + runs-on: ubuntu-20.04 + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + version: latest + install: true + + - name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + + - name: Build and push + id: docker_build_push + uses: docker/build-push-action@v2 + with: + context: tests + file: tests/Dockerfile + platforms: ${{ env.PLATFORMS }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Show image digest + run: echo ${{ steps.docker_build_push.outputs.digest }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 342adea1..e137b1d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,6 +6,9 @@ on: branches: - master +env: + IMAGE_NAME: "pitop/pt-miniscreen-test-runner" + jobs: test: runs-on: ubuntu-20.04 @@ -13,21 +16,12 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - - name: Setup Python - uses: actions/setup-python@v4 - with: - python-version: 3.9 - - - name: Install dependencies - run: | - sudo apt update - sudo apt install python3-pip -y - pip3 install -r tests/requirements.txt - - name: Run tests run: | - pytest --verbose --cov-report term-missing --cov=pt_miniscreen - coverage xml + docker run \ + --rm \ + --volume "$PWD":/src \ + ${{ env.IMAGE_NAME }} - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d545c8b3..3ca8f8dd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -49,5 +49,5 @@ repos: rev: v1.8.0 hooks: - id: mypy - additional_dependencies: [types-all] + additional_dependencies: [types-setuptools] exclude: ^legacy_widgets diff --git a/setup.cfg b/setup.cfg index 647cc03b..1373e2b2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,4 +56,4 @@ profile = black max-line-length = 150 [tool:pytest] -addopts = -n auto --dist=loadfile +addopts = -n auto --dist=loadfile --reruns 2 --reruns-delay 1 diff --git a/tests/Dockerfile b/tests/Dockerfile new file mode 100644 index 00000000..e8bff2be --- /dev/null +++ b/tests/Dockerfile @@ -0,0 +1,26 @@ +FROM debian:bookworm + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt update + +RUN apt install -y \ + python3 \ + python3-pip \ + libfreetype6-dev \ + libjpeg-dev \ + zlib1g-dev + +RUN rm -f /usr/lib/python3.11/EXTERNALLY-MANAGED + +COPY requirements.txt /tmp/requirements.txt + +RUN pip3 install -r /tmp/requirements.txt --extra-index-url=https://packagecloud.io/pi-top/pypi/pypi/simple + +RUN rm -f /tmp/requirements.txt + +COPY scripts/run.sh / + +WORKDIR /src + +ENTRYPOINT [ "/run.sh" ] diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..6be91504 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,42 @@ +## Tests + +We recommend running tests in a docker image. Most of the tests produce images that are compared with +a base snapshot, but sometimes results might be a bit different from machine to machine (e.g.: image might be shifted 1 pixel). + +### Using pi-top test runner image + +``` +$ docker run \ + --rm \ + --volume "$PWD":/src \ + pitop/pt-miniscreen-test-runner:latest +``` + +### Building the image + +Build the image by running: + +``` +$ docker build -t pt-miniscreen-test-runner tests +``` + +Then, run the tests with: + +``` +$ docker run \ + --rm \ + --volume "$PWD":/src \ + pt-miniscreen-test-runner +``` + +### Updating base images + +Override the entrypoint to access the image using `bash`. Then use `pytest --snapshot-update` to update the snapshots in the tests folder. + +``` +$ docker run \ + --rm \ + --volume "$PWD":/src \ + --entrypoint bash \ + pitop/pt-miniscreen-test-runner:latest +``` diff --git a/tests/scripts/run.sh b/tests/scripts/run.sh new file mode 100755 index 00000000..e64034b3 --- /dev/null +++ b/tests/scripts/run.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ ! -d "/src" ]; then + echo "Error: /src directory not found, make sure to mount the project root directory to /src" + exit 1 +fi + +cd /src + +# Install new dependencies +pip3 install -r tests/requirements.txt --extra-index-url=https://packagecloud.io/pi-top/pypi/pypi/simple + +# Run the tests +pytest --verbose --cov-report term-missing --cov=pt_miniscreen || exit 1 + +# Generate the coverage report +coverage xml