|
1 |
| -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
2 | 2 |
|
3 |
| -set -e |
| 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. |
4 | 6 |
|
5 |
| -run_tests() { |
6 |
| - cd "$1" |
| 7 | +# Example: verify all exercises |
| 8 | +# bin/verify-exercises |
7 | 9 |
|
8 |
| - exercise_name=$(basename $1) |
9 |
| - local file="${exercise_name//-/_}" |
| 10 | +# Example: verify single exercise |
| 11 | +# bin/verify-exercises two-fer |
10 | 12 |
|
11 |
| - sed -i.bak 's#TEST_IGNORE();#// &#' "${file}"_test.c |
| 13 | +set -eo pipefail |
12 | 14 |
|
13 |
| - # concept exercises have "exemplar" solutions (ideal, to be strived to) |
14 |
| - if [ -f .meta/exemplar.asm ]; then |
15 |
| - mv .meta/exemplar.asm "${file}".asm |
16 |
| - fi |
| 15 | +die() { echo "$*" >&2; exit 1; } |
17 | 16 |
|
18 |
| - # practice exercises have "example" solutions (one of many possible solutions with no single ideal approach) |
19 |
| - if [ -f .meta/example.asm ]; then |
20 |
| - mv .meta/example.asm "${file}".asm |
21 |
| - fi |
| 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 | +} |
22 | 21 |
|
23 |
| - make clean |
24 |
| - make |
| 22 | +required_tool jq |
25 | 23 |
|
26 |
| - cd ../.. |
| 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 |
27 | 29 | }
|
28 | 30 |
|
29 |
| -main() { |
30 |
| - cd "$(dirname "$0")"/.. |
| 31 | +verify_exercise() { |
| 32 | + local dir |
| 33 | + local slug |
| 34 | + local tmp_dir |
| 35 | + |
| 36 | + dir=$(realpath "${1}") |
| 37 | + slug=$(basename "${dir}") |
| 38 | + tmp_dir=$(mktemp -d -t "exercism-verify-${slug}-XXXXX") |
31 | 39 |
|
32 |
| - rm -rf build |
33 |
| - cp -r exercises build |
| 40 | + echo "Verifying ${slug} exercise..." |
34 | 41 |
|
35 |
| - cd build |
| 42 | + ( |
| 43 | + trap 'rm -rf "$tmp_dir"' EXIT # remove tempdir when subshell ends |
| 44 | + cp -r "${dir}/." "${tmp_dir}" |
| 45 | + cd "${tmp_dir}" |
36 | 46 |
|
37 |
| - exercises=`echo */*` |
| 47 | + copy_example_or_examplar_to_solution |
| 48 | + sed -i.bak 's#TEST_IGNORE();#// &#' "${slug//-/_}"_test.c |
| 49 | + make clean |
| 50 | + make |
| 51 | + ) |
| 52 | +} |
38 | 53 |
|
39 |
| - if [[ ! -z "$@" ]]; then |
40 |
| - pattern=$(echo "$@" | sed 's/ /|/g') |
41 |
| - exercises=$(find $exercises -maxdepth 0 | grep -E "$pattern") |
42 |
| - fi |
| 54 | +verify_exercises() { |
| 55 | + local exercise_slug |
43 | 56 |
|
44 |
| - # test each exercise |
45 |
| - for exercise in $exercises; do |
46 |
| - if [ -d ${exercise} ]; then |
47 |
| - run_tests "${exercise}" |
48 |
| - fi |
49 |
| - done |
| 57 | + exercise_slug="${1}" |
50 | 58 |
|
| 59 | + shopt -s nullglob |
| 60 | + count=0 |
| 61 | + for exercise_dir in ./exercises/{concept,practice}/${exercise_slug}/; do |
| 62 | + if [[ -d "${exercise_dir}" ]]; then |
| 63 | + verify_exercise "${exercise_dir}" |
| 64 | + ((++count)) |
| 65 | + fi |
| 66 | + done |
| 67 | + ((count > 0)) || die 'no matching exercises found!' |
51 | 68 | }
|
52 | 69 |
|
53 |
| -main "$@" |
| 70 | +exercise_slug="${1:-*}" |
| 71 | +verify_exercises "${exercise_slug}" |
0 commit comments