lots of exercises in java... from https://github.com/exercism/java

journey-test.sh 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/usr/bin/env bash
  2. # This script is shared between the Java and Kotlin tracks. If you make an update to this script on
  3. # one track, please create a companion PR to merge it in to the other. Thank you!
  4. TRACK=java
  5. TRACK_REPO="$TRACK"
  6. TRACK_SRC_EXT="java"
  7. on_exit() {
  8. echo ">>> on_exit()"
  9. if [[ "$xapi_pid" != "" ]] ; then
  10. kill $xapi_pid
  11. echo "stopped x-api (pid=${xapi_pid})"
  12. fi
  13. cd $EXECPATH
  14. echo "<<< on_exit()"
  15. }
  16. assert_installed() {
  17. local binary=$1
  18. echo ">>> assert_installed(binary=\"${binary}\")"
  19. if [[ "`which $binary`" == "" ]]; then
  20. echo "${binary} not found; it is required to perform this test."
  21. echo -e "Have you completed the setup instructions at https://github.com/exercism/${TRACK_REPO} ?\n"
  22. echo "PATH=${PATH}"
  23. echo "aborting."
  24. exit 1
  25. fi
  26. echo "<<< assert_installed()"
  27. }
  28. assert_ruby_installed() {
  29. local ruby_app_home="$1"
  30. echo ">>> assert_ruby_installed(ruby_app_home=\"${ruby_app_home}\")"
  31. pushd "${ruby_app_home}"
  32. local current_ruby_ver=`ruby --version | egrep --only-matching "[0-9]+\.[0-9]+\.[0-9]+"`
  33. local expected_ruby_ver=`cat Gemfile | awk -F\' '/ruby /{print $2}'`
  34. popd
  35. if [[ "${expected_ruby_ver}" != "" && "${current_ruby_ver}" != "${expected_ruby_ver}" ]]; then
  36. echo "${ruby_app_home} requires Ruby ${expected_ruby_ver}; current Ruby version is ${current_ruby_ver}."
  37. echo -e "Ruby used: `which ruby`.\n"
  38. echo -e "Have you completed the setup instructions at https://github.com/exercism/${TRACK_REPO} ?\n"
  39. echo "PATH=${PATH}"
  40. echo "aborting."
  41. exit 1
  42. fi
  43. echo "<<< assert_ruby_installed()"
  44. }
  45. clean() {
  46. local build_dir="$1"
  47. echo ">>> clean(build_dir=\"${build_dir}\")"
  48. # empty, absolute path, or parent reference are considered dangerous to rm -rf against.
  49. if [[ "${build_dir}" == "" || ${build_dir} =~ ^/ || ${build_dir} =~ \.\. ]] ; then
  50. echo "Value for build_dir looks dangerous. Aborting."
  51. exit 1
  52. fi
  53. local build_path=$( pwd )/${build_dir}
  54. if [[ -d "${build_path}" ]] ; then
  55. echo "Cleaning journey script build output directory (${build_path})."
  56. rm -rf "${build_path}"
  57. fi
  58. cd exercises
  59. "$EXECPATH"/gradlew clean
  60. cd ..
  61. echo "<<< clean()"
  62. }
  63. get_operating_system() {
  64. case $(uname) in
  65. (Darwin*)
  66. echo "mac";;
  67. (Linux*)
  68. echo "linux";;
  69. (Windows*)
  70. echo "windows";;
  71. (MINGW*)
  72. echo "windows";;
  73. (*)
  74. echo "linux";;
  75. esac
  76. }
  77. get_cpu_architecture() {
  78. case $(uname -m) in
  79. (*64*)
  80. echo 64bit;;
  81. (*686*)
  82. echo 32bit;;
  83. (*386*)
  84. echo 32bit;;
  85. (*)
  86. echo 64bit;;
  87. esac
  88. }
  89. download_exercism_cli() {
  90. local os="$1"
  91. local arch="$2"
  92. local exercism_home="$3"
  93. echo ">>> download_exercism_cli(os=\"${os}\", arch=\"${arch}\", exercism_home=\"${exercism_home}\")"
  94. local CLI_RELEASES=https://github.com/exercism/cli/releases
  95. local latest=${CLI_RELEASES}/latest
  96. # "curl..." :: HTTP 302 headers, including "Location" -- URL to redirect to.
  97. # "awk..." :: pluck last path segment from "Location" (i.e. the version number)
  98. local version="$(curl --head --silent ${latest} | awk -v FS=/ '/Location:/{print $NF}' | tr -d '\r')"
  99. local download_url_suffix
  100. local unzip_command
  101. local unzip_from_file_option
  102. if [[ ${os} == "windows" ]] ; then
  103. download_url_suffix="zip"
  104. unzip_command="unzip -d"
  105. unzip_from_file_option=""
  106. else
  107. download_url_suffix="tgz"
  108. unzip_command="tar xz -C"
  109. unzip_from_file_option="-f"
  110. fi
  111. local download_url=${CLI_RELEASES}/download/${version}/exercism-${os}-${arch}.${download_url_suffix}
  112. mkdir -p ${exercism_home}
  113. local temp=`mktemp`
  114. curl -s --location ${download_url} > ${temp}
  115. ${unzip_command} ${exercism_home} ${unzip_from_file_option} ${temp}
  116. echo "<<< download_exercism_cli()"
  117. }
  118. git_clone() {
  119. local repo_name="$1"
  120. local dest_path="$2"
  121. echo ">>> git_clone(repo_name=\"${repo_name}\", dest_path=\"${dest_path}\")"
  122. git clone https://github.com/exercism/${repo_name} ${dest_path}
  123. echo "<<< git_clone()"
  124. }
  125. make_local_trackler() {
  126. local trackler="$1"
  127. local xapi_home="$2"
  128. echo ">>> make_local_trackler(trackler=\"${trackler}\", xapi_home=\"${xapi_home}\")"
  129. local track_root=$( pwd )
  130. pushd ${trackler}
  131. # Get the version of Trackler x-api is currently using
  132. local version=$( grep -m 1 'trackler' ${xapi_home}/Gemfile.lock | sed 's/.*(//' | sed 's/)//' )
  133. git checkout v${version}
  134. git submodule init -- problem-specifications
  135. git submodule update
  136. # Bake in local copy of this track; this is what we are testing.
  137. rmdir tracks/${TRACK}
  138. mkdir -p tracks/${TRACK}/exercises
  139. cp ${track_root}/config.json tracks/${TRACK}
  140. cp -r ${track_root}/exercises tracks/${TRACK}
  141. gem install bundler
  142. bundle install -j4
  143. gem build trackler.gemspec
  144. # Make *this* the gem that x-api uses when we build x-api.
  145. gem install --local trackler-*.gem
  146. popd > /dev/null
  147. echo "<<< make_local_trackler()"
  148. }
  149. start_x_api() {
  150. local xapi_home="$1"
  151. echo ">>> start_x_api(xapi_home=\"${xapi_home}\")"
  152. pushd $xapi_home
  153. gem install bundler
  154. bundle install -j4
  155. RACK_ENV=development rackup &
  156. xapi_pid=$!
  157. sleep 5
  158. echo "x-api is running (pid=${xapi_pid})."
  159. popd > /dev/null
  160. echo "<<< start_x_api()"
  161. }
  162. configure_exercism_cli() {
  163. local exercism_home="$1"
  164. local exercism_configfile="$2"
  165. local xapi_port=$3
  166. echo ">>> configure_exercism_cli(exercism_home=\"${exercism_home}\", exercism_configfile=\"${exercism_configfile}\", xapi_port=${xapi_port})"
  167. local exercism="./exercism --config ${exercism_configfile}"
  168. mkdir -p "${exercism_home}"
  169. pushd "${exercism_home}"
  170. $exercism configure --dir="${exercism_home}"
  171. $exercism configure --api http://localhost:${xapi_port}
  172. $exercism debug
  173. popd
  174. echo "<<< configure_exercism_cli()"
  175. }
  176. solve_all_exercises() {
  177. local exercism_exercises_dir="$1"
  178. local exercism_configfile="$2"
  179. echo ">>> solve_all_exercises(exercism_exercises_dir=\"${exercism_exercises_dir}\", exercism_configfile=\"${exercism_configfile}\")"
  180. local track_root=$( pwd )
  181. local exercism_cli="./exercism --config ${exercism_configfile}"
  182. local exercises=`cat config.json | jq '.exercises[].slug + " "' --join-output`
  183. local total_exercises=`cat config.json | jq '.exercises | length'`
  184. local current_exercise_number=1
  185. local tempfile="${TMPDIR:-/tmp}/journey-test.sh-unignore_all_tests.txt"
  186. pushd ${exercism_exercises_dir}
  187. for exercise in $exercises; do
  188. echo -e "\n\n"
  189. echo "=================================================="
  190. echo "${current_exercise_number} of ${total_exercises} -- ${exercise}"
  191. echo "=================================================="
  192. ${exercism_cli} fetch ${TRACK} $exercise
  193. cp -R -H ${track_root}/exercises/${exercise}/src/example/${TRACK}/* ${exercism_exercises_dir}/${TRACK}/${exercise}/src/main/${TRACK}/
  194. pushd ${exercism_exercises_dir}/${TRACK}/${exercise}
  195. # Check that tests compile before we strip @Ignore annotations
  196. "$EXECPATH"/gradlew compileTestJava --configure-on-demand --parallel
  197. # Ensure we run all the tests (as delivered, all but the first is @Ignore'd)
  198. for testfile in `find . -name "*Test.${TRACK_SRC_EXT}"`; do
  199. # Strip @Ignore annotations to ensure we run the tests (as delivered, all but the first is @Ignore'd).
  200. # Note that unit-test.sh also strips @Ignore annotations via the Gradle task copyTestsFilteringIgnores.
  201. # The stripping implementations here and in copyTestsFilteringIgnores should be kept consistent.
  202. sed 's/@Ignore\(\(.*\)\)\{0,1\}//' ${testfile} > "${tempfile}" && mv "${tempfile}" "${testfile}"
  203. done
  204. "$EXECPATH"/gradlew test --configure-on-demand --parallel &
  205. popd
  206. current_exercise_number=$((current_exercise_number + 1))
  207. done
  208. popd
  209. }
  210. main() {
  211. # all functions assume current working directory is repository root.
  212. cd "${SCRIPTPATH}/.."
  213. local track_root=$( pwd )
  214. local build_dir="build"
  215. local build_path="${track_root}/${build_dir}"
  216. local xapi_home="${build_path}/x-api"
  217. local trackler_home="${build_path}/trackler"
  218. local exercism_home="${build_path}/exercism"
  219. local exercism_configfile=".journey-test.exercism.json"
  220. local xapi_port=9292
  221. # fail fast if required binaries are not installed.
  222. assert_installed "jq"
  223. clean "${build_dir}"
  224. # Download everything we need in parallel
  225. git_clone "x-api" "${xapi_home}" &
  226. git_clone "trackler" "${trackler_home}" &
  227. download_exercism_cli $(get_operating_system) $(get_cpu_architecture) "${exercism_home}" &
  228. wait
  229. # Check and install the ruby stuff we need in parallel
  230. assert_ruby_installed "${trackler_home}" &
  231. assert_ruby_installed "${xapi_home}" &
  232. wait
  233. # Make a local version of trackler
  234. make_local_trackler "${trackler_home}" "${xapi_home}"
  235. # Start-up a local instance of x-api so we can fetch the exercises through it.
  236. start_x_api "${xapi_home}"
  237. # Create a CLI install and config just for this build; this script does not use your CLI install.
  238. configure_exercism_cli "${exercism_home}" "${exercism_configfile}" "${xapi_port}"
  239. solve_all_exercises "${exercism_home}" "${exercism_configfile}"
  240. }
  241. ##########################################################################
  242. # Execution begins here...
  243. # If any command fails, fail the script.
  244. set -ex
  245. SCRIPTPATH=$( pushd `dirname $0` > /dev/null && pwd && popd > /dev/null )
  246. EXECPATH=$( pwd )
  247. # Make output easier to read in CI
  248. TERM=dumb
  249. xapi_pid=""
  250. trap on_exit EXIT
  251. main