name: Test esptool

on: [push, pull_request]

jobs:
  test_esptool:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ['3.7', '3.8', '3.9', '3.10']

    steps:
    - name: Checkout ref commit
      uses: actions/checkout@master

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

    - name: SoftHSM2 setup
      run: |
        sudo apt-get update
        sudo apt-get install -y softhsm2
        sudo chmod -R a+rx /etc/softhsm
        sudo chmod a+r /etc/softhsm/softhsm2.conf
        sudo chown -R $(whoami) /var/lib/softhsm
        ./ci/setup_softhsm2.sh || exit 1

    - name: Test python components (fast)
      run: |
        python setup.py build
        pip install --extra-index-url https://dl.espressif.com/pypi -e .[dev,hsm]
        pytest test/test_imagegen.py
        pytest test/test_espsecure.py
        pytest test/test_espsecure_hsm.py
        pytest test/test_merge_bin.py
        pytest test/test_image_info.py
        pytest test/test_modules.py

    - name: Check the installed versions can run
      run: |
        esptool.py --help
        espefuse.py --help
        espsecure.py --help

    - name: Build stub (Python 3.7 only)
      if: matrix.python-version == 3.7
      run: |
        export TOOLCHAIN_DIR=$HOME/toolchain

        export ESP8266_BINDIR=$TOOLCHAIN_DIR/xtensa-lx106-elf/bin
        export ESP32_BINDIR=$TOOLCHAIN_DIR/xtensa-esp32-elf/bin
        export ESP32S2_BINDIR=$TOOLCHAIN_DIR/xtensa-esp32s2-elf/bin
        export ESP32S3_BINDIR=$TOOLCHAIN_DIR/xtensa-esp32s3-elf/bin
        export ESP32C3_BINDIR=$TOOLCHAIN_DIR/riscv32-esp-elf/bin

        export PATH=$PATH:$ESP8266_BINDIR:$ESP32_BINDIR:$ESP32S2_BINDIR:$ESP32S3_BINDIR:$ESP32C3_BINDIR

        ./ci/setup_ci_build_env.sh
        make -C flasher_stub V=1
        cd flasher_stub && python ./compare_stubs.py

  lint_esptool:
      runs-on: ubuntu-latest
      steps:
        - name: Checkout
          uses: actions/checkout@master

        - name: Lint with flake8
          run: |
            pip install --extra-index-url https://dl.espressif.com/pypi -e .[dev]
            python -m flake8

        - name: Check formatting with Black
          uses: psf/black@stable
