tests/run-test.sh
663f6ca2
 #!/usr/bin/env bash
6b64e61c
 #
663f6ca2
 #  run-test.sh
 #  MulleObjC
fbf48179
 #
 #  Created by Nat! on 01.11.13.
 #  Copyright (c) 2013 Mulle kybernetiK. All rights reserved.
663f6ca2
 #  (was run-mulle-scion-test)
 
 set -m
 
6b64e61c
 # check if running a single test or all
 
 executable=`basename $0`
 executable=`basename $executable .sh`
 
d5423eed
 
 VERBOSE="YES"
 
 while [ $# -ne 0 ]
 do
    case "$1" in
9efac45c
       -v|-vv|-vvv|-V) # not really the same :)
d5423eed
          VERBOSE="YES"
       ;;
 
       -q)
          VERBOSE="NO"
       ;;
 
       --path-prefix)
          shift # ignore argument
       ;;
       -*)
          echo "unknown option $1" >&2
          exit 1
       ;;
 
       *)
          break
       ;;
    esac
6b64e61c
    shift
d5423eed
 done
 
 TEST="$1"
 shift
6b64e61c
 
 trace_ignore()
 {
   return 0
 }
 
e5534308
 trap trace_ignore 5 6
6b64e61c
 
fbf48179
 
da962797
 # parse optional parameters
d5423eed
 MULLE_SCION=`ls -1 "${PWD}/../build/mulle-scion" | tail -1 2> /dev/null`
fbf48179
 
da962797
 if [ -z "${MULLE_SCION}" ]
 then
d5423eed
    echo "mulle-scion can not be found ($PWD)" >&2
da962797
    exit 1
 fi
 
d5423eed
 DIR=`pwd`
fbf48179
 shift
 
d5423eed
 HAVE_WARNED="NO"
fbf48179
 RUNS=0
 
 
 search_plist()
 {
    local plist
    local root
 
    dir=`dirname "$1"`
    plist=`basename "$1"`
    root="$2"
 
    while :
    do
       if [ -f "$dir"/"$plist" ]
       then
          echo "$dir/$plist"
          break
       fi
 
       if [ "$dir" = "$root" ]
       then
          break
       fi
 
       next=`dirname "$dir"`
       if [ "$next" = "$dir" ]
       then
          break
       fi
       dir="$next"
    done
 }
 
 
c2031d93
 relpath()
fbf48179
 {
c2031d93
    python -c "import os.path; print os.path.relpath('$1', '$2')"
da962797
 }
fbf48179
 
 
 run()
 {
    local template
    local plist
    local stdin
    local stdout
    local stderr
    local output
    local errput
    local random
6b64e61c
    local fail
    local match
fbf48179
 
    template="$1"
    plist="$2"
    stdin="$3"
    stdout="$4"
    stderr="$5"
 
470d0463
    random=`mktemp -t "mulle-scion-XXXX"`
fbf48179
    output="$random.stdout"
    errput="$random.stderr"
6b64e61c
    errors=`basename $template .scion`.errors
da962797
 
6c3c2db4
    pwd=`pwd`
    pretty_template=`relpath "$pwd"/"$template" "$root"`
d5423eed
    if [ "$VERBOSE" = "YES" ]
6b64e61c
    then
d5423eed
       echo "$pretty_template" >&2
6b64e61c
    fi
 
fbf48179
    RUNS=`expr $RUNS + 1`
6b64e61c
 
    # plz2shutthefuckup bash
da962797
    set +m
6b64e61c
    set +b
    set +v
    # denied, will always print TRACE/BPT
 
    $MULLE_SCION "$template" "$plist" < "$stdin" > "$output" 2> "$errput"
 
fbf48179
    if [ $? -ne 0 ]
    then
6b64e61c
       if [ ! -f "$errors" ]
fbf48179
       then
          echo "TEMPLATE CRASHED: \"$pretty_template\"" >& 2
          echo "DIAGNOSTICS:" >& 2
          cat  "$errput"
          exit 1
6b64e61c
       else
da962797
          fail=0
6b64e61c
          while read expect
          do
             match=`grep "$expect" "$errput"`
             if [ "$match" = "" ]
             then
                if [ $fail -eq 0 ]
                then
                   echo "TEMPLATE FAILED TO PRODUCE ERRORS: \"$pretty_template\"" >& 2
                   fail=1
da962797
                fi
6b64e61c
                echo "   $expect" >&2
             fi
          done < "$errors"
          if [ $fail -eq 1 ]
          then
             exit 1
          fi
          rm "$output" "$errput" 2> /dev/null
          return 0
fbf48179
       fi
    else
6b64e61c
       if [ -f "$errors" ]
fbf48179
       then
          echo "TEMPLATE FAILED TO CRASH: \"$pretty_template\"" >& 2
6b64e61c
          echo "DIAGNOSTICS:" >&2
fbf48179
          cat  "$errput"
          exit 1
       fi
    fi
 
6b64e61c
 
fbf48179
    if [ "$stdout" != "-" ]
    then
       result=`diff -q "$stdout" "$output"`
       if [ "$result" != "" ]
       then
          white=`diff -q -w "$stdout" "$output"`
          if [ "$white" != "" ]
          then
             echo "FAILED: \"$pretty_template\" produced unexpected output" >& 2
             echo "DIFF: ($stdout vs. $output)" >& 2
             diff -y "$stdout" "$output" >& 2
          else
             echo "FAILED: \"$pretty_template\" produced different whitespace output" >& 2
             echo "DIFF: ($stdout vs. $output)" >& 2
da962797
             od -a "$output" > "$output".actual.hex
             od -a "$stdout" > "$output".expect.hex
fbf48179
             diff -y "$output".expect.hex "$output".actual.hex >& 2
          fi
 
          echo "DIAGNOSTICS:" >& 2
          cat  "$errput"
          exit 2
       fi
    fi
 
    if [ "$stderr" != "-" ]
    then
       result=`diff "$stderr" "$errput"`
       if [ "$result" != "" ]
       then
          echo "WARNING: \"$pretty_template\" produced unexpected diagnostics ($errput)" >& 2
          echo "" >& 2
          diff "$stderr" "$errput" >& 2
          echo "DIAGNOSTICS:" >& 2
          cat  "$errput"
          exit 3
       fi
    fi
6b64e61c
    rm "$output" "$errput" 2> /dev/null
fbf48179
 }
 
 
 run_test()
 {
    local stdin
    local stdout
    local stderr
    local template
    local plist
    local root
 
    template="$1.scion"
    plist="$1.plist"
    root="$2"
 
    if [ ! -f "$plist" ]
    then
       start=`pwd`/default.plist
       plist=`search_plist "$start" "$root"`
       if [ "$plist" = "" ]
       then
          plist="none"
d5423eed
          if [ "$HAVE_WARNED" != "YES" ]
fbf48179
          then
             echo "warning: no default.plist found" >&2
d5423eed
             HAVE_WARNED="YES"
fbf48179
          fi
       fi
    fi
 
    stdin="$1.stdin"
    if [ ! -f "$stdin" ]
    then
       stdin="provide/$1.stdin"
    fi
    if [ ! -f "$stdin" ]
    then
       stdin="default.stdin"
    fi
    if [ ! -f "$stdin" ]
    then
       stdin="/dev/null"
    fi
 
    stdout="$1.stdout"
    if [ ! -f "$stdout" ]
    then
       stdout="expect/$1.stdout"
    fi
    if [ ! -f "$stdout" ]
    then
       stdout="default.stdout"
    fi
    if [ ! -f "$stdout" ]
    then
       stdout="-"
    fi
 
    stderr="$1.stderr"
    if [ ! -f "$stderr" ]
    then
       stderr="expect/$1.stderr"
    fi
    if [ ! -f "$stderr" ]
    then
       stderr="default.stderr"
    fi
    if [ ! -f "$stderr" ]
    then
       stderr="-"
    fi
 
    run "$template" "$plist" "$stdin" "$stdout" "$stderr"
 }
 
 
 scan_directory()
 {
    local i
    local filename
    local root
    local dir
 
    root="$1"
 
6b64e61c
    for i in [^_]*
fbf48179
    do
       if [ -d "$i" ]
       then
          dir=`pwd`
da962797
          cd "$i"
          scan_directory "$root"
fbf48179
          cd "$dir"
       else
          filename=`basename "$i" .scion`
          if [ "$filename" != "$i" ]
          then
             run_test "$filename" "$root"
          fi
       fi
    done
 }
 
 
 test_binary()
 {
    local random
    local output
    local errput
 
470d0463
    random=`mktemp -t "mulle-scion-XXXX"`
fbf48179
    output="$random.stdout"
    errput="$random.stderr"
 
    $MULLE_SCION > /dev/null 2>&1
da962797
    code=$?
fbf48179
 
    if [ $code -eq 127 ]
    then
dda12564
       echo "$MULLE_SCION can not be run (missing shared library probably ($PWD, $PATH)" >&2
fbf48179
       exit 1
da962797
    fi
fbf48179
 
    if [ $code -ne 253 ]
    then
27dc2200
       echo "${MULLE_SCION} is wrong executable" >&2
fbf48179
       exit 1
776920e7
    fi
da962797
 
    echo "using ${MULLE_SCION} to test" >&2
fbf48179
 }
 
 
 absolute_path_if_relative()
 {
    case "$1" in
da962797
       .*)  echo `pwd`/"$1"
fbf48179
 	   ;;
       *)   echo "$1"
 	   ;;
    esac
 }
 
 
65ba6fec
 
27dc2200
 trace_ignore()
 {
   return 0
 }
 
 
 main()
 {
    trap trace_ignore 5 6
fbf48179
 
27dc2200
    while [ $# -ne 0 ]
    do
       case "$1" in
          -q)
             VERBOSE="no"
          ;;
fbf48179
 
27dc2200
          -v)
             VERBOSE="yes"
          ;;
6b64e61c
 
27dc2200
          --path-prefix)
             shift
          ;;
 
          -*)
             echo "unknown option \"$1\"" >&2 && exit 1
          ;;
65ba6fec
 
27dc2200
          *)
             break
          ;;
       esac
fbf48179
 
27dc2200
       shift
    done
 
 
    TEST="$1"
 
    #
    # find executable
    #
3601af91
    exe=`ls -1 ./bin/mulle-scion 2> /dev/null | tail -1`
27dc2200
    if [ ! -x "${exe}" ]
6b64e61c
    then
27dc2200
       exe=`ls -1 ../?uild/Products/*/mulle-scion 2> /dev/null | tail -1`
       if [ ! -x "${exe}" ]
       then
          exe=`ls -1 ../?uild/*/mulle-scion 2> /dev/null | tail -1 2> /dev/null`
          if [ ! -x "${exe}" ]
          then
             exe=`ls -1 ../?uild*/mulle-scion 2> /dev/null | tail -1 2> /dev/null`
          fi
       fi
    fi
fbf48179
 
6b64e61c
 
27dc2200
    if [ -x "${exe}" ]
6b64e61c
    then
27dc2200
       MULLE_SCION="${exe}"
6b64e61c
    else
27dc2200
       MULLE_SCION="`which mulle-scion`"
    fi
 
    if [ -z "${MULLE_SCION}" ]
    then
       echo "mulle-scion can not be found" >&2
6b64e61c
       exit 1
    fi
fbf48179
 
27dc2200
    MULLE_SCION=`absolute_path_if_relative "${MULLE_SCION}"`
 
    DEPENDENCIES="`mulle-bootstrap paths dependencies 2> /dev/null`"
    if [ ! -z "${DEPENDENCIES}" ]
    then
       case "`uname`" in
          Darwin)
             DYLD_FALLBACK_FRAMEWORK_PATH="${DEPENDENCIES}/Frameworks"
             export DYLD_FALLBACK_FRAMEWORK_PATH
3601af91
             echo "DYLD_FALLBACK_FRAMEWORK_PATH='${DEPENDENCIES}/Frameworks'" >&2
27dc2200
             ;;
 
          *)
             LD_LIBRARY_PATH="${DEPENDENCIES}/lib:${LD_LIBRARY_PATH}"
             export LD_LIBRARY_PATH
3601af91
             echo "LD_LIBRARY_PATH='${DEPENDENCIES}/lib:${LD_LIBRARY_PATH}'" >&2
27dc2200
             ;;
       esac
    fi
 
    test_binary "$MULLE_SCION"
 
    DIR="`pwd -P`"
 
    HAVE_WARNED="no"
    RUNS=0
 
    if [ -z "$TEST" ]
    then
       scan_directory "$DIR"
 
       if [ "$RUNS" -ne 0 ]
       then
           echo "All tests ($RUNS) passed successfully"
       else
          echo "no tests found" >&2
          exit 1
       fi
    else
        local directory
 
        directory="`dirname "$TEST"`"
        if [ "${directory}" = "" ]
        then
           directory="."
        fi
 
        file=`basename "$TEST"`
        filename=`basename "$file" .scion`
 
        if [ "$file" = "$filename" ]
        then
           echo "error: template file must have .scion extension" >& 2
           exit 1
        fi
 
        if [ ! -f "$TEST" ]
        then
           echo "error: template file not found" >& 2
           exit 1
        fi
 
        old=`pwd`
        cd "${directory}"
        run_test "$filename" "${directory}"
        rval=$?
        cd "$old"
        exit $rval
    fi
 }
6b64e61c
 
27dc2200
 main "$@"