name: Test Bats Unix

on:
  pull_request:
    branches: [ main ]
    paths:
      - 'go/**'
      - 'integration-tests/**'

jobs:
  test:
    name: Bats tests
    defaults:
      run:
        shell: bash
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        os: [ ubuntu-18.04, macos-10.15 ]
        dolt_fmt: [ "", "__DOLT_DEV__" ]
        exclude:
          - os: "macos-10.15"
            dolt_fmt: "__DOLT_DEV__"
    env:
      use_credentials: ${{ secrets.AWS_SECRET_ACCESS_KEY != '' && secrets.AWS_ACCESS_KEY_ID != '' }}
    steps:
      - name: Conditionally Set ENV VARS for AWS tests
        run: |
          if [[ $use_credentials == true ]]; then
            echo "AWS_SDK_LOAD_CONFIG=1" >> $GITHUB_ENV
            echo "AWS_REGION=us-west-2" >> $GITHUB_ENV
            echo "DOLT_BATS_AWS_TABLE=dolt-ci-bats-manifests-us-west-2" >> $GITHUB_ENV
            echo "DOLT_BATS_AWS_BUCKET=dolt-ci-bats-chunks-us-west-2" >> $GITHUB_ENV
            echo "DOLT_BATS_AWS_EXISTING_REPO=aws_remote_bats_tests" >> $GITHUB_ENV
          fi
      - name: Configure AWS Credentials
        if: ${{ env.use_credentials == 'true' }}
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-west-2
          role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
          role-duration-seconds: 3600
      - name: Setup Go 1.x
        uses: actions/setup-go@v2
        with:
          go-version: ^1.18
        id: go
      - name: Setup Python 3.x
        uses: actions/setup-python@v2
        with:
          python-version: ^3.6
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: ^12
      - name: Create CI Bin
        run: |
          mkdir -p ./.ci_bin
          echo "$(pwd)/.ci_bin" >> $GITHUB_PATH
      - name: Install Bats
        run: |
          npm i bats
          echo "$(pwd)/node_modules/.bin" >> $GITHUB_PATH
        working-directory: ./.ci_bin
      - name: Install Python Deps
        run: |
          pip install mysql-connector-python
          pip install pandas
          pip install pyarrow
      - name: Install Dolt
        working-directory: ./go
        run: |
          go build -mod=readonly -o ../.ci_bin/dolt ./cmd/dolt/.
          go build -mod=readonly -o ../.ci_bin/git-dolt ./cmd/git-dolt/.
          go build -mod=readonly -o ../.ci_bin/git-dolt-smudge ./cmd/git-dolt-smudge/.
          go build -mod=readonly -o ../.ci_bin/remotesrv ./utils/remotesrv/.
          go build -mod=readonly -o ../.ci_bin/noms ./store/cmd/noms/.
      - name: Setup Dolt Config
        run: |
          dolt config --global --add user.name 'Dolthub Actions'
          dolt config --global --add user.email 'actions@dolthub.com'
      - name: Install expect
        if: matrix.os == 'ubuntu-18.04'
        run: sudo apt-get install -y expect
      - name: Install Parquet-tools
        run: brew install parquet-tools
      - name: Check expect
        run: expect -v
      - name: Test all Unix
        env:
          DOLT_FMT: ${{ matrix.dolt_fmt }}
        run: |
          if [ -n "$DOLT_FMT" ]; then export DOLT_DEFAULT_BIN_FORMAT="$DOLT_FMT"; fi
          bats --tap .
        working-directory: ./integration-tests/bats
