name: Build and Test
on: [push, pull_request]

env:
  # go needs absolute directories, using the $HOME variable doesn't work here.
  GOCACHE: /home/runner/work/go/pkg/build
  GOPATH: /home/runner/work/go
  GOBIN: /home/runner/work/go/bin
  GO_VERSION: 1.22.11

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Check out source
        uses: actions/checkout@v4

      - name: Build
        run: make build

  test-cover:
    name: Unit coverage
    runs-on: ubuntu-latest
    steps:
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Check out source
        uses: actions/checkout@v4

      - name: Test
        run: make unit-cover

      - name: Send top-level coverage
        uses: coverallsapp/github-action@v2
        continue-on-error: true
        with:
          file: coverage.txt
          flag-name: btcd
          format: 'golang'
          parallel: true

      - name: Send btcec
        uses: coverallsapp/github-action@v2
        continue-on-error: true
        with:
          file: btcec/coverage.txt
          flag-name: btcec
          format: 'golang'
          parallel: true

      - name: Send btcutil coverage
        uses: coverallsapp/github-action@v2
        continue-on-error: true
        with:
          file: btcutil/coverage.txt
          flag-name: btcutil
          format: 'golang'
          parallel: true

      - name: Send btcutil coverage for psbt package
        uses: coverallsapp/github-action@v2
        continue-on-error: true
        with:
          file: btcutil/psbt/coverage.txt
          flag-name: btcutilpsbt
          format: 'golang'
          parallel: true
      
      - name: Notify coveralls all reports sent
        uses: coverallsapp/github-action@v2
        continue-on-error: true
        with:
          parallel-finished: true

  test-race:
    name: Unit race
    runs-on: ubuntu-latest
    steps:
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}

      - name: Check out source
        uses: actions/checkout@v4

      - name: Test
        run: make unit-race
