name: Tests

on:
  # NOTE: github.event context is push payload:
  # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push
  push:
    branches:
      - main
      - feature/**

  # NOTE: github.event context is pull_request payload:
  # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
  pull_request:

concurrency:
  # Concurrency group that uses the workflow name and PR number if available
  # or commit SHA as a fallback. If a new build is triggered under that
  # concurrency group while a previous build is running it will be canceled.
  # Repeated pushes to a PR will cancel all previous builds, while multiple
  # merges to main will not cancel.
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash -l {0}
    strategy:
      fail-fast: false
      matrix:
        python-version: ['3.10', '3.11']

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          architecture: "x64"
          cache: "pip"

      - name: Setup Miniconda
        uses: conda-incubator/setup-miniconda@v2
        with:
          python-version: ${{ matrix.python-version }}
          channels: defaults
          activate-environment: test_env
          auto-update-conda: false
          auto-activate-base: false
          show-channel-urls: true

      - name: Source Scripts
        run: |
          set -x
          # conda is our test dependency but can't be pip installed
          conda install --quiet conda pip
          pip install -e .[test]
          conda info --json
          echo "condarc"
          cat ~/.condarc
          echo "conda_pkgs_dir"
          ls /home/runner/conda_pkgs_dir
          echo "miniconda/pkgs"
          ls /usr/share/miniconda/pkgs
          echo "test_env"
          ls /usr/share/miniconda/envs/test_env
          pytest

  analyze:
    name: Analyze test results
    needs: [linux]
    if: always()
    runs-on: ubuntu-latest
    steps:
      - name: Download test results
        uses: actions/download-artifact@v3

      - name: Upload combined test results
        # provides one downloadable archive of all .coverage/test-report.xml files
        # of all matrix runs for further analysis.
        uses: actions/upload-artifact@v3
        with:
          name: test-results-${{ github.sha }}-all
          path: test-results-${{ github.sha }}-*
          retention-days: 90  # default: 90

      - name: Test Summary
        uses: test-summary/action@v2
        with:
          paths: ./test-results-${{ github.sha }}-**/test-report*.xml

      - name: Decide whether the needed jobs succeeded or failed
        uses: re-actors/alls-green@release/v1
        with:
          jobs: ${{ toJSON(needs) }}
