name: Go

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:

  linters:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.21
    - name: Install golangci-lint
      run: |
        cd ..
        curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.50.0
    - name: Check vendored dependencies
      run: |
        go mod tidy
        go mod verify
        go mod vendor -v
        git status
        if [[ -n "$(git status --porcelain vendor)" ]]; then
          echo 'vendor/ is out-of-date: run `go mod tidy && go mod vendor` and then check in the changes'
          echo 'If `go mod tidy && go mod vendor` results in no changes, make sure you are using the latest relase of Go'
          git status --porcelain vendor
          exit 1
        fi
    - name: gofmt
      run: test -z "$(gofmt -s -l $(find -name '*.go' | grep -v /vendor/))"
    - name: golangci-lint
      run: |
        golangci-lint run ./... --disable-all --enable=revive \
          --skip-dirs=pkg/uroot/test \
          --skip-dirs=cmds/core/elvish \
          --exclude="exported (function|type|const|method|var) (\w|_|\.)+ should have comment" \
            --exclude="don't use underscores" \
          --exclude="don't use ALL_CAPS" \
          --exclude="comment on exported (function|type|const|method|var) (\w|_|\.)+ should be of the form" \
          --exclude="package comment should be of the form" \
          --exclude="(function|type|const|method|var|type field|struct field) (\w|_|\.)+ should be (\w|_|\.)+" \
          --exclude="stutter" \
          --exclude="which can be annoying to use"
    - name: ineffassign
      run: golangci-lint run ./... --disable-all --enable=ineffassign

  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.21

    - name: Build
      run: go build -mod=mod -v ./...

    - name: Vet Test
      run: go vet -composites=false -mod=mod ./...

    - name: Test
      run: "CGO_ENABLED=0 go env && CGO_ENABLED=0 go test -mod=mod  ./pkg/uroot/..."

  badbuild:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.21

    - name: Build fail
      id: buildfail
      continue-on-error: true
      run: |
        echo bad >> cmds/core/date/date.go
        go build .
        ./u-root

    - name: Fail if Build Failure did not fail
      run: |
        echo buildfail got ${{ steps.buildfail.outcome }} and should get failure
        bash -c "if [[ ${{ steps.buildfail.outcome }} == failure ]]; then exit 0; else exit 1; fi"
