# SPDX-FileCopyrightText: (c) TagStudio Contributors
# SPDX-License-Identifier: GPL-3.0-only
---
name: pytest

on: [push, pull_request]

jobs:
  pytest-linux:
    name: Run pytest (Linux)
    runs-on: ubuntu-24.04

    steps:
      - &checkout
        name: Checkout repo
        uses: actions/checkout@v4

      - &setup-python
        name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - &install-dependencies
        name: Install Python dependencies
        run: |
          python -m pip install --upgrade uv
          uv pip install --system .[pytest]

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            libegl1 \
            libgl1 \
            libopengl0 \
            libpulse0 \
            libxcb-cursor0 \
            libxcb-icccm4 \
            libxcb-image0 \
            libxcb-keysyms1 \
            libxcb-randr0 \
            libxcb-render-util0 \
            libxcb-xinerama0 \
            libxkbcommon-x11-0 \
            libyaml-dev \
            ripgrep \
            x11-utils

      - name: Execute pytest
        run: |
          xvfb-run pytest --cov-report xml --cov=tagstudio

      - name: Upload coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage
          path: coverage.xml

  pytest-windows:
    name: Run pytest (Windows)
    runs-on: windows-2025

    steps:
      - *checkout
      - *setup-python
      - *install-dependencies

      - name: Install system dependencies
        run: |
          choco install ripgrep

      - name: Execute pytest
        run: |
          pytest

  coverage:
    name: Check coverage
    runs-on: ubuntu-latest
    needs: pytest-linux

    steps:
      - name: Fetch coverage
        uses: actions/download-artifact@v4
        with:
          name: coverage

      - name: Check coverage
        uses: yedpodtrzitko/coverage@main
        with:
          thresholdAll: 0.1
          thresholdNew: 0.1
          thresholdModified: 0.1
          coverageFile: coverage.xml
          token: ${{ secrets.GITHUB_TOKEN }}
          sourceDir: tagstudio/src
