name: Test

on:
  push:
    branches: [ "main" ]
    paths:
      - '**.go'
      - '**.wasm'
      - '**.wasm.bz2'
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

jobs:
  test:
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-go@v5
      with: { go-version: stable }

    - name: Git LFS pull
      uses: ./.github/actions/lfs

    - name: Format
      run: gofmt -s -w . && git diff --exit-code
      if: matrix.os != 'windows-latest'

    - name: Tidy
      run: go mod tidy && git diff --exit-code

    - name: Download
      run: go mod download

    - name: Verify
      run: go mod verify

    - name: Vet
      run: go vet ./...

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

    - name: Test
      run: go test -v ./... -bench . -benchtime=1x

    - name: Test BSD locks
      run: go test -v -tags sqlite3_flock ./...
      if: matrix.os == 'macos-latest'

    - name: Test no shared memory
      run: go test -v -tags sqlite3_noshm ./...
      if: matrix.os == 'ubuntu-latest'

    - name: Test no locks
      run: go test -v -tags sqlite3_nosys ./...
      if: matrix.os == 'ubuntu-latest'

    - name: Test GORM
      shell: bash
      run: gormlite/test.sh

    - name: Collect coverage
      run: |
        go install github.com/dave/courtney@latest
        courtney
      if: |
        github.event_name == 'push' &&
        matrix.os == 'ubuntu-latest'

    - uses: ncruces/go-coverage-report@v0
      with:
        coverage-file: coverage.out
        chart: true
        amend: true
      if: |
        github.event_name == 'push' &&
        matrix.os == 'ubuntu-latest'

  test-intel:
    runs-on: macos-13
    needs: test

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-go@v5
      with: { go-version: stable }

    - name: Git LFS pull
      uses: ./.github/actions/lfs

    - name: Test
      run: go test -v ./...

  test-bsd:
    strategy:
      matrix:
        os:
          - name: freebsd
            version: '14.1'
            flags: '-test.v'
          - name: openbsd
            version: '7.5'
            flags: '-test.v -test.short'
          - name: netbsd
            version: '10.0'
            flags: '-test.v -test.short'
    runs-on: ubuntu-latest
    needs: test

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-go@v5
      with: { go-version: stable }

    - name: Git LFS pull
      uses: ./.github/actions/lfs

    - name: Build
      env:
        GOOS: ${{ matrix.os.name }}
        TESTFLAGS: ${{ matrix.os.flags }}
      run: .github/workflows/build-test.sh

    - name: Test
      uses: cross-platform-actions/action@v0.25.0
      with:
        operating_system: ${{ matrix.os.name }}
        version: ${{ matrix.os.version }}
        shell: bash
        run: . ./test.sh
        sync_files: runner-to-vm

  test-qemu:
    runs-on: ubuntu-latest
    needs: test

    steps:
    - uses: docker/setup-qemu-action@v3
    - uses: actions/checkout@v4
    - uses: actions/setup-go@v5
      with: { go-version: stable }

    - name: Git LFS pull
      uses: ./.github/actions/lfs

    - name: Test 386 (32-bit)
      run: GOARCH=386 go test -v -short ./...

    - name: Test arm64 (compiler)
      run: GOARCH=arm64 go test -v -short ./...

    - name: Test riscv64 (interpreter)
      run: GOARCH=riscv64 go test -v -short ./...

    - name: Test ppc64le (interpreter)
      run: GOARCH=ppc64le go test -v -short ./...

    - name: Test s390x (big-endian, z/OS demo)
      run: GOARCH=s390x go test -v -short -tags sqlite3_flock ./...

  test-vm:
    runs-on: ubuntu-latest
    needs: test

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-go@v5
      with: { go-version: stable }

    - name: Git LFS pull
      uses: ./.github/actions/lfs

    - name: Build illumos
      env:
        GOOS: illumos
        TESTFLAGS: '-test.v -test.short'
      run: .github/workflows/build-test.sh

    - name: Test illumos
      uses: vmactions/omnios-vm@v1
      with:
        usesh: true
        copyback: false
        run: . ./test.sh

    - name: Build Solaris
      env:
        GOOS: solaris
        TESTFLAGS: '-test.v -test.short'
      run: .github/workflows/build-test.sh

    - name: Test Solaris
      uses: vmactions/solaris-vm@v1
      with:
        usesh: true
        copyback: false
        run: . ./test.sh
      continue-on-error: true