summaryrefslogtreecommitdiff
path: root/test/clitest
blob: 38521d5fdf40519b56a45806e2dc7d6262522ca3 (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
#!/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 "--info          display information messages";
    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.0";
}

##############################################################################
# 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

ECHO="echo -e"
DIFF="diff";

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

LEVEL="silent";
while [ $# -gt 0 ]; do
    case "$1" in
        --info       ) LEVEL="info"; 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

printf_initialization "${COLOR:-many}";

printf_outline "Test directory is : ${TMP_DIR}\\n";
printf_outline "Scala binaries in : ${BIN_DIR}\\n";
jvm_version=`${JAVACMD:=java} -version 2>&1 | head -3 | tail -1`
printf_outline "Java runtime is   : $jvm_version\\n\\n";

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

SOURCE_DIR=${TMP_DIR}/src
OUTPUT_DIR=${TMP_DIR}/classes

SCALA_PACKAGE1=test1
SCALA_PACKAGE2=test2

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

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

SOURCE_FILE1=${SOURCE_DIR_PACKAGE1}/Main.scala
SOURCE_FILE2=${SOURCE_DIR_PACKAGE2}/Main.scala

LOG_FILE=${TMP_DIR}/${SCRIPT}.log
CHECK_FILE=${TMP_DIR}/${SCRIPT}.check

##############################################################################
# Tests

rm -rf ${TMP_DIR}/*
mkdir -p ${SOURCE_DIR_PACKAGE1} ${SOURCE_DIR_PACKAGE2}
mkdir -p ${OUTPUT_DIR}

(cat << EOF
package ${SCALA_PACKAGE1}
object Main {
  def main(args: Array[String]) = {
    val arg = if (args.length > 0) args(0) else "?"
    Console.println("1: test " + arg + " passed")
  }
}
EOF
) > ${SOURCE_FILE1}

################################# scalac #####################################
printf_outline "Test the 'scalac' command\\n"

printf "\\tCase 0: do not use any classpath information (0 dependency)\\n"
env CLASSAPTH= ${SCALAC} -d ${OUTPUT_DIR} ${SOURCE_FILE1} 2>> ${LOG_FILE}

if [ "$?" = "0" ] ; then

    ################################## scala #################################
    printf_outline "\\nTest the 'scala' command\\n"

    printf "\\tCase 1: use the CLASSPATH environment variable\\n"
    env CLASSPATH=${OUTPUT_DIR} ${SCALA} ${SCALA_MAIN1} 1 \
        1>> ${LOG_FILE}

    printf "\\tCase 2: use the -classpath option\\n"
    env CLASSPATH= ${SCALA} -cp ${OUTPUT_DIR} ${SCALA_MAIN1} 2 \
        1>> ${LOG_FILE}

    printf "\\tCase 3: use the current directory as default classpath\\n"
    (cd ${OUTPUT_DIR} &&
    env CLASSPATH= ${SCALA} ${SCALA_MAIN1} 3 1>> ${LOG_FILE})

    ############################## scalaint ##################################
    printf_outline "\\nTest the 'scalaint' command\\n"

    printf "\\tCase 1: use the CLASSPATH environment variable\\n"
    (env CLASSPATH=${OUTPUT_DIR} \
    printf "${SCALA_MAIN1}.main(Array(\"1\"))\n:q" | ${SCALAINT} \
        1>> ${LOG_FILE})
    printf "\\n" >> ${LOG_FILE}  # newline

    printf "\\tCase 2: use the -classpath option\\n"
    (env CLASSPATH= \
    printf "${SCALA_MAIN1}.main(Array(\"2\"))\n:q" | ${SCALAINT} \
        -classpath ${OUTPUT_DIR} 1>> ${LOG_FILE})
    printf "\\n" >> ${LOG_FILE}  # newline

    printf "\\tCase 3: use the current directory as default classpath\\n"
    (cd ${OUTPUT_DIR} &&
    printf "${SCALA_MAIN1}.main(Array(\"3\"))\n:q" | ${SCALAINT} 1>> ${LOG_FILE})
    printf "\\n" >> ${LOG_FILE}  # newline

fi

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

(cat << EOF
package ${SCALA_PACKAGE2}
object Main {
  def main(args: Array[String]) = {
    Console.print("2: ")
    ${SCALA_MAIN1}.main(args)
  }
}
EOF
) > ${SOURCE_FILE2}

################################# scalac #####################################
printf_outline "\\nTest the 'scalac' command\n"

printf "\\tCase 0: do not use any classpath information (1 dependency)\\n"
env CLASSPATH= ${SCALAC} ${SOURCE_FILE2} 2>> ${LOG_FILE}

printf "\\tCase 1: use the CLASSPATH environment variable\\n"
env CLASSPATH=${OUTPUT_DIR} ${SCALAC} \
        -d ${OUTPUT_DIR} ${SOURCE_FILE2} 2>> ${LOG_FILE}

printf "\\tCase 2: use the -classpath option\\n"
env CLASSPATH= ${SCALAC} -classpath ${OUTPUT_DIR} \
        -d ${OUTPUT_DIR} ${SOURCE_FILE2} 2>> ${LOG_FILE}

printf "\\tCase 3: use the current directory as default classpath\\n"
(cd ${OUTPUT_DIR} &&
env CLASSPATH= ${SCALAC} ${SOURCE_FILE2} 2>> ${LOG_FILE})

if [ "$?" = "0" ] ; then

    ############################## scala #####################################
    printf_outline "\\nTest the 'scala' command\\n"

    printf "\\tCase 1: use the CLASSPATH environment variable\\n"
    env CLASSPATH=${OUTPUT_DIR} ${SCALA} ${SCALA_MAIN2} 1 \
        1>> ${LOG_FILE}

    printf "\\tCase 2: use the -classpath option\\n"
    env CLASSPATH= ${SCALA} -cp ${OUTPUT_DIR} ${SCALA_MAIN2} 2 \
        1>> ${LOG_FILE}

    printf "\\tCase 3: use the current directory as default classpath\\n"
    (cd ${OUTPUT_DIR} &&
    env CLASSPATH= ${SCALA} ${SCALA_MAIN2} 3 1>> ${LOG_FILE})

fi

##############################################################################
# Check file

(cat << EOF
1: test 1 passed
1: test 2 passed
1: test 3 passed
This is an interpreter for Scala.
Type in expressions to have them evaluated.
Type :quit to exit the interpreter.
Type :compile followed by a filename to compile a complete Scala file.
Type :load followed by a filename to load a sequence of interpreter commands.
Type :help to repeat this message later.

scala> 1: test 1 passed
line0: scala.Unit = ()

scala> 
This is an interpreter for Scala.
Type in expressions to have them evaluated.
Type :quit to exit the interpreter.
Type :compile followed by a filename to compile a complete Scala file.
Type :load followed by a filename to load a sequence of interpreter commands.
Type :help to repeat this message later.

scala> 1: test 2 passed
line0: scala.Unit = ()

scala> 
This is an interpreter for Scala.
Type in expressions to have them evaluated.
Type :quit to exit the interpreter.
Type :compile followed by a filename to compile a complete Scala file.
Type :load followed by a filename to load a sequence of interpreter commands.
Type :help to repeat this message later.

scala> 1: test 3 passed
line0: scala.Unit = ()

scala> 
${TMP_DIR}/src/test2/Main.scala:5 error: not found: value test1
    test1.Main.main(args)
    ^
one error found
2: 1: test 1 passed
2: 1: test 2 passed
2: 1: test 3 passed
EOF
) > ${CHECK_FILE}

printf_outline "\\nDifferences between check file and log file:\\n"
${DIFF} ${CHECK_FILE} ${LOG_FILE}

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

if [ "$LEVEL" = "info" ] ; then
    printf_outline "\\nSource files:\\n"
    cat ${SOURCE_FILE1} ${SOURCE_FILE2}
elif [ "$LEVEL" = "verbose" ] ; then
    printf_outline "\\nSource files:\\n"
    cat ${SOURCE_FILE1} ${SOURCE_FILE2}
    printf_outline "\\nTest directory:\\n"
    tree ${TMP_DIR}
fi

rm -rf ${TMP_DIR}

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