name: CI Tests

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  workflow_dispatch:
    inputs:
      tmate_debug_enabled:
        description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
        required: false
        default: false

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  ci:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ ubuntu-latest, macOS-latest, windows-latest ]
        python-version: [ "3.7", "3.8", "3.9","3.10" ]
    name: Test Python ${{ matrix.python-version }} ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'
          cache-dependency-path: 'requirements/tests.txt'
      - name: Install dependencies
        run: |
          python -m pip --version
          python -m pip install wheel
          python -m pip install -r requirements/tests.txt
      - name: Setup tmate session
        uses: mxschmitt/action-tmate@v3
        if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.tmate_debug_enabled }}
        timeout-minutes: 15
      - name: Run QA Checks
        if: ${{ startsWith(matrix.os, 'ubuntu') && startsWith(matrix.python-version, '3.10') }}
        run: |
          python -m pip install -e . --no-deps
          python -m xonsh run-tests.xsh qa
      - name: Run tests
        if: ${{ !startsWith(matrix.python-version, '3.10') }}
        run: |
          python -m pip install . --no-deps
          python -m xonsh run-tests.xsh test -- --timeout=240
      - name: Run tests with coverage
        if: ${{ startsWith(matrix.python-version, '3.10') }}
        run: |
          python -m pip install -e . --no-deps
          python -m xonsh run-tests.xsh test --report-coverage --no-amalgam -- --timeout=240
      - name: Upload coverage to Codecov
        if: ${{ startsWith(matrix.python-version, '3.10') }}
        uses: codecov/codecov-action@v2
        with:
          verbose: true
          flags: ${{ matrix.os }}
