|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Test if the example/exemplar solution of each |
| 4 | +# Practice/Concept Exercise passes the exercise's tests. |
| 5 | + |
| 6 | +# Example: |
| 7 | +# ./bin/test-with-test-runner |
| 8 | + |
| 9 | +set -eo pipefail |
| 10 | + |
| 11 | +docker pull exercism/java-test-runner |
| 12 | + |
| 13 | +exit_code=0 |
| 14 | + |
| 15 | +function run_test_runner() { |
| 16 | + local slug=$1 |
| 17 | + local solution_dir=$2 |
| 18 | + local output_dir=$3 |
| 19 | + |
| 20 | + docker run \ |
| 21 | + --rm \ |
| 22 | + --network none \ |
| 23 | + --mount type=bind,src="${solution_dir}",dst=/solution \ |
| 24 | + --mount type=bind,src="${output_dir}",dst=/output \ |
| 25 | + --tmpfs /tmp:rw \ |
| 26 | + exercism/java-test-runner "${slug}" "/solution" "/output" |
| 27 | +} |
| 28 | + |
| 29 | +function verify_exercise() { |
| 30 | + local dir=$(realpath $1) |
| 31 | + local slug=$(basename "${dir}") |
| 32 | + local output_dir="${dir}/build" |
| 33 | + local implementation_file_key=$2 |
| 34 | + local implementation_files=($(jq -r --arg d "${dir}" --arg k "${implementation_file_key}" '$d + "/" + .files[$k][]' "${dir}/.meta/config.json")) |
| 35 | + local stub_files=($(jq -r --arg d "${dir}" '$d + "/" + .files.solution[]' "${dir}/.meta/config.json")) |
| 36 | + local results_file="${output_dir}/results.json" |
| 37 | + |
| 38 | + mkdir -p "${output_dir}" |
| 39 | + |
| 40 | + for stub_file in "${stub_files[@]}"; do |
| 41 | + cp "${stub_file}" "${stub_file}.bak" |
| 42 | + done |
| 43 | + |
| 44 | + for impl_file in "${implementation_files[@]}"; do |
| 45 | + cp "${impl_file}" "${dir}/src/main/java/$(basename ${impl_file})" |
| 46 | + done |
| 47 | + |
| 48 | + run_test_runner "${slug}" "${dir}" "${output_dir}" |
| 49 | + |
| 50 | + if [[ $(jq -r '.status' "${results_file}") != "pass" ]]; then |
| 51 | + echo "${slug}: ${implementation_file_key} solution did not pass the tests" |
| 52 | + exit_code=1 |
| 53 | + fi |
| 54 | + |
| 55 | + for impl_file in "${implementation_files[@]}"; do |
| 56 | + rm "${dir}/src/main/java/$(basename ${impl_file})" |
| 57 | + done |
| 58 | + |
| 59 | + for stub_file in "${stub_files[@]}"; do |
| 60 | + mv "${stub_file}.bak" "${stub_file}" |
| 61 | + done |
| 62 | +} |
| 63 | + |
| 64 | +# Verify the Concept Exercises |
| 65 | +for concept_exercise_dir in ./exercises/concept/*/; do |
| 66 | + if [ -d $concept_exercise_dir ]; then |
| 67 | + echo "Checking $(basename "${concept_exercise_dir}") exercise..." |
| 68 | + verify_exercise $concept_exercise_dir "exemplar" |
| 69 | + fi |
| 70 | +done |
| 71 | + |
| 72 | +# Verify the Practice Exercises |
| 73 | +for practice_exercise_dir in ./exercises/practice/*/; do |
| 74 | + if [ -d $practice_exercise_dir ]; then |
| 75 | + echo "Checking $(basename "${practice_exercise_dir}") exercise..." |
| 76 | + verify_exercise $practice_exercise_dir "example" |
| 77 | + fi |
| 78 | +done |
| 79 | + |
| 80 | +exit ${exit_code} |
0 commit comments