summaryrefslogtreecommitdiff
path: root/test/clitest
blob: 5da82f74636eb402b52cf2e125f85439927c2524 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/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 "--javac         run same tests with the Sun Java compiler";
    echo "--jikes         run same tests with the IBM Java compiler";
    echo "--jredir=<dir>  specify the Sun JRE installation directory (jikes)";
    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.2";
}

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";
        file=`echo $check | sed -e "s#$PREFIX/##g"`;
        printf "$file\\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 [ "$COMPILER" = "javac" ]; then
        [ "$LANG" = "java5" ] && suffix=".${COMPILER}5";
        [ "$LANG" = "java6" ] && suffix=".${COMPILER}6";
    else
        suffix=".$COMPILER";
    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= $COMPILER_COMMAND \
        -dd $OUTPUT_DIR $source 2>> $LOG_FILE 1>> $LOG_FILE";

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

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

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

    printf "Compile $file ($info) with current directory as default classpath\\n"
    test_run "(cd $OUTPUT_DIR &&
        env CLASSPATH= $COMPILER_COMMAND $source 2>> $LOG_FILE 1>> $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;
    suffix=".$LANG";
    [ "$LANG" = "java6" ] && suffix=".java5";

    [ -f "$LOG_FILE" ] && rm $LOG_FILE;
    printf "\\n";

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

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

    printf "Execute $main with option -classpath\\n"
    test_run "env CLASSPATH= \
        $RUNTIME_COMMAND -classpath $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= $RUNTIME_COMMAND $main 3 2>> $LOG_FILE 1>> $LOG_FILE)";

    printf "Execute $main with quoted argument\\n"
    test_run "env CLASSPATH= \
        $RUNTIME_COMMAND -classpath $OUTPUT_DIR $main 4 \"a b c\" \
        2>> $LOG_FILE 1>> $LOG_FILE";

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

test_interpret() {
    [ $# = 2 ] || abort "internal error";
    main="$1"; shift 1;
    check="$1"; shift 1;
    [ "$LANG" = "scala" ] || 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' | $INTERPRETER_COMMAND \
        -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' | $INTERPRETER_COMMAND \
        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' | $INTERPRETER_COMMAND \
        -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' | $INTERPRETER_COMMAND \
        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

if [ -d "$PREFIX/test" ]; then
    TEST_DIR="$PREFIX/test";
elif [ -d "$PREFIX/misc/scala-test" ]; then
    TEST_DIR="$PREFIX/misc/scala-test";
else
    abort "Test directory not found";
fi;
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="scala";
COMPILER="scalac";
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;;
        --javac    ) LANG="java"; COMPILER="javac"; shift 1;;
        --jikes    ) LANG="java"; COMPILER="jikes"; shift 1;;
        --jredir=* ) JRE_DIR=`expr "$1" : "--jredir=\(.*\)"`; 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_add_file "$1"; shift 1;; #test_print_usage; exit 0;;
    esac;
done;

if [ "$LANG" = "scala" ]; then
    RUNTIME_COMMAND="${BIN_DIR}scala"
    COMPILER_COMMAND="${BIN_DIR}$COMPILER"
    INTERPRETER_COMMAND="${BIN_DIR}scalaint"
    SUFFIX=".scala"
elif [ "$LANG" = "java" ]; then
    RUNTIME_COMMAND=`which java`
    COMPILER_COMMAND=`which $COMPILER`
    BIN_DIR=`dirname $COMPILER_COMMAND`
    SUFFIX=".java"
else
    abort "unknown language '$LANG'"
fi
[ -x "$COMPILER_COMMAND" ] || abort "Command $COMPILER_COMMAND not found";
[ -x "$RUNTIME_COMMAND"  ] || abort "Command $RUNTIME_COMMAND not found";

printf_initialization "${COLOR:-many}";

printf_outline "Output directory is : $OUTPUT_DIR\\n";
printf_outline "Compiler command is : $COMPILER_COMMAND\\n";
printf_outline "Runtime command is  : $RUNTIME_COMMAND\\n";

jvm_version=`${JAVACMD:=java} -version 2>&1 | head -3 | tail -1`
printf_outline "Java runtime is     : $jvm_version\\n";
if [ "$LANG" = "java" ]; then
    [ `echo "$jvm_version" | grep -c "J9"` = "1" ] && LANG="j9vm";
    if [ "$COMPILER" = "jikes" ]; then
         if [ "$LANG" = "java" ]; then
             jre_home=`dirname $RUNTIME_COMMAND`/..;
         elif [ -n "$JRE_DIR" ]; then
             jre_home=$JRE_DIR;
         else
             abort "Jikes requires Sun JVM (use option '--jredir')";
         fi;
         cpath=`find $jre_home -name "rt.jar"`;
         COMPILER_COMMAND="${COMPILER_COMMAND} -bootclasspath $cpath";
    fi;
    [ `echo "$jvm_version" | grep -c "1\.5"` = "1" ] && LANG="${LANG}5";
    [ `echo "$jvm_version" | grep -c "1\.6"` = "1" ] && LANG="${LANG}6";
fi

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

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

#for testfile in "$SOURCE_DIR/test2/Main.scala"; do
for testfile in "" `find "$SOURCE_DIR" -name "Main$SUFFIX" -a -type f -print`; do
    [ -z "$testfile" ] && continue;
    checkfile=`dirname "$testfile"`/`basename "$testfile" "$SUFFIX"`.check
    info=`awk '$1 ~ /^\/\//{i=index($0,"@info ");if(i>0){print substr($0,i+6)}}' "$testfile"`;
    test_compile "$testfile" "$checkfile" "$info";
    if [ "$?" = "0" ] ; then
        scala_main=`echo "$testfile" | \
            sed -e "s#${SOURCE_DIR}/\(.*\)\${SUFFIX}#\1#g" -e "s#\/#\.#g"`;
        test_execute "$scala_main" "$checkfile";
        test_interpret "$scala_main" "$checkfile";
    fi
done;

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

rm -rf $TMP_DIR

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