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

journey-test.sh 8.9KB

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