#!/bin/bash
# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

# Script to run a "Ravenwood" host side test.
#
# A proper way to run these tests is to use `atest`, but `atest` has a known issue of loading
# unrelated jar files as the class path, so for now we use this script to run host side tests.

# Copy (with some changes) some functions from ../common.sh, so this script can be used without it.

m() {
  if (( $SKIP_BUILD )) ; then
    echo "Skipping build: $*" 1>&2
    return 0
  fi
  run ${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode "$@"
}

run() {
  echo "Running: $*" 1>&2
  "$@"
}

run_junit_test_jar() {
  local jar="$1"
  echo "Starting test: $jar ..."
  run cd "${jar%/*}"

  run ${JAVA:-java} $JAVA_OPTS \
      -cp $jar \
      org.junit.runner.JUnitCore \
      com.android.hoststubgen.hosthelper.HostTestSuite || return 1
  return 0
}

help() {
  cat <<'EOF'

  run-ravenwood-test -- Run Ravenwood host tests

  Usage:
    run-ravenwood-test [options] MODULE-NAME ...

  Options:
    -h: Help
    -t: Run test only, without building
    -b: Build only, without running

  Example:
    run-ravenwood-test HostStubGenTest-framework-test-host-test

EOF
}

#-------------------------------------------------------------------------
# Parse options
#-------------------------------------------------------------------------
build=0
test=0

while getopts "htb" opt; do
  case "$opt" in
    h) help; exit 0 ;;
    t)
      test=1
      ;;
    b)
      build=1
      ;;
  esac
done
shift $(($OPTIND - 1))

# If neither -t nor -b is provided, then build and run./
if (( ( $build + $test ) == 0 )) ; then
  build=1
  test=1
fi


modules=("${@}")

if (( "${#modules[@]}" == 0 )); then
  help
  exit 1
fi

#-------------------------------------------------------------------------
# Build
#-------------------------------------------------------------------------
if (( $build )) ; then
  run m "${modules[@]}"
fi

#-------------------------------------------------------------------------
# Run
#-------------------------------------------------------------------------

failures=0
if (( test )) ; then
  for module in "${modules[@]}"; do
    if run_junit_test_jar "$ANDROID_BUILD_TOP/out/host/linux-x86/testcases/${module}/${module}.jar"; then
      : # passed.
    else
      failures=$(( failures + 1 ))
    fi
  done

  if (( $failures > 0 )) ; then
    echo "$failures test jar(s) failed." 1>&2
    exit 2
  fi
fi

exit 0