Skip to content

Commit 2dba53c

Browse files
Add verify-exercises-in-docker script
1 parent 52166b9 commit 2dba53c

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

bin/verify-exercises-in-docker

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
splice_example_with_tests() {
26+
local src example
27+
src=$(jq -r '.files.solution[0]' .meta/config.json)
28+
example=$(jq -r '.files.example[0]' .meta/config.json)
29+
30+
{
31+
cat "$example"
32+
sed -nE '
33+
/unittest/,$ {
34+
s/(int allTestsEnabled =).*/\1 1;/
35+
p
36+
}
37+
' "$src"
38+
} > "${src}.tmp"
39+
mv "${src}.tmp" "$src"
40+
}
41+
42+
pull_docker_image() {
43+
docker pull "${image}" ||
44+
die $'Could not find the `'"${image}"$'` Docker image.\nCheck the test runner docs at https://exercism.org/docs/building/tooling/test-runners for more information.'
45+
}
46+
47+
run_tests() {
48+
local slug
49+
slug="${1}"
50+
51+
docker run \
52+
--rm \
53+
--network none \
54+
--read-only \
55+
--mount type=bind,src="${PWD}",dst=/solution \
56+
--mount type=bind,src="${PWD}",dst=/output \
57+
--mount type=tmpfs,dst=/tmp \
58+
--mount type=tmpfs,dst=/root \
59+
"${image}" "${slug}" /solution /output
60+
jq -e '.status == "pass"' "${PWD}/results.json" >/dev/null 2>&1
61+
}
62+
63+
verify_exercise() {
64+
local dir
65+
local slug
66+
local tmp_dir
67+
dir=$(realpath "${1}")
68+
slug=$(basename "${dir}")
69+
tmp_dir=$(mktemp -d -t "exercism-verify-${slug}-XXXXX")
70+
71+
echo "Verifying ${slug} exercise..."
72+
73+
(
74+
trap 'rm -rf "$tmp_dir"' EXIT # remove tempdir when subshell ends
75+
cp -r "${dir}/." "${tmp_dir}"
76+
cd "${tmp_dir}"
77+
78+
splice_example_with_tests
79+
run_tests "${slug}" || { cat "${PWD}/results.json"; exit 1; }
80+
)
81+
}
82+
83+
verify_exercises() {
84+
local exercise_slug
85+
exercise_slug="${1}"
86+
87+
shopt -s nullglob
88+
count=0
89+
for exercise_dir in ./exercises/{concept,practice}/${exercise_slug}/; do
90+
if [[ -d "${exercise_dir}" ]]; then
91+
verify_exercise "${exercise_dir}"
92+
((++count))
93+
fi
94+
done
95+
((count > 0)) || die 'no matching exercises found!'
96+
}
97+
98+
image=''
99+
while getopts :i: opt; do
100+
case $opt in
101+
i) image=$OPTARG ;;
102+
?) echo >&2 "Unknown option: -$OPTARG"; exit 1 ;;
103+
esac
104+
done
105+
shift "$((OPTIND - 1))"
106+
107+
if [[ -z "${image}" ]]; then
108+
image="exercism/d-test-runner"
109+
pull_docker_image
110+
fi
111+
112+
exercise_slug="${1:-*}"
113+
verify_exercises "${exercise_slug}"

0 commit comments

Comments
 (0)