name: Run Tests

on:
  push:
    branches:
      - main
    tags:
      - v2.*
  pull_request:
    branches:
      - main

jobs:
  test:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        go: [1.17.x, 1.18.x, 1.19.x]
    name: ${{ matrix.os }} @ Go ${{ matrix.go }}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Set up Go ${{ matrix.go }}
        uses: actions/setup-go@v3
        with:
          go-version: ${{ matrix.go }}

      - name: Set PATH
        run: echo "${GITHUB_WORKSPACE}/.local/bin" >>"${GITHUB_PATH}"

      - name: install goimports
        if: matrix.go == '1.19.x' && matrix.os == 'ubuntu-latest'
        run: GOBIN=${PWD}/.local/bin go install golang.org/x/tools/cmd/goimports@latest

      - name: Checkout Code
        uses: actions/checkout@v3

      - name: goimports check
        if: matrix.go == '1.19.x' && matrix.os == 'ubuntu-latest'
        run: test -z $(goimports -l .)

      - name: vet
        run: go run internal/build/build.go vet

      - name: test with urfave_cli_no_docs tag
        run: go run internal/build/build.go -tags urfave_cli_no_docs test

      - name: test
        run: go run internal/build/build.go test

      - name: test urfave-cli-genflags
        run: make -C cmd/urfave-cli-genflags

      - name: check-binary-size
        run: go run internal/build/build.go check-binary-size

      - name: check-binary-size with tags (informational only)
        run: go run internal/build/build.go -tags urfave_cli_no_docs check-binary-size

      - name: Upload coverage to Codecov
        if: success() && matrix.go == '1.19.x' && matrix.os == 'ubuntu-latest'
        uses: codecov/codecov-action@v2
        with:
          fail_ci_if_error: true

  test-docs:
    name: test-docs
    runs-on: ubuntu-latest
    steps:
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: 1.19.x

      - name: Use Node.js 16
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Set PATH
        run: echo "${GITHUB_WORKSPACE}/.local/bin" >>"${GITHUB_PATH}"

      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Install Dependencies
        run: |
          mkdir -p "${GITHUB_WORKSPACE}/.local/bin"
          curl -fsSL -o "${GITHUB_WORKSPACE}/.local/bin/gfmrun" "https://github.com/urfave/gfmrun/releases/download/v1.3.0/gfmrun-$(go env GOOS)-$(go env GOARCH)-v1.3.0"
          chmod +x "${GITHUB_WORKSPACE}/.local/bin/gfmrun"

      - name: gfmrun
        run: go run internal/build/build.go gfmrun --walk docs/v2/

      - name: diff check
        run: |
          git diff --exit-code
          git diff --cached --exit-code

  publish:
    if: startswith(github.ref, 'refs/tags/')
    name: publish
    needs: [test-docs]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup mkdocs
        run: |
          pip install -U pip
          pip install -r mkdocs-requirements.txt
          git remote rm origin
          git remote add origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/urfave/cli.git

      - name: Publish Docs
        run: |
          mkdocs gh-deploy --force