name: Go
on:
  push:
    branches: [ master ]
  pull_request: {}
permissions:
  contents: read

jobs:
  build:
    name: Go Build
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os:
          - macos-latest
          - ubuntu-latest
          - windows-latest

    steps:
    - name: Set up Go
      uses: actions/setup-go@v5.0.2
      with:
        go-version: '1.20'
      id: go

    - name: Set up Python
      if: runner.os != 'Windows'
      uses: actions/setup-python@v5.2.0
      with:
        python-version: "3.9"
      id: python

    - name: Check out code into the Go module directory
      uses: actions/checkout@v4

    - name: Build
      env:
        GO111MODULE: on
      run: go build -v .

    - name: Test
      env:
        GO111MODULE: on
      run: go test -v ./...

    - name: Test Host bash
      # FIXME: make this work on Windows as well
      if: runner.os != 'Windows'
      env:
        GO111MODULE: on
      run: make test-stdlib test-bash

    - name: GitHub Actions Env Test Setup
      # FIXME: make this work on Windows as well
      if: runner.os != 'Windows'
      run: |
        cd test/scenarios/github-actions/
        ../../../direnv allow
        ../../../direnv export gha >> "$GITHUB_ENV"

    - name: GitHub Actions Env Test Verification
      # FIXME: make this work on Windows as well
      if: runner.os != 'Windows'
      run: |
        [[ -z ${TEST_EXPORT_DIRENV_GITHUB_ACTIONS:-} ]] && echo "TEST_EXPORT_DIRENV_GITHUB_ACTIONS is unset or empty" >&2 && exit 1
        tee TEST_EXPORT_DIRENV_GITHUB_ACTIONS.got <<<"$TEST_EXPORT_DIRENV_GITHUB_ACTIONS"
        echo "${GITHUB_SHA}"$'\n'"${GITHUB_RUN_ID}"$'\n'"${GITHUB_RUN_NUMBER}" | tee TEST_EXPORT_DIRENV_GITHUB_ACTIONS.want
        diff -u TEST_EXPORT_DIRENV_GITHUB_ACTIONS.want TEST_EXPORT_DIRENV_GITHUB_ACTIONS.got
