# Create a temporary directory for this test case.
# Copy the specified package and target runtime into the temporary directory.
# Echo the path that the test case should use as its cwd.
#
# Arguments:
#   1: Workspace-relative path to the target that is going to be tested
function make_test_case_environment() {
  local target package working_tree
  target="$1"
  package="$(dirname "$target")"
  working_tree="$(mktemp --directory -p "$TEST_TMPDIR")"
  readonly target package working_tree

  mkdir --parents \
    "$(dirname "$working_tree/$target.runfiles/zoox/$package")"
  cp --recursive --dereference \
    "$package" \
    "$working_tree/$target.runfiles/zoox/$package"
  chmod --recursive +w "$working_tree"

  echo "$working_tree/$target.runfiles/zoox"
}

# Create a script that echos exactly what it was called as.
# This includes the name of the command used to invoke the script and any
# arguments passed.
#
# Arguments:
#   1: File path of destination file.
function make_echo_script() {
  local location
  location="$1"
  readonly location

  cat >"$location" <<EOF
#!/usr/bin/env bash
echo \${0#"\$(pwd)/"} \$@
EOF
  chmod +x "$location"
}

# Assert that the specified command exits with the specified error code and
# produces the output on stderr that matches the specified regex.
#
# Arguments:
#   1: Return code.
#   2: Stderr output regex.
#   3+: Command to run.
function assert_failure() {
  local expected_return_code expected_output
  expected_return_code="$1"
  expected_output="$2"
  readonly expected_return_code expected_output
  shift 2

  (
    set +e
    actual_output="$("$@" 2>&1)"
    actual_return_code=$?
    set -e
    [ $actual_return_code -eq $expected_return_code ]
    [[ "$actual_output" =~ $expected_output ]]
  )
}
