summaryrefslogblamecommitdiff
path: root/test/clitest
blob: f2e43aecbc75018c72967c073d540f8ccf1aec81 (plain) (tree)




















































































                                                                              

                                                                         
                                                                   
                                                        

                                                                  







                                                               


































































































































                                                                                 






















                                                                              
                                 
                               
                                       



                                   
                              









                                  



                              







                                               


         

                      
                                                              

                                                







                                                                          


         




                             






                                                                      


                                       





                                                       
                                                                


                                                                



                                                                              

                    
                    


                                  
                                  


                                                                       
                                                                       
 


                                               
 


                                             
 






                                     
 
                                                                              
 
                                                            

                        

                                                 



                                                                              
                                                           

                        


                                                 
 
                                                                              
 
                                                             
 


                                                 


                                                                              

        
               

                                                                              
#!/bin/sh
##############################################################################
#                      __                                                    #
#      ________ ___   / /  ___     Scala Tools Launch Script                 #
#     / __/ __// _ | / /  / _ |    (c) 2002-2006, LAMP/EPFL                  #
#   __\ \/ /__/ __ |/ /__/ __ |                                              #
#  /____/\___/_/ |_/____/_/ | |                                              #
#                           |/                                               #
##############################################################################

# $Id: $

##############################################################################
# Error functions

# Prints a warning message on stderr.
warning() {
    echo "$0: warning:" "$@" 1>&2;
}

# Prints an error message on stderr.
error() {
    echo "$0:" "$@" 1>&2;
}

# Prints an error message on stderr and exits with a non-zero status.
abort() {
    error "$@";
    exit 1;
}

##############################################################################
# Printing functions

# Initializes the printf functions
printf_initialization() {
    case "$1" in
        many )
            printf_font_outline="printf \\033[1;39m";
            printf_font_success="printf \\033[1;32m";
            printf_font_failure="printf \\033[1;31m";
            printf_font_warning="printf \\033[1;33m";
            printf_font_default="printf \\033[0;39m";
            ;;
        some )
            printf_font_outline="printf \\033[1m";
            printf_font_success="printf \\033[0m";
            printf_font_failure="printf \\033[1m";
            printf_font_warning="printf \\033[1m";
            printf_font_default="printf \\033[0m";
            ;;
        none )
            printf_font_outline="";
            printf_font_success="";
            printf_font_failure="";
            printf_font_warning="";
            printf_font_default="";
            ;;
        * )
            abort "unknown color mode \`$1'";
            ;;
    esac;
}

# Prints formated text in outline font.
printf_outline() {
    $printf_font_outline;
    printf "$@";
    $printf_font_default;
}

##############################################################################
# Implementation of clitest

# Prints the clitest usage.
test_print_usage() {
    [ $# = 0 ] || abort "internal error";
    echo "Usage: $0 [OPTION]..."
}

# Prints the clitest help.
test_print_help() {
    [ $# = 0 ] || abort "internal error";
    test_print_usage;
    echo "";
    echo "--quick         use the 'quick' build instead of distribution";
    echo '--installed     use the installed programs on $PATH'
    echo "--debug         redirect all outputs to standard output";
    echo "--info          display information messages";
    echo "--java          run same tests for Java";
    echo "--objdir=<dir>  specify where to place generated files";
    echo "--verbose       display verbose messages";
    echo "--help, -?      display this help and exit";
    echo "--version       output version information and exit";
}

# Prints the clitest version.
test_print_version() {
    [ $# = 0 ] || abort "internal error";
    echo "$SCRIPT 1.1";
}

test_run() {
    [ $# = 1 ] || abort "internal error";
    cmd="$1"; shift 1;
    [ "$DEBUG" = "debug" ] && ( printf_outline "[DEBUG]"; echo "$cmd");
    eval "$cmd";
}

test_diff() {
    [ $# = 2 ] || abort "internal error";
    check="$1"; shift 1;
    log="$1"; shift 1;
    [ "$DEBUG" = "debug" ] && return;
    sed -e "s#$TEST_DIR/##g" $log > $TMP_FILE;
    $DIFF $check $TMP_FILE > $DIFF_FILE
    if [ -n "`cat $DIFF_FILE`" ] ; then
        printf_outline "\\nDifferences between check and log files:\\n";
        cat $DIFF_FILE;
    fi
}

test_compile() {
    [ $# = 3 ] || abort "internal error";
    source="$1"; shift 1;
    check="$1"; shift 1;
    info="$1"; shift 1;
    [ -d "$OUTPUT_DIR" ] || mkdir -p $OUTPUT_DIR;
    [ -f "$LOG_FILE" ] && rm $LOG_FILE;

    if [ "$LANG" = "java" ] ; then
       suffix=".javac"
    elif [ "$LANG" = "java5" ] ; then
       suffix=".javac5";
    else
       suffix=".scalac"
    fi

    file=`echo $source | sed -e "s#$PREFIX/##g"`;
    if [ "$LEVEL" = "info" ] || [ "$LEVEL" = "verbose" ] ; then
        printf_outline "\\nSource file: $file\\n"
        cat $source;
    fi
    printf "\\n";

    printf "Compile $file ($info) with unknown option '-dd'\\n"
    test_run "env CLASSPATH= $SCALAC -dd $OUTPUT_DIR $source 2>> $LOG_FILE";

    printf "Compile $file ($info) with no classpath information\\n"
    test_run "env CLASSPATH= $SCALAC -d $OUTPUT_DIR $source 2>> $LOG_FILE";

    printf "Compile $file ($info) with variable CLASSPATH\\n"
    test_run "env CLASSPATH=$OUTPUT_DIR $SCALAC \
        -d $OUTPUT_DIR $source 2>> $LOG_FILE";

    printf "Compile $file ($info) with option -classpath\\n"
    test_run "env CLASSPATH= $SCALAC -classpath $OUTPUT_DIR \
        -d $OUTPUT_DIR $source 2>> $LOG_FILE";

    printf "Compile $file ($info) with current directory as default classpath\\n"
    test_run "(cd $OUTPUT_DIR &&
        env CLASSPATH= $SCALAC $source 2>> $LOG_FILE)";

    test_diff "$check$suffix" "$LOG_FILE";

    if [ "$LEVEL" = "verbose" ] ; then
        printf_outline "\\nTest directory:\\n"
        tree $TMP_DIR
    fi
}

test_execute() {
    [ $# = 2 ] || abort "internal error";
    main="$1"; shift 1;
    check="$1"; shift 1;
    [ -f "$LOG_FILE" ] && rm $LOG_FILE;
    printf "\\n";

    printf "Execute $main with unknown option '-cpp'\\n"
    test_run "env CLASSPATH= $SCALA -cpp $OUTPUT_DIR $main 2 \
        2>> $LOG_FILE 1>> $LOG_FILE";

    printf "Execute $main with variable CLASSPATH\\n"
    test_run "env CLASSPATH=$OUTPUT_DIR $SCALA $main 1 \
        2>> $LOG_FILE 1>> $LOG_FILE";

    printf "Execute $main with option -classpath\\n"
    test_run "env CLASSPATH= $SCALA -cp $OUTPUT_DIR $main 2 \
        2>> $LOG_FILE 1>> $LOG_FILE";

    printf "Execute $main with current directory as default classpath\\n"
    test_run "(cd $OUTPUT_DIR &&
    env CLASSPATH= $SCALA $main 3 2>> $LOG_FILE 1>> $LOG_FILE)";

    test_diff "$check.scala" "$LOG_FILE";
}

test_interpret() {
    [ $# = 2 ] || abort "internal error";
    main="$1"; shift 1;
    check="$1"; shift 1;
    ([ "$LANG" = "java" ] || [ "$LANG" = "java5" ]) && return;
    [ -f "$LOG_FILE" ] && rm $LOG_FILE;
    printf "\\n"

    printf "Interpret $main with unknown option '-cpp'\\n"
    test_run "(env CLASSPATH= \
        printf $main'.main(Array(\"0\"))\n:q' | $SCALAINT \
        -cpp $OUTPUT_DIR 2>> $LOG_FILE 1>> $LOG_FILE)";
    printf "\\n" >> $LOG_FILE  # newline

    printf "Interpret $main with variable CLASSPATH\\n"
    test_run "(env CLASSPATH=$OUTPUT_DIR \
        printf $main'.main(Array(\"1\"))\n:q' | $SCALAINT \
        2>> $LOG_FILE 1>> $LOG_FILE)";
    printf "\\n" >> $LOG_FILE  # newline

    printf "Interpret $main with option -classpath\\n"
    test_run "(env CLASSPATH= \
        printf $main'.main(Array(\"2\"))\n:q' | $SCALAINT \
        -classpath $OUTPUT_DIR 2>> $LOG_FILE 1>> $LOG_FILE)";
    printf "\\n" >> $LOG_FILE  # newline

    printf "Interpret $main with current directory as default classpath\\n"
    test_run "(cd $OUTPUT_DIR &&
        printf $main'.main(Array(\"3\"))\n:q' | $SCALAINT \
        2>> $LOG_FILE 1>> $LOG_FILE)";
    printf "\\n" >> $LOG_FILE # newline

    test_diff "$check.scalaint" "$LOG_FILE";
}

##############################################################################
# Initialization

unset SCRIPT;
UNAME=`uname`;
SOURCE=$0;
SCRIPT=`basename "$SOURCE"`;
while [ -h "$SOURCE" ]; do
    SCRIPT=`basename "$SOURCE"`;
    LOOKUP=`ls -ld "$SOURCE"`;
    TARGET=`expr "$LOOKUP" : '.*-> \(.*\)$'`;
    if expr "${TARGET:-.}/" : '/.*/$' > /dev/null; then
        SOURCE=${TARGET:-.};
    else
        SOURCE=`dirname "$SOURCE"`/${TARGET:-.};
    fi;
done;
PREFIX=`dirname "$SOURCE"`/..;
prefix=$PREFIX;
PREFIX=`cd "$PREFIX"; pwd`;

QUICK="$PREFIX/build/quick/exec/"
if [ -d "$PREFIX/dists" ]; then
    LATEST="$PREFIX/dists/latest/bin/";
else
    if [ -d "$PREFIX/build" ]; then
        LATEST="$QUICK";
    else
        LATEST="$PREFIX/bin/";
    fi;
fi;
BIN_DIR="$LATEST"

if [ -n "`which mktemp`" ] ; then
    TMP_DIR=`mktemp -d`
else
    TMP_DIR=${TMPDIR-/tmp}/tmp.123
fi

TEST_DIR=$PREFIX/test
SOURCE_DIR=$TEST_DIR/files/cli
OUTPUT_DIR=$TMP_DIR/classes

DIFF="diff";

case `uname` in
    CYGWIN* )
        DIFF="diff --text --strip-trailing-cr";
        ;;
esac;

DEBUG="";
LANG="";
LEVEL="";
while [ $# -gt 0 ]; do
    case "$1" in
        --debug    ) DEBUG="debug"; LEVEL="verbose"; shift 1;;
        --quick    ) BIN_DIR="$QUICK"; shift 1;;
        --installed) BIN_DIR=""; shift 1;;
        --info     ) LEVEL="info"; shift 1;;
        --java     ) LANG="java"; shift 1;;
        --objdir=* ) OUTPUT_DIR=`expr "$1" : "--objdir=\(.*\)"`; shift 1;;
        --verbose  ) LEVEL="verbose"; shift 1;;
        --help     ) test_print_help; exit 0;;
        --version  ) test_print_version; exit 0;;
        -*         ) abort "unknown option $1";;
        *          ) test_print_usage; exit 0;;
    esac;
done;

SCALA="${BIN_DIR}scala"
SCALAC="${BIN_DIR}scalac"
SCALAINT="${BIN_DIR}scalaint"
SUFFIX=".scala"

if [ "$LANG" = "java" ] ; then
    SCALAC=`which javac`
    BIN_DIR=`dirname $SCALAC`
    SCALA=$BIN_DIR/java
    SUFFIX=".java"
fi
[ -x "$SCALAC" ] || ( printf "Command $SCALAC not found\\n"; exit 1 );

printf_initialization "${COLOR:-many}";

printf_outline "Output directory is : $OUTPUT_DIR\\n";
if [ "$LANG" = "java" ] ; then
    printf_outline "Java binaries in    : $BIN_DIR\\n";
else
    printf_outline "Scala binaries in   : $BIN_DIR\\n";
fi
jvm_version=`${JAVACMD:=java} -version 2>&1 | head -3 | tail -1`
printf_outline "Java runtime is     : $jvm_version\\n";

[ `echo "$jvm_version" | grep -c "1.5"` = "1" ] && LANG="java5";

##############################################################################
# Variables

SCALA_PACKAGE1=test1
SCALA_PACKAGE2=test2
SCALA_PACKAGE3=test3

SCALA_MAIN1=${SCALA_PACKAGE1}.Main
SCALA_MAIN2=${SCALA_PACKAGE2}.Main
SCALA_MAIN3=${SCALA_PACKAGE3}.Main

SOURCE_DIR_PACKAGE1=${SOURCE_DIR}/`echo ${SCALA_PACKAGE1} | tr '.' '/'`
SOURCE_DIR_PACKAGE2=${SOURCE_DIR}/`echo ${SCALA_PACKAGE2} | tr '.' '/'`
SOURCE_DIR_PACKAGE3=${SOURCE_DIR}/`echo ${SCALA_PACKAGE3} | tr '.' '/'`

SOURCE_FILE1=${SOURCE_DIR_PACKAGE1}/Main$SUFFIX
SOURCE_FILE2=${SOURCE_DIR_PACKAGE2}/Main$SUFFIX
SOURCE_FILE3=${SOURCE_DIR_PACKAGE3}/Main$SUFFIX

CHECK_FILE1=${SOURCE_DIR_PACKAGE1}/Main.check
CHECK_FILE2=${SOURCE_DIR_PACKAGE2}/Main.check
CHECK_FILE3=${SOURCE_DIR_PACKAGE3}/Main.check

if [ "$DEBUG" = "debug" ] ; then
    LOG_FILE=/dev/tty
else
    LOG_FILE=${TMP_DIR}/${SCRIPT}.log
fi
DIFF_FILE=${TMP_DIR}/${SCRIPT}.diff
TMP_FILE=${TMP_DIR}/${SCRIPT}.tmp

##############################################################################

test_compile "$SOURCE_FILE1" "$CHECK_FILE1" "no dependency";

if [ "$?" = "0" ] ; then
    test_execute "$SCALA_MAIN1" "$CHECK_FILE1";
    test_interpret "$SCALA_MAIN1" "$CHECK_FILE1";
fi

##############################################################################

test_compile "$SOURCE_FILE2" "$CHECK_FILE2" "1 dependency";

if [ "$?" = "0" ] ; then
    test_execute "$SCALA_MAIN2" "$CHECK_FILE2";
    test_interpret "$SCALA_MAIN2" "$CHECK_FILE2";
fi

##############################################################################

test_compile "$SOURCE_FILE3" "$CHECK_FILE3" "2 dependencies";

if [ "$?" = "0" ] ; then
    test_execute "$SCALA_MAIN3" "$CHECK_FILE3";
    test_interpret "$SCALA_MAIN3" "$CHECK_FILE3";
fi

##############################################################################
# Epilog

rm -rf $TMP_DIR

##############################################################################