Sfoglia il codice sorgente

Merge pull request #159 from jtigger/journey-builds-trackler

Journey builds trackler
John Ryan 10 anni fa
parent
commit
ed74b36858
1 ha cambiato i file con 165 aggiunte e 128 eliminazioni
  1. 165
    128
      bin/journey-test.sh

+ 165
- 128
bin/journey-test.sh Vedi File

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