test/make/autoconf/test-utils.sh
branchihse-test-autonconf-branch
changeset 57058 48d12f5a824c
equal deleted inserted replaced
57057:9525129eaa3b 57058:48d12f5a824c
       
     1 #!/bin/sh
       
     2 
       
     3 # BUILDDIR is set to where configure will be run
       
     4 # CONFIGURE is set to the configure script to test
       
     5 # TEMPOUT is a temporary file logging stdout
       
     6 # TEMPERR is a temporary file logging stderr
       
     7 
       
     8 GREP=grep
       
     9 
       
    10 Setup() {
       
    11 # If TESTSRC not set, use current directory. TESTSRC is set by jtreg
       
    12     BUILDDIR=${TESTSRC-$PWD}/`mktemp -d build.XXXXXXXXX`
       
    13 
       
    14 # Set the configure script to test relative to BUILDDIR.
       
    15     CONFIGURE=${BUILDDIR}/../../../../configure
       
    16 
       
    17 # Set temporary files for stdout and stderr from the configure script execution
       
    18     TEMPOUT=`mktemp`
       
    19     TEMPERR=`mktemp`
       
    20 
       
    21 # Create build directory to run configure in
       
    22     Sys rm -rf ${BUILDDIR} 
       
    23     Sys mkdir ${BUILDDIR}
       
    24     Sys cd ${BUILDDIR}
       
    25 
       
    26 # Clean up
       
    27     trap 'Cleanup' EXIT
       
    28 }
       
    29 
       
    30 # Run command and verify that it returned an expected result code != 0
       
    31 # $1 = command
       
    32 
       
    33 Failure() {
       
    34     Test failure "$@"
       
    35 }
       
    36 
       
    37 # Run command and verify that it returned an expected result code = 0
       
    38 # $1 = command
       
    39 Success() {
       
    40     Test success "$@"
       
    41 }
       
    42 
       
    43 # Verify the existence of a certain string in a file
       
    44 # $1 = string to look for (using "grep" format)
       
    45 # $2 = file to look in
       
    46 
       
    47 StringInFile() {
       
    48     AdditionalTest success "$GREP" "$1" "$2"
       
    49 }
       
    50 
       
    51 # Verify that a certain string doesn't exist in a file
       
    52 # $1 = string to look for (using "grep" format)
       
    53 # $2 = file to look in
       
    54 
       
    55 StringNotInFile() {
       
    56     AdditionalTest failure "$GREP" "$1" "$2"
       
    57 }
       
    58 
       
    59 # Present a summary of the results
       
    60 ResultSummary() {
       
    61     HorizontalRule
       
    62     if test -n "$failed"; then
       
    63 	count=`printf "%s" "$failed" | wc -c | tr -d ' '`
       
    64 	echo "FAIL: $count tests failed"
       
    65 	exit 1
       
    66     else
       
    67 	echo "PASS: all tests gave expected results"
       
    68 	exit 0
       
    69     fi
       
    70 }
       
    71 
       
    72 #
       
    73 # Local functions.
       
    74 # The functions below should normally not be called from the test scripts
       
    75 #
       
    76 Cleanup() {
       
    77     HorizontalRule
       
    78     Sys rm -rf ${BUILDDIR}
       
    79     Sys rm ${TEMPOUT} ${TEMPERR}
       
    80 }
       
    81 
       
    82 # Copied from Util.sh
       
    83 failed=""
       
    84 
       
    85 Fail() { 
       
    86     echo "FAIL: $1"
       
    87     failed="${failed}."
       
    88 }
       
    89 
       
    90 Die() { 
       
    91     printf "%s\n" "$*"
       
    92     exit 1
       
    93 }
       
    94 
       
    95 Sys() {
       
    96     printf "%s\n" "$*"
       
    97     "$@"
       
    98     rc="$?"
       
    99     test "$rc" -eq 0 || Die "Command \"$*\" failed with exitValue $rc"
       
   100 }
       
   101 
       
   102 HorizontalRule() {
       
   103     echo "-----------------------------------------------------------------"
       
   104 }
       
   105 
       
   106 # Result (pass/fail) based on result code ($?) from the test run.
       
   107 # $1 = expected result (success/failure) based on result code from the executed command
       
   108 # $2 = result code from test run
       
   109 
       
   110 Report() {
       
   111     test "$#" != 2 && Die "Usage: Report testtype rc"
       
   112 
       
   113     if test "$1" = "success" -a "$2" = 0; then
       
   114 	echo "PASS: succeeded as expected"
       
   115     elif test "$1" = "success" -a "$2" != 0; then
       
   116 	Fail "test failed unexpectedly"
       
   117 
       
   118     elif test "$1" = "failure" -a "$2" != 0; then
       
   119 	echo "PASS: failed as expected"
       
   120     elif test "$1" = "failure" -a "$2" = 0; then
       
   121 	Fail "test succeeded unexpectedly"
       
   122     else
       
   123 
       
   124 	Die "Usage: Report testtype rc"
       
   125     fi
       
   126 }
       
   127 
       
   128 # Run test and log stdout and stderr to temporary files $TEMPOUT and $TEMPERR
       
   129 # $1 = expected result (success/failure) 
       
   130 # $2 = command
       
   131 Test() {
       
   132     HorizontalRule
       
   133     expectedResult="$1"
       
   134     shift
       
   135     printf "%s\n" "$*"
       
   136 # We need to redirect to temp files to be able to look for errors in the stdout and stderr. But
       
   137 # we also need to send it to console so jtreg can pick it up. 3>&1 1>&2 2>&3 swaps stdout and stderr since 
       
   138 # tee only takes stdout. Then we swap it back to get correct stdout and stderr for jtreg.
       
   139 # Then we need to get the $? from the command that's run in the subshell so we store it in a temp file (if
       
   140 # we could use bash we could use $PIPESTATUS instead, but jtreg only uses 'sh')
       
   141     
       
   142     (s=`mktemp` ; (("$@" ; echo $?>$s) | tee $TEMPOUT) 3>&1 1>&2 2>&3 | tee $TEMPERR; exit $(cat $s; rm $s)) 3>&2 2>&1 1>&3
       
   143     Report "$expectedResult" "$?"
       
   144 }
       
   145 
       
   146 # Run test without logging stdout and stderr to temporary files
       
   147 # $1 = expected result (success/failure) 
       
   148 # $2 = command
       
   149 AdditionalTest() {
       
   150     HorizontalRule
       
   151     expectedResult="$1"
       
   152     shift
       
   153     printf "%s\n" "$*"
       
   154     "$@"
       
   155     Report "$expectedResult" "$?"
       
   156 }
       
   157