Skip to content

Commit 995c194

Browse files
Update test script
1 parent d13155a commit 995c194

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

bin/test

+53-41
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,79 @@
11
#!/usr/bin/env bash
22

3-
# Test if the example/exemplar solution of each
4-
# Practice/Concept Exercise passes the exercise's tests.
3+
# Synopsis:
4+
# Verify that each exercise's example/exemplar solution passes the tests.
5+
# You can either verify all exercises or a single exercise.
56

6-
# Example:
7-
# ./bin/test
7+
# Example: verify all exercises
8+
# bin/verify-exercises
9+
10+
# Example: verify single exercise
11+
# bin/verify-exercises two-fer
812

913
set -eo pipefail
1014

15+
die() { echo "$*" >&2; exit 1; }
16+
17+
required_tool() {
18+
command -v "${1}" >/dev/null 2>&1 ||
19+
die "${1} is required but not installed. Please install it and make sure it's in your PATH."
20+
}
21+
22+
required_tool jq
23+
24+
copy_example_or_examplar_to_solution() {
25+
jq -c '[.files.solution, .files.exemplar // .files.example] | transpose | map({src: .[1], dst: .[0]}) | .[]' .meta/config.json \
26+
| while read -r src_and_dst; do
27+
cp "$(jq -r '.src' <<< "${src_and_dst}")" "$(jq -r '.dst' <<< "${src_and_dst}")"
28+
done
29+
}
30+
1131
docker pull exercism/unison-test-runner
1232

13-
exit_code=0
33+
run_tests() {
34+
local slug
1435

15-
function run_test_runner() {
16-
local dir=$1
17-
local slug=$2
36+
slug="${1}"
1837

1938
docker run \
2039
--rm \
2140
--network none \
22-
--mount type=bind,src="${dir}",dst=/solution \
23-
--mount type=bind,src="${dir}",dst=/output \
41+
--mount type=bind,src="${PWD}",dst=/solution \
42+
--mount type=bind,src="${PWD}",dst=/output \
2443
--tmpfs /tmp:rw \
2544
exercism/unison-test-runner "${slug}" "/solution" "/output"
45+
jq -e '.status == "pass"' "${PWD}/results.json" >/dev/null 2>&1
2646
}
2747

28-
function verify_exercise() {
29-
local dir=$(realpath $1)
30-
local slug=$(basename "${dir}")
31-
local implementation_file_key=$2
32-
local implementation_file=$(jq -r --arg d "${dir}" --arg k "${implementation_file_key}" '$d + "/" + .files[$k][0]' "${dir}/.meta/config.json")
33-
local stub_file=$(jq -r --arg d "${dir}" '$d + "/" + .files.solution[0]' "${dir}/.meta/config.json")
34-
local stub_backup_file="${stub_file}.bak"
35-
local results_file="${dir}/results.json"
48+
verify_exercise() {
49+
local dir
50+
local slug
51+
local tmp_dir
3652

37-
cp "${stub_file}" "${stub_backup_file}"
38-
cp "${implementation_file}" "${stub_file}"
53+
dir=$(realpath "${1}")
54+
slug=$(basename "${dir}")
55+
tmp_dir=$(mktemp -d -t "exercism-verify-${slug}-XXXXX")
3956

40-
run_test_runner "${dir}" "${slug}"
57+
echo "Verifying ${slug} exercise..."
4158

42-
if [[ $(jq -r '.status' "${results_file}") != "pass" ]]; then
43-
echo "${slug}: ${implementation_file_key} solution did not pass the tests"
44-
exit_code=1
45-
fi
59+
(
60+
trap 'rm -rf "$tmp_dir"' EXIT # remove tempdir when subshell ends
61+
cp -r "${dir}/." "${tmp_dir}"
62+
cd "${tmp_dir}"
4663

47-
mv "${stub_backup_file}" "${stub_file}"
48-
rm -f "${results_file}"
64+
copy_example_or_examplar_to_solution
65+
run_tests "${slug}"
66+
)
4967
}
5068

51-
# Verify the Concept Exercises
52-
for concept_exercise_dir in ./exercises/concept/*/; do
53-
if [ -d $concept_exercise_dir ]; then
54-
echo "Checking $(basename "${concept_exercise_dir}") exercise..."
55-
verify_exercise $concept_exercise_dir "exemplar"
56-
fi
57-
done
69+
exercise_slug="${1:-*}"
5870

59-
# Verify the Practice Exercises
60-
for practice_exercise_dir in ./exercises/practice/*/; do
61-
if [ -d $practice_exercise_dir ]; then
62-
echo "Checking $(basename "${practice_exercise_dir}") exercise..."
63-
verify_exercise $practice_exercise_dir "example"
71+
shopt -s nullglob
72+
count=0
73+
for exercise_dir in ./exercises/{concept,practice}/${exercise_slug}/; do
74+
if [[ -d "${exercise_dir}" ]]; then
75+
verify_exercise "${exercise_dir}"
76+
((++count))
6477
fi
6578
done
66-
67-
exit ${exit_code}
79+
((count > 0)) || die 'no matching exercises found!'

0 commit comments

Comments
 (0)