Blame test/Execute-Tests

Packit 78deda
#! /bin/bash
Packit 78deda
Packit 78deda
# See if PBM_TEST_PATH is set.
Packit 78deda
# PBM_TEST_PATH is the list of directories with the Netpbm programs
Packit 78deda
# you want to test.
Packit 78deda
#
Packit 78deda
# (1) check-tree: set to a long list of directories which contain
Packit 78deda
# the relevant executables.
Packit 78deda
#
Packit 78deda
# (2) check-package: set to the [package]/bin directory.
Packit 78deda
# 
Packit 78deda
# (3) check-install: empty string.  Executables will be sought from
Packit 78deda
# the default execution path ($PATH).
Packit 78deda
#
Packit 78deda
# You can set it here by de-commenting and modifying the next line:
Packit 78deda
#export PBM_TEST_PATH="/usr/local/bin/"
Packit 78deda
Packit 78deda
case ${CHECK_TYPE} in
Packit 78deda
tree)
Packit 78deda
    echo
Packit 78deda
    echo "Checking programs in source tree" ;;
Packit 78deda
package)  if [ -z $PBM_TEST_PATH ]
Packit 78deda
  then
Packit 78deda
    echo "Error: PBM_TEST_PATH is not set."
Packit 78deda
    exit 1
Packit 78deda
  elif [ ! -d $PBM_TEST_PATH ]
Packit 78deda
    then
Packit 78deda
    echo
Packit 78deda
    echo "Error: No directory named $PBM_TEST_PATH."
Packit 78deda
    echo
Packit 78deda
    echo "You must run \"make package\" before this test."
Packit 78deda
    echo
Packit 78deda
    echo "If you specified the package directory for \"make package\""
Packit 78deda
    echo "you must do the same for \"make check\"."
Packit 78deda
    echo
Packit 78deda
  exit 1
Packit 78deda
  fi ;;
Packit 78deda
install)
Packit 78deda
  echo
Packit 78deda
  echo "Programs in the default execution path:"
Packit 78deda
  echo $PATH
Packit 78deda
  echo "will be tested." ;;
Packit 78deda
*)
Packit 78deda
  echo "Invalid test type: ${CHECK_TYPE}"
Packit 78deda
  exit 1 ;;
Packit 78deda
esac
Packit 78deda
Packit 78deda
# Set srcdir, which is the directory which contains Execute-Tests (this
Packit 78deda
# script), programs that run the test, including *.test and helpers that they
Packit 78deda
# invoke, the list of tests to run ('Test-Order'), and *.ok files that
Packit 78deda
# indicate the expected results of tests.
Packit 78deda
Packit 78deda
srcdir=$(dirname $0)
Packit 78deda
Packit 78deda
# Set tmpdir, which is used in some of the test scripts.  By default
Packit 78deda
# this is created by mktemp.  The user can override and specify tmpdir,
Packit 78deda
# but in this case it must be an existing directory and must not be
Packit 78deda
# either $srcdir or current work.
Packit 78deda
Packit 78deda
if [ -z $tmpdir ]
Packit 78deda
  then
Packit 78deda
    tmpdir_created=$(mktemp -d "${TMPDIR:-/tmp}/netpbm.XXXXXXXX") || exit 1;
Packit 78deda
  export tmpdir=${tmpdir_created}
Packit 78deda
  else
Packit 78deda
  tmpdir_created="";
Packit 78deda
  if [ ! -d ${tmpdir} ]
Packit 78deda
     then echo "Specified temporary directory $tmpdir does not exist."
Packit 78deda
     exit 1;
Packit 78deda
  elif [ ${tmpdir} -ef ${srcdir} ]
Packit 78deda
     then echo "Temporary directory must not be $srcdir."
Packit 78deda
     exit 1;
Packit 78deda
  elif [ ${tmpdir} -ef $PWD ]
Packit 78deda
     then echo "Temporary directory must not be current directory."
Packit 78deda
     exit 1;
Packit 78deda
  fi
Packit 78deda
fi
Packit 78deda
Packit 78deda
# If necessary set the RGBDEF environment variable.
Packit 78deda
#export RGBDEF=/etc/rgb.txt
Packit 78deda
#export RGBDEF=/usr/local/netpbm/lib/rgb.txt
Packit 78deda
#export RGBDEF=/usr/share/emacs/*/etc/rgb.txt
Packit 78deda
Packit 78deda
Packit 78deda
# Declare arrays used to count and report test results.
Packit 78deda
# "UNEXPECTED SUCCESS" and "EXPECTED FAILURE" are not used now;
Packit 78deda
# they are reserved for future expansion.
Packit 78deda
declare -a array=(0 0 0 0 0)
Packit 78deda
Packit 78deda
# Older versions of bash get confused when array elements contain
Packit 78deda
# spaces.
Packit 78deda
Packit 78deda
status[0]="SUCCESS"
Packit 78deda
status[1]="FAILURE"
Packit 78deda
status[2]="UNEXPECTED SUCCESS"
Packit 78deda
status[3]="EXPECTED FAILURE"
Packit 78deda
status[4]="NOT TESTABLE"
Packit 78deda
Packit 78deda
Packit 78deda
Packit 78deda
# Copy test image files to the current work directory
Packit 78deda
Packit 78deda
if [ ! -f ./testgrid.pbm ]
Packit 78deda
  then cp -v ${srcdir}/testgrid.pbm ./testgrid.pbm
Packit 78deda
fi
Packit 78deda
Packit 78deda
if [ ! -f ./testimg.ppm ]
Packit 78deda
  then cp -v ${srcdir}/testimg.ppm  ./testimg.ppm 
Packit 78deda
fi
Packit 78deda
Packit 78deda
# The block-bin directory
Packit 78deda
#
Packit 78deda
# This directory contains dummy executables with the names of the
Packit 78deda
# Netpbm programs that are to be tested.  These dummy executables
Packit 78deda
# exist to prevent execution of programs (typically from a previous
Packit 78deda
# installation of Netpbm) in the default path during the tests.
Packit 78deda
# They report error when accessed and nothing else.
Packit 78deda
# The directory is placed in PATH between PBM_TEST_PATH and
Packit 78deda
# default PATH.
Packit 78deda
Packit 78deda
# Create block-bin.  If it already exists, erase and create anew.
Packit 78deda
Packit 78deda
if [ ! -z $PBM_TEST_PATH ]
Packit 78deda
  then
Packit 78deda
  blockbin=$PWD/block-bin
Packit 78deda
  if [ -d  $blockbin ]
Packit 78deda
    then rm -rf $blockbin
Packit 78deda
  fi
Packit 78deda
  mkdir $blockbin
Packit 78deda
  cp ${srcdir}/BLOCK $blockbin/BLOCK
Packit 78deda
  chmod +x $blockbin/BLOCK
Packit 78deda
Packit 78deda
# Populate the block-bin directory using all-in-place.ok and
Packit 78deda
# legacy-names.ok which together make a complete list of programs.
Packit 78deda
Packit 78deda
  sed 's/: ok$//' \
Packit 78deda
       ${srcdir}/all-in-place.ok ${srcdir}/legacy-names.ok | \
Packit 78deda
     tr ' ' '\n' | while read prog
Packit 78deda
    do
Packit 78deda
    ln -s $blockbin/BLOCK $blockbin/$prog
Packit 78deda
    done
Packit 78deda
Packit 78deda
  testpath=$PBM_TEST_PATH:$blockbin:$PATH:$BUILDDIR/test
Packit 78deda
else
Packit 78deda
# We don't need block-bin when testing after installation.
Packit 78deda
  testpath=$PATH:$BUILDDIR/test
Packit 78deda
fi
Packit 78deda
Packit 78deda
Packit 78deda
Packit 78deda
# Execute the tests, as described in the "Test-Order" file.
Packit 78deda
#
Packit 78deda
# Each test outputs a ".out" file, which is compared against a
Packit 78deda
# corresponding ".ok" file.  For example the output from "pbmmake.test"
Packit 78deda
# is "pbmmake.out" and when this matches "pbmmake.ok" we declare the
Packit 78deda
# test a success.
Packit 78deda
# In the error case the ".out" file is retained in the current work
Packit 78deda
# directory.
Packit 78deda
#
Packit 78deda
# All tests are self-contained.
Packit 78deda
#
Packit 78deda
# By default the tests are executed in the order described in the
Packit 78deda
# file Test-Order.  Copy this file from the source directory
Packit 78deda
# to the work directory.
Packit 78deda
Packit 78deda
Packit 78deda
# Provision for running programs under valgrind.
Packit 78deda
# Note that valgrind tests consume time.
Packit 78deda
#
Packit 78deda
# Output from valgrind must be redirected in some manner because some
Packit 78deda
# tests examine standard error (fd2) output.  Here we use --log-file.
Packit 78deda
# (See below)
Packit 78deda
Packit 78deda
if [ -z $VALGRIND_TESTS ]
Packit 78deda
  then VALGRIND_TESTS="off";
Packit 78deda
elif [ $VALGRIND_TESTS = "on" ]
Packit 78deda
  then
Packit 78deda
  valdir=$PWD/valgrind;
Packit 78deda
  mkdir $valdir
Packit 78deda
 
Packit 78deda
  vg_command_base="valgrind --trace-children=yes";
Packit 78deda
Packit 78deda
  for i in awk cat cksum cmp cp cut date dirname egrep fgrep file grep gs \
Packit 78deda
    head mkdir mktemp perl rm sed seq sh tee testrandom tr uniq \
Packit 78deda
    Available-Testprog
Packit 78deda
Packit 78deda
    # Tell valgrind not to probe execution of the above programs.
Packit 78deda
Packit 78deda
    do vg_skip=$vg_skip"/*/"$i","; done;
Packit 78deda
Packit 78deda
  vg_command_base=$vg_command_base" --trace-children-skip="$vg_skip;
Packit 78deda
  #
Packit 78deda
  # If using an older version of valgrind (< v.3.6) that does not
Packit 78deda
  # support --trace-children-skip=... , comment out the above line
Packit 78deda
  # and let valgrind probe execution of all programs listed above.
Packit 78deda
  # This greatly increases execution time.
Packit 78deda
Packit 78deda
fi
Packit 78deda
Packit 78deda
Packit 78deda
# Provision for running only chosen tests.
Packit 78deda
# The string "target" is a comma-separated list of target programs.
Packit 78deda
# When set only tests for programs in the list will be run.
Packit 78deda
# 
Packit 78deda
# The --no-clobber version comes useful when the user wants a modified
Packit 78deda
# (pared-down) version of Test-Order.
Packit 78deda
Packit 78deda
if [ ! -z $target ]
Packit 78deda
  then
Packit 78deda
  echo $target | sed 's/,/\n/g' | \
Packit 78deda
                 sed -e 's/^/# This script tests: .*\\</' -e 's/$/\\>/' \
Packit 78deda
                     -e '/^$/q' | \
Packit 78deda
                 grep -f - ${srcdir}/*.test -l | \
Packit 78deda
                   while read i ; do echo ${i##*/} ; done | \
Packit 78deda
                 grep -f - ${srcdir}/Test-Order > ./Test-Order ;
Packit 78deda
  if [ ! -s ./Test-Order ]
Packit 78deda
    then echo;
Packit 78deda
         echo "Error: No testable program names in target: "$target;
Packit 78deda
         echo; exit 1
Packit 78deda
  fi
Packit 78deda
else 
Packit 78deda
       cp ${srcdir}/Test-Order ./Test-Order ;
Packit 78deda
       #cp --no-clobber ${srcdir}/Test-Order ./Test-Order ;
Packit 78deda
fi
Packit 78deda
Packit 78deda
echo
Packit 78deda
Packit 78deda
for tname in `grep -v "^#" ./Test-Order | fgrep ".test"`
Packit 78deda
do
Packit 78deda
echo == $tname ==
Packit 78deda
Packit 78deda
# If running tests under valgrind, complete vg_command by prepending
Packit 78deda
# the valgrind output file, which is test-specific.
Packit 78deda
  
Packit 78deda
if [ $VALGRIND_TESTS = "on" ]
Packit 78deda
  then
Packit 78deda
  vg_command="$vg_command_base --log-file=${valdir}/${tname%.test}.%p.vout"
Packit 78deda
fi
Packit 78deda
Packit 78deda
# Execute a single test and test its result.
Packit 78deda
# But first check if the .ok file exists.  (Some .ok files are
Packit 78deda
# dynamically created.)  Then see if target programs and requirements
Packit 78deda
# are in place.  If either of these conditions are not met, do
Packit 78deda
# not execute the test and report "Not Testable".
Packit 78deda
Packit 78deda
if [ ! -s ${srcdir}/${tname%.test}.ok ]
Packit 78deda
then
Packit 78deda
  let result=4;
Packit 78deda
else
Packit 78deda
  ${srcdir}/Available-Testprog \
Packit 78deda
    `sed -n -e '/^# This script tests: /s/# This script tests: //p' \
Packit 78deda
            -e '/^# Also requires: /s/^# Also requires: //p' \
Packit 78deda
            -e '/^$/q' ${srcdir}/$tname | tr '\n' ' '`
Packit 78deda
  case $? in
Packit 78deda
  0)
Packit 78deda
    PATH=${testpath} $vg_command ${srcdir}/$tname > ${tname%.test}.out;
Packit 78deda
    let retval=$?
Packit 78deda
    case $retval in
Packit 78deda
    0)   cmp -s ${tname%.test}.out ${srcdir}/${tname%.test}.ok ;
Packit 78deda
         if [ $? -eq 0 ]
Packit 78deda
            then let result=0;  rm  ${tname%.test}.out ;
Packit 78deda
            else let result=1;
Packit 78deda
                 grep "^##" ${srcdir}/$tname  # Print failure message.
Packit 78deda
         fi ;;
Packit 78deda
    80) let result=4 ;;
Packit 78deda
    *)  let result=1 ;;
Packit 78deda
    esac ;;
Packit 78deda
  
Packit 78deda
  1)   let result=4 ;;
Packit 78deda
  *)   let result=1 ;;
Packit 78deda
  esac
Packit 78deda
fi
Packit 78deda
Packit 78deda
# Report whether a single test succeeded or failed.
Packit 78deda
# Increment counters.
Packit 78deda
Packit 78deda
echo $tname: ${status[${result}]}; echo
Packit 78deda
let array[${result}]=${array[${result}]}+1
Packit 78deda
let total_scripts=${total_scripts}+1
Packit 78deda
Packit 78deda
done
Packit 78deda
Packit 78deda
# Erase temporary bin directory and its contents.
Packit 78deda
Packit 78deda
rm -rf ${srcdir}/bin
Packit 78deda
Packit 78deda
Packit 78deda
# Erase temporary directory and its contents, if it was created.
Packit 78deda
Packit 78deda
if [ -n $tmpdir_created ]
Packit 78deda
    then rm -rf $tmpdir_created
Packit 78deda
fi
Packit 78deda
Packit 78deda
Packit 78deda
# Erase test image files in the current (work) directory.
Packit 78deda
# (Do not erase them if we are working from the source directory.)
Packit 78deda
Packit 78deda
if [ ! $PWD -ef ${srcdir} ]
Packit 78deda
    then rm ./testimg.ppm ./testgrid.pbm
Packit 78deda
fi
Packit 78deda
Packit 78deda
Packit 78deda
# Calculate success / failure totals and print a summary report.
Packit 78deda
# Report date and time of completion.
Packit 78deda
Packit 78deda
echo "Test summary:"
Packit 78deda
echo ==================
Packit 78deda
Packit 78deda
for s in 0 1 2 3 4
Packit 78deda
  do
Packit 78deda
    if [[ ${array[${s}]} -gt 0 || s -eq 1 ]]
Packit 78deda
    then echo ${status[${s}]} ${array[${s}]}
Packit 78deda
    fi
Packit 78deda
  done
Packit 78deda
Packit 78deda
let total_testable=${total_scripts}-${array[4]}
Packit 78deda
echo "TOTAL TESTABLE" $total_testable
Packit 78deda
Packit 78deda
echo ==================
Packit 78deda
echo "All tests done."
Packit 78deda
date -u +"%a, %d %b %Y %H:%M:%S %z"
Packit 78deda
Packit 78deda
Packit 78deda
# Exit with status 0 if all possible tests succeeded, 1 otherwise.
Packit 78deda
Packit 78deda
if [[ ${array[0]} -eq ${total_testable} ]]
Packit 78deda
then exit 0
Packit 78deda
else exit 1
Packit 78deda
fi