# https://github.com/adafruit/circuitpython/blob/24a8927d0ce1d77addd584bf809ab73cb0386e75/.github/workflows/run-tests.yml

# Copyright 2013-2023 Damien P. George
#
# Licensed under the MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

name: Run tests

on:
  workflow_call:
    inputs:
      cp-version:
        required: true
        type: string

jobs:
  run:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        test: [all, mpy, native, native_mpy]
    env:
      CP_VERSION: ${{ inputs.cp-version }}
      MICROPY_CPYTHON3: python3.12
      MICROPY_MICROPYTHON: ../ports/unix/build-coverage/micropython
      TEST_all:
      TEST_mpy: --via-mpy -d basics float micropython
      TEST_native: --emit native
      TEST_native_mpy: --via-mpy --emit native -d basics float micropython
    steps:
      - name: Set up repository
        uses: actions/checkout@v4
        with:
          submodules: false
          show-progress: false
          fetch-depth: 1
      - name: Set up python
        uses: actions/setup-python@v5
        with:
          python-version: 3.12
      - name: Set up submodules
        uses: ./.github/actions/deps/submodules
        with:
          target: tests
      - name: Set up external
        if: matrix.test == 'all'
        uses: ./.github/actions/deps/external
      - name: Set up mpy-cross
        uses: ./.github/actions/mpy_cross
        with:
          cp-version: ${{ inputs.cp-version }}
      - name: Build unix port
        run: make -C ports/unix VARIANT=coverage -j4
      - name: Run tests
        run: ./run-tests.py -j4 ${{ env[format('TEST_{0}', matrix.test)] }}
        working-directory: tests
      - name: Print failure info
        run: ./run-tests.py -j4 --print-failures
        if: failure()
        working-directory: tests
    # Not working after MicroPython v1.23 merge.
    # - name: Build native modules
    #   if: matrix.test == 'all'
    #   run: |
    #     make -C examples/natmod/features1
    #     make -C examples/natmod/features2
    #     make -C examples/natmod/heapq
    #     make -C examples/natmod/random
    #     make -C examples/natmod/re
    # - name: Test native modules
    #   if: matrix.test == 'all'
    #   run: ./run-natmodtests.py extmod/{heapq*,random*,re*}.py
    #   working-directory: tests
