name: Tests / Code Coverage
#  Tests / Code Coverage workflow runs unit tests and uploads a code coverage report
#  This workflow is run on pushes to master & every Pull Requests where a .go, .mod, .sum have been changed
on:
  pull_request:
  push:
    branches:
      - master
jobs:
  cleanup-runs:
    runs-on: ubuntu-latest
    steps:
      - uses: rokroskar/workflow-run-cleanup-action@master
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
    if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"

  install-tparse:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - name: install tparse
        run: |
          export GO111MODULE="on" && go get github.com/mfridman/tparse@v0.8.3
      - uses: actions/cache@v2.1.2
        with:
          path: ~/go/bin
          key: ${{ runner.os }}-go-tparse-binary

  test-cosmovisor:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PREFIX_FILTER: |
            cosmovisor
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - name: Run cosmovisor tests
        run: cd cosmovisor; make
        if: env.GIT_DIFF

  split-test-files:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - name: Create a file with all the pkgs
        run: go list ./... > pkgs.txt
      - name: Split pkgs into 4 files
        run: split -n l/4 --additional-suffix=.txt ./pkgs.txt
      # cache multiple
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-aa"
          path: ./xaa.txt
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-ab"
          path: ./xab.txt
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-ac"
          path: ./xac.txt
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-ad"
          path: ./xad.txt

  test-coverage-run-1:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-aa"
        if: env.GIT_DIFF
      - name: test & coverage report creation
        run: |
          cat xaa.txt | xargs go test -mod=readonly -timeout 15m -coverprofile=coverage.txt -covermode=atomic -tags='norace ledger test_ledger_mock'
        if: env.GIT_DIFF
      - name: filter out DONTCOVER
        run: |
          excludelist="$(find ./ -type f -name '*.go' | xargs grep -l 'DONTCOVER')"
          excludelist+=" $(find ./ -type f -name '*.pb.go')"
          excludelist+=" $(find ./ -type f -path './tests/mocks/*.go')"
          for filename in ${excludelist}; do
            filename=$(echo $filename | sed 's/^./github.com\/cosmos\/cosmos-sdk/g')
            echo "Excluding ${filename} from coverage report..."
            sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt
          done
        if: env.GIT_DIFF
      - uses: codecov/codecov-action@v1.0.14
        with:
          file: ./coverage.txt
        if: env.GIT_DIFF

  test-coverage-run-2:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ab"
        if: env.GIT_DIFF
      - name: test & coverage report creation
        run: |
          cat xab.txt | xargs go test -mod=readonly -timeout 15m -coverprofile=coverage.txt -covermode=atomic -tags='norace ledger test_ledger_mock'
        if: env.GIT_DIFF
      - name: filter out DONTCOVER
        run: |
          excludelist="$(find ./ -type f -name '*.go' | xargs grep -l 'DONTCOVER')"
          excludelist+=" $(find ./ -type f -name '*.pb.go')"
          excludelist+=" $(find ./ -type f -path './tests/mocks/*.go')"
          for filename in ${excludelist}; do
            filename=$(echo $filename | sed 's/^./github.com\/cosmos\/cosmos-sdk/g')
            echo "Excluding ${filename} from coverage report..."
            sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt
          done
        if: env.GIT_DIFF
      - uses: codecov/codecov-action@v1.0.14
        with:
          file: ./coverage.txt
        if: env.GIT_DIFF

  test-coverage-run-3:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ac"
        if: env.GIT_DIFF
      - name: test & coverage report creation
        run: |
          cat xac.txt | xargs go test -mod=readonly -timeout 15m -coverprofile=coverage.txt -covermode=atomic -tags='norace ledger test_ledger_mock'
        if: env.GIT_DIFF
      - name: filter out DONTCOVER
        run: |
          excludelist="$(find ./ -type f -name '*.go' | xargs grep -l 'DONTCOVER')"
          excludelist+=" $(find ./ -type f -name '*.pb.go')"
          excludelist+=" $(find ./ -type f -path './tests/mocks/*.go')"
          for filename in ${excludelist}; do
            filename=$(echo $filename | sed 's/^./github.com\/cosmos\/cosmos-sdk/g')
            echo "Excluding ${filename} from coverage report..."
            sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt
          done
        if: env.GIT_DIFF
      - uses: codecov/codecov-action@v1.0.14
        with:
          file: ./coverage.txt
        if: env.GIT_DIFF

  test-coverage-run-4:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ad"
        if: env.GIT_DIFF
      - name: test & coverage report creation
        run: |
          cat xad.txt | xargs go test -mod=readonly -timeout 15m -coverprofile=coverage.txt -covermode=atomic -tags='norace ledger test_ledger_mock'
        if: env.GIT_DIFF
      - name: filter out DONTCOVER
        run: |
          excludelist="$(find ./ -type f -name '*.go' | xargs grep -l 'DONTCOVER')"
          excludelist+=" $(find ./ -type f -name '*.pb.go')"
          excludelist+=" $(find ./ -type f -path './tests/mocks/*.go')"
          for filename in ${excludelist}; do
            filename=$(echo $filename | sed 's/^./github.com\/cosmos\/cosmos-sdk/g')
            echo "Excluding ${filename} from coverage report..."
            sed -i.bak "/$(echo $filename | sed 's/\//\\\//g')/d" coverage.txt
          done
        if: env.GIT_DIFF
      - uses: codecov/codecov-action@v1.0.14
        with:
          file: ./coverage.txt
        if: env.GIT_DIFF

  test-race-1:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-aa"
        if: env.GIT_DIFF
      - name: Run tests with race detector
        run: cat xaa.txt | xargs go test -mod=readonly -json -timeout 30m -race -tags='cgo ledger test_ledger_mock' > xaa-race-output.txt
        if: env.GIT_DIFF
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-aa-race-output"
          path: ./xaa-race-output.txt

  test-race-2:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ab"
        if: env.GIT_DIFF
      - name: Run tests with race detector
        run: cat xab.txt | xargs go test -mod=readonly -json -timeout 30m -race -tags='cgo ledger test_ledger_mock' > xab-race-output.txt
        if: env.GIT_DIFF
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-ab-race-output"
          path: ./xab-race-output.txt

  test-race-3:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ac"
        if: env.GIT_DIFF
      - name: Run tests with race detector
        run: cat xac.txt | xargs go test -mod=readonly -json -timeout 30m -race -tags='cgo ledger test_ledger_mock' > xac-race-output.txt
        if: env.GIT_DIFF
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-ac-race-output"
          path: ./xac-race-output.txt

  test-race-4:
    runs-on: ubuntu-latest
    needs: split-test-files
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-go@v2.1.3
        with:
          go-version: 1.15
      - name: Display go version
        run: go version
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ad"
        if: env.GIT_DIFF
      - name: Run tests with race detector
        run: cat xad.txt | xargs go test -mod=readonly -json -timeout 30m -race -tags='cgo ledger test_ledger_mock' > xad-race-output.txt
        if: env.GIT_DIFF
      - uses: actions/upload-artifact@v2
        with:
          name: "${{ github.sha }}-ad-race-output"
          path: ./xad-race-output.txt

  race-detector-report:
    runs-on: ubuntu-latest
    needs: [test-race-1, test-race-2, test-race-3, test-race-4, install-tparse]
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-aa-race-output"
        if: env.GIT_DIFF
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ab-race-output"
        if: env.GIT_DIFF
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ac-race-output"
        if: env.GIT_DIFF
      - uses: actions/download-artifact@v2
        with:
          name: "${{ github.sha }}-ad-race-output"
        if: env.GIT_DIFF
      - uses: actions/cache@v2.1.2
        with:
          path: ~/go/bin
          key: ${{ runner.os }}-go-tparse-binary
        if: env.GIT_DIFF
      - name: Generate test report (go test -race)
        run: cat xa*-race-output.txt | ~/go/bin/tparse
        if: env.GIT_DIFF

  liveness-test:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v4
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - name: start localnet
        run: |
          make clean build-simd-linux localnet-start
        if: env.GIT_DIFF
      - name: test liveness
        run: |
          ./contrib/localnet_liveness.sh 100 5 50 localhost
        if: env.GIT_DIFF

  docker-build:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2
      - name: build docker image
        run: |
          docker build --pull --rm -f "Dockerfile" -t simapp:latest "."
