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
|
#!/bin/bash
#
# $Id: gmtest.in 15178 2015-11-06 10:45:03Z fwobbe $
#
# Functions to be used with test scripts
test -z "$1" && exit 1
# Where the current script resides (need absolute path)
script_name="$1"
script="@GMT_SOURCE_DIR@/doc/scripts/${script_name}"
src="@GMT_SOURCE_DIR@/doc/scripts"
tut="@GMT_SOURCE_DIR@/doc/tutorial"
if ! [ -x "${script}" ]; then
echo "error: cannot execute script ${script}." >&2
exit 1
fi
shift
# choose awk
if type gawk >/dev/null 2>&1 ; then
export AWK=gawk
elif type nawk >/dev/null 2>&1 ; then
export AWK=nawk
else
export AWK=awk
fi
# Temporary change LANG to C
LANG=C
# Additional variables needed in gmtest
GRAPHICSMAGICK="@GRAPHICSMAGICK@"
# Reset error count
ERROR=0
# valgrind gmt wrapper
function gmt()
{
if [ -n "${VALGRIND_ARGS}" ]; then
valgrind ${VALGRIND_ARGS} --log-file=valgrind_%p.log \
--dsymutil=yes @GMT_BINARY_DIR@/src/gmt "$@"
else
"@GMT_BINARY_DIR@/src/gmt" "$@"
fi
}
# export function definitions to subshells
export -f gmt
# invalidate module calls without "gmt" prefix, which would bypass gmt from build dir
. "@GMT_SOURCE_DIR@/test/invalidate_modules.sh"
# Convert PS to PDF
function make_pdf()
{
pdf="${ps%.ps}.pdf"
test -f "$ps" || return 1
gmt psconvert -Tf -A -P "$ps" || ((++ERROR))
test -f "$pdf" || ((++ERROR))
}
# Compare the ps file with its original.
pscmp () {
test -f "$ps" || return 1
if ! [ -x "$GRAPHICSMAGICK" ]; then
echo "[PASS] (without comparison)"
return
fi
# syntax: gm compare [ options ... ] reference-image [ options ... ] compare-image [ options ... ]
rms=$("${GRAPHICSMAGICK}" compare -density 200 -maximum-error 0.001 -highlight-color magenta -highlight-style assign -metric rmse -file "${ps%.ps}.png" "$ps" "${src}/${ps}") || pscmpfailed="yes"
rms=$(perl -ne 'print $1 if /Total: ([0-9.]+)/' <<< "$rms")
if [ -z "$rms" ]; then
rms="NA"
else
rms=$(printf "%.3f\n" $rms)
fi
if [ "$pscmpfailed" ]; then
now=$(date "+%F %T")
echo "${ps}: RMS Error = $rms [FAIL]"
echo "$now ${ps}: RMS Error = $rms" >> "@CMAKE_CURRENT_BINARY_DIR@/fail_count.d"
make_pdf "$ps" # try to make pdf file
((++ERROR))
else
test -z "$rms" && rms=NA
echo "${ps}: RMS Error = $rms [PASS]"
fi
}
# Make sure to cleanup at end
function cleanup()
{
memtrack_err=0
for log_file in gmt_memtrack_*.log; do
test -f ${log_file} || continue
n_err=$(perl -lne '$a++ if /(Memory not freed|^!)/; END {print $a+0}' ${log_file})
(( memtrack_err += n_err )) || : # second assignment in case return code != 0
test ${n_err} -eq 0 && rm -f ${log_file} # remove logs w/o errors
done
echo "memtrack errors: $memtrack_err" >&2
valgrind_err=0
if [ -n "${VALGRIND_ARGS}" ]; then
for log_file in valgrind_*.log; do
test -f ${log_file} || continue
n_err=$(perl -ne 'print $1 if /ERROR SUMMARY: ([0-9]+)/' ${log_file})
n_err=${n_err:-1} # if valgrind crashes itself, there is no ERROR SUMMARY
(( valgrind_err += n_err )) || : # second assignment in case return code != 0
test ${n_err} -eq 0 && rm -f ${log_file} # remove logs w/o errors
done
echo "valgrind errors: $valgrind_err" >&2
fi
cd "@CMAKE_CURRENT_BINARY_DIR@" # get out of exec_dir before removing it
test "$ERROR" -eq 0 -a "$memtrack_err" -eq 0 -a "$valgrind_err" -eq 0 && rm -rf "$exec_dir"
echo "exit status: $ERROR" >&2
exit $ERROR
}
# Test the output image before exiting
function on_exit()
{
trap - EXIT # Restore EXIT trap
pscmp
cleanup
}
trap on_exit EXIT
set -E # Shell functions and subshells need to inherit ERR trap
function on_err()
{
trap - EXIT ERR SIGSEGV SIGTRAP SIGBUS # Restore trap
((++ERROR))
echo "ERROR: ${1}:${2}" >&2 # Report error line
cleanup
}
trap 'on_err "${BASH_SOURCE}" "${LINENO}"' ERR SIGSEGV SIGTRAP SIGBUS
# Create a temporary directory exec_dir in the build dir
# Then copy all of its contents (except *.ps files)
# Run remainder of this GMT script there
exec_dir="@CMAKE_CURRENT_BINARY_DIR@/${script_name%.sh}"
rm -rf "$exec_dir"
mkdir -p "$exec_dir"
cd "$exec_dir"
ln -sf "$script" .
# Make a script to capture everything that can be run again
cat > gmtest.sh << EOF
LANG=C
# Define variables that are needed *within* test scripts
export PATH="@GMT_BINARY_DIR@/src:\$PATH"
unset GMT5_SHAREDIR
export GMT_SHAREDIR="@GMT_SOURCE_DIR@/share"
export GMT_USERDIR="@GMT_BINARY_DIR@/share"
export GMT_DATADIR="$src:@GMT_TEST_DATA@/"
export GMT_SRCDIR="$src"
export GSHHG_DIR="@GSHHG_PATH@"
export DCW_DIR="@DCW_PATH@"
export GMT_BINARY_DIR="@GMT_BINARY_DIR@"
export GMT_SOURCE_DIR="@GMT_SOURCE_DIR@"
export GMT_VERSION="@GMT_PACKAGE_VERSION_WITH_SVN_REVISION@"
# Start with proper GMT defaults
gmt set -Du PS_CHAR_ENCODING ISOLatin1+
# Tentative PS file name
ps="${script_name%.sh}.ps"
# Now run the original script
. "${script}"
EOF
chmod 755 gmtest.sh
. ./gmtest.sh
# vim: ft=sh
|