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

journey-test.sh 9.3KB

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