Skip to content

Commit c85d0a3

Browse files
committed
(refactor) Cleanup Journey Test script.
1 parent 993fbd0 commit c85d0a3

1 file changed

Lines changed: 153 additions & 143 deletions

File tree

‎bin/journey-test.sh‎

Lines changed: 153 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -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
82

9-
function on_exit() {
3+
on_exit() {
104
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})"
148
fi
159
cd $EXECPATH
1610
echo "<<< on_exit()"
1711
}
1812

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}\")"
3116

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."
3719
echo -e "Have you completed the setup instructions at https://github.com/exercism/xjava ?\n"
3820
echo "PATH=${PATH}"
3921
echo "aborting."
4022
exit 1
4123
fi
42-
echo "<<< assert_jq_installed()"
24+
echo "<<< assert_installed()"
4325
}
4426

45-
assert_required_ruby_installed() {
46-
echo ">>> assert_required_ruby_installed(\"$1\")"
47-
local RUBY_APP_HOME="$1"
27+
assert_ruby_installed() {
28+
local ruby_app_home="$1"
29+
echo ">>> assert_ruby_installed(ruby_app_home=\"${ruby_app_home}\")"
4830

49-
pushd "${RUBY_APP_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 [ "$EXPECTED_RUBY_VER" != "" -a "$CURRENT_RUBY_VER" != "$EXPECTED_RUBY_VER" ]; then
54-
echo "${RUBY_APP_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}."
5537
echo -e "Ruby used: `which ruby`.\n"
5638
echo -e "Have you completed the setup instructions at https://github.com/exercism/xjava ?\n"
5739
echo "PATH=${PATH}"
5840
echo "aborting."
5941
exit 1
6042
fi
61-
echo "<<< assert_required_ruby_installed()"
43+
echo "<<< assert_ruby_installed()"
6244
}
6345

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}\")"
49+
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
6855

69-
if [ -d "$BUILD_DIR" ] ; then
70-
echo "Cleaning journey script build (${BUILD_DIR})."
71-
rm -rf "$BUILD_DIR"
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}"
7260
fi
73-
pushd ${REPO_ROOT}/exercises > /dev/null
61+
cd exercises
7462
gradle clean
75-
popd > /dev/null
76-
echo "<<< clean_build()"
63+
cd ..
64+
echo "<<< clean()"
7765
}
7866

7967
get_operating_system() {
@@ -102,158 +90,180 @@ get_cpu_architecture() {
10290
esac
10391
}
10492

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
110100

111-
local LATEST=https://github.com/exercism/cli/releases/latest
101+
local latest=${CLI_RELEASES}/latest
112102
# "curl..." :: HTTP 302 headers, including "Location" -- URL to redirect to.
113103
# "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
116106

117-
mkdir -p ${EXERCISM_HOME}
118-
curl -s --location ${CLI_URL} | tar xz -C ${EXERCISM_HOME}
119-
echo "<<< fetch_exercism_cli()"
107+
mkdir -p ${exercism_home}
108+
curl -s --location ${download_url} | tar xz -C ${exercism_home}
109+
echo "<<< download_exercism_cli()"
120110
}
121111

122-
fetch_x_api() {
123-
echo ">>> fetch_x_api(\"$1\")"
124-
local XAPI_HOME="$1"
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}\")"
125116

126-
git clone https://github.com/exercism/x-api ${XAPI_HOME}
117+
git clone https://github.com/exercism/${repo_name} ${dest_path}
127118

128-
echo "<<< fetch_x_api()"
119+
echo "<<< git_clone()"
129120
}
130121

131-
fetch_trackler() {
132-
echo ">>> fetch_trackler(\"$1\")"
133-
local TRACKLER_HOME="$1"
122+
make_local_trackler() {
123+
local trackler="$1"
124+
echo ">>> make_local_trackler(trackler=\"${trackler}\")"
134125

135-
git clone https://github.com/exercism/trackler ${TRACKLER_HOME}
136-
137-
echo "<<< fetch_trackler()"
138-
}
139-
140-
build_trackler() {
141-
echo ">>> build_trackler(\"$1\")"
142-
local TRACKLER_HOME="$1"
143-
144-
pushd ${TRACKLER_HOME} >/dev/null
126+
local xjava=$( pwd )
127+
pushd ${trackler}
145128
git submodule init -- common
146129
git submodule update
130+
131+
# Bake in local copy of xjava; this is what we are testing.
147132
rmdir tracks/java
148133
mkdir -p tracks/java/exercises
149-
cp $REPO_ROOT/config.json tracks/java
150-
cp -r $REPO_ROOT/exercises tracks/java
134+
cp ${xjava}/config.json tracks/java
135+
cp -r ${xjava}/exercises tracks/java
151136

152137
gem install bundler
153138
bundle install
154139
gem build trackler.gemspec
140+
141+
# Make *this* the gem that x-api uses when we build x-api.
155142
gem install --local trackler-*.gem
156143

157144
popd > /dev/null
158-
echo "<<< build_trackler()"
145+
echo "<<< make_local_trackler()"
159146
}
160147

161148
start_x_api() {
162-
echo ">>> start_x_api(\"$1\")"
163-
local XAPI_HOME="$1"
149+
local xapi_home="$1"
150+
echo ">>> start_x_api(xapi_home=\"${xapi_home}\")"
164151

165-
pushd $XAPI_HOME >/dev/null
152+
pushd $xapi_home
166153

167154
gem install bundler
168155
bundle install
169156
RACK_ENV=development rackup &
170-
XAPI_PID=$!
157+
xapi_pid=$!
171158
sleep 5
172159

173-
echo "X-API is running (pid=${XAPI_PID})."
160+
echo "x-api is running (pid=${xapi_pid})."
174161

175162
popd > /dev/null
176163
echo "<<< start_x_api()"
177164
}
178165

179166
configure_exercism_cli() {
180-
echo ">>> configure_exercism_cli(\"$1\", \"$2\", $3)"
181-
local EXERCISM_HOME="$1"
182-
local EXERCISM_CONFIGFILE="$2"
183-
local XAPI_PORT=$3
184-
local EXERCISM="./exercism --config ${EXERCISM_CONFIGFILE}"
185-
186-
mkdir -p "${EXERCISM_HOME}"
187-
pushd "${EXERCISM_HOME}" >/dev/null
188-
$EXERCISM configure --dir="${EXERCISM_HOME}"
189-
$EXERCISM configure --api http://localhost:${XAPI_PORT}
190-
$EXERCISM debug
191-
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
192179

193180
echo "<<< configure_exercism_cli()"
194181
}
195182

196183
solve_all_exercises() {
197-
echo ">>> solve_all_exercises(\"$1\", \"$2\")"
198-
199-
local EXERCISM_HOME="$1"
200-
local EXERCISM_CONFIGFILE="$2"
201-
202-
local EXERCISM="./exercism --config ${EXERCISM_CONFIGFILE}"
203-
local EXERCISES=`cat config.json | jq '.problems []' --raw-output`
204-
local TOTAL_EXERCISES=`cat config.json | jq '.problems | length'`
205-
local CURRENT_EXERCISE_NUMBER=1
206-
local TEMPFILE="${TMPDIR:-/tmp}/journey-test.sh-unignore_all_tests.txt"
207-
208-
pushd ${EXERCISM_HOME} >/dev/null
209-
for EXERCISE in $EXERCISES; do
210-
echo ''
211-
echo '****' Testing $EXERCISE '('$CURRENT_EXERCISE_NUMBER / $TOTAL_EXERCISES') ****'
212-
echo ''
213-
$EXERCISM fetch java $EXERCISE
214-
cp -R -H $REPO_ROOT/exercises/$EXERCISE/src/example/java/* $EXERCISM_HOME/java/$EXERCISE/src/main/java/
215-
pushd $EXERCISM_HOME/java/$EXERCISE/ >/dev/null
216-
for TESTFILE in `find . -name "*Test.java"`; do
217-
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}"
218209
done
219210
gradle test
220-
popd >/dev/null
211+
popd
221212

222-
CURRENT_EXERCISE_NUMBER=$((CURRENT_EXERCISE_NUMBER + 1))
213+
current_exercise_number=$((current_exercise_number + 1))
223214
done
224-
popd >/dev/null
215+
popd
225216
}
226217

227218
main() {
228-
# allow all functions to assume current working directory is repository root.
219+
# all functions assume current working directory is repository root.
229220
cd "${SCRIPTPATH}/.."
230221

231-
local REPO_ROOT="${PWD}"
232-
local BUILD_DIR="${REPO_ROOT}/build"
233-
local XAPI_HOME="${BUILD_DIR}/x-api"
234-
local TRACKLER_HOME="${BUILD_DIR}/trackler"
235-
local EXERCISM_HOME="${BUILD_DIR}/exercism"
236-
local EXERCISM_CONFIGFILE=".journey-test.exercism.json"
237-
local XAPI_PORT=9292
238-
239-
assert_gradle_installed
240-
assert_jq_installed
241-
clean_build "${REPO_ROOT}" "${BUILD_DIR}"
242-
fetch_exercism_cli $(get_operating_system) $(get_cpu_architecture) "${EXERCISM_HOME}"
243-
fetch_x_api "${XAPI_HOME}"
244-
fetch_trackler "${TRACKLER_HOME}"
245-
assert_required_ruby_installed "${XAPI_HOME}"
246-
assert_required_ruby_installed "${TRACKLER_HOME}"
247-
build_trackler "${TRACKLER_HOME}"
248-
start_x_api "${XAPI_HOME}"
249-
configure_exercism_cli "${EXERCISM_HOME}" "${EXERCISM_CONFIGFILE}" "${XAPI_PORT}"
250-
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}"
251254
}
252255

253256
##########################################################################
254257
# Execution begins here...
255258

259+
# If any command fails, fail the script.
256260
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=""
257267
trap on_exit EXIT
258268
main
259269

0 commit comments

Comments
 (0)