Skip to content

Commit f69b866

Browse files
Add script to verify exercises in docker (#467)
1 parent cd121d2 commit f69b866

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

bin/verify-exercises-in-docker

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
3+
# Synopsis:
4+
# Verify that each exercise's example/exemplar solution passes the tests
5+
# using the track's test runner Docker image.
6+
# You can either verify all exercises or a single exercise.
7+
8+
# Example: verify all exercises in Docker
9+
# bin/verify-exercises-in-docker
10+
11+
# Example: verify single exercise in Docker
12+
# bin/verify-exercises-in-docker two-fer
13+
14+
set -eo pipefail
15+
16+
die() { echo "$*" >&2; exit 1; }
17+
18+
required_tool() {
19+
command -v "${1}" >/dev/null 2>&1 ||
20+
die "${1} is required but not installed. Please install it and make sure it's in your PATH."
21+
}
22+
23+
required_tool docker
24+
25+
copy_example_or_examplar_to_solution() {
26+
jq -c '[.files.solution, .files.exemplar // .files.example] | transpose | map({src: .[1], dst: .[0]}) | .[]' .meta/config.json \
27+
| while read -r src_and_dst; do
28+
cp "$(jq -r '.src' <<< "${src_and_dst}")" "$(jq -r '.dst' <<< "${src_and_dst}")"
29+
done
30+
}
31+
32+
pull_docker_image() {
33+
# shellcheck disable=SC1083
34+
docker pull exercism/prolog-test-runner ||
35+
die $'Could not find the `exercism/prolog-test-runner` Docker image.\nCheck the test runner docs at https://exercism.org/docs/building/tooling/test-runners for more information.'
36+
}
37+
38+
run_tests() {
39+
local slug
40+
slug="${1}"
41+
42+
# shellcheck disable=SC1083
43+
docker run \
44+
--rm \
45+
--network none \
46+
--read-only \
47+
--mount type=bind,src="${PWD}",dst=/solution \
48+
--mount type=bind,src="${PWD}",dst=/output \
49+
--mount type=tmpfs,dst=/tmp \
50+
exercism/prolog-test-runner "${slug}" /solution /output
51+
jq -e '.status == "pass"' "${PWD}/results.json" >/dev/null 2>&1
52+
}
53+
54+
verify_exercise() {
55+
local dir
56+
local slug
57+
local tmp_dir
58+
dir=$(realpath "${1}")
59+
slug=$(basename "${dir}")
60+
tmp_dir=$(mktemp -d -t "exercism-verify-${slug}-XXXXX")
61+
62+
echo "Verifying ${slug} exercise..."
63+
64+
(
65+
trap 'rm -rf "$tmp_dir"' EXIT # remove tempdir when subshell ends
66+
cp -r "${dir}/." "${tmp_dir}"
67+
cd "${tmp_dir}"
68+
69+
copy_example_or_examplar_to_solution
70+
run_tests "${slug}"
71+
)
72+
}
73+
74+
verify_exercises() {
75+
local exercise_slug
76+
exercise_slug="${1}"
77+
78+
shopt -s nullglob
79+
count=0
80+
for exercise_dir in ./exercises/{concept,practice}/${exercise_slug}/; do
81+
if [[ -d "${exercise_dir}" ]]; then
82+
verify_exercise "${exercise_dir}"
83+
((++count))
84+
fi
85+
done
86+
((count > 0)) || die 'no matching exercises found!'
87+
}
88+
89+
pull_docker_image
90+
91+
exercise_slug="${1:-*}"
92+
verify_exercises "${exercise_slug}"

0 commit comments

Comments
 (0)