name: Test

on:
  push:
    branches: [ 'main' ]
    paths:
      - '**.go'
      - '**.mod'
      - '**.wasm'
      - '**.yml'
  pull_request:
    branches: [ 'main' ]
    paths:
      - '**.go'
      - '**.mod'
      - '**.wasm'
      - '**.yml'
  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: 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 != 'windows-latest'

    - name: Test dot locks
      run: go test -v -tags sqlite3_dotlk ./...
      if: matrix.os != 'windows-latest'

    - name: Test modules
      shell: bash
      run: |
        go work init .
        go work use -r embed gormlite
        go test -v ./embed/bcw2/...

    - name: Test GORM
      shell: bash
      run: gormlite/test.sh
      if: matrix.os != 'windows-latest'

    - name: Collect coverage
      run: |
        go get -tool github.com/dave/courtney@v0.4.4
        go tool 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-bsd:
    strategy:
      matrix:
        os:
          - name: freebsd
            version: '14.2'
            flags: '-test.v'
          - name: netbsd
            version: '10.1'
            flags: '-test.v'
          - name: freebsd
            arch: arm64
            version: '14.2'
            flags: '-test.v -test.short'
          - name: netbsd
            arch: arm64
            version: '10.1'
            flags: '-test.v -test.short'
          - name: openbsd
            version: '7.6'
            flags: '-test.v -test.short'
    runs-on: ubuntu-latest
    needs: test

    steps:
    - uses: actions/checkout@v4

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

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

  test-vm:
    strategy:
      matrix:
        os:
          - name: dragonfly
            action: 'vmactions/dragonflybsd-vm@v1'
            tflags: '-test.v'
          - name: illumos
            action: 'vmactions/omnios-vm@v1'
            tflags: '-test.v'
          - name: solaris
            action: 'vmactions/solaris-vm@v1'
            bflags: '-tags sqlite3_dotlk'
            tflags: '-test.v'
    runs-on: ubuntu-latest
    needs: test

    steps:
    - uses: actions/checkout@v4

    - name: Build
      env:
        GOOS: ${{ matrix.os.name }}
        BUILDFLAGS: ${{ matrix.os.bflags }}
        TESTFLAGS:  ${{ matrix.os.tflags }}
        VMACTIONS:  ${{ matrix.os.action }}
      run: .github/workflows/build-test.sh

    - name: Test
      uses: ./.github/actions/vmactions

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

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

    - name: Set path
      run: echo "$(go env GOROOT)/lib/wasm" >> "$GITHUB_PATH"

    - name: Test wasmtime
      env:
        GOOS: wasip1
        GOARCH: wasm
        GOWASIRUNTIME: wasmtime
        GOWASIRUNTIMEARGS: '--env CI=true'
      run: go test -v -short -tags sqlite3_dotlk -skip Example ./...

  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: Test 386 (32-bit)
      run: GOARCH=386 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)
      run: GOARCH=s390x go test -v -short -tags sqlite3_dotlk ./...

  test-linuxarm:
    runs-on: ubuntu-24.04-arm
    needs: test

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

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

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

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

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