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

journey-test.sh 8.5KB

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