name: Code testing

on: [push, pull_request]

jobs:
  Tests:

    runs-on: ubuntu-latest
    permissions:
      checks: write
      pull-requests: write
    strategy:
      matrix:
        python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']

    steps:
    - uses: actions/checkout@v6
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
    - uses: astral-sh/setup-uv@v7

    - name: Install minimal dependencies
      run: |
        uv venv
        uv pip install -e .
        . .venv/bin/activate
        python -c "import skrf; print(skrf.__version__)"  # Test import with minimal dependencies

    # run the tests located in skrf/
    - name: Test the code, tutorials and examples
      if: ${{ always() }}
      run: |
        uv pip install -e .[test,visa,netw,xlsx,plot,docs,testspice] --compile
        . .venv/bin/activate
        python -m pytest -n auto --junitxml=test-results/junit-${{ matrix.python-version }}.xml --junit-prefix=${{ matrix.python-version }}

    - name: Upload Test Results
      if: always()
      uses: actions/upload-artifact@v6
      with:
        name: Test Results (Python ${{ matrix.python-version }})
        path: test-results/*.xml


    # Upload coverage data to coveralls.io
    - name: Upload coverage data to coveralls.io
      continue-on-error: true
      if: ${{ always() }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        COVERALLS_FLAG_NAME: ${{ matrix.test-name }}
        COVERALLS_PARALLEL: true
      run: |
        pip install coveralls
        coveralls --service=github

  coveralls:
    name: Indicate completion to coveralls.io (Finish)
    continue-on-error: true
    if: ${{ always() }}
    needs: Tests
    runs-on: ubuntu-latest
    container: python:3-slim
    steps:
      - name: Finished
        run: |
          pip3 install --upgrade coveralls
          coveralls --finish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}


  publish-test-results:
    name: "Publish Tests Results"
    needs: Tests
    runs-on: ubuntu-latest
    permissions:
      checks: write

      # only needed unless run with comment_mode: off
      pull-requests: write

    if: always()

    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v6
        with:
          path: artifacts

      - name: Publish Test Results
        uses: EnricoMi/publish-unit-test-result-action@v2
        with:
          files: "artifacts/**/*.xml"
