name: test

on:
  push:
  pull_request:

jobs:
  test-matrix:
    strategy:
      matrix:
        operating-system: [ubuntu-20.04, windows-latest, macOS-latest]

    runs-on: ${{ matrix.operating-system }}

    steps:
      - name: Disable EOL conversions
        run: git config --global core.autocrlf false

      - name: Checkout
        uses: actions/checkout@v2

      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: "1.16"

      - name: Install Go deps
        # Since 10/23/2019 pwsh is the default shell
        # on Windows, but pwsh fails to install protoc-gen-go so
        # we force bash as default shell for all OSes in this task
        run: |
          go get github.com/golangci/govet
          go get golang.org/x/lint/golint
          go get github.com/golang/protobuf/protoc-gen-go
          go get github.com/cmaglie/go.rice/rice
        shell: bash

      - name: Install Taskfile
        uses: Arduino/actions/setup-taskfile@master
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          version: 3.x

      - name: Install Python
        uses: actions/setup-python@v1
        with:
          python-version: "3.8"
          architecture: "x64"

      - name: Install Poetry
        run: pip install poetry

      - name: Install buf (protoc linter)
        if: runner.os == 'Linux'
        run: |
          go install github.com/bufbuild/buf/cmd/buf@latest
          go install github.com/bufbuild/buf/cmd/protoc-gen-buf-breaking@latest
          go install github.com/bufbuild/buf/cmd/protoc-gen-buf-lint@latest
        shell: bash

      - name: Check the code is good
        if: runner.os == 'Linux'
        run: task check

      - name: Install protoc compiler
        if: runner.os == 'Linux'
        uses: arduino/setup-protoc@v1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Check protocol buffers compile correctly
        if: runner.os == 'Linux'
        run: task protoc:compile

      - name: Build the CLI
        run: task build

      - name: Run unit tests
        run: task test-unit

      - name: Run unit tests on the legacy package
        # Run legacy tests on one platform only
        if: matrix.operating-system == 'ubuntu-20.04'
        run: task test-legacy

      - name: Run integration tests
        run: task test-integration

      - name: Send unit tests coverage to Codecov
        if: >
          matrix.operating-system == 'ubuntu-20.04' &&
          github.event_name == 'push'
        uses: codecov/codecov-action@v1
        with:
          file: ./coverage_unit.txt
          flags: unit

      - name: Send legacy tests coverage to Codecov
        if: >
          matrix.operating-system == 'ubuntu-20.04' &&
          github.event_name == 'push'
        uses: codecov/codecov-action@v1
        with:
          file: ./coverage_legacy.txt
          flags: unit

      - name: Send integration tests coverage to Codecov
        if: >
          matrix.operating-system == 'ubuntu-20.04' &&
          github.event_name == 'push'
        uses: codecov/codecov-action@v1
        with:
          file: ./coverage_integ.txt
          flags: integ

  create-test-artifacts:
    runs-on: ubuntu-20.04
    needs: test-matrix

    steps:
      - name: checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 0

      - name: Install Taskfile
        uses: arduino/actions/setup-taskfile@master
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          version: 3.x

      - name: Build
        run: |
          PACKAGE_NAME_PREFIX="${{ github.workflow }}"
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            PACKAGE_NAME_PREFIX="$PACKAGE_NAME_PREFIX-${{ github.event.number }}"
          fi
          PACKAGE_NAME_PREFIX="$PACKAGE_NAME_PREFIX-${{ github.sha }}-"
          export PACKAGE_NAME_PREFIX
          task dist:all

      # Uploads all architectures as separate artifacts
      - name: Upload Linux 32 bit artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux_32bit
          path: dist/*Linux_32bit.tar.gz

      - name: Upload Linux 64 bit artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux_64bit
          path: dist/*Linux_64bit.tar.gz

      - name: Upload Windows 32 bit artifact
        uses: actions/upload-artifact@v2
        with:
          name: Windows_32bit
          path: dist/*Windows_32bit.zip

      - name: Upload Windows 64 bit artifact
        uses: actions/upload-artifact@v2
        with:
          name: Windows_64bit
          path: dist/*Windows_64bit.zip

      - name: Upload Linux ARMv6 artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux_ARMv6
          path: dist/*Linux_ARMv6.tar.gz

      - name: Upload Linux ARMv7 artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux_ARMv7
          path: dist/*Linux_ARMv7.tar.gz

      - name: Upload Linux ARM64 artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux_ARM64
          path: dist/*Linux_ARM64.tar.gz

      - name: Upload Linux ARM64 bit artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux_ARM64
          path: dist/*Linux_ARM64.tar.gz

      - name: Upload MacOS 64 bit artifact
        uses: actions/upload-artifact@v2
        with:
          name: macOS_64bit
          path: dist/*macOS_64bit.tar.gz

      - name: Upload checksums
        uses: actions/upload-artifact@v2
        with:
          name: checksums
          path: dist/*checksums.txt
