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

journey-test.sh 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. echo ">>> make_local_trackler(trackler=\"${trackler}\")"
  107. local xjava=$( pwd )
  108. pushd ${trackler}
  109. git submodule init -- common
  110. git submodule update
  111. # Bake in local copy of xjava; this is what we are testing.
  112. rmdir tracks/java
  113. mkdir -p tracks/java/exercises
  114. cp ${xjava}/config.json tracks/java
  115. cp -r ${xjava}/exercises tracks/java
  116. gem install bundler
  117. bundle install
  118. gem build trackler.gemspec
  119. # Make *this* the gem that x-api uses when we build x-api.
  120. gem install --local trackler-*.gem
  121. popd > /dev/null
  122. echo "<<< make_local_trackler()"
  123. }
  124. start_x_api() {
  125. local xapi_home="$1"
  126. echo ">>> start_x_api(xapi_home=\"${xapi_home}\")"
  127. pushd $xapi_home
  128. gem install bundler
  129. bundle install
  130. RACK_ENV=development rackup &
  131. xapi_pid=$!
  132. sleep 5
  133. echo "x-api is running (pid=${xapi_pid})."
  134. popd > /dev/null
  135. echo "<<< start_x_api()"
  136. }
  137. configure_exercism_cli() {
  138. local exercism_home="$1"
  139. local exercism_configfile="$2"
  140. local xapi_port=$3
  141. echo ">>> configure_exercism_cli(exercism_home=\"${exercism_home}\", exercism_configfile=\"${exercism_configfile}\", xapi_port=${xapi_port})"
  142. local exercism="./exercism --config ${exercism_configfile}"
  143. mkdir -p "${exercism_home}"
  144. pushd "${exercism_home}"
  145. $exercism configure --dir="${exercism_home}"
  146. $exercism configure --api http://localhost:${xapi_port}
  147. $exercism debug
  148. popd
  149. echo "<<< configure_exercism_cli()"
  150. }
  151. solve_all_exercises() {
  152. local exercism_exercises_dir="$1"
  153. local exercism_configfile="$2"
  154. echo ">>> solve_all_exercises(exercism_exercises_dir=\"${exercism_exercises_dir}\", exercism_configfile=\"${exercism_configfile}\")"
  155. local xjava=$( pwd )
  156. local exercism_cli="./exercism --config ${exercism_configfile}"
  157. local exercises=`cat config.json | jq '.problems []' --raw-output`
  158. local total_exercises=`cat config.json | jq '.problems | length'`
  159. local current_exercise_number=1
  160. local tempfile="${TMPDIR:-/tmp}/journey-test.sh-unignore_all_tests.txt"
  161. pushd ${exercism_exercises_dir}
  162. for exercise in $exercises; do
  163. echo -e "\n\n"
  164. echo "=================================================="
  165. echo "${current_exercise_number} of ${total_exercises} -- ${exercise}"
  166. echo "=================================================="
  167. ${exercism_cli} fetch java $exercise
  168. cp -R -H ${xjava}/exercises/${exercise}/src/example/java/* ${exercism_exercises_dir}/java/${exercise}/src/main/java/
  169. pushd ${exercism_exercises_dir}/java/${exercise}
  170. # Ensure we run all the tests (as delivered, all but the first is @Ignore'd)
  171. for testfile in `find . -name "*Test.java"`; do
  172. sed 's/@Ignore//' ${testfile} > "${tempfile}" && mv "${tempfile}" "${testfile}"
  173. done
  174. gradle test
  175. popd
  176. current_exercise_number=$((current_exercise_number + 1))
  177. done
  178. popd
  179. }
  180. main() {
  181. # all functions assume current working directory is repository root.
  182. cd "${SCRIPTPATH}/.."
  183. local xjava=$( pwd )
  184. local build_dir="build"
  185. local build_path="${xjava}/${build_dir}"
  186. local xapi_home="${build_path}/x-api"
  187. local trackler_home="${build_path}/trackler"
  188. local exercism_home="${build_path}/exercism"
  189. local exercism_configfile=".journey-test.exercism.json"
  190. local xapi_port=9292
  191. # fail fast if required binaries are not installed.
  192. assert_installed "gradle"
  193. assert_installed "jq"
  194. clean "${build_dir}"
  195. # Make a version of trackler that includes the source from this repo.
  196. git_clone "trackler" "${trackler_home}"
  197. assert_ruby_installed "${trackler_home}"
  198. make_local_trackler "${trackler_home}"
  199. # Stand-up a local instance of x-api so we can fetch the exercises through it.
  200. git_clone "x-api" "${xapi_home}"
  201. assert_ruby_installed "${xapi_home}"
  202. start_x_api "${xapi_home}"
  203. # Create a CLI install and config just for this build; this script does not use your CLI install.
  204. download_exercism_cli $(get_operating_system) $(get_cpu_architecture) "${exercism_home}"
  205. configure_exercism_cli "${exercism_home}" "${exercism_configfile}" "${xapi_port}"
  206. solve_all_exercises "${exercism_home}" "${exercism_configfile}"
  207. }
  208. ##########################################################################
  209. # Execution begins here...
  210. # If any command fails, fail the script.
  211. set -e
  212. SCRIPTPATH=$( pushd `dirname $0` > /dev/null && pwd && popd > /dev/null )
  213. EXECPATH=$( pwd )
  214. # Make output easier to read in CI
  215. TERM=dumb
  216. xapi_pid=""
  217. trap on_exit EXIT
  218. main