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
20 needs cmd after eventloop
22 # Sleep which supports fractions of a second
23 if {[info commands sleep] eq {}} {
25 after [expr {int($n * 1000)}]
30 puts $f "#![info nameofexecutable]"
32 set seconds [lindex $argv 0]
33 after [expr {int($seconds * 1000)}]
36 #catch {exec chmod +x sleepx}
37 set sleepx [list [info nameofexecutable] sleepx]
41 test exec-1.1 {basic exec operation} {
44 test exec-1.2 {pipelining} {
45 exec echo a b c d | cat | cat
47 test exec-1.3 {pipelining} {
48 set a [exec echo a b c d | cat | wc]
49 list [scan $a "%d %d %d" b c d] $b $c
51 set arg {12345678901234567890123456789012345678901234567890}
52 set arg "$arg$arg$arg$arg$arg$arg"
53 test exec-1.4 {long command lines} {
58 # I/O redirection: input from Tcl command.
60 test exec-2.1 {redirecting input from immediate source} {
61 exec cat << "Sample text"
63 test exec-2.2 {redirecting input from immediate source} {
64 exec << "Sample text" cat | cat
66 test exec-2.3 {redirecting input from immediate source} {
67 exec cat << "Sample text" | cat
69 test exec-2.4 {redirecting input from immediate source} {
70 exec cat | cat << "Sample text"
72 test exec-2.5 {redirecting input from immediate source} {
73 exec cat "<<Joined to arrows"
75 test exec-2.6 {redirecting input from immediate source, with UTF} {
76 # If this fails, it may give back:
77 # "\uC3\uA9\uC3\uA0\uC3\uBC\uC3\uB1"
78 # If it does, this means that the UTF -> external conversion did not
79 # occur before writing out the temp file.
80 exec cat << "\uE9\uE0\uFC\uF1"
82 test exec-2.7 {redirecting input from immediate source with nulls} {
83 exec cat << "Sample\0text"
86 # I/O redirection: output to file.
89 test exec-3.1 {redirecting output to file} {
90 exec echo "Some simple words" > gorp.file
93 test exec-3.2 {redirecting output to file} {
94 exec echo "More simple words" | >gorp.file cat | cat
97 test exec-3.3 {redirecting output to file} {
98 exec > gorp.file echo "Different simple words" | cat | cat
100 } "Different simple words"
101 test exec-3.4 {redirecting output to file} {
102 exec echo "Some simple words" >gorp.file
104 } "Some simple words"
105 test exec-3.5 {redirecting output to file} {
106 exec echo "First line" >gorp.file
107 exec echo "Second line" >> gorp.file
109 } "First line\nSecond line"
110 test exec-3.6 {redirecting output to file} {
111 exec echo "First line" >gorp.file
112 exec echo "Second line" >>gorp.file
114 } "First line\nSecond line"
115 test exec-3.7 {redirecting output to file} {
116 set f [open gorp.file w]
119 exec echo "More text" >@ $f
120 exec echo >@$f "Even more"
124 } "Line 1\nMore text\nEven more\nLine 3"
126 # I/O redirection: output and stderr to file.
128 file delete gorp.file
129 test exec-4.1 {redirecting output and stderr to file} {
130 exec echo "test output" >& gorp.file
133 test exec-4.2 {redirecting output and stderr to file} {
134 list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
137 test exec-4.3 {redirecting output and stderr to file} {
138 exec echo "first line" > gorp.file
139 list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
141 } "{} {first line\nfoo bar}"
142 test exec-4.4 {redirecting output and stderr to file} {
143 set f [open gorp.file w]
146 exec echo "More text" >&@ $f
147 exec echo >&@$f "Even more"
151 } "Line 1\nMore text\nEven more\nLine 3"
152 test exec-4.5 {redirecting output and stderr to file} {
153 set f [open gorp.file w]
156 exec >&@ $f sh -c "echo foo bar 1>&2"
157 exec >&@$f sh -c "echo xyzzy 1>&2"
161 } "Line 1\nfoo bar\nxyzzy\nLine 3"
163 # I/O redirection: input from file.
165 exec echo "Just a few thoughts" > gorp.file
167 test exec-5.1 {redirecting input from file} {
169 } {Just a few thoughts}
170 test exec-5.2 {redirecting input from file} {
171 exec cat | cat < gorp.file
172 } {Just a few thoughts}
173 test exec-5.3 {redirecting input from file} {
174 exec cat < gorp.file | cat
175 } {Just a few thoughts}
176 test exec-5.4 {redirecting input from file} {
177 exec < gorp.file cat | cat
178 } {Just a few thoughts}
179 test exec-5.5 {redirecting input from file} {
181 } {Just a few thoughts}
182 test exec-5.6 {redirecting input from file} {
183 set f [open gorp.file r]
184 set result [exec cat <@ $f]
187 } {Just a few thoughts}
188 test exec-5.7 {redirecting input from file} {
189 set f [open gorp.file r]
190 set result [exec <@$f cat]
193 } {Just a few thoughts}
195 # I/O redirection: standard error through a pipeline.
197 test exec-6.1 {redirecting stderr through a pipeline} {
198 exec sh -c "echo foo bar" |& cat
200 test exec-6.2 {redirecting stderr through a pipeline} {
201 exec sh -c "echo foo bar 1>&2" |& cat
203 test exec-6.3 {redirecting stderr through a pipeline} {
204 exec sh -c "echo foo bar 1>&2" \
208 # I/O redirection: combinations.
210 file delete gorp.file2
211 test exec-7.1 {multiple I/O redirections} {
212 exec << "command input" > gorp.file2 cat < gorp.file
214 } {Just a few thoughts}
215 test exec-7.2 {multiple I/O redirections} {
216 exec < gorp.file << "command input" cat
219 # Long input to command and output from command.
221 set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n"
222 set a [concat $a $a $a $a]
223 set a [concat $a $a $a $a]
224 set a [concat $a $a $a $a]
225 set a [concat $a $a $a $a]
226 test exec-8.1 {long input and output} {
230 # More than 20 arguments to exec.
232 test exec-8.1 {long input and output} {
233 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
234 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23}
236 # Commands that return errors.
238 test exec-9.1 {commands returning errors} {
241 test exec-9.2 {commands returning errors} {
242 catch {exec echo foo | foo123} msg
244 test exec-9.3 {commands returning errors} {
245 list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
247 test exec-9.4 {commands returning errors} jim {
248 list [catch {exec false | echo "foo bar"} msg] $msg
250 test exec-9.5 {commands returning errors} {
251 list [catch {exec gorp456 | echo a b c} msg]
253 test exec-9.6 {commands returning errors} jim {
254 list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
256 test exec-9.7 {commands returning errors} jim {
257 # Note: Use sleep here to ensure the order
258 list [catch {exec sh -c "echo error msg 1 1>&2" \
259 | sh -c "sleep 0.1; echo error msg 2 1>&2"} msg] $msg
263 # Errors in executing the Tcl command, as opposed to errors in the
264 # processes that are invoked.
266 test exec-10.1 {errors in exec invocation} {
267 list [catch {exec} msg]
269 test exec-10.2 {errors in exec invocation} {
270 list [catch {exec | cat} msg] $msg
271 } {1 {illegal use of | or |& in command}}
272 test exec-10.3 {errors in exec invocation} {
273 list [catch {exec cat |} msg] $msg
274 } {1 {illegal use of | or |& in command}}
275 test exec-10.4 {errors in exec invocation} {
276 list [catch {exec cat | | cat} msg] $msg
277 } {1 {illegal use of | or |& in command}}
278 test exec-10.5 {errors in exec invocation} {
279 list [catch {exec cat | |& cat} msg] $msg
280 } {1 {illegal use of | or |& in command}}
281 test exec-10.6 {errors in exec invocation} {
282 list [catch {exec cat |&} msg] $msg
283 } {1 {illegal use of | or |& in command}}
284 test exec-10.7 {errors in exec invocation} {
285 list [catch {exec cat <} msg] $msg
286 } {1 {can't specify "<" as last word in command}}
287 test exec-10.8 {errors in exec invocation} {
288 list [catch {exec cat >} msg] $msg
289 } {1 {can't specify ">" as last word in command}}
290 test exec-10.9 {errors in exec invocation} {
291 list [catch {exec cat <<} msg] $msg
292 } {1 {can't specify "<<" as last word in command}}
293 test exec-10.10 {errors in exec invocation} {
294 list [catch {exec cat >>} msg] $msg
295 } {1 {can't specify ">>" as last word in command}}
296 test exec-10.11 {errors in exec invocation} {
297 list [catch {exec cat >&} msg] $msg
298 } {1 {can't specify ">&" as last word in command}}
299 test exec-10.12 {errors in exec invocation} {
300 list [catch {exec cat >>&} msg] $msg
301 } {1 {can't specify ">>&" as last word in command}}
302 test exec-10.13 {errors in exec invocation} {
303 list [catch {exec cat >@} msg] $msg
304 } {1 {can't specify ">@" as last word in command}}
305 test exec-10.14 {errors in exec invocation} {
306 list [catch {exec cat <@} msg] $msg
307 } {1 {can't specify "<@" as last word in command}}
308 test exec-10.15 {errors in exec invocation} {
309 list [catch {exec cat < a/b/c} msg] [string tolower $msg]
310 } {1 {couldn't read file "a/b/c": no such file or directory}}
311 test exec-10.16 {errors in exec invocation} {
312 list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
313 } {1 {couldn't write file "a/b/c": no such file or directory}}
314 test exec-10.17 {errors in exec invocation} {
315 list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
316 } {1 {couldn't write file "a/b/c": no such file or directory}}
317 set f [open gorp.file w]
318 test exec-10.18 {errors in exec invocation} {
319 list [catch {exec cat <<test <@ $f} msg]
322 set f [open gorp.file r]
323 test exec-10.19 {errors in exec invocation} {
324 list [catch {exec cat <<test >@ $f} msg]
328 # Commands in background.
330 test exec-11.1 {commands in background} {
331 set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
334 test exec-11.2 {commands in background} {
335 list [catch {exec echo a &b} msg] $msg
337 test exec-11.3 {commands in background} {
338 llength [exec {*}$sleepx 0.1 &]
340 test exec-11.4 {commands in background} {
341 llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
344 # Make sure that background commands are properly reaped when
345 # they eventually die.
349 test exec-12.1 {reaping background processes} -body {
350 for {set i 0} {$i < 20} {incr i} {
351 exec echo foo > exec.tmp1 &
354 catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
357 file delete exec.tmp1
360 # Redirecting standard error separately from standard output
362 test exec-15.1 {standard error redirection} {
363 exec echo "First line" > gorp.file
364 list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
367 test exec-15.2 {standard error redirection} {
368 list [exec sh -c "echo foo bar 1>&2" \
369 | echo biz baz >gorp.file 2> gorp.file2] \
370 [exec cat gorp.file] \
371 [exec cat gorp.file2]
372 } {{} {biz baz} {foo bar}}
373 test exec-15.3 {standard error redirection} {
374 list [exec sh -c "echo foo bar 1>&2" \
375 | echo biz baz 2>gorp.file > gorp.file2] \
376 [exec cat gorp.file] \
377 [exec cat gorp.file2]
378 } {{} {foo bar} {biz baz}}
379 test exec-15.4 {standard error redirection} {
380 set f [open gorp.file w]
383 exec sh -c "echo foo bar 1>&2" 2>@ $f
390 test exec-15.5 {standard error redirection} {
391 exec echo "First line" > gorp.file
392 exec sh -c "echo foo bar 1>&2" 2>> gorp.file
396 test exec-15.6 {standard error redirection} {
397 exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
398 >& gorp.file 2> gorp.file2 | echo biz baz
399 list [exec cat gorp.file] [exec cat gorp.file2]
400 } {{biz baz} {foo bar}}
401 test exec-15.7 {combine standard output/standard error} -body {
402 exec sh -c "echo foo bar 1>&2" > gorp.file 2>@1
405 file delete gorp.file gorp.file2
408 test exec-16.1 {flush output before exec} -body {
409 set f [open gorp.file w]
411 exec echo "Second line" >@ $f
416 file delete gorp.file
417 } -result {First line