|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +##################### global options ######################### |
| 4 | +# file names |
| 5 | +FILE_IN="in" |
| 6 | +FILE_REF="ref" |
| 7 | +FILE_OUT="out" |
| 8 | +FILE_STDERR="stderr" |
| 9 | +FILE_ERR="err" |
| 10 | +FILE_CMD="cmd" |
| 11 | + |
| 12 | +# colours |
| 13 | +COLOR_FAIL='\033[0;31m' # red |
| 14 | +COLOR_OK='\033[0;32m' # green |
| 15 | +NC='\033[0m' |
| 16 | + |
| 17 | +# default options |
| 18 | +DEFAULT_CMD="" |
| 19 | +TEST_DIR="" |
| 20 | +ACTION="TEST" |
| 21 | + |
| 22 | +# fail counter |
| 23 | +FAIL_COUNT=0 |
| 24 | + |
| 25 | +#################### helper functions ######################## |
| 26 | +clean() { |
| 27 | + DIR=$1 |
| 28 | + for d in "${DIR}"/*/ ; do |
| 29 | + rm -f "$d/${FILE_OUT}" "$d/${FILE_STDERR}" |
| 30 | + done |
| 31 | +} |
| 32 | + |
| 33 | +print_ok_color() { |
| 34 | + echo -e "${COLOR_OK}${1}${NC}" |
| 35 | +} |
| 36 | + |
| 37 | +print_fail_color() { |
| 38 | + echo -e "${COLOR_FAIL}${1}${NC}" |
| 39 | +} |
| 40 | + |
| 41 | +expect_ok() { |
| 42 | + CURR_TEST="${1::-1}" |
| 43 | + CMD="$2" |
| 44 | + |
| 45 | + # run command |
| 46 | + if [ -f "${CURR_TEST}/${FILE_IN}" ]; then |
| 47 | + ${CMD} <"${CURR_TEST}/${FILE_IN}" >"${CURR_TEST}/${FILE_OUT}" 2>"${CURR_TEST}/${FILE_STDERR}" |
| 48 | + else |
| 49 | + ${CMD} >"${CURR_TEST}/${FILE_OUT}" 2>"${CURR_TEST}/${FILE_STDERR}" |
| 50 | + fi |
| 51 | + |
| 52 | + ERR_CODE="${?}" |
| 53 | + DIFF="$(diff "${CURR_TEST}"/${FILE_REF} "${CURR_TEST}"/${FILE_OUT})" |
| 54 | + |
| 55 | + if [ -z "${DIFF}" ] && [ "${ERR_CODE}" -eq 0 ]; then |
| 56 | + print_ok_color "[PASSED] ${CURR_TEST}" |
| 57 | + else |
| 58 | + FAIL_COUNT=$((FAIL_COUNT+1)) |
| 59 | + |
| 60 | + print_fail_color "[FAILED] ${CURR_TEST}" |
| 61 | + |
| 62 | + # print command |
| 63 | + echo "${CMD} ${CURR_TEST}/${FILE_IN}" |
| 64 | + |
| 65 | + # print diff |
| 66 | + # TODO diff -U0 --label="" --label="" test/002-fail/ref test/002-fail/out |
| 67 | + # show only differences |
| 68 | + echo "${DIFF}" |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +expect_err() { |
| 73 | + CURR_TEST="${1::-1}" |
| 74 | + CMD="$2" |
| 75 | + ERR_EXPECT="$3" |
| 76 | + |
| 77 | + # run command |
| 78 | + if [ -f "${CURR_TEST}/${FILE_IN}" ]; then |
| 79 | + ${CMD} <"${CURR_TEST}/${FILE_IN}" >"${CURR_TEST}/${FILE_OUT}" 2>"${CURR_TEST}/${FILE_STDERR}" |
| 80 | + else |
| 81 | + ${CMD} >"${CURR_TEST}/${FILE_OUT}" 2>"${CURR_TEST}/${FILE_STDERR}" |
| 82 | + fi |
| 83 | + |
| 84 | + ERR_CODE="${?}" |
| 85 | + |
| 86 | + if [ "${ERR_CODE}" -eq "${ERR_EXPECT}" ]; then |
| 87 | + print_ok_color "[PASSED] ${CURR_TEST}" |
| 88 | + else |
| 89 | + FAIL_COUNT=$(${FAIL_COUNT} + 1) |
| 90 | + |
| 91 | + print_fail_color "[FAILED] ${CURR_TEST}" |
| 92 | + |
| 93 | + echo "expected err: ${ERR_EXPECT}" |
| 94 | + echo "got: ${ERR_CODE}" |
| 95 | + |
| 96 | + # print command |
| 97 | + echo "${CMD} ${CURR_TEST}/${FILE_IN}" |
| 98 | + |
| 99 | + cat "${CURR_TEST}/${FILE_OUT}" |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +test() { |
| 104 | + DIR="$1" |
| 105 | + DEF_CMD="$2" |
| 106 | + |
| 107 | + for d in "${DIR}"/*/ ; do |
| 108 | + |
| 109 | + # if error file exists and is other than 0 we should test for expected error |
| 110 | + EXPECTED_ERR=0 |
| 111 | + if [ -f "${d}${FILE_ERR}" ]; then |
| 112 | + EXPECTED_ERR=$(cat "${d}${FILE_ERR}") |
| 113 | + fi |
| 114 | + |
| 115 | + # if file with alternative command exists then we should use that |
| 116 | + CMD="${DEF_CMD}" |
| 117 | + if [ -f "${d}${FILE_CMD}" ]; then |
| 118 | + CMD=$(cat "${d}${FILE_CMD}") |
| 119 | + fi |
| 120 | + |
| 121 | + if [ "${EXPECTED_ERR}" -ne 0 ]; then |
| 122 | + expect_err "${d}" "${CMD}" "${EXPECTED_ERR}" |
| 123 | + else |
| 124 | + expect_ok "${d}" "${CMD}" |
| 125 | + fi |
| 126 | + |
| 127 | + done |
| 128 | +} |
| 129 | + |
| 130 | +print_final_score() { |
| 131 | + if [ "${FAIL_COUNT}" -eq 0 ]; then |
| 132 | + print_ok_color "ALL TESTS PASSED" |
| 133 | + else |
| 134 | + print_fail_color "${FAIL_COUNT} TESTS FAILED" |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | +###################### argument parsing ######################### |
| 139 | +while getopts "ce:d:" opt; do |
| 140 | + case ${opt} in |
| 141 | + c ) |
| 142 | + ACTION="CLEAN" |
| 143 | + ;; |
| 144 | + e ) |
| 145 | + DEFAULT_CMD=$OPTARG |
| 146 | + ;; |
| 147 | + d ) |
| 148 | + TEST_DIR=$OPTARG |
| 149 | + ;; |
| 150 | + \? ) |
| 151 | + echo "Usage: stest.sh [-c] [-e \"default_command\"] [-d \"directory\"]" |
| 152 | + ;; |
| 153 | + esac |
| 154 | +done |
| 155 | +shift $((OPTIND -1)) |
| 156 | + |
| 157 | +if [ -z "${TEST_DIR}" ]; then |
| 158 | + echo "Usage: stest.sh [-c] [-e \"default_command\"] [-d \"directory\"]" |
| 159 | + exit |
| 160 | +fi |
| 161 | + |
| 162 | +############################ MAIN ################################ |
| 163 | +if [ "${ACTION}" = "CLEAN" ]; then |
| 164 | + clean "${TEST_DIR}" |
| 165 | + exit |
| 166 | +else |
| 167 | + test "${TEST_DIR}" "${DEFAULT_CMD}" |
| 168 | + print_final_score |
| 169 | +fi |
0 commit comments