#!/usr/bin/env bash
usage() { echo "Usage: $0 [-c <bazelrc config>] [-f use file output]" 1>&2; exit 0; }
[ $# -eq 0 ] && usage
while getopts ":f:c:h" opt; do
    case $opt in 
        f) targets_file="$OPTARG" ;;
        c) config="--config=$OPTARG" ;;
        h | *) usage ;;
    esac
done

# set -eu

# get the targets line by line
tests=()
while IFS='' read -r line; do
  if [[ -z "$line" ]]; then 
    continue
  fi

  tests+=("$line");
done <"$targets_file"

if [[ -z "${tests[*]}" ]]; then
    echo "No L1 tests to run"
    exit 0;
fi

echo "Testing targets:"
echo "${tests[@]}"


# run tests that arent slow, disruptive, serial, flaky, or privileged
test_exit=0
bazel run //test/rosa int "${tests[@]}" "$config" -- --test_arg=--skip-labels=slow=true,disruptive=true,serial=true,flaky=true,privileged=true || test_exit=$?

# Ok to exit 0 if bazel test exit is 4 (test requsted but none to run)
if [[ $test_exit == 4 ]]; then
    echo "No tests to run"
    exit 0;
fi

exit $test_exit