Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
45 changes: 35 additions & 10 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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]}"
Expand All @@ -160,23 +178,26 @@ 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"

local error_message=""

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
Expand Down Expand Up @@ -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() {
Expand All @@ -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 "$@"
2 changes: 1 addition & 1 deletion tests/data/first_fails/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "fail",
"test-environment": {
"bash": "5.0.17(1)-release",
Expand Down
2 changes: 1 addition & 1 deletion tests/data/last_fails/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "fail",
"tests": [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/data/middle_fails/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "fail",
"tests": [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/data/missing_script/expected_results.json
Original file line number Diff line number Diff line change
@@ -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)"
}
2 changes: 1 addition & 1 deletion tests/data/one_passing/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "pass",
"tests": [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/data/three_passing/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "pass",
"tests": [
{
Expand Down
34 changes: 34 additions & 0 deletions tests/data/with_tasks/expected_results.json
Original file line number Diff line number Diff line change
@@ -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\" ]"
}
]
}
31 changes: 31 additions & 0 deletions tests/data/with_tasks/with_tasks.bats
Original file line number Diff line number Diff line change
@@ -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" ]
}
6 changes: 6 additions & 0 deletions tests/data/with_tasks/with_tasks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
case $1 in
one) echo one ;;
two) echo two ;;
three) echo wrong ;;
esac
9 changes: 9 additions & 0 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}