Skip to content

Commit 3a05fdd

Browse files
Add test runner (#5)
Add a basic V1 test runner for Nushell
1 parent 93c7b94 commit 3a05fdd

16 files changed

Lines changed: 52 additions & 27 deletions

File tree

Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
FROM alpine:3.18
1+
FROM alpine:3.22
22

3-
# install packages required to run the tests
4-
RUN apk add --no-cache jq coreutils
3+
RUN apk add --no-cache jq nushell
54

65
WORKDIR /opt/test-runner
76
COPY . .

bin/run.sh

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env sh
2-
32
# Synopsis:
43
# Run the test runner on a solution.
5-
64
# Arguments:
75
# $1: exercise slug
86
# $2: path to solution folder
@@ -33,28 +31,24 @@ echo "${slug}: testing..."
3331

3432
# Run the tests for the provided implementation file and redirect stdout and
3533
# stderr to capture it
36-
test_output=$(false)
37-
# TODO: substitute "false" with the actual command to run the test:
38-
# test_output=$(command_to_run_tests 2>&1)
34+
test_output="$(nu "$solution_dir/tests.nu" 2>&1)"
3935

4036
# Write the results.json file based on the exit code of the command that was
4137
# just executed that tested the implementation file
4238
if [ $? -eq 0 ]; then
43-
jq -n '{version: 1, status: "pass"}' > ${results_file}
39+
jq -n '{version: 1, status: "pass"}' > "${results_file}"
4440
else
45-
# OPTIONAL: Sanitize the output
46-
# In some cases, the test output might be overly verbose, in which case stripping
47-
# the unneeded information can be very helpful to the student
48-
# sanitized_test_output=$(printf "${test_output}" | sed -n '/Test results:/,$p')
49-
50-
# OPTIONAL: Manually add colors to the output to help scanning the output for errors
51-
# If the test output does not contain colors to help identify failing (or passing)
52-
# tests, it can be helpful to manually add colors to the output
53-
# colorized_test_output=$(echo "${test_output}" \
54-
# | GREP_COLOR='01;31' grep --color=always -E -e '^(ERROR:.*|.*failed)$|$' \
55-
# | GREP_COLOR='01;32' grep --color=always -E -e '^.*passed$|$')
56-
57-
jq -n --arg output "${test_output}" '{version: 1, status: "fail", message: $output}' > ${results_file}
41+
status="error"
42+
case "$test_output" in
43+
*"Assertion failed"*)
44+
status="fail"
45+
;;
46+
esac
47+
48+
# Remove file paths specific to the local machine from the output
49+
test_output=$(echo "$test_output" | sed 's|[^ ]*/\([^ ]*\)|\1|g')
50+
51+
jq -n --arg output "$test_output" --arg status "$status" '{version: 1, status: $status, message: $output}' > "${results_file}"
5852
fi
5953

6054
echo "${slug}: done"

tests/all-fail/all-fail.nu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export def hello [] {
2+
"Goodbye, Mars!"
3+
}
4+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"version": 1,
33
"status": "fail",
4-
"message": "TODO: replace with correct output"
4+
"message": "Error: x Assertion failed.\n tests.nu:3:14]\n 2 | use assert\n 3 | assert equal (hello) \"Hello, World!\"\n : ^^^^^^^^^^^|^^^^^^^^^^^\n : `-| These are not equal.\n : | Left : '\"Goodbye, Mars!\"'\n : | Right : '\"Hello, World!\"'\n `----"
55
}

tests/all-fail/tests.nu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use all-fail.nu hello
2+
use std/assert
3+
assert equal (hello) "Hello, World!"

tests/empty-file/empty-file.nu

Whitespace-only changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"version": 1,
3-
"status": "fail",
4-
"message": "TODO: replace with correct output"
3+
"status": "error",
4+
"message": "Error: nu::parser::export_not_found\n\n x Export not found.\n tests.nu:1:19]\n 1 | use empty-file.nu hello\n : ^^|^^\n : `-- could not find imports\n 2 | use assert\n `----"
55
}

tests/empty-file/tests.nu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use empty-file.nu hello
2+
use std/assert
3+
assert equal (hello) "Hello, World!"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"version": 1,
33
"status": "fail",
4-
"message": "TODO: replace with correct output"
4+
"message": "Error: x Assertion failed.\n tests.nu:4:14]\n 3 | assert equal (two_fer \"Alice\") \"One for Alice, one for me.\"\n 4 | assert equal (two_fer) \"One for you, one for me.\"\n : ^^^^^^^^^^^^^^^^^^|^^^^^^^^^^^^^^^^^\n : `-| These are not equal.\n : | Left : '\"One for foo, one for me.\"'\n : | Right : '\"One for you, one for me.\"'\n `----"
55
}

tests/partial-fail/partial-fail.nu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export def two_fer [name = "foo"] {
2+
$"One for ($name), one for me."
3+
}
4+

0 commit comments

Comments
 (0)