1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
#!/usr/bin/env bash
#
# boxes - Command line filter to draw/remove ASCII boxes around text
# Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
# License, version 3, as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
#____________________________________________________________________________________________________________________
#
# Test runner for the black-box tests.
#____________________________________________________________________________________________________________________
set -uo pipefail
# Global constants
declare -r SRC_DIR=../src
declare -r OUT_DIR=../out
declare -r BASELINE_FILE=${OUT_DIR}/lcov-baseline.info
declare -r COVERAGE_FILE=${OUT_DIR}/lcov-total.info
# Command Line Options
declare opt_coverage=false
declare opt_coverage_per_test=false
declare opt_suite=false
declare opt_testCase=""
function print_usage()
{
echo 'Usage: testrunner.sh [--coverage] [--coverage-per-test] {--suite | <opt_testCase>}'
echo ' Returns 0 for success, else non-zero'
}
function parse_arguments()
{
if [ $# -eq 0 ]; then
print_usage
exit 2
fi
for i in "$@"; do
case ${i} in
--coverage)
opt_coverage=true
shift
;;
--coverage-per-test)
opt_coverage_per_test=true
opt_coverage=true
;;
--suite)
opt_suite=true
shift
;;
-h | --help)
print_usage
exit 0
;;
*)
if [ -z "${opt_testCase}" ]; then
opt_testCase=${i}
else
print_usage
exit 2
fi
;;
esac
done
if [[ ${opt_testCase} == "" && ${opt_suite} != "true" ]]; then
print_usage
exit 2
fi
}
function check_prereqs()
{
if [ "${PWD##*/}" != "test" ]; then
>&2 echo "Please run this script from the test folder."
exit 2
fi
if [ ! -d ${OUT_DIR} ]; then
>&2 echo "Please run 'make' from the project root to build an executable before running tests."
exit 2
fi
if [[ ${opt_coverage} == true && $(find ${OUT_DIR} -maxdepth 1 -name '*.gcno' 2>/dev/null | wc -l) -lt 1 ]]; then
>&2 echo "Binaries not instrumented. Run 'make cov' from the project root."
exit 5
fi
if [ ! -f ${opt_testCase} ]; then
>&2 echo "Test Case '${opt_testCase}' not found."
exit 3
fi
}
function cov_baseline()
{
if [ ${opt_coverage} == true ]; then
if [ ${opt_suite} == true ]; then
rm -f ${BASELINE_FILE}
fi
if [ ! -f ${BASELINE_FILE} ]; then
echo "Creating coverage baseline ..."
lcov --capture --initial --no-recursion --directory ${OUT_DIR} --base-directory ${SRC_DIR} \
--exclude '*/lex.yy.c' --exclude '*/parser.c' --output-file ${BASELINE_FILE}
echo -e "Coverage baseline created in ${BASELINE_FILE}\n"
fi
fi
}
function cov_args()
{
if [ ${opt_coverage_per_test} == true ]; then
echo '--coverage-per-test'
elif [ ${opt_coverage} == true ]; then
echo '--coverage'
fi
}
function execute_suite()
{
local countExecuted=0
local countFailed=0
local tc
# Note we are getting an encoding error with test 111 which is
# unique and runs under ISO_8859-15. But this only happens on macOS.
# So, if we run test 111 on macOS, we should run with LC_ALL=C
for tc in *.txt; do
if [[ $(uname) == "Darwin" ]] && [[ ${tc} == "111"* ]]; then
LC_ALL=C $0 "$(cov_args)" "${tc}"
else
$0 "$(cov_args)" "${tc}"
fi
if [ $? -ne 0 ]; then
overallResult=1
countFailed=$((countFailed + 1))
fi
countExecuted=$((countExecuted + 1))
done
echo "${countExecuted} tests executed, $((countExecuted - countFailed)) successful, ${countFailed} failed."
}
function measure_coverage()
{
if [ ${opt_coverage} == true ]; then
local testResultsDir=${OUT_DIR}/test-results/${tcBaseName}
mkdir -p "${testResultsDir}"
cp ${OUT_DIR}/*.gc* "${testResultsDir}"
lcov --capture --directory "${testResultsDir}" --base-directory ${SRC_DIR} --test-name "${tcBaseName}" --quiet \
--exclude '*/lex.yy.c' --exclude '*/parser.c' --rc "${branchCoverage}=1" \
--output-file "${testResultsDir}/coverage.info"
echo -n " Coverage: "
lcov --summary "${testResultsDir}/coverage.info" 2>&1 | grep 'lines...' | grep -oP '\d+\.\d*%'
fi
}
function consolidate_coverage()
{
if [[ ${opt_coverage} == true ]]; then
echo -e "\nConsolidating test coverage ..."
pushd ${OUT_DIR}/test-results || exit 1
find . -name "*.info" | xargs printf -- '--add-tracefile %s\n' | xargs --exit \
lcov --rc "${branchCoverage}=1" --exclude '*/lex.yy.c' --exclude '*/parser.c' \
--output-file ../${COVERAGE_FILE} --add-tracefile ../${BASELINE_FILE}
popd || exit 1
echo ""
fi
}
function report_coverage()
{
local testReportDir=${OUT_DIR}/report
mkdir -p ${testReportDir}
genhtml --title "Boxes / All Tests" --branch-coverage --legend \
--output-directory ${testReportDir} ${COVERAGE_FILE}
echo -e "\nTest coverage report available at ${testReportDir}/index.html"
}
function clear_gcda_traces()
{
if [ ${opt_coverage} == true ]; then
rm -f ${OUT_DIR}/*.gcda
fi
}
function check_mandatory_sections()
{
local sectionName
for sectionName in :ARGS :INPUT :OUTPUT-FILTER :EXPECTED :EOF; do
if [ $(grep -c ^$sectionName $opt_testCase) -ne 1 ]; then
>&2 echo "Missing section $sectionName in test case '$opt_testCase'."
exit 4
fi
done
}
function arrange_environment()
{
local boxesEnv=""
if [[ $(grep -c "^:ENV" "${opt_testCase}") -eq 1 ]]; then
boxesEnv=$(sed -n '/^:ENV/,/^:ARGS/p;' < "${opt_testCase}" | sed '1d;$d' | tr -d '\r')
fi
if [ -n "$boxesEnv" ]; then
echo "$boxesEnv" | sed -e 's/export/\n export/g' | sed '1d'
unset BOXES
eval "$boxesEnv"
else
export BOXES=../boxes-config
fi
}
function arrange_test_fixtures()
{
if [ $(grep -c "^:EXPECTED-ERROR " ${opt_testCase}) -eq 1 ]; then
expectedReturnCode=$(grep "^:EXPECTED-ERROR " "${opt_testCase}" | sed -e 's/:EXPECTED-ERROR //')
fi
if [ $(grep -c "^:EXPECTED discard-stderr" ${opt_testCase}) -eq 1 ]; then
discardStderr=true
fi
cat "${opt_testCase}" | sed -n '/^:INPUT/,/^:OUTPUT-FILTER/p;' | sed '1d;$d' | tr -d '\r' > "${testInputFile}"
cat "${opt_testCase}" | sed -n '/^:OUTPUT-FILTER/,/^:EXPECTED\b.*$/p;' | sed '1d;$d' | tr -d '\r' > "${testFilterFile}"
cat "${opt_testCase}" | sed -n '/^:EXPECTED/,/^:EOF/p;' | sed '1d;$d' | tr -d '\r' > "${testExpectationFile}"
}
function run_boxes()
{
local boxesBinary=${OUT_DIR}/boxes.exe
if [ ! -x $boxesBinary ]; then
boxesBinary=${OUT_DIR}/boxes
fi
echo " Invoking: $(basename $boxesBinary) $boxesArgs"
if [ -z "${BOXES_TEST_XXD:-}" ]; then
if [ ${discardStderr} == true ]; then
eval "$boxesBinary $boxesArgs" < "$testInputFile" > "$testOutputFile" 2> "$testErrorFile"
else
eval "$boxesBinary $boxesArgs" < "$testInputFile" > "$testOutputFile" 2>&1
fi
else
if [ ${discardStderr} == true ]; then
eval "$boxesBinary $boxesArgs" < "$testInputFile" | xxd > "$testOutputFile" 2> "$testErrorFile"
else
eval "$boxesBinary $boxesArgs" < "$testInputFile" | xxd > "$testOutputFile" 2>&1
fi
fi
actualReturnCode=$?
}
function assert_outcome()
{
tr -d '\r' < "${testOutputFile}" | sed -E -f "${testFilterFile}" | diff - "${testExpectationFile}"
if [ $? -ne 0 ]; then
>&2 echo "Error in test case: ${opt_testCase} (top: actual; bottom: expected)"
exit 5
fi
if [ ${actualReturnCode} -ne "${expectedReturnCode}" ]; then
>&2 echo -n "Error in test case: ${opt_testCase}"
>&2 echo " (error code was ${actualReturnCode}, but expected ${expectedReturnCode})"
exit 5
fi
}
parse_arguments "$@"
check_prereqs
cov_baseline
declare tcBaseName=${opt_testCase%.txt}
declare branchCoverage=lcov_branch_coverage
if [[ $(uname) == "Darwin" ]]; then
branchCoverage=branch_coverage
fi
# Execute the entire test suite
if [ ${opt_suite} == true ]; then
declare -i overallResult=0
clear_gcda_traces
execute_suite
if [ ${opt_coverage_per_test} == false ]; then
tcBaseName=black-box-all
measure_coverage
fi
if [ ${opt_coverage} == true ]; then
consolidate_coverage
report_coverage
fi
exit ${overallResult}
fi
# Execute only a single test
echo "Running test case: ${opt_testCase}"
if [ ${opt_coverage_per_test} == true ]; then
clear_gcda_traces
fi
check_mandatory_sections
declare -i expectedReturnCode=0
declare -r testInputFile=${opt_testCase/%.txt/.input.tmp}
declare -r testExpectationFile=${opt_testCase/%.txt/.expected.tmp}
declare -r testFilterFile=${opt_testCase/%.txt/.sed.tmp}
declare -r testOutputFile=${opt_testCase/%.txt/.out.tmp}
declare -r testErrorFile=${opt_testCase/%.txt/.err.tmp}
declare -r boxesArgs=$(sed -n '/^:ARGS/,+1p' < "${opt_testCase}" | grep -v ^:INPUT | sed '1d' | tr -d '\r')
declare discardStderr=false
arrange_environment
arrange_test_fixtures
declare -i actualReturnCode=100
run_boxes
if [ ${opt_coverage_per_test} == true ]; then
measure_coverage
fi
assert_outcome
rm "${testInputFile}"
rm "${testFilterFile}"
rm "${testExpectationFile}"
rm "${testOutputFile}"
rm -f "${testErrorFile}"
echo " OK"
exit 0
|