summaryrefslogtreecommitdiff
path: root/tests/exec.test
blob: 76a1b2db1b1ba7913f2de09d3da915687e74708b (plain)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Commands covered:  exec
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1994 The Regents of the University of California.
# Copyright (c) 1994-1997 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: exec.test,v 1.8.2.1 2001/10/17 19:29:25 das Exp $

source [file dirname [info script]]/testing.tcl

needs cmd exec
needs cmd flush
needs cmd after eventloop

testConstraint unix [expr {$tcl_platform(platform) eq {unix}}]

# Sleep which supports fractions of a second
if {[info commands sleep] eq {}} {
    proc sleep {n} {
	after [expr {int($n * 1000)}]
    }
}

set f [open sleepx w]
puts $f "#![info nameofexecutable]"
puts $f {
    set seconds [lindex $argv 0]
    after [expr {int($seconds * 1000)}]
}
close $f
#catch {exec chmod +x sleepx}
set sleepx [list [info nameofexecutable] sleepx]

# Basic operations.

test exec-1.1 {basic exec operation} {
    exec echo a b c
} "a b c"
test exec-1.2 {pipelining} {
    exec echo a b c d | cat | cat
} "a b c d"
test exec-1.3 {pipelining} {
    set a [exec echo a b c d | cat | wc]
    list [scan $a "%d %d %d" b c d] $b $c
} {3 1 4}
set arg {12345678901234567890123456789012345678901234567890}
set arg "$arg$arg$arg$arg$arg$arg"
test exec-1.4 {long command lines} {
    exec echo $arg
} $arg
set arg {}

# I/O redirection: input from Tcl command.

test exec-2.1 {redirecting input from immediate source} {
    exec cat << "Sample text"
} {Sample text}
test exec-2.2 {redirecting input from immediate source} {
    exec << "Sample text" cat | cat
} {Sample text}
test exec-2.3 {redirecting input from immediate source} {
    exec cat << "Sample text" | cat
} {Sample text}
test exec-2.4 {redirecting input from immediate source} {
    exec cat | cat << "Sample text"
} {Sample text}
test exec-2.5 {redirecting input from immediate source} {
    exec cat "<<Joined to arrows"
} {Joined to arrows}
test exec-2.6 {redirecting input from immediate source, with UTF} {
    # If this fails, it may give back:
    # "\uC3\uA9\uC3\uA0\uC3\uBC\uC3\uB1"
    # If it does, this means that the UTF -> external conversion did not 
    # occur before writing out the temp file.
    exec cat << "\uE9\uE0\uFC\uF1"
} "\uE9\uE0\uFC\uF1"
test exec-2.7 {redirecting input from immediate source with nulls} {
    exec cat << "Sample\0text"
} "Sample\0text"

# I/O redirection: output to file.

file delete gorp.file
test exec-3.1 {redirecting output to file} {
    exec echo "Some simple words" > gorp.file
    exec cat gorp.file
} "Some simple words"
test exec-3.2 {redirecting output to file} {
    exec echo "More simple words" | >gorp.file cat | cat
    exec cat gorp.file
} "More simple words"
test exec-3.3 {redirecting output to file} {
    exec > gorp.file echo "Different simple words" | cat | cat
    exec cat gorp.file
} "Different simple words"
test exec-3.4 {redirecting output to file} {
    exec echo "Some simple words" >gorp.file
    exec cat gorp.file
} "Some simple words"
test exec-3.5 {redirecting output to file} {
    exec echo "First line" >gorp.file
    exec echo "Second line" >> gorp.file
    exec cat gorp.file
} "First line\nSecond line"
test exec-3.6 {redirecting output to file} {
    exec echo "First line" >gorp.file
    exec echo "Second line" >>gorp.file
    exec cat gorp.file
} "First line\nSecond line"
test exec-3.7 {redirecting output to file} {
    set f [open gorp.file w]
    puts $f "Line 1"
    flush $f
    exec echo "More text" >@ $f
    exec echo >@$f "Even more"
    puts $f "Line 3"
    close $f
    exec cat gorp.file
} "Line 1\nMore text\nEven more\nLine 3"

# I/O redirection: output and stderr to file.

file delete gorp.file
test exec-4.1 {redirecting output and stderr to file} {
    exec echo "test output" >& gorp.file
    exec cat gorp.file
} "test output"
test exec-4.2 {redirecting output and stderr to file} {
    list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
	    [exec cat gorp.file]
} {{} {foo bar}}
test exec-4.3 {redirecting output and stderr to file} {
    exec echo "first line" > gorp.file
    list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
	    [exec cat gorp.file]
} "{} {first line\nfoo bar}"
test exec-4.4 {redirecting output and stderr to file} {
    set f [open gorp.file w]
    puts $f "Line 1"
    flush $f
    exec echo "More text" >&@ $f
    exec echo >&@$f "Even more"
    puts $f "Line 3"
    close $f
    exec cat gorp.file
} "Line 1\nMore text\nEven more\nLine 3"
test exec-4.5 {redirecting output and stderr to file} {
    set f [open gorp.file w]
    puts $f "Line 1"
    flush $f
    exec >&@ $f sh -c "echo foo bar 1>&2"
    exec >&@$f sh -c "echo xyzzy 1>&2"
    puts $f "Line 3"
    close $f
    exec cat gorp.file
} "Line 1\nfoo bar\nxyzzy\nLine 3"

# I/O redirection: input from file.

exec echo "Just a few thoughts" > gorp.file

test exec-5.1 {redirecting input from file} {
    exec cat < gorp.file
} {Just a few thoughts}
test exec-5.2 {redirecting input from file} {
    exec cat | cat < gorp.file
} {Just a few thoughts}
test exec-5.3 {redirecting input from file} {
    exec cat < gorp.file | cat
} {Just a few thoughts}
test exec-5.4 {redirecting input from file} {
    exec < gorp.file cat | cat
} {Just a few thoughts}
test exec-5.5 {redirecting input from file} {
    exec cat <gorp.file
} {Just a few thoughts}
test exec-5.6 {redirecting input from file} {
    set f [open gorp.file r]
    set result [exec cat <@ $f]
    close $f
    set result
} {Just a few thoughts}
test exec-5.7 {redirecting input from file} {
    set f [open gorp.file r]
    set result [exec <@$f cat]
    close $f
    set result
} {Just a few thoughts}

# I/O redirection: standard error through a pipeline.

test exec-6.1 {redirecting stderr through a pipeline} {
    exec sh -c "echo foo bar" |& cat
} "foo bar"
test exec-6.2 {redirecting stderr through a pipeline} {
    exec sh -c "echo foo bar 1>&2" |& cat
} "foo bar"
test exec-6.3 {redirecting stderr through a pipeline} {
    exec sh -c "echo foo bar 1>&2" \
	|& cat |& cat
} "foo bar"

# I/O redirection: combinations.

file delete gorp.file2
test exec-7.1 {multiple I/O redirections} {
    exec << "command input" > gorp.file2 cat < gorp.file
    exec cat gorp.file2
} {Just a few thoughts}
test exec-7.2 {multiple I/O redirections} {
    exec < gorp.file << "command input" cat
} {command input}

# Long input to command and output from command.

set a [string repeat a 1000000]
test exec-8.1 {long input and output} {
    string length [exec cat << $a]
} 1000000

# More than 20 arguments to exec.

test exec-8.1 {long input and output} {
    exec echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23}

# Commands that return errors.

test exec-9.1 {commands returning errors} {
    catch {exec gorp456}
} {1}
test exec-9.2 {commands returning errors} {
    catch {exec echo foo | foo123} msg
} {1}
test exec-9.3 {commands returning errors} {
    list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
} {1}
test exec-9.4 {commands returning errors} jim {
    list [catch {exec false | echo "foo bar"} msg] $msg
} {1 {foo bar}}
test exec-9.5 {commands returning errors} {
    list [catch {exec gorp456 | echo a b c} msg]
} {1}
test exec-9.6 {commands returning errors} jim {
    list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
} {0 {error msg}}
test exec-9.7 {commands returning errors} jim {
    # Note: Use sleep here to ensure the order
    list [catch {exec sh -c "echo error msg 1 1>&2" \
		     | sh -c "sleep 0.1; echo error msg 2 1>&2"} msg] $msg
} {0 {error msg 1
error msg 2}}

# Errors in executing the Tcl command, as opposed to errors in the
# processes that are invoked.

test exec-10.1 {errors in exec invocation} {
    list [catch {exec} msg]
} {1}
test exec-10.2 {errors in exec invocation} {
    list [catch {exec | cat} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.3 {errors in exec invocation} {
    list [catch {exec cat |} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.4 {errors in exec invocation} {
    list [catch {exec cat | | cat} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.5 {errors in exec invocation} {
    list [catch {exec cat | |& cat} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.6 {errors in exec invocation} {
    list [catch {exec cat |&} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.7 {errors in exec invocation} {
    list [catch {exec cat <} msg] $msg
} {1 {can't specify "<" as last word in command}}
test exec-10.8 {errors in exec invocation} {
    list [catch {exec cat >} msg] $msg
} {1 {can't specify ">" as last word in command}}
test exec-10.9 {errors in exec invocation} {
    list [catch {exec cat <<} msg] $msg
} {1 {can't specify "<<" as last word in command}}
test exec-10.10 {errors in exec invocation} {
    list [catch {exec cat >>} msg] $msg
} {1 {can't specify ">>" as last word in command}}
test exec-10.11 {errors in exec invocation} {
    list [catch {exec cat >&} msg] $msg
} {1 {can't specify ">&" as last word in command}}
test exec-10.12 {errors in exec invocation} {
    list [catch {exec cat >>&} msg] $msg
} {1 {can't specify ">>&" as last word in command}}
test exec-10.13 {errors in exec invocation} {
    list [catch {exec cat >@} msg] $msg
} {1 {can't specify ">@" as last word in command}}
test exec-10.14 {errors in exec invocation} {
    list [catch {exec cat <@} msg] $msg
} {1 {can't specify "<@" as last word in command}}
test exec-10.15 {errors in exec invocation} {
    list [catch {exec cat < a/b/c} msg] [string tolower $msg]
} {1 {couldn't read file "a/b/c": no such file or directory}}
test exec-10.16 {errors in exec invocation} {
    list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
} {1 {couldn't write file "a/b/c": no such file or directory}}
test exec-10.17 {errors in exec invocation} {
    list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
} {1 {couldn't write file "a/b/c": no such file or directory}}
set f [open gorp.file w]
test exec-10.18 {errors in exec invocation} {
    list [catch {exec cat <<test <@ $f} msg]
} 1
close $f
set f [open gorp.file r]
test exec-10.19 {errors in exec invocation} {
    list [catch {exec cat <<test >@ $f} msg]
} 1
close $f

# Commands in background.

test exec-11.1 {commands in background} {
    set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
    expr $x<1000000
} 1
test exec-11.2 {commands in background} {
    list [catch {exec echo a &b} msg] $msg
} {0 {a &b}}
test exec-11.3 {commands in background} {
    llength [exec {*}$sleepx 0.1 &]
} 1
test exec-11.4 {commands in background} {
    llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
} 3

# Make sure that background commands are properly reaped when
# they eventually die.

exec {*}$sleepx 0.3

test exec-12.1 {reaping background processes} -constraints unix -body {
    for {set i 0} {$i < 20} {incr i} {
        exec echo foo > exec.tmp1 &
    }
    exec {*}$sleepx 0.1
    catch {exec ps | fgrep "echo foo" | fgrep -v grep | wc} msg
    lindex $msg 0
} -cleanup {
    file delete exec.tmp1
} -result 0

# Redirecting standard error separately from standard output

test exec-15.1 {standard error redirection} {
    exec echo "First line" > gorp.file
    list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
	    [exec cat gorp.file]
} {{} {foo bar}}
test exec-15.2 {standard error redirection} {
    list [exec sh -c "echo foo bar 1>&2" \
		| echo biz baz >gorp.file 2> gorp.file2] \
	    [exec cat gorp.file] \
	    [exec cat gorp.file2]
} {{} {biz baz} {foo bar}}
test exec-15.3 {standard error redirection} {
    list [exec sh -c "echo foo bar 1>&2" \
	        | echo biz baz 2>gorp.file > gorp.file2] \
	    [exec cat gorp.file] \
	    [exec cat gorp.file2]
} {{} {foo bar} {biz baz}}
test exec-15.4 {standard error redirection} {
    set f [open gorp.file w]
    puts $f "Line 1"
    flush $f
    exec sh -c "echo foo bar 1>&2" 2>@ $f
    puts $f "Line 3"
    close $f
    exec cat gorp.file
} {Line 1
foo bar
Line 3}
test exec-15.5 {standard error redirection} {
    exec echo "First line" > gorp.file
    exec sh -c "echo foo bar 1>&2" 2>> gorp.file
    exec cat gorp.file
} {First line
foo bar}
test exec-15.6 {standard error redirection} {
    exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
	    >& gorp.file 2> gorp.file2 | echo biz baz
    list [exec cat gorp.file] [exec cat gorp.file2]
} {{biz baz} {foo bar}}
test exec-15.7 {combine standard output/standard error} -body {
    exec sh -c "echo foo bar 1>&2" > gorp.file 2>@1
    exec cat gorp.file
} -cleanup {
    file delete gorp.file gorp.file2
} -result {foo bar}

test exec-16.1 {flush output before exec} -body {
    set f [open gorp.file w]
    puts $f "First line"
    exec echo "Second line" >@ $f
    puts $f "Third line"
    close $f
    exec cat gorp.file
} -cleanup {
    file delete gorp.file
} -result {First line
Second line
Third line}

file delete sleepx

testreport