From 9caa3195ab1f5404d7a36edbf65b4400860edf02 Mon Sep 17 00:00:00 2001 From: Christian Merten Date: Wed, 8 Oct 2025 01:15:34 +0200 Subject: [PATCH] chore(CI): add tests action (#1) We run the test suite on every commit and compute the coverage. The action succeeds only if the coverage is at 100%. --- .github/workflows/test.yml | 60 ++++++++++++++++++++++++++++++++ Makefile | 4 ++- README.md | 2 +- docker/test/entrypoint-master.sh | 2 ++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..7605281 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,60 @@ +name: Tests + +on: + push: + branches: + - main + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ hashFiles('requirements.txt', 'docker/test/Dockerfile') }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Build Docker image with cache + run: | + cd docker/test + docker buildx build \ + --cache-from type=local,src=/tmp/.buildx-cache \ + --cache-to type=local,dest=/tmp/.buildx-cache-new,mode=max \ + --load \ + -t kompass:test \ + -f Dockerfile \ + ../../ + + - name: Move cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache + + - name: Run tests + run: make test-only + + - name: Check coverage + run: | + COVERAGE=$(python3 -c "import json; data=json.load(open('docker/test/htmlcov/coverage.json')); print(data['totals']['percent_covered'])") + echo "Coverage: ${COVERAGE}%" + if (( $(echo "$COVERAGE < 100" | bc -l) )); then + echo "Error: Coverage is ${COVERAGE}%, must be 100%" + exit 1 + fi diff --git a/Makefile b/Makefile index f40252a..b148b4c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ build-test: cd docker/test; docker compose build -test: build-test +test-only: mkdir -p docker/test/htmlcov chmod 777 docker/test/htmlcov ifeq ($(keepdb), true) @@ -10,3 +10,5 @@ else cd docker/test; docker compose up --abort-on-container-exit endif echo "Generated coverage report. To read it, point your browser to:\n\nfile://$$(pwd)/docker/test/htmlcov/index.html" + +test: build-test test-only diff --git a/README.md b/README.md index b62aa3a..0571403 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # jdav Kompass -[![Build Status](https://jenkins.merten.dev/buildStatus/icon?job=gitea%2Fkompass%2Fmain)](https://jenkins.merten.dev/job/gitea/job/kompass/job/main/) +![Build Status](https://github.com/chrisflav/kompass/actions/workflows/test.yml/badge.svg?branch=main) Kompass is an administration platform designed for local sections of the Young German Alpine Club. It provides tools to contact and (automatically) manage members, groups, material, excursions and statements. diff --git a/docker/test/entrypoint-master.sh b/docker/test/entrypoint-master.sh index 6dc1db9..cb78061 100755 --- a/docker/test/entrypoint-master.sh +++ b/docker/test/entrypoint-master.sh @@ -44,3 +44,5 @@ else coverage run manage.py test startpage finance members contrib logindata mailer material ludwigsburgalpin jdav_web -v 2 --noinput fi coverage html +coverage json -o htmlcov/coverage.json +coverage report