---
name: test
on: workflow_call

jobs:
  test:
    runs-on: ubuntu-latest
    permissions: {}
    timeout-minutes: 20
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
      - uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
        with:
          go-version-file: go.mod
      - uses: aquaproj/aqua-installer@5e54e5cee8a95ee2ce7c04cb993da6dfad13e59c # v3.1.2
        with:
          aqua_version: v2.51.2
        env:
          AQUA_GITHUB_TOKEN: ${{github.token}}
      - run: rm "$(aqua root-dir)/bin/pinact"
      - run: golangci-lint run --timeout 120s
        env:
          AQUA_GITHUB_TOKEN: ${{github.token}}
      - run: go test -v ./... -race -covermode=atomic
      - run: |
          go install \
            -ldflags "-X main.version=v1.0.0 -X main.commit=$GITHUB_SHA -X main.date=$(date +"%Y-%m-%dT%H:%M:%SZ%:z" | tr -d '+')" \
            ./cmd/pinact
      - run: pinact -v

      - run: |
          if pinact run --check testdata/foo.yaml; then
            echo "::error:: pinact run --check should fail"
            exit 1
          fi
        env:
          GITHUB_TOKEN: ${{github.token}}

      - run: |
          if ! git diff --exit-code testdata/foo.yaml; then
            echo "::error:: pinact run --check should not change files"
            exit 1
          fi

      - run: pinact run testdata/foo.yaml
        env:
          GITHUB_TOKEN: ${{github.token}}
      - run: diff testdata/foo.yaml testdata/foo.yaml.after
      - run: pinact run
        working-directory: testdata/actions
        env:
          GITHUB_TOKEN: ${{github.token}}
      - run: diff -r testdata/actions testdata/actions.after

      - run: pinact migrate
        working-directory: testdata/migrate
        env:
          GITHUB_TOKEN: ${{github.token}}
      - run: diff .pinact.yaml pinact.yaml.after
        working-directory: testdata/migrate

      - run: pinact run -u
        env:
          GITHUB_TOKEN: ${{github.token}}
