auto.def: tclprefix should not be enabled by default
[jimtcl.git] / tests / exec.test
blob76a1b2db1b1ba7913f2de09d3da915687e74708b
1 # Commands covered:  exec
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands.  Sourcing this file into Tcl runs the tests and
5 # generates output for errors.  No output means no errors were found.
7 # Copyright (c) 1991-1994 The Regents of the University of California.
8 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 # RCS: @(#) $Id: exec.test,v 1.8.2.1 2001/10/17 19:29:25 das Exp $
16 source [file dirname [info script]]/testing.tcl
18 needs cmd exec
19 needs cmd flush
20 needs cmd after eventloop
22 testConstraint unix [expr {$tcl_platform(platform) eq {unix}}]
24 # Sleep which supports fractions of a second
25 if {[info commands sleep] eq {}} {
26     proc sleep {n} {
27         after [expr {int($n * 1000)}]
28     }
31 set f [open sleepx w]
32 puts $f "#![info nameofexecutable]"
33 puts $f {
34     set seconds [lindex $argv 0]
35     after [expr {int($seconds * 1000)}]
37 close $f
38 #catch {exec chmod +x sleepx}
39 set sleepx [list [info nameofexecutable] sleepx]
41 # Basic operations.
43 test exec-1.1 {basic exec operation} {
44     exec echo a b c
45 } "a b c"
46 test exec-1.2 {pipelining} {
47     exec echo a b c d | cat | cat
48 } "a b c d"
49 test exec-1.3 {pipelining} {
50     set a [exec echo a b c d | cat | wc]
51     list [scan $a "%d %d %d" b c d] $b $c
52 } {3 1 4}
53 set arg {12345678901234567890123456789012345678901234567890}
54 set arg "$arg$arg$arg$arg$arg$arg"
55 test exec-1.4 {long command lines} {
56     exec echo $arg
57 } $arg
58 set arg {}
60 # I/O redirection: input from Tcl command.
62 test exec-2.1 {redirecting input from immediate source} {
63     exec cat << "Sample text"
64 } {Sample text}
65 test exec-2.2 {redirecting input from immediate source} {
66     exec << "Sample text" cat | cat
67 } {Sample text}
68 test exec-2.3 {redirecting input from immediate source} {
69     exec cat << "Sample text" | cat
70 } {Sample text}
71 test exec-2.4 {redirecting input from immediate source} {
72     exec cat | cat << "Sample text"
73 } {Sample text}
74 test exec-2.5 {redirecting input from immediate source} {
75     exec cat "<<Joined to arrows"
76 } {Joined to arrows}
77 test exec-2.6 {redirecting input from immediate source, with UTF} {
78     # If this fails, it may give back:
79     # "\uC3\uA9\uC3\uA0\uC3\uBC\uC3\uB1"
80     # If it does, this means that the UTF -> external conversion did not 
81     # occur before writing out the temp file.
82     exec cat << "\uE9\uE0\uFC\uF1"
83 } "\uE9\uE0\uFC\uF1"
84 test exec-2.7 {redirecting input from immediate source with nulls} {
85     exec cat << "Sample\0text"
86 } "Sample\0text"
88 # I/O redirection: output to file.
90 file delete gorp.file
91 test exec-3.1 {redirecting output to file} {
92     exec echo "Some simple words" > gorp.file
93     exec cat gorp.file
94 } "Some simple words"
95 test exec-3.2 {redirecting output to file} {
96     exec echo "More simple words" | >gorp.file cat | cat
97     exec cat gorp.file
98 } "More simple words"
99 test exec-3.3 {redirecting output to file} {
100     exec > gorp.file echo "Different simple words" | cat | cat
101     exec cat gorp.file
102 } "Different simple words"
103 test exec-3.4 {redirecting output to file} {
104     exec echo "Some simple words" >gorp.file
105     exec cat gorp.file
106 } "Some simple words"
107 test exec-3.5 {redirecting output to file} {
108     exec echo "First line" >gorp.file
109     exec echo "Second line" >> gorp.file
110     exec cat gorp.file
111 } "First line\nSecond line"
112 test exec-3.6 {redirecting output to file} {
113     exec echo "First line" >gorp.file
114     exec echo "Second line" >>gorp.file
115     exec cat gorp.file
116 } "First line\nSecond line"
117 test exec-3.7 {redirecting output to file} {
118     set f [open gorp.file w]
119     puts $f "Line 1"
120     flush $f
121     exec echo "More text" >@ $f
122     exec echo >@$f "Even more"
123     puts $f "Line 3"
124     close $f
125     exec cat gorp.file
126 } "Line 1\nMore text\nEven more\nLine 3"
128 # I/O redirection: output and stderr to file.
130 file delete gorp.file
131 test exec-4.1 {redirecting output and stderr to file} {
132     exec echo "test output" >& gorp.file
133     exec cat gorp.file
134 } "test output"
135 test exec-4.2 {redirecting output and stderr to file} {
136     list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
137             [exec cat gorp.file]
138 } {{} {foo bar}}
139 test exec-4.3 {redirecting output and stderr to file} {
140     exec echo "first line" > gorp.file
141     list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
142             [exec cat gorp.file]
143 } "{} {first line\nfoo bar}"
144 test exec-4.4 {redirecting output and stderr to file} {
145     set f [open gorp.file w]
146     puts $f "Line 1"
147     flush $f
148     exec echo "More text" >&@ $f
149     exec echo >&@$f "Even more"
150     puts $f "Line 3"
151     close $f
152     exec cat gorp.file
153 } "Line 1\nMore text\nEven more\nLine 3"
154 test exec-4.5 {redirecting output and stderr to file} {
155     set f [open gorp.file w]
156     puts $f "Line 1"
157     flush $f
158     exec >&@ $f sh -c "echo foo bar 1>&2"
159     exec >&@$f sh -c "echo xyzzy 1>&2"
160     puts $f "Line 3"
161     close $f
162     exec cat gorp.file
163 } "Line 1\nfoo bar\nxyzzy\nLine 3"
165 # I/O redirection: input from file.
167 exec echo "Just a few thoughts" > gorp.file
169 test exec-5.1 {redirecting input from file} {
170     exec cat < gorp.file
171 } {Just a few thoughts}
172 test exec-5.2 {redirecting input from file} {
173     exec cat | cat < gorp.file
174 } {Just a few thoughts}
175 test exec-5.3 {redirecting input from file} {
176     exec cat < gorp.file | cat
177 } {Just a few thoughts}
178 test exec-5.4 {redirecting input from file} {
179     exec < gorp.file cat | cat
180 } {Just a few thoughts}
181 test exec-5.5 {redirecting input from file} {
182     exec cat <gorp.file
183 } {Just a few thoughts}
184 test exec-5.6 {redirecting input from file} {
185     set f [open gorp.file r]
186     set result [exec cat <@ $f]
187     close $f
188     set result
189 } {Just a few thoughts}
190 test exec-5.7 {redirecting input from file} {
191     set f [open gorp.file r]
192     set result [exec <@$f cat]
193     close $f
194     set result
195 } {Just a few thoughts}
197 # I/O redirection: standard error through a pipeline.
199 test exec-6.1 {redirecting stderr through a pipeline} {
200     exec sh -c "echo foo bar" |& cat
201 } "foo bar"
202 test exec-6.2 {redirecting stderr through a pipeline} {
203     exec sh -c "echo foo bar 1>&2" |& cat
204 } "foo bar"
205 test exec-6.3 {redirecting stderr through a pipeline} {
206     exec sh -c "echo foo bar 1>&2" \
207         |& cat |& cat
208 } "foo bar"
210 # I/O redirection: combinations.
212 file delete gorp.file2
213 test exec-7.1 {multiple I/O redirections} {
214     exec << "command input" > gorp.file2 cat < gorp.file
215     exec cat gorp.file2
216 } {Just a few thoughts}
217 test exec-7.2 {multiple I/O redirections} {
218     exec < gorp.file << "command input" cat
219 } {command input}
221 # Long input to command and output from command.
223 set a [string repeat a 1000000]
224 test exec-8.1 {long input and output} {
225     string length [exec cat << $a]
226 } 1000000
228 # More than 20 arguments to exec.
230 test exec-8.1 {long input and output} {
231     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
232 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23}
234 # Commands that return errors.
236 test exec-9.1 {commands returning errors} {
237     catch {exec gorp456}
238 } {1}
239 test exec-9.2 {commands returning errors} {
240     catch {exec echo foo | foo123} msg
241 } {1}
242 test exec-9.3 {commands returning errors} {
243     list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
244 } {1}
245 test exec-9.4 {commands returning errors} jim {
246     list [catch {exec false | echo "foo bar"} msg] $msg
247 } {1 {foo bar}}
248 test exec-9.5 {commands returning errors} {
249     list [catch {exec gorp456 | echo a b c} msg]
250 } {1}
251 test exec-9.6 {commands returning errors} jim {
252     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
253 } {0 {error msg}}
254 test exec-9.7 {commands returning errors} jim {
255     # Note: Use sleep here to ensure the order
256     list [catch {exec sh -c "echo error msg 1 1>&2" \
257                      | sh -c "sleep 0.1; echo error msg 2 1>&2"} msg] $msg
258 } {0 {error msg 1
259 error msg 2}}
261 # Errors in executing the Tcl command, as opposed to errors in the
262 # processes that are invoked.
264 test exec-10.1 {errors in exec invocation} {
265     list [catch {exec} msg]
266 } {1}
267 test exec-10.2 {errors in exec invocation} {
268     list [catch {exec | cat} msg] $msg
269 } {1 {illegal use of | or |& in command}}
270 test exec-10.3 {errors in exec invocation} {
271     list [catch {exec cat |} msg] $msg
272 } {1 {illegal use of | or |& in command}}
273 test exec-10.4 {errors in exec invocation} {
274     list [catch {exec cat | | cat} msg] $msg
275 } {1 {illegal use of | or |& in command}}
276 test exec-10.5 {errors in exec invocation} {
277     list [catch {exec cat | |& cat} msg] $msg
278 } {1 {illegal use of | or |& in command}}
279 test exec-10.6 {errors in exec invocation} {
280     list [catch {exec cat |&} msg] $msg
281 } {1 {illegal use of | or |& in command}}
282 test exec-10.7 {errors in exec invocation} {
283     list [catch {exec cat <} msg] $msg
284 } {1 {can't specify "<" as last word in command}}
285 test exec-10.8 {errors in exec invocation} {
286     list [catch {exec cat >} msg] $msg
287 } {1 {can't specify ">" as last word in command}}
288 test exec-10.9 {errors in exec invocation} {
289     list [catch {exec cat <<} msg] $msg
290 } {1 {can't specify "<<" as last word in command}}
291 test exec-10.10 {errors in exec invocation} {
292     list [catch {exec cat >>} msg] $msg
293 } {1 {can't specify ">>" as last word in command}}
294 test exec-10.11 {errors in exec invocation} {
295     list [catch {exec cat >&} msg] $msg
296 } {1 {can't specify ">&" as last word in command}}
297 test exec-10.12 {errors in exec invocation} {
298     list [catch {exec cat >>&} msg] $msg
299 } {1 {can't specify ">>&" as last word in command}}
300 test exec-10.13 {errors in exec invocation} {
301     list [catch {exec cat >@} msg] $msg
302 } {1 {can't specify ">@" as last word in command}}
303 test exec-10.14 {errors in exec invocation} {
304     list [catch {exec cat <@} msg] $msg
305 } {1 {can't specify "<@" as last word in command}}
306 test exec-10.15 {errors in exec invocation} {
307     list [catch {exec cat < a/b/c} msg] [string tolower $msg]
308 } {1 {couldn't read file "a/b/c": no such file or directory}}
309 test exec-10.16 {errors in exec invocation} {
310     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
311 } {1 {couldn't write file "a/b/c": no such file or directory}}
312 test exec-10.17 {errors in exec invocation} {
313     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
314 } {1 {couldn't write file "a/b/c": no such file or directory}}
315 set f [open gorp.file w]
316 test exec-10.18 {errors in exec invocation} {
317     list [catch {exec cat <<test <@ $f} msg]
318 } 1
319 close $f
320 set f [open gorp.file r]
321 test exec-10.19 {errors in exec invocation} {
322     list [catch {exec cat <<test >@ $f} msg]
323 } 1
324 close $f
326 # Commands in background.
328 test exec-11.1 {commands in background} {
329     set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
330     expr $x<1000000
331 } 1
332 test exec-11.2 {commands in background} {
333     list [catch {exec echo a &b} msg] $msg
334 } {0 {a &b}}
335 test exec-11.3 {commands in background} {
336     llength [exec {*}$sleepx 0.1 &]
337 } 1
338 test exec-11.4 {commands in background} {
339     llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
340 } 3
342 # Make sure that background commands are properly reaped when
343 # they eventually die.
345 exec {*}$sleepx 0.3
347 test exec-12.1 {reaping background processes} -constraints unix -body {
348     for {set i 0} {$i < 20} {incr i} {
349         exec echo foo > exec.tmp1 &
350     }
351     exec {*}$sleepx 0.1
352     catch {exec ps | fgrep "echo foo" | fgrep -v grep | wc} msg
353     lindex $msg 0
354 } -cleanup {
355     file delete exec.tmp1
356 } -result 0
358 # Redirecting standard error separately from standard output
360 test exec-15.1 {standard error redirection} {
361     exec echo "First line" > gorp.file
362     list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
363             [exec cat gorp.file]
364 } {{} {foo bar}}
365 test exec-15.2 {standard error redirection} {
366     list [exec sh -c "echo foo bar 1>&2" \
367                 | echo biz baz >gorp.file 2> gorp.file2] \
368             [exec cat gorp.file] \
369             [exec cat gorp.file2]
370 } {{} {biz baz} {foo bar}}
371 test exec-15.3 {standard error redirection} {
372     list [exec sh -c "echo foo bar 1>&2" \
373                 | echo biz baz 2>gorp.file > gorp.file2] \
374             [exec cat gorp.file] \
375             [exec cat gorp.file2]
376 } {{} {foo bar} {biz baz}}
377 test exec-15.4 {standard error redirection} {
378     set f [open gorp.file w]
379     puts $f "Line 1"
380     flush $f
381     exec sh -c "echo foo bar 1>&2" 2>@ $f
382     puts $f "Line 3"
383     close $f
384     exec cat gorp.file
385 } {Line 1
386 foo bar
387 Line 3}
388 test exec-15.5 {standard error redirection} {
389     exec echo "First line" > gorp.file
390     exec sh -c "echo foo bar 1>&2" 2>> gorp.file
391     exec cat gorp.file
392 } {First line
393 foo bar}
394 test exec-15.6 {standard error redirection} {
395     exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
396             >& gorp.file 2> gorp.file2 | echo biz baz
397     list [exec cat gorp.file] [exec cat gorp.file2]
398 } {{biz baz} {foo bar}}
399 test exec-15.7 {combine standard output/standard error} -body {
400     exec sh -c "echo foo bar 1>&2" > gorp.file 2>@1
401     exec cat gorp.file
402 } -cleanup {
403     file delete gorp.file gorp.file2
404 } -result {foo bar}
406 test exec-16.1 {flush output before exec} -body {
407     set f [open gorp.file w]
408     puts $f "First line"
409     exec echo "Second line" >@ $f
410     puts $f "Third line"
411     close $f
412     exec cat gorp.file
413 } -cleanup {
414     file delete gorp.file
415 } -result {First line
416 Second line
417 Third line}
419 file delete sleepx
421 testreport