1 #! /usr/bin/ksh93
   2 #
   3 #
   4 # This file and its contents are supplied under the terms of the
   5 # Common Development and Distribution License ("CDDL"), version 1.0.
   6 # You may only use this file in accordance with the terms of version
   7 # 1.0 of the CDDL.
   8 #
   9 # A full copy of the text of the CDDL should have accompanied this
  10 # source.  A copy of the CDDL is also available via the Internet at
  11 # http://www.illumos.org/license/CDDL.
  12 #
  13 
  14 #
  15 # Copyright 2020 Joyent, Inc.
  16 #
  17 
  18 AWK=/usr/bin/awk
  19 WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX)
  20 
  21 SUCCESSES=0
  22 TOTAL=0
  23 
  24 function proctemplate {
  25         bash <<-EOF
  26         IFS= read -rd '' OUTPUT < $1;
  27         printf "%s" "\${OUTPUT//\\\$AWK/\$AWK}";
  28         EOF
  29 }
  30 
  31 while [[ $# -gt 0 ]]; do
  32         case $1 in
  33                 -o)
  34                 AWK=$2
  35                 shift 2
  36                 ;;
  37                 *)
  38                 printf 'Usage: runtests.sh [-o <override awk executable>]\n' >&2
  39                 exit 1
  40                 ;;
  41         esac
  42 done
  43 
  44 # Make path absolute so we can change directories.
  45 AWK=$(cd $(dirname $AWK); pwd)/$(basename $AWK)
  46 TOP=$(cd $(dirname $0); pwd)
  47 
  48 # Move into $TOP in case we were run from elsewhere.
  49 cd $TOP
  50 
  51 if [[ ! -x $AWK ]]; then
  52         printf 'awk executable "%s" is not executable\n' "$AWK" >&2
  53         exit 1
  54 fi
  55 
  56 if [[ ! -x /bin/bash ]]; then
  57         printf 'executable "/bin/bash" not found\n' >&2
  58         exit 1
  59 fi
  60 
  61 if [[ "$(id -u)" == "0" ]]; then
  62         printf 'runtests.sh should not be run as root\n' >&2
  63         exit 1
  64 fi
  65 
  66 
  67 export AWK
  68 export WORKDIR
  69 export UMEM_DEBUG="default"
  70 
  71 mkdir -p $WORKDIR
  72 
  73 printf 'Running AWK tests ($AWK="%s")\n' "$AWK"
  74 
  75 printf '\n# Examples from "The AWK Programming Environment"\n\n'
  76 
  77 for script in examples/awk/p.*; do
  78         ((TOTAL+=1))
  79         printf "$script... "
  80         if cmp -s <($AWK -f ${script} data/test.countries 2>&1) ${script/awk/out}; then
  81                 printf "ok\n"
  82                 ((SUCCESSES+=1))
  83         else
  84                 printf "failed\n"
  85         fi
  86 done
  87 
  88 printf '\n# One True AWK Example Programs\n\n'
  89 
  90 for script in examples/awk/t.*; do
  91         ((TOTAL+=1))
  92         printf "$script... "
  93         if diff <($AWK -f ${script} data/test.data 2>&1) ${script/awk/out}; then
  94                 printf "ok\n"
  95                 ((SUCCESSES+=1))
  96         else
  97                 printf "failed\n"
  98         fi
  99 done
 100 
 101 cd bugs-fixed || exit 1
 102 for PROG in *.awk; do
 103         ((TOTAL+=1))
 104         export LANG=C
 105         printf "$PROG... "
 106         $AWK -f $PROG > $WORKDIR/test.temp.out 2>&1 || \
 107             echo EXIT CODE: $? >> $WORKDIR/test.temp.out
 108         if diff $WORKDIR/test.temp.out <(proctemplate ${PROG/.awk/.ok}); then
 109                 printf "ok\n"
 110                 ((SUCCESSES+=1))
 111         else
 112                 printf "failed\n"
 113         fi
 114 done
 115 cd $TOP
 116 
 117 # Run the test programs
 118 
 119 printf '\n# One True AWK Test Programs\n\n'
 120 
 121 cd tests || exit 1
 122 for script in ./T.*; do
 123         ((TOTAL+=1))
 124         rm -f $WORKDIR/test.temp*
 125         printf "$script... "
 126         if $script > /dev/null 2>&1; then
 127                 printf "ok\n"
 128                 ((SUCCESSES+=1))
 129         else
 130                 printf "failed\n"
 131         fi
 132 done
 133 cd $TOP
 134 
 135 printf '\n# Imported GAWK Test Programs\n\n'
 136 
 137 cd gnu || exit 1
 138 for PROG in *.awk; do
 139         ((TOTAL+=1))
 140         export LANG=C
 141         printf "$PROG... "
 142         INPUT="${PROG/.awk/.in}"
 143         if [[ -f $INPUT ]]; then
 144                 $AWK -f $PROG < $INPUT > $WORKDIR/test.temp.out 2>&1 || \
 145                     echo EXIT CODE: $? >> $WORKDIR/test.temp.out
 146         else
 147                 $AWK -f $PROG > $WORKDIR/test.temp.out 2>&1 || \
 148                     echo EXIT CODE: $? >> $WORKDIR/test.temp.out
 149         fi
 150         if diff $WORKDIR/test.temp.out ${PROG/.awk/.ok}; then
 151                 printf "ok\n"
 152                 ((SUCCESSES+=1))
 153         else
 154                 printf "failed\n"
 155         fi
 156 done
 157 
 158 for script in ./*.sh; do
 159         ((TOTAL+=1))
 160         export LANG=C
 161         printf "$script... "
 162         $script > $WORKDIR/test.temp.out 2>&1
 163         if diff $WORKDIR/test.temp.out ${script/.sh/.ok}; then
 164                 printf "ok\n"
 165                 ((SUCCESSES+=1))
 166         else
 167                 printf "failed\n"
 168         fi
 169 done
 170 cd $TOP
 171 
 172 printf '\n# Imported GAWK Syntax Tests\n\n'
 173 
 174 cd syn || exit 1
 175 for PROG in *.awk; do
 176         ((TOTAL+=1))
 177         printf "$PROG... "
 178         if $AWK -f $PROG /dev/null > /dev/null 2> $WORKDIR/test.temp.out; then
 179                 printf "failed (should exit nonzero)\n"
 180                 continue
 181         fi
 182 
 183         if diff $WORKDIR/test.temp.out <(proctemplate ${PROG/.awk/.ok}); then
 184                 printf "ok\n"
 185                 ((SUCCESSES+=1))
 186         else
 187                 printf "failed\n"
 188         fi
 189 done
 190 cd $TOP
 191 
 192 printf '\n\nTOTAL: %d/%d\n' "$SUCCESSES" "$TOTAL"
 193 
 194 rm -rf $WORKDIR
 195 
 196 if [[ $SUCCESSES != $TOTAL ]]; then
 197         exit 1
 198 fi