#!/bin/bash

# Runs through a series of tests using multiple versions of gradle to
# ensure the plugin works in all of the expected versions.
#
# You can expect the tests to run for 20 to 30 seconds _per_ version.

set -eu

VERSIONS=(
  2.14.1
  2.13
  2.12
  2.11
  2.10
  2.9
  2.8
  2.7
  2.6
  2.5
  2.4
)

main(){
  echo "## Building plugin and running unit tests"
  assemble_plugin

  cd src/test/gradle
  for GRADLE_VERSION in ${VERSIONS[*]}; do
    echo
    echo "###################### $GRADLE_VERSION ######################"
    write_wrapper_props_file "$GRADLE_VERSION"
    integration_tests
  done

  echo "## All tests passed"
}

assemble_plugin(){
  ./gradlew clean check jar
}

integration_tests(){
  cp really-executable.base.sh really-executable.sh
  echo "## Building and verifying capsules"
  ./gradlew clean buildAll runScript -PtestScript=TestCapsule.groovy

  echo "## Modifying some files and rebuilding"
  cp really-executable.modified.sh really-executable.sh
  ./gradlew buildAll runScript -PtestScript=TestModifications.groovy
}

write_wrapper_props_file(){
  cat gradle/wrapper/gradle-wrapper.properties.base | \
    sed "s/gradle-[0-9.]*-bin.zip/gradle-$1-bin.zip/" \
    > gradle/wrapper/gradle-wrapper.properties
}

main