name: CI
on:
  push:
  pull_request:
  schedule:
    - cron: '0 6 * * 6'
jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', 'pypy-3.9-v7.x']
        os: [ubuntu-latest, windows-latest, macos-latest]
        include:
          - python-version: pypy-3.9-v7.x
            py: pypy3
        # Just to slim down the test matrix:
        exclude:
          - python-version: '3.9'
            os: macos-latest
          - python-version: '3.9'
            os: windows-latest
          - python-version: '3.10'
            os: ubuntu-latest
          - python-version: '3.11'
            os: macos-latest
          - python-version: '3.11'
            os: windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python -m pip install --upgrade hatch
      - name: Run tests
        run: |
          hatch run +py=${{ matrix.py || matrix.python-version }} test:with-coverage
      - name: Run integration tests
        run: |
          hatch run +py=${{ matrix.py || matrix.python-version }} integration:test
        shell: bash
      - name: Upload Codecov Results
        if: success()
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.xml
          flags: unittests
          name: ${{ matrix.os }}/${{ matrix.python-version }}
          fail_ci_if_error: false

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade hatch
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - name: Check with ruff
        if: always()
        run: hatch run style:lint
      - name: Check with mypy
        if: always()
        run: hatch run types:check
      - name: Check Markdown style
        if: always()
        run: hatch run lint:markdown
      - name: Check JS style
        if: always()
        run: hatch run lint:js
      - name: Check spelling
        if: always()
        run: hatch run lint:spelling

  package:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Install dependencies
        run: pip install -U build
      - name: Build package
        run: python -m build
      - name: Check packaged files
        shell: bash -e -x {0}
        run: |
          expected_wheel=(-emkdocs/{templates/sitemap.xml,config/base.py,py.typed,contrib/search/lunr-language/lunr.nl.js,themes/{mkdocs,readthedocs}/{base.html,locales/{de,es}/LC_MESSAGES/messages.mo}})
          expected_sdist=("${expected_wheel[@]}" -e{pyproject.toml,hatch_build.py})
          test "$(tar -ztf dist/mkdocs-*.tar.gz | grep -F "${expected_sdist[@]}" | tee /dev/stderr | wc -l)" -eq "${#expected_sdist[@]}"
          test "$(unzip -l dist/mkdocs-*any.whl | grep -F "${expected_wheel[@]}" | tee /dev/stderr | wc -l)" -eq "${#expected_wheel[@]}"
