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
|
#!/bin/sh
# Doctest-alike IRAF test script. This will take text files with
# MarkDown syntax, extract all code lines (enclosed with ```), execute
# the lines that start with cl> or similar and compare the other lines
# with the output of the IRAF shells (cl, ecl, vocl).
#
# Copyright: 2017 Ole Streicher
# License: as IRAF
#
if [ -z "$iraf" ] ; then
export iraf=$(cd "$(dirname "$0")/.." && pwd)/
fi
if [ -z "$IRAFARCH" ] ; then
export IRAFARCH=$(${iraf}unix/hlib/irafarch.sh -current)
fi
export arch=.${IRAFARCH}
export bin=${iraf}bin${arch}/
if [ ! -d $bin -a -d ${iraf}bin ] ; then
bin=${iraf}bin/
unset arch IRAFARCH
fi
export XC_CFLAGS="${XC_CFLAGS} -w"
TEST_FILES=""
IRAF_SHELLS=""
verbose=0
while getopts "h?vc:" opt; do
case "$opt" in
h|\?)
echo 'Run the IRAF test suite. See iraf$test/README.md for details.'
echo
echo 'Usage:'
echo ' run_tests [-h] [-v] [-c CL.e]... [TEST.md]...'
echo
echo 'Arguments:'
echo ' -h this help'
echo ' -v verbose output'
echo ' -c CL.e use CL.e as IRAF shell (may be given several times)'
echo ' TEST.md run test script TEST.md (may be given several times)'
echo
echo 'If no IRAF shell is specified, ecl.e is used.'
echo 'If no test scripts are specified, all test scripts under iraf$test'
echo 'are executed.'
exit 0
;;
v) verbose=1
;;
c) IRAF_SHELLS="$IRAF_SHELLS $OPTARG"
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# IRAF shells to be tested
if [ -z "$IRAF_SHELLS" ] ; then
IRAF_SHELLS="ecl.e"
fi
# Test scripts
for f in $@ ; do
if [ "$(echo "$f" | cut -c1)" != "/" ] ; then
f=$(pwd)/$f
fi
TEST_FILES="$TEST_FILES $f"
done
if [ -z "$TEST_FILES" ] ; then
TEST_FILES="${iraf}test/"*.md
fi
base_dir=$(mktemp -d -t iraftest.XXXXXXXXX)
failure_file=${base_dir}/failures.txt
summary_file=${base_dir}/summary.txt
# Run one test. If a failure happens, the full message will be appended to
# the $failure_file.
#
# Parameters:
#
# - Executable of IRAF shell
# - Commands to be executed
# - Expected result string
# - Test script reference (file name and line number)
#
run_test () {
local CL="$1"
local command="$2"
local result="$3"
local src="$4"
local options="$5"
local skip=$(echo "$options" | tr \ \\n | grep ^skip)
local xfail=$(echo "$options" | tr \ \\n | grep ^xfail)
local archs=$(echo "$options" | tr \ \\n | grep ^arch= | cut -d= -f2)
local decimals=$(echo "$options" | tr \ \\n | grep ^decimals= | cut -d= -f2)
if [ "$skip" ] ; then
echo "skipped" >> "$summary_file"
printf "s"
return
fi
if [ "$archs" ] && [ -z "$(echo "$archs" | tr , \\n | grep ^"$IRAFARCH"\$)" ] ; then
echo "skipped" >> "$summary_file"
printf "a"
return
fi
local cmd_file="${base_dir}/cmd"
printf "%b\\nlogout\\n" "$command" > "$cmd_file"
res_file="${base_dir}/expected"
printf "%b\\n" "$result" > "$res_file" 2>&1
clres_file="${base_dir}/output"
"${CL}" -f "$cmd_file" > "$clres_file" 2>&1
diff_file="${base_dir}/diff"
diff_floatingpoint "$res_file" "$clres_file" "$decimals" > "$diff_file"
if [ -s "$diff_file" ] ; then
if [ "$xfail" ] ; then
printf "x"
echo "xfailed" >> "$summary_file"
return
else
cat >> "$failure_file" <<EOF
=================== Failure in $src with $(basename "$CL") ===================
Expected
========
$(cat "$res_file")
Output
======
$(cat "$clres_file")
Diff
====
$(tail -n +3 "$diff_file")
EOF
echo "failed" >> "$summary_file"
printf "F"
return
fi
else
echo "passed" >> "$summary_file"
printf "."
return
fi
rm -f "$cmd_file" "$res_file" "$clres_file" "$diff_file"
}
# Diff two files, with an optionally given floating point precision
# (in relevant decimals)
#
# Parameters:
#
# - first file
# - second file
# - relevant decimals (optional)
#
diff_floatingpoint () {
local file1="$1"
local file2="$2"
local decimals="$3"
if [ -z "$decimals" ] ; then
diff -uw "$file1" "$file2"
else
perl -pe 's/(-?\d+\.\d*)(E-?\d+)?/sprintf("%.'"${decimals}"'f%s", $1, $2)/ge;s/\-(0+\.0*)/$1/ge' < "$file1" > "$file1".fp
perl -pe 's/(-?\d+\.\d*)(E-?\d+)?/sprintf("%.'"${decimals}"'f%s", $1, $2)/ge;s/\-(0+\.0*)/$1/ge' < "$file2" > "$file2".fp
diff -uw "$file1".fp "$file2".fp
rm -f "$file1".fp "$file2".fp
fi
}
# Run all tests for a given IRAF shell and test script
#
# Parameters:
#
# - Executable of IRAF shell
# - File name for test script
#
run_tests () {
local CL="$1"
FILE="$2"
in_code=0 # detect whether we are in a code section
command='' # collected commands
result='' # collected expected results
filename='' # File name to read in
options='' # Test options
lineno=0 # line counter
cat "$FILE" | while IFS= read -r line ; do
lineno=$(( lineno + 1 ))
if [ "$in_code" = 1 ] ; then
if echo "$line" | grep -q '^```$' ; then
# we are at the end of a code section,
# so let's execute the test if there was one
if [ "$command" ] ; then
if [ "$verbose" = 1 ] && [ -n "$headline" ] ; then
printf "\\n %s: " "$headline"
headline=""
fi
run_test "$CL" "$command" "$result" "$(basename "${FILE}"):${l_start}" "$options"
fi
command=''
result=''
filename=''
options=''
in_code=0
elif [ "$filename" ] ; then
# We are in a code line that is to be stored in a file
printf "%s\\n" "$line" >> "$filename"
elif echo "$line" | grep -q -E '^[a-z>]+> ' ; then
# We are in a code line that contains a command.
# First add a command to print the full line so that
# it appears in the output. Then add the command itself
# And add the command to to the expected results.
cmd=$(echo "$line" | sed 's/^[a-z>]*> //')
if [ "$command" ] ; then
command="${command}\\nprint '${line}'"
result="${result}\\n${line}"
else
command="print '${line}'"
result="${line}"
fi
command="${command}\\n${cmd}"
else
# We are in a code line that contains the response from IRAF.
# Just add it to the expected results.
result="${result}\\n${line}"
fi
elif echo "$line" | grep -q '^```$' ; then
# A new code section started. Store the next line number for
# reference
l_start=$(( lineno + 1 ))
in_code=1
elif echo "$line" | grep -q -E '^File: `.+`$' ; then
# There is some file content printed out in the next block.
# Just prepare to save it.
filename="$(echo "$line" | cut '-d`' -f2)"
elif echo "$line" | grep -q -E '^Test options: `.+`$' ; then
# Special test options
options="$(echo "$line" | cut '-d`' -f2)"
elif echo "$line" | grep -q -E '^#' ; then
# Head line
headline="$(echo "$line" | sed s/^#*\ //)"
else
filename=""
options=""
fi
done
printf "\\n"
}
for CL in $IRAF_SHELLS ; do
cl="${bin}${CL}"
if [ -x "${cl}" ] ; then
for t in $TEST_FILES ; do
test_dir="${base_dir}/testroot"
export uparm="${test_dir}/.uparm/"
export cache="${test_dir}/.cache/"
export imdir="${test_dir}/.imdir/"
mkdir -p "${test_dir}" "${uparm}" "${cache}" "${imdir}"
printf "%b: %b " "${CL}" "$(basename "${t}")"
cd "$test_dir" && run_tests "${cl}" "${t}"
rm -rf "${test_dir}"
done
else
echo "IRAF shell ${cl} not found"
stat "${cl}" >> "$failure_file" 2>&1
fi
done
if [ -s "$summary_file" ] ; then
printf "Test summary: %s\\n" "$(sort "$summary_file" | uniq -c | sed ':a;N;$!ba;s/\\n */, /g' 2> /dev/null)"
fi
if [ -s "$failure_file" ] ; then
cat "$failure_file"
rm -rf "${base_dir}"
exit 1
fi
rm -rf "${base_dir}"
exit 0
|