2014-09-23 23:50:36 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2021-02-10 22:14:09 +01:00
|
|
|
# boxes - Command line filter to draw/remove ASCII boxes around text
|
2023-03-26 21:32:08 +02:00
|
|
|
# Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
|
2021-02-10 22:14:09 +01:00
|
|
|
#
|
2022-09-18 14:56:30 +02:00
|
|
|
# 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.
|
2021-02-10 22:14:09 +01:00
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
|
2022-09-18 14:56:30 +02:00
|
|
|
# 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/>.
|
|
|
|
#____________________________________________________________________________________________________________________
|
2014-09-23 23:50:36 +02:00
|
|
|
#
|
2021-09-09 21:44:49 +02:00
|
|
|
# Test runner for the black-box tests.
|
2022-09-18 14:56:30 +02:00
|
|
|
#____________________________________________________________________________________________________________________
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
set -uo pipefail
|
|
|
|
|
|
|
|
# Global constants
|
|
|
|
declare -r SRC_DIR=../src
|
|
|
|
declare -r OUT_DIR=../out
|
|
|
|
declare -r BASELINE_FILE=${OUT_DIR}/lcov-baseline.info
|
2021-10-20 22:28:34 +02:00
|
|
|
declare -r COVERAGE_FILE=${OUT_DIR}/lcov-total.info
|
2021-09-10 21:26:47 +02:00
|
|
|
|
|
|
|
# Command Line Options
|
|
|
|
declare opt_coverage=false
|
2023-12-24 14:00:46 +01:00
|
|
|
declare opt_coverage_per_test=false
|
2021-09-10 21:26:47 +02:00
|
|
|
declare opt_suite=false
|
|
|
|
declare opt_testCase=""
|
|
|
|
|
|
|
|
|
|
|
|
function print_usage()
|
|
|
|
{
|
2023-12-24 14:00:46 +01:00
|
|
|
echo 'Usage: testrunner.sh [--coverage] [--coverage-per-test] {--suite | <opt_testCase>}'
|
2014-09-23 23:50:36 +02:00
|
|
|
echo ' Returns 0 for success, else non-zero'
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function parse_arguments()
|
|
|
|
{
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
print_usage
|
2021-09-09 21:44:49 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
|
|
|
|
for i in "$@"; do
|
|
|
|
case ${i} in
|
|
|
|
--coverage)
|
|
|
|
opt_coverage=true
|
|
|
|
shift
|
|
|
|
;;
|
2023-12-24 14:00:46 +01:00
|
|
|
--coverage-per-test)
|
|
|
|
opt_coverage_per_test=true
|
|
|
|
opt_coverage=true
|
|
|
|
;;
|
2021-09-10 21:26:47 +02:00
|
|
|
--suite)
|
|
|
|
opt_suite=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-h | --help)
|
|
|
|
print_usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
2022-10-25 22:10:39 +02:00
|
|
|
if [ -z "${opt_testCase}" ]; then
|
2021-09-10 21:26:47 +02:00
|
|
|
opt_testCase=${i}
|
|
|
|
else
|
|
|
|
print_usage
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ${opt_testCase} == "" && ${opt_suite} != "true" ]]; then
|
|
|
|
print_usage
|
2021-09-09 21:44:49 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
2021-09-09 21:44:49 +02:00
|
|
|
|
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
function check_prereqs()
|
|
|
|
{
|
2022-10-25 22:10:39 +02:00
|
|
|
if [ "${PWD##*/}" != "test" ]; then
|
2021-09-10 21:26:47 +02:00
|
|
|
>&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
|
2022-10-25 22:10:39 +02:00
|
|
|
if [[ ${opt_coverage} == true && $(find ${OUT_DIR} -maxdepth 1 -name '*.gcno' 2>/dev/null | wc -l) -lt 1 ]]; then
|
2021-09-09 21:44:49 +02:00
|
|
|
>&2 echo "Binaries not instrumented. Run 'make cov' from the project root."
|
|
|
|
exit 5
|
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
if [ ! -f ${opt_testCase} ]; then
|
|
|
|
>&2 echo "Test Case '${opt_testCase}' not found."
|
|
|
|
exit 3
|
2021-09-09 21:44:49 +02:00
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-09-09 21:44:49 +02:00
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
|
2023-12-24 14:00:46 +01:00
|
|
|
function cov_args()
|
|
|
|
{
|
|
|
|
if [ ${opt_coverage_per_test} == true ]; then
|
|
|
|
echo '--coverage-per-test'
|
|
|
|
elif [ ${opt_coverage} == true ]; then
|
|
|
|
echo '--coverage'
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
function execute_suite()
|
|
|
|
{
|
|
|
|
local countExecuted=0
|
|
|
|
local countFailed=0
|
|
|
|
local tc
|
2023-11-25 20:48:22 +01:00
|
|
|
|
|
|
|
# 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
|
2014-09-23 23:50:36 +02:00
|
|
|
for tc in *.txt; do
|
2023-12-24 14:00:46 +01:00
|
|
|
if [[ $(uname) == "Darwin" ]] && [[ ${tc} == "111"* ]]; then
|
|
|
|
LC_ALL=C $0 "$(cov_args)" "${tc}"
|
2021-09-09 21:44:49 +02:00
|
|
|
else
|
2023-12-24 14:00:46 +01:00
|
|
|
$0 "$(cov_args)" "${tc}"
|
2021-09-09 21:44:49 +02:00
|
|
|
fi
|
2014-09-23 23:50:36 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
overallResult=1
|
2021-09-10 21:26:47 +02:00
|
|
|
countFailed=$((countFailed + 1))
|
2014-09-23 23:50:36 +02:00
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
countExecuted=$((countExecuted + 1))
|
2014-09-23 23:50:36 +02:00
|
|
|
done
|
2022-10-25 22:10:39 +02:00
|
|
|
echo "${countExecuted} tests executed, $((countExecuted - countFailed)) successful, ${countFailed} failed."
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
2014-09-23 23:50:36 +02:00
|
|
|
|
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
function measure_coverage()
|
|
|
|
{
|
|
|
|
if [ ${opt_coverage} == true ]; then
|
2023-12-24 14:00:46 +01:00
|
|
|
local testResultsDir=${OUT_DIR}/test-results/${tcBaseName}
|
2022-10-25 22:10:39 +02:00
|
|
|
mkdir -p "${testResultsDir}"
|
|
|
|
cp ${OUT_DIR}/*.gc* "${testResultsDir}"
|
|
|
|
lcov --capture --directory "${testResultsDir}" --base-directory ${SRC_DIR} --test-name "${tcBaseName}" --quiet \
|
2023-12-02 14:36:19 +01:00
|
|
|
--exclude '*/lex.yy.c' --exclude '*/parser.c' --rc "${branchCoverage}=1" \
|
2022-10-25 22:10:39 +02:00
|
|
|
--output-file "${testResultsDir}/coverage.info"
|
2021-09-10 21:26:47 +02:00
|
|
|
echo -n " Coverage: "
|
2022-10-25 22:10:39 +02:00
|
|
|
lcov --summary "${testResultsDir}/coverage.info" 2>&1 | grep 'lines...' | grep -oP '\d+\.\d*%'
|
2021-09-10 21:26:47 +02:00
|
|
|
fi
|
|
|
|
}
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2023-12-02 14:36:19 +01:00
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
function consolidate_coverage()
|
|
|
|
{
|
2023-12-24 14:00:46 +01:00
|
|
|
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
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function report_coverage()
|
|
|
|
{
|
2021-10-20 22:28:34 +02:00
|
|
|
local testReportDir=${OUT_DIR}/report
|
2021-09-10 21:26:47 +02:00
|
|
|
mkdir -p ${testReportDir}
|
2021-10-20 22:28:34 +02:00
|
|
|
genhtml --title "Boxes / All Tests" --branch-coverage --legend \
|
2021-09-10 21:26:47 +02:00
|
|
|
--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
|
2014-09-23 23:50:36 +02:00
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2021-02-22 22:21:18 +01:00
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
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
|
|
|
|
}
|
2014-09-23 23:50:36 +02:00
|
|
|
|
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
function arrange_environment()
|
|
|
|
{
|
|
|
|
local boxesEnv=""
|
2023-12-24 14:00:46 +01:00
|
|
|
if [[ $(grep -c "^:ENV" "${opt_testCase}") -eq 1 ]]; then
|
|
|
|
boxesEnv=$(sed -n '/^:ENV/,/^:ARGS/p;' < "${opt_testCase}" | sed '1d;$d' | tr -d '\r')
|
2021-09-10 21:26:47 +02:00
|
|
|
fi
|
2022-10-25 22:10:39 +02:00
|
|
|
if [ -n "$boxesEnv" ]; then
|
|
|
|
echo "$boxesEnv" | sed -e 's/export/\n export/g' | sed '1d'
|
2021-09-10 21:26:47 +02:00
|
|
|
unset BOXES
|
2022-10-25 22:10:39 +02:00
|
|
|
eval "$boxesEnv"
|
2021-09-10 21:26:47 +02:00
|
|
|
else
|
|
|
|
export BOXES=../boxes-config
|
|
|
|
fi
|
|
|
|
}
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2014-10-09 21:28:14 +02:00
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
function arrange_test_fixtures()
|
|
|
|
{
|
|
|
|
if [ $(grep -c "^:EXPECTED-ERROR " ${opt_testCase}) -eq 1 ]; then
|
2023-12-24 14:00:46 +01:00
|
|
|
expectedReturnCode=$(grep "^:EXPECTED-ERROR " "${opt_testCase}" | sed -e 's/:EXPECTED-ERROR //')
|
2021-09-10 21:26:47 +02:00
|
|
|
fi
|
2014-09-23 23:50:36 +02:00
|
|
|
|
2023-12-24 14:00:46 +01:00
|
|
|
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}"
|
2021-09-10 21:26:47 +02:00
|
|
|
}
|
2021-09-09 21:44:49 +02:00
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
|
|
|
|
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
|
2022-10-25 22:10:39 +02:00
|
|
|
eval "$boxesBinary $boxesArgs" < "$testInputFile" > "$testOutputFile" 2>&1
|
2021-09-10 21:26:47 +02:00
|
|
|
else
|
2022-10-25 22:10:39 +02:00
|
|
|
eval "$boxesBinary $boxesArgs" < "$testInputFile" | xxd > "$testOutputFile" 2>&1
|
2021-09-10 21:26:47 +02:00
|
|
|
fi
|
|
|
|
actualReturnCode=$?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function assert_outcome()
|
|
|
|
{
|
2022-10-25 22:10:39 +02:00
|
|
|
tr -d '\r' < "${testOutputFile}" | sed -E -f "${testFilterFile}" | diff - "${testExpectationFile}"
|
2021-09-10 21:26:47 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
>&2 echo "Error in test case: ${opt_testCase} (top: actual; bottom: expected)"
|
|
|
|
exit 5
|
|
|
|
fi
|
2022-10-25 22:10:39 +02:00
|
|
|
if [ ${actualReturnCode} -ne "${expectedReturnCode}" ]; then
|
2021-09-10 21:26:47 +02:00
|
|
|
>&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
|
|
|
|
|
2023-12-24 14:00:46 +01:00
|
|
|
declare tcBaseName=${opt_testCase%.txt}
|
2023-12-02 14:36:19 +01:00
|
|
|
declare branchCoverage=lcov_branch_coverage
|
|
|
|
if [[ $(uname) == "Darwin" ]]; then
|
|
|
|
branchCoverage=branch_coverage
|
|
|
|
fi
|
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
# Execute the entire test suite
|
|
|
|
if [ ${opt_suite} == true ]; then
|
|
|
|
declare -i overallResult=0
|
2023-12-24 14:00:46 +01:00
|
|
|
clear_gcda_traces
|
2021-09-10 21:26:47 +02:00
|
|
|
execute_suite
|
|
|
|
|
2023-12-24 14:00:46 +01:00
|
|
|
if [ ${opt_coverage_per_test} == false ]; then
|
|
|
|
tcBaseName=black-box-all
|
|
|
|
measure_coverage
|
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
if [ ${opt_coverage} == true ]; then
|
|
|
|
consolidate_coverage
|
|
|
|
report_coverage
|
|
|
|
fi
|
|
|
|
exit ${overallResult}
|
2014-09-23 23:50:36 +02:00
|
|
|
fi
|
|
|
|
|
2021-09-10 21:26:47 +02:00
|
|
|
# Execute only a single test
|
|
|
|
echo "Running test case: ${opt_testCase}"
|
2023-12-24 14:00:46 +01:00
|
|
|
if [ ${opt_coverage_per_test} == true ]; then
|
|
|
|
clear_gcda_traces
|
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
|
|
|
|
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}
|
2023-12-24 14:00:46 +01:00
|
|
|
declare -r boxesArgs=$(sed -n '/^:ARGS/,+1p' < "${opt_testCase}" | grep -v ^:INPUT | sed '1d' | tr -d '\r')
|
2021-09-10 21:26:47 +02:00
|
|
|
|
|
|
|
arrange_environment
|
|
|
|
arrange_test_fixtures
|
|
|
|
|
|
|
|
declare -i actualReturnCode=100
|
|
|
|
run_boxes
|
|
|
|
|
2023-12-24 14:00:46 +01:00
|
|
|
if [ ${opt_coverage_per_test} == true ]; then
|
|
|
|
measure_coverage
|
|
|
|
fi
|
2021-09-10 21:26:47 +02:00
|
|
|
assert_outcome
|
|
|
|
|
2023-12-24 14:00:46 +01:00
|
|
|
rm "${testInputFile}"
|
|
|
|
rm "${testFilterFile}"
|
|
|
|
rm "${testExpectationFile}"
|
|
|
|
rm "${testOutputFile}"
|
2014-09-23 23:50:36 +02:00
|
|
|
|
|
|
|
echo " OK"
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
#EOF
|