#!/bin/bash

set -e
source "${REPO_ROOT}/ci/scripts/functions-ci.sh"
START_DIR="${PWD}"  # Differs for CI and manual execution

if [ -n "$FOCUS" ]; then
  echo "------------------------------------------------------------------"
  echo "FOCUS is set. Will only run tests matching '$FOCUS'"
  echo "------------------------------------------------------------------"
  ADDITIONAL_ARGS=("--focus" "$FOCUS")
fi

git_pull

if [ -f ".git/resource/changed_files" ]; then
  if skip_ci "ci/.ci-ignore" ".git/resource/changed_files"; then
    echo "SKIP TEST: Only .ci-ignored changes found."
    exit 0
  else
    echo "RUN TEST: There is at least one non-ignored change found."
  fi
fi

prepare_bosh

cd "acceptance-tests"

echo "----- Installing dependencies"
go mod download
go install github.com/onsi/ginkgo/v2/ginkgo

echo "----- Running tests"

export PATH=$PATH:$GOPATH/bin
ginkgo version

PARALLELISM="-p"
if [ -n "$FOCUS" ]; then
  PARALLELISM="--nodes=1"
fi

ginkgo -v "$PARALLELISM" -r --trace --show-node-events --randomize-all --flake-attempts 5 "${ADDITIONAL_ARGS[@]}"

keep_running_info
