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

journey-test.sh 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. (*)
  67. echo "linux";;
  68. esac
  69. }
  70. get_cpu_architecture() {
  71. case $(uname -m) in
  72. (*64*)
  73. echo 64bit;;
  74. (*686*)
  75. echo 32bit;;
  76. (*386*)
  77. echo 32bit;;
  78. (*)
  79. echo 64bit;;
  80. esac
  81. }
  82. download_exercism_cli() {
  83. local os="$1"
  84. local arch="$2"
  85. local exercism_home="$3"
  86. echo ">>> download_exercism_cli(os=\"${os}\", arch=\"${arch}\", exercism_home=\"${exercism_home}\")"
  87. local CLI_RELEASES=https://github.com/exercism/cli/releases
  88. local latest=${CLI_RELEASES}/latest
  89. # "curl..." :: HTTP 302 headers, including "Location" -- URL to redirect to.
  90. # "awk..." :: pluck last path segment from "Location" (i.e. the version number)
  91. local version="$(curl --head --silent ${latest} | awk -v FS=/ '/Location:/{print $NF}' | tr -d '\r')"
  92. local download_url=${CLI_RELEASES}/download/${version}/exercism-${os}-${arch}.tgz
  93. mkdir -p ${exercism_home}
  94. curl -s --location ${download_url} | tar xz -C ${exercism_home}
  95. echo "<<< download_exercism_cli()"
  96. }
  97. git_clone() {
  98. local repo_name="$1"
  99. local dest_path="$2"
  100. echo ">>> git_clone(repo_name=\"${repo_name}\", dest_path=\"${dest_path}\")"
  101. git clone https://github.com/exercism/${repo_name} ${dest_path}
  102. echo "<<< git_clone()"
  103. }
  104. make_local_trackler() {
  105. local trackler="$1"
  106. local xapi_home="$2"
  107. echo ">>> make_local_trackler(trackler=\"${trackler}\", xapi_home=\"${xapi_home}\")"
  108. local xjava=$( pwd )
  109. pushd ${trackler}
  110. git submodule init -- common
  111. git submodule update
  112. # Bake in local copy of xjava; this is what we are testing.
  113. rmdir tracks/java
  114. mkdir -p tracks/java/exercises
  115. cp ${xjava}/config.json tracks/java
  116. cp -r ${xjava}/exercises tracks/java
  117. # Set the version to that expected by x-api
  118. version=$( grep -m 1 'trackler' ${xapi_home}/Gemfile.lock | sed 's/.*(//' | sed 's/)//' )
  119. echo "module Trackler VERSION = \"${version}\" end" > lib/trackler/version.rb
  120. gem install bundler
  121. bundle install
  122. gem build trackler.gemspec
  123. # Make *this* the gem that x-api uses when we build x-api.
  124. gem install --local trackler-*.gem
  125. popd > /dev/null
  126. echo "<<< make_local_trackler()"
  127. }
  128. start_x_api() {
  129. local xapi_home="$1"
  130. echo ">>> start_x_api(xapi_home=\"${xapi_home}\")"
  131. pushd $xapi_home
  132. gem install bundler
  133. bundle install
  134. RACK_ENV=development rackup &
  135. xapi_pid=$!
  136. sleep 5
  137. echo "x-api is running (pid=${xapi_pid})."
  138. popd > /dev/null
  139. echo "<<< start_x_api()"
  140. }
  141. configure_exercism_cli() {
  142. local exercism_home="$1"
  143. local exercism_configfile="$2"
  144. local xapi_port=$3
  145. echo ">>> configure_exercism_cli(exercism_home=\"${exercism_home}\", exercism_configfile=\"${exercism_configfile}\", xapi_port=${xapi_port})"
  146. local exercism="./exercism --config ${exercism_configfile}"
  147. mkdir -p "${exercism_home}"
  148. pushd "${exercism_home}"
  149. $exercism configure --dir="${exercism_home}"
  150. $exercism configure --api http://localhost:${xapi_port}
  151. $exercism debug
  152. popd
  153. echo "<<< configure_exercism_cli()"
  154. }
  155. solve_all_exercises() {
  156. local exercism_exercises_dir="$1"
  157. local exercism_configfile="$2"
  158. echo ">>> solve_all_exercises(exercism_exercises_dir=\"${exercism_exercises_dir}\", exercism_configfile=\"${exercism_configfile}\")"
  159. local xjava=$( pwd )
  160. local exercism_cli="./exercism --config ${exercism_configfile}"
  161. local exercises=`cat config.json | jq '.problems []' --raw-output`
  162. local total_exercises=`cat config.json | jq '.problems | length'`
  163. local current_exercise_number=1
  164. local tempfile="${TMPDIR:-/tmp}/journey-test.sh-unignore_all_tests.txt"
  165. pushd ${exercism_exercises_dir}
  166. for exercise in $exercises; do
  167. echo -e "\n\n"
  168. echo "=================================================="
  169. echo "${current_exercise_number} of ${total_exercises} -- ${exercise}"
  170. echo "=================================================="
  171. ${exercism_cli} fetch java $exercise
  172. cp -R -H ${xjava}/exercises/${exercise}/src/example/java/* ${exercism_exercises_dir}/java/${exercise}/src/main/java/
  173. pushd ${exercism_exercises_dir}/java/${exercise}
  174. # Ensure we run all the tests (as delivered, all but the first is @Ignore'd)
  175. for testfile in `find . -name "*Test.java"`; do
  176. sed 's/@Ignore//' ${testfile} > "${tempfile}" && mv "${tempfile}" "${testfile}"
  177. done
  178. gradle test
  179. popd
  180. current_exercise_number=$((current_exercise_number + 1))
  181. done
  182. popd
  183. }
  184. main() {
  185. # all functions assume current working directory is repository root.
  186. cd "${SCRIPTPATH}/.."
  187. local xjava=$( pwd )
  188. local build_dir="build"
  189. local build_path="${xjava}/${build_dir}"
  190. local xapi_home="${build_path}/x-api"
  191. local trackler_home="${build_path}/trackler"
  192. local exercism_home="${build_path}/exercism"
  193. local exercism_configfile=".journey-test.exercism.json"
  194. local xapi_port=9292
  195. # fail fast if required binaries are not installed.
  196. assert_installed "gradle"
  197. assert_installed "jq"
  198. clean "${build_dir}"
  199. # Make a version of trackler that includes the source from this repo.
  200. git_clone "x-api" "${xapi_home}"
  201. git_clone "trackler" "${trackler_home}"
  202. assert_ruby_installed "${trackler_home}"
  203. make_local_trackler "${trackler_home}" "${xapi_home}"
  204. # Stand-up a local instance of x-api so we can fetch the exercises through it.
  205. assert_ruby_installed "${xapi_home}"
  206. start_x_api "${xapi_home}"
  207. # Create a CLI install and config just for this build; this script does not use your CLI install.
  208. download_exercism_cli $(get_operating_system) $(get_cpu_architecture) "${exercism_home}"
  209. configure_exercism_cli "${exercism_home}" "${exercism_configfile}" "${xapi_port}"
  210. solve_all_exercises "${exercism_home}" "${exercism_configfile}"
  211. }
  212. ##########################################################################
  213. # Execution begins here...
  214. # If any command fails, fail the script.
  215. set -ex
  216. SCRIPTPATH=$( pushd `dirname $0` > /dev/null && pwd && popd > /dev/null )
  217. EXECPATH=$( pwd )
  218. # Make output easier to read in CI
  219. TERM=dumb
  220. xapi_pid=""
  221. trap on_exit EXIT
  222. main