# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

name: Test

on:
  push:
    branches-ignore:
    - deepsource-fix-**
    - renovate/**
    - weblate
  pull_request:
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-24.04
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      matrix:
        python-version:
        - '3.11'
        - '3.13'
        requirements:
        - latest
        database:
        - postgresql
        experimental: [false]
        include:
        # Reduced testing for mysql
        - python-version: '3.13'
          requirements: latest
          database: mysql
          experimental: false
        # Reduced testing for mariadb
        - python-version: '3.13'
          requirements: latest
          database: mariadb
          experimental: false
        # Reduced testing for minimal deps
        - python-version: '3.11'
          requirements: minimal
          database: postgresql
          experimental: false
        # Reduced testing for edge deps
        - python-version: '3.13'
          requirements: edge
          database: postgresql
          experimental: true
        # Test with Python 3.12.
        - python-version: '3.12'
          requirements: latest
          database: postgresql
          experimental: false
    name: py${{ matrix.python-version }}, ${{ matrix.database }}, ${{ matrix.requirements }} deps
    env:
      CI_DATABASE: ${{ matrix.database }}
      CI_REDIS_HOST: 127.0.0.1
      CI_REDIS_PORT: '60001'
      CI_DB_PASSWORD: weblate
      CI_DB_HOST: 127.0.0.1
      CI_DB_PORT: '60000'
      CI_SELENIUM: '1'
      DJANGO_SETTINGS_MODULE: weblate.settings_test
      PYTHONWARNINGS: default,ignore:unclosed:ResourceWarning
      PYTHONUNBUFFERED: 1
    steps:
    - uses: actions/checkout@v4
    - uses: actions/cache@v4
      with:
        path: data-test/cache/tesseract
        key: tesseract-data
    - uses: astral-sh/setup-uv@v5
      with:
        enable-cache: true
        cache-dependency-glob: ''
        cache-suffix: ${{ matrix.python-version }}
    - name: Start services
      run: ./ci/services-up ${{ matrix.database }}
    - name: Install apt dependencies
      run: sudo ./ci/apt-install $CI_DATABASE
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        allow-prereleases: true
    - name: Used versions
      run: ./ci/print-versions
    - name: Install Python dependencies
      run: ./ci/pip-install ${{ matrix.requirements }}
    - name: Prepare database
      run: ./ci/prepare-database
    - name: Compile MO files
      run: uv run --no-sources coverage run ./manage.py compilemessages
    - name: Collect static files
      run: uv run --no-sources coverage run ./manage.py collectstatic --noinput --verbosity 0
    - name: Migrate database
      run: uv run --no-sources coverage run ./manage.py migrate --noinput --traceback
    - name: Django checks
      run: uv run --no-sources coverage run ./manage.py check
    - name: Demo import
      run: uv run --no-sources coverage run ./manage.py import_demo
    - name: Test with Django
      run: |
        uv run --no-sources --all-extras pytest --junitxml=junit.xml --cov=weblate weblate
        cp .coverage .coverage.pytest
    - name: Test wsgi startup
      env:
        PYTHONPATH: .
      run: uv run --no-sources coverage run weblate/wsgi.py
    - name: Coverage
      run: |
        uv run --no-sources coverage combine
        uv run --no-sources coverage xml
    - uses: codecov/codecov-action@v5
      with:
        token: ${{secrets.CODECOV_TOKEN}}
        flags: unittests
        name: Tests py${{ matrix.python-version }}, ${{ matrix.database }}, ${{ matrix.requirements }} deps
    - name: Upload test results to Codecov
      if: ${{ !cancelled() }}
      uses: codecov/test-results-action@v1
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
    - name: DeepSource
      uses: deepsourcelabs/test-coverage-action@v1.1.2
      if: ${{ github.event_name == 'push' }}
      with:
        key: python
        coverage-file: coverage.xml
        dsn: ${{ secrets.DEEPSOURCE_DSN }}
    - name: Stop services
      if: always()
      run: ./ci/services-down ${{ matrix.database }}