Skip to content

Commit c218cec

Browse files
pre-commit manual fixes
We update bin/verify-exercises from generic-track
1 parent 1c17b79 commit c218cec

File tree

4 files changed

+56
-38
lines changed

4 files changed

+56
-38
lines changed

bin/create-exercise

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if [[ -z $author ]]; then
2727
read -rp "What's your github username? " author
2828
fi
2929

30-
read -p "What's the difficulty for ${slug}? " difficulty
30+
read -rp "What's the difficulty for ${slug}? " difficulty
3131

3232
bin/fetch-configlet
3333
bin/configlet create --practice-exercise "${slug}" --author "${author}" --difficulty "${difficulty}"

bin/verify-exercises

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,71 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

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.
46

5-
run_tests() {
6-
cd "$1"
7+
# Example: verify all exercises
8+
# bin/verify-exercises
79

8-
exercise_name=$(basename $1)
9-
local file="${exercise_name//-/_}"
10+
# Example: verify single exercise
11+
# bin/verify-exercises two-fer
1012

11-
sed -i.bak 's#TEST_IGNORE();#// &#' "${file}"_test.c
13+
set -eo pipefail
1214

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; }
1716

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+
}
2221

23-
make clean
24-
make
22+
required_tool jq
2523

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
2729
}
2830

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")
3139

32-
rm -rf build
33-
cp -r exercises build
40+
echo "Verifying ${slug} exercise..."
3441

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}"
3646

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+
}
3853

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
4356

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}"
5058

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!'
5168
}
5269

53-
main "$@"
70+
exercise_slug="${1:-*}"
71+
verify_exercises "${exercise_slug}"

generators/exercises/affine_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def gen_func_body(prop, inp, expected):
1212
str_list = []
13-
if expected.__class__ == dict:
13+
if isinstance(expected, dict):
1414
expected = ""
1515
phrase = inp["phrase"]
1616
a = inp["key"]["a"]

generators/exercises/phone_number.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def gen_func_body(prop, inp, expected):
1313
str_list.append("char buffer[BUFFER_SIZE];\n\n")
1414
str_list.append(f'{prop}(buffer, "{phrase}");\n')
1515

16-
if expected.__class__ == dict:
16+
if isinstance(expected, dict):
1717
expected = ""
1818

1919
str_list.append(f'TEST_ASSERT_EQUAL_STRING("{expected}", buffer);\n')

0 commit comments

Comments
 (0)