diff --git a/README.md b/README.md index aca308b..ef99658 100644 --- a/README.md +++ b/README.md @@ -49,3 +49,15 @@ When you've made modifications to the code that will result in a new "golden" st [test-runners]: https://github.com/exercism/automated-tests/blob/master/docs/introduction.md [golden]: https://ro-che.info/articles/2017-12-04-golden-tests [exercism]: https://exercism.io + +## Linking tests to tasks + +The runner implements version 3 of the test runner interface. +A test is linked to a task of a concept exercise with a bats tag comment above the test: + +```bash +# bats test_tags=task:1 +test_expected_minutes_in_oven() { # @test +``` + +The number is reported as `task_id` for that test in `results.json`. diff --git a/bin/run.sh b/bin/run.sh index 18a6171..620efb8 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -14,8 +14,8 @@ # then parses that TAP file to produce the JSON file. # the version of the test-runner interface: -# https://github.com/exercism/docs/blob/50bcff91e8871f08b9a69b76ccbd45e5a90493dd/building/tooling/test-runners/interface.md -INTERFACE_VERSION=2 +# https://github.com/exercism/docs/blob/main/building/tooling/test-runners/interface.md +INTERFACE_VERSION=3 main() { if (($# < 3)) || [[ $1 == '-h' ]]; then @@ -44,6 +44,7 @@ main() { local json_result_file="$output_dir/results.json" local -A test_bodies # populated in get_test_bodies + local -A task_ids # populated in get_test_bodies run_tests "$slug" "$solution_dir" "$output_file" build_report "$output_file" "$json_result_file" @@ -87,18 +88,28 @@ run_tests() { get_test_bodies() { local test_file=$1 - local name line indent + local name line indent task_id local state="out" local body=() test_bodies=() + task_ids=() local start_test_re='^@test ['\''"](.+)['\''"] \{[[:blank:]]*$' + # function-style tests: `test_name() { # @test` + local start_func_re='^([A-Za-z_][A-Za-z0-9_]*)[[:blank:]]*\(\)[[:blank:]]*\{[[:blank:]]*#[[:blank:]]*@test[[:blank:]]*$' + # tag comment: `# bats test_tags=task:1,other` + local tags_re='^[[:blank:]]*#[[:blank:]]*bats[[:blank:]]+test_tags[[:blank:]]*=(.*)$' + local task_re='(^|[[:blank:],])task:([0-9]+)([[:blank:],]|$)' local end_test_re='^\}[[:blank:]]*$' while IFS= read -r line; do case "$state" in out) - if [[ $line =~ $start_test_re ]]; then + if [[ $line =~ $tags_re ]]; then + if [[ ${BASH_REMATCH[1]} =~ $task_re ]]; then + task_id=${BASH_REMATCH[2]} + fi + elif [[ $line =~ $start_test_re || $line =~ $start_func_re ]]; then name=${BASH_REMATCH[1]} body=() state="in" @@ -107,6 +118,10 @@ get_test_bodies() { in) if [[ $line =~ $end_test_re ]]; then test_bodies["$name"]=$(printf '%s\n' "${body[@]}") + if [[ -n ${task_id:-} ]]; then + task_ids["$name"]=$task_id + fi + unset task_id state="out" elif [[ $line == *BATS_RUN_SKIPPED*skip* ]]; then # skip the skips @@ -148,9 +163,12 @@ build_report() { local results local status="pass" - local test_body + local test_body task_id for ((i = 1; i < ${#output[@]}; i++)); do + # skip stray diagnostics, such as "# tags: task:1" after a passing test + [[ ${output[$i]} == "#"* ]] && continue + if [[ ${output[$i]} =~ ^(not )?ok\ [0-9]+\ (.*)$ ]]; then local failed="${BASH_REMATCH[1]}" local test_name="${BASH_REMATCH[2]}" @@ -160,9 +178,10 @@ build_report() { fi test_body=${test_bodies[$test_name]:-} + task_id=${task_ids[$test_name]:-} if [[ -z $failed ]]; then - results+=("$(print_passed_test "$test_name" "$test_body")") + results+=("$(print_passed_test "$test_name" "$test_body" "$task_id")") else status="fail" @@ -170,13 +189,15 @@ build_report() { for ((j = i + 1; j < ${#output[@]}; j++)); do if [[ ${output[$j]} =~ ^#\ ?(.*)$ ]]; then - error_message+="${BASH_REMATCH[1]}"$'\n' + # bats reports tags as a diagnostic: not part of the error + [[ ${BASH_REMATCH[1]} == "tags:"* ]] || + error_message+="${BASH_REMATCH[1]}"$'\n' else break fi done - results+=("$(print_failed_test "$test_name" "$error_message" "$test_body")") + results+=("$(print_failed_test "$test_name" "$error_message" "$test_body" "$task_id")") ((i = j - 1)) fi @@ -243,7 +264,9 @@ print_failed_test() { --arg message "$2" \ --arg code "$3" \ --arg status "fail" \ - '{name: $name, status: $status, test_code: $code, message: $message}' + --arg task_id "${4:-}" \ + '{name: $name, status: $status, test_code: $code, message: $message} + + (if $task_id == "" then {} else {task_id: ($task_id | tonumber)} end)' } print_passed_test() { @@ -252,7 +275,9 @@ print_passed_test() { --arg name "$1" \ --arg code "$2" \ --arg status "pass" \ - '{name: $name, status: $status, test_code: $code}' + --arg task_id "${3:-}" \ + '{name: $name, status: $status, test_code: $code} + + (if $task_id == "" then {} else {task_id: ($task_id | tonumber)} end)' } main "$@" diff --git a/tests/data/first_fails/expected_results.json b/tests/data/first_fails/expected_results.json index 1c54e74..930b00a 100644 --- a/tests/data/first_fails/expected_results.json +++ b/tests/data/first_fails/expected_results.json @@ -1,5 +1,5 @@ { - "version": 2, + "version": 3, "status": "fail", "test-environment": { "bash": "5.0.17(1)-release", diff --git a/tests/data/last_fails/expected_results.json b/tests/data/last_fails/expected_results.json index 3746b0b..e79b652 100644 --- a/tests/data/last_fails/expected_results.json +++ b/tests/data/last_fails/expected_results.json @@ -1,5 +1,5 @@ { - "version": 2, + "version": 3, "status": "fail", "tests": [ { diff --git a/tests/data/middle_fails/expected_results.json b/tests/data/middle_fails/expected_results.json index 942db9f..96126c3 100644 --- a/tests/data/middle_fails/expected_results.json +++ b/tests/data/middle_fails/expected_results.json @@ -1,5 +1,5 @@ { - "version": 2, + "version": 3, "status": "fail", "tests": [ { diff --git a/tests/data/missing_script/expected_results.json b/tests/data/missing_script/expected_results.json index 78d8909..2e12077 100644 --- a/tests/data/missing_script/expected_results.json +++ b/tests/data/missing_script/expected_results.json @@ -1,5 +1,5 @@ { - "version": 2, + "version": 3, "status": "error", "message": "1..1\nnot ok 1 say hello\n# (in test file missing_script.bats, line 5)\n# `[ \"$status\" -eq 0 ]' failed\n\nThe following warnings were encountered during tests:\nBW01: `run`'s command `bash one_passing.sh` exited with code 127, indicating 'Command not found'. Use run's return code checks, e.g. `run -127`, to fix this message.\n (from function `run' in file /home/linuxbrew/.linuxbrew/Cellar/bats-core/1.11.0/lib/bats-core/test_functions.bash, line 426,\n in test file missing_script.bats, line 4)" } diff --git a/tests/data/one_passing/expected_results.json b/tests/data/one_passing/expected_results.json index e9b0ef0..ae74d04 100644 --- a/tests/data/one_passing/expected_results.json +++ b/tests/data/one_passing/expected_results.json @@ -1,5 +1,5 @@ { - "version": 2, + "version": 3, "status": "pass", "tests": [ { diff --git a/tests/data/three_passing/expected_results.json b/tests/data/three_passing/expected_results.json index c903783..32d4a91 100644 --- a/tests/data/three_passing/expected_results.json +++ b/tests/data/three_passing/expected_results.json @@ -1,5 +1,5 @@ { - "version": 2, + "version": 3, "status": "pass", "tests": [ { diff --git a/tests/data/with_tasks/expected_results.json b/tests/data/with_tasks/expected_results.json new file mode 100644 index 0000000..746ba16 --- /dev/null +++ b/tests/data/with_tasks/expected_results.json @@ -0,0 +1,34 @@ +{ + "version": 3, + "status": "fail", + "test-environment": { + "bash": "5.3.20(1)-release", + "bats": "Bats 1.14.0" + }, + "tests": [ + { + "name": "say_one", + "status": "pass", + "test_code": "run bash with_tasks.sh one\n[ \"$status\" -eq 0 ]\n[ \"$output\" == \"one\" ]", + "task_id": 1 + }, + { + "name": "say_two", + "status": "pass", + "test_code": "run bash with_tasks.sh two\n[ \"$status\" -eq 0 ]\n[ \"$output\" == \"two\" ]", + "task_id": 1 + }, + { + "name": "say_three", + "status": "fail", + "test_code": "run bash with_tasks.sh three\n[ \"$status\" -eq 0 ]\n[ \"$output\" == \"three\" ]", + "message": "(in test file with_tasks.bats, line 24)\n `[ \"$output\" == \"three\" ]' failed\n", + "task_id": 2 + }, + { + "name": "say_four", + "status": "pass", + "test_code": "run bash with_tasks.sh two\n[ \"$output\" == \"two\" ]" + } + ] +} diff --git a/tests/data/with_tasks/with_tasks.bats b/tests/data/with_tasks/with_tasks.bats new file mode 100644 index 0000000..d7c4f83 --- /dev/null +++ b/tests/data/with_tasks/with_tasks.bats @@ -0,0 +1,31 @@ +#!/usr/bin/env bats + +# bats test_tags=task:1 +say_one() { # @test + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash with_tasks.sh one + [ "$status" -eq 0 ] + [ "$output" == "one" ] +} + +# bats test_tags=task:1 +say_two() { # @test + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash with_tasks.sh two + [ "$status" -eq 0 ] + [ "$output" == "two" ] +} + +# bats test_tags=task:2 +say_three() { # @test + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash with_tasks.sh three + [ "$status" -eq 0 ] + [ "$output" == "three" ] +} + +# no tags +say_four() { # @test + run bash with_tasks.sh two + [ "$output" == "two" ] +} diff --git a/tests/data/with_tasks/with_tasks.sh b/tests/data/with_tasks/with_tasks.sh new file mode 100644 index 0000000..75c5549 --- /dev/null +++ b/tests/data/with_tasks/with_tasks.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +case $1 in + one) echo one ;; + two) echo two ;; + three) echo wrong ;; +esac diff --git a/tests/tests.bats b/tests/tests.bats index 572ad7f..5c19375 100644 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -79,3 +79,12 @@ actual_matches_expected() { [[ "$status" -eq 1 ]] } + +@test "tests linked to tasks (function-style tests with tags)" { + TEST="with_tasks" + TEST_DIR="$DATA_DIR/$TEST" + run "$RUN_SCRIPT" "$TEST" "$TEST_DIR" "$TEST_DIR" + + [[ "$status" -eq 0 ]] + actual_matches_expected "$TEST_DIR/results.json" "$TEST_DIR/expected_results.json" +}