#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"

# Use the Bazel binary specified by the integration test. Otherise, fall back 
# to bazel.
bazel="${BIT_BAZEL_BINARY:-bazel}"

assert_match() {
  local pattern=${1}
  local actual="${2}"
  local err_msg="Expected to match. pattern: ${pattern}, actual: ${actual}"
  [[ "${actual}" =~ ${pattern} ]] || (echo >&2 "${err_msg}" && exit 1)
}

# Generate Swift external deps and update build files
"${bazel}" run //:tidy

# Ensure that it builds and tests pass
"${bazel}" test //...

# Run MyExecutable target
output="$( "${bazel}" run //Sources/MyExecutable 2>&1 )"
assert_match "Hello, World!" "${output}"
