|  | 
| 1 |  | -#!/usr/bin/env sh | 
|  | 1 | +#!/usr/bin/env bash | 
| 2 | 2 | 
 | 
| 3 | 3 | # Synopsis: | 
| 4 | 4 | # Run the test runner on a solution. | 
|  | 
| 24 | 24 | slug="$1" | 
| 25 | 25 | input_dir="${2%/}" | 
| 26 | 26 | output_dir="${3%/}" | 
|  | 27 | +exercise=$(echo "${slug}" | sed -r 's/(^|-)([a-z])/\U\2/g') | 
|  | 28 | +tests_file="${input_dir}/$(jq -r '.files.test[0]' "${input_dir}/.meta/config.json")" | 
|  | 29 | +tests_file_original="${tests_file}.original" | 
| 27 | 30 | results_file="${output_dir}/results.json" | 
| 28 | 31 | 
 | 
| 29 | 32 | # Create the output directory if it doesn't exist | 
| 30 | 33 | mkdir -p "${output_dir}" | 
| 31 | 34 | 
 | 
| 32 | 35 | echo "${slug}: testing..." | 
| 33 | 36 | 
 | 
|  | 37 | +cp "${tests_file}" "${tests_file_original}" | 
|  | 38 | + | 
|  | 39 | +# Unskip tests | 
|  | 40 | +sed -i -E 's/Skip *:= *"Remove this Skip property to run this test"//' "${tests_file}" | 
|  | 41 | + | 
|  | 42 | +pushd "${input_dir}" > /dev/null | 
|  | 43 | + | 
|  | 44 | +dotnet restore > /dev/null | 
|  | 45 | + | 
| 34 | 46 | # Run the tests for the provided implementation file and redirect stdout and | 
| 35 | 47 | # stderr to capture it | 
| 36 |  | -# TODO: Replace 'RUN_TESTS_COMMAND' with the command to run the tests | 
| 37 |  | -test_output=$(RUN_TESTS_COMMAND 2>&1) | 
|  | 48 | +test_output=$(dotnet test --no-restore 2>&1) | 
|  | 49 | +exit_code=$? | 
|  | 50 | + | 
|  | 51 | +popd > /dev/null | 
|  | 52 | + | 
|  | 53 | +# Restore the original file | 
|  | 54 | +mv -f "${tests_file_original}" "${tests_file}" | 
| 38 | 55 | 
 | 
| 39 | 56 | # Write the results.json file based on the exit code of the command that was  | 
| 40 | 57 | # just executed that tested the implementation file | 
| 41 |  | -if [ $? -eq 0 ]; then | 
|  | 58 | +if [ ${exit_code} -eq 0 ]; then | 
| 42 | 59 |     jq -n '{version: 1, status: "pass"}' > ${results_file} | 
| 43 | 60 | else | 
| 44 |  | -    # OPTIONAL: Sanitize the output | 
| 45 |  | -    # In some cases, the test output might be overly verbose, in which case stripping | 
| 46 |  | -    # the unneeded information can be very helpful to the student | 
| 47 |  | -    # sanitized_test_output=$(printf "${test_output}" | sed -n '/Test results:/,$p') | 
| 48 |  | - | 
| 49 |  | -    # OPTIONAL: Manually add colors to the output to help scanning the output for errors | 
| 50 |  | -    # If the test output does not contain colors to help identify failing (or passing) | 
| 51 |  | -    # tests, it can be helpful to manually add colors to the output | 
| 52 |  | -    # colorized_test_output=$(echo "${test_output}" \ | 
| 53 |  | -    #      | GREP_COLOR='01;31' grep --color=always -E -e '^(ERROR:.*|.*failed)$|$' \ | 
| 54 |  | -    #      | GREP_COLOR='01;32' grep --color=always -E -e '^.*passed$|$') | 
| 55 |  | - | 
| 56 |  | -    jq -n --arg output "${test_output}" '{version: 1, status: "fail", message: $output}' > ${results_file} | 
|  | 61 | +    # Sanitize the output | 
|  | 62 | +    if grep -q "matched the specified pattern" <<< "${test_output}" ; then | 
|  | 63 | +        sanitized_test_output=$(printf "${test_output}" | sed -n -E -e '1,/matched the specified pattern.$/!p') | 
|  | 64 | +    else | 
|  | 65 | +        sanitized_test_output="${test_output}" | 
|  | 66 | +    fi | 
|  | 67 | + | 
|  | 68 | +    jq -n --arg output "${sanitized_test_output}" '{version: 1, status: "fail", message: $output}' > ${results_file} | 
| 57 | 69 | fi | 
| 58 | 70 | 
 | 
| 59 | 71 | echo "${slug}: done" | 
0 commit comments