# Copyright (c) 2021-2022 Chris Reed

# pyocd workflow for:
# - checking code with flake8
# - running unit tests
# - run test/import_all.py script
# - build source distro and wheel, check with twine

name: basic test

# Don't run for changes that only affect the docs directory.
on:
  push:
    paths-ignore:
      - 'docs/**'
  pull_request:
    paths-ignore:
      - 'docs/**'

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
        - ubuntu-latest
        - macos-latest
        - windows-latest
        python-version:
        - "3.8"
        - "3.9"
        - "3.10"
        - "3.11"
        - "3.12"
        - "3.13"

    steps:
    # Only check out HEAD. We don't need the full history.
    - uses: actions/checkout@v3
      with:
        fetch-depth: 1

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    # This requires pip 20.1+. As of 28 Feb 2021 this condition is met for all supported
    # Python versions on all OSes.
    - name: Get pip cache dir
      id: pip-cache
      run: |
        echo "::set-output name=dir::$(pip cache dir)"

    - name: Setup pip cache
      uses: actions/cache@v3
      with:
        path: ${{ steps.pip-cache.outputs.dir }}
        key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.cfg') }}
        restore-keys: |
          ${{ runner.os }}-pip-

    - name: Install dependencies
      run: |
        python -m pip install --upgrade setuptools pip wheel build twine
        pip install -e .[test]

    - name: Setup flake8 annotations
      # This commit is v1.1. Using exact commit for security.
      uses: rbialon/flake8-annotations@48819b39d57c621b5a64a1cdce40a5caa6a43b89

    # Stop the build if there are Python syntax errors or undefined names.
    - name: Lint with flake8
      run: |
        flake8 pyocd test --count --select=E9,F63,F7,F82 --show-source --statistics

    - name: Test with pytest
      run: |
        pytest --junitxml=test-results-${{ matrix.os }}-${{ matrix.python-version }}.xml --cov=pyocd

    - name: Test importing all modules
      run: |
        python test/import_all.py

    - name: Test build
      run: |
        python -mbuild

    - name: Check packages
      run: |
        twine check dist/*

    - name: Upload pytest test results
      uses: actions/upload-artifact@v4
      with:
        name: pytest-results-${{ matrix.python-version }}-${{ matrix.os }}
        path: |
          test-results-${{ matrix.os }}-${{ matrix.python-version }}.xml
          .coverage*
      # Use always() to always run this step to publish test results when there are test failures
      if: ${{ always() }}
