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
21 testConstraint unix [expr {$tcl_platform(platform) eq {unix}}]
23 # Sleep which supports fractions of a second
24 if {[info commands sleep] eq {}} {
35 #catch {exec chmod +x sleepx}
36 set sleepx [list sh sleepx]
40 test exec-1.1 {basic exec operation} {
43 test exec-1.2 {pipelining} {
44 exec echo a b c d | cat | cat
46 test exec-1.3 {pipelining} {
47 set a [exec echo a b c d | cat | wc]
48 list [scan $a "%d %d %d" b c d] $b $c
50 set arg {12345678901234567890123456789012345678901234567890}
51 set arg "$arg$arg$arg$arg$arg$arg"
52 test exec-1.4 {long command lines} {
57 # I/O redirection: input from Tcl command.
59 test exec-2.1 {redirecting input from immediate source} {
60 exec cat << "Sample text"
62 test exec-2.2 {redirecting input from immediate source} {
63 exec << "Sample text" cat | cat
65 test exec-2.3 {redirecting input from immediate source} {
66 exec cat << "Sample text" | cat
68 test exec-2.4 {redirecting input from immediate source} {
69 exec cat | cat << "Sample text"
71 test exec-2.5 {redirecting input from immediate source} {
72 exec cat "<<Joined to arrows"
74 test exec-2.6 {redirecting input from immediate source, with UTF} {
75 # If this fails, it may give back:
76 # "\uC3\uA9\uC3\uA0\uC3\uBC\uC3\uB1"
77 # If it does, this means that the UTF -> external conversion did not
78 # occur before writing out the temp file.
79 exec cat << "\uE9\uE0\uFC\uF1"
81 test exec-2.7 {redirecting input from immediate source with nulls} {
82 exec cat << "Sample\0text"
85 # I/O redirection: output to file.
88 test exec-3.1 {redirecting output to file} {
89 exec echo "Some simple words" > gorp.file
92 test exec-3.2 {redirecting output to file} {
93 exec echo "More simple words" | >gorp.file cat | cat
96 test exec-3.3 {redirecting output to file} {
97 exec > gorp.file echo "Different simple words" | cat | cat
99 } "Different simple words"
100 test exec-3.4 {redirecting output to file} {
101 exec echo "Some simple words" >gorp.file
103 } "Some simple words"
104 test exec-3.5 {redirecting output to file} {
105 exec echo "First line" >gorp.file
106 exec echo "Second line" >> gorp.file
108 } "First line\nSecond line"
109 test exec-3.6 {redirecting output to file} {
110 exec echo "First line" >gorp.file
111 exec echo "Second line" >>gorp.file
113 } "First line\nSecond line"
114 test exec-3.7 {redirecting output to file} {
115 set f [open gorp.file w]
118 exec echo "More text" >@ $f
119 exec echo >@$f "Even more"
123 } "Line 1\nMore text\nEven more\nLine 3"
125 # I/O redirection: output and stderr to file.
127 file delete gorp.file
128 test exec-4.1 {redirecting output and stderr to file} {
129 exec echo "test output" >& gorp.file
132 test exec-4.2 {redirecting output and stderr to file} {
133 list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
136 test exec-4.3 {redirecting output and stderr to file} {
137 exec echo "first line" > gorp.file
138 list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
140 } "{} {first line\nfoo bar}"
141 test exec-4.4 {redirecting output and stderr to file} {
142 set f [open gorp.file w]
145 exec echo "More text" >&@ $f
146 exec echo >&@$f "Even more"
150 } "Line 1\nMore text\nEven more\nLine 3"
151 test exec-4.5 {redirecting output and stderr to file} {
152 set f [open gorp.file w]
155 exec >&@ $f sh -c "echo foo bar 1>&2"
156 exec >&@$f sh -c "echo xyzzy 1>&2"
160 } "Line 1\nfoo bar\nxyzzy\nLine 3"
162 # I/O redirection: input from file.
164 exec echo "Just a few thoughts" > gorp.file
166 test exec-5.1 {redirecting input from file} {
168 } {Just a few thoughts}
169 test exec-5.2 {redirecting input from file} {
170 exec cat | cat < gorp.file
171 } {Just a few thoughts}
172 test exec-5.3 {redirecting input from file} {
173 exec cat < gorp.file | cat
174 } {Just a few thoughts}
175 test exec-5.4 {redirecting input from file} {
176 exec < gorp.file cat | cat
177 } {Just a few thoughts}
178 test exec-5.5 {redirecting input from file} {
180 } {Just a few thoughts}
181 test exec-5.6 {redirecting input from file} {
182 set f [open gorp.file r]
183 set result [exec cat <@ $f]
186 } {Just a few thoughts}
187 test exec-5.7 {redirecting input from file} {
188 set f [open gorp.file r]
189 set result [exec <@$f cat]
192 } {Just a few thoughts}
194 # I/O redirection: standard error through a pipeline.
196 test exec-6.1 {redirecting stderr through a pipeline} {
197 exec sh -c "echo foo bar" |& cat
199 test exec-6.2 {redirecting stderr through a pipeline} {
200 exec sh -c "echo foo bar 1>&2" |& cat
202 test exec-6.3 {redirecting stderr through a pipeline} {
203 exec sh -c "echo foo bar 1>&2" \
207 # I/O redirection: combinations.
209 file delete gorp.file2
210 test exec-7.1 {multiple I/O redirections} {
211 exec << "command input" > gorp.file2 cat < gorp.file
213 } {Just a few thoughts}
214 test exec-7.2 {multiple I/O redirections} {
215 exec < gorp.file << "command input" cat
218 # Long input to command and output from command.
220 set a [string repeat a 1000000]
221 test exec-8.1 {long input and output} {
222 string length [exec cat << $a]
225 # More than 20 arguments to exec.
227 test exec-8.1 {long input and output} {
228 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
229 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23}
231 # Commands that return errors.
233 test exec-9.1 {commands returning errors} {
236 test exec-9.2 {commands returning errors} {
237 catch {exec echo foo | foo123} msg
239 test exec-9.3 {commands returning errors} {
240 list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
242 test exec-9.4 {commands returning errors} jim {
243 list [catch {exec false | echo "foo bar"} msg] $msg
245 test exec-9.5 {commands returning errors} {
246 list [catch {exec gorp456 | echo a b c} msg]
248 test exec-9.6 {commands returning errors} jim {
249 list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
251 test exec-9.7 {commands returning errors} jim {
252 # Note: Use sleep here to ensure the order
253 list [catch {exec sh -c "echo error msg 1 1>&2" \
254 | sh -c "sleep 0.1; echo error msg 2 1>&2"} msg] $msg
258 # Errors in executing the Tcl command, as opposed to errors in the
259 # processes that are invoked.
261 test exec-10.1 {errors in exec invocation} {
262 list [catch {exec} msg]
264 test exec-10.2 {errors in exec invocation} {
265 list [catch {exec | cat} msg] $msg
266 } {1 {illegal use of | or |& in command}}
267 test exec-10.3 {errors in exec invocation} {
268 list [catch {exec cat |} msg] $msg
269 } {1 {illegal use of | or |& in command}}
270 test exec-10.4 {errors in exec invocation} {
271 list [catch {exec cat | | cat} msg] $msg
272 } {1 {illegal use of | or |& in command}}
273 test exec-10.5 {errors in exec invocation} {
274 list [catch {exec cat | |& cat} msg] $msg
275 } {1 {illegal use of | or |& in command}}
276 test exec-10.6 {errors in exec invocation} {
277 list [catch {exec cat |&} msg] $msg
278 } {1 {illegal use of | or |& in command}}
279 test exec-10.7 {errors in exec invocation} {
280 list [catch {exec cat <} msg] $msg
281 } {1 {can't specify "<" as last word in command}}
282 test exec-10.8 {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.9 {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.10 {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.11 {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.12 {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.13 {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.14 {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.15 {errors in exec invocation} {
304 list [catch {exec cat < a/b/c} msg] [string tolower $msg]
305 } {1 {couldn't read file "a/b/c": no such file or directory}}
306 test exec-10.16 {errors in exec invocation} {
307 list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
308 } {1 {couldn't write file "a/b/c": no such file or directory}}
309 test exec-10.17 {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 set f [open gorp.file w]
313 test exec-10.18 {errors in exec invocation} {
314 list [catch {exec cat <<test <@ $f} msg]
317 set f [open gorp.file r]
318 test exec-10.19 {errors in exec invocation} {
319 list [catch {exec cat <<test >@ $f} msg]
323 # Commands in background.
325 test exec-11.1 {commands in background} {
326 set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
329 test exec-11.2 {commands in background} {
330 list [catch {exec echo a &b} msg] $msg
332 test exec-11.3 {commands in background} {
333 llength [exec {*}$sleepx 0.1 &]
335 test exec-11.4 {commands in background} {
336 llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
339 # Make sure that background commands are properly reaped when
340 # they eventually die.
344 test exec-12.1 {reaping background processes} -constraints unix -body {
345 for {set i 0} {$i < 20} {incr i} {
346 exec echo foo > exec.tmp1 &
349 catch {exec ps | fgrep "echo foo" | fgrep -v grep | wc} msg
352 file delete exec.tmp1
355 # Redirecting standard error separately from standard output
357 test exec-15.1 {standard error redirection} {
358 exec echo "First line" > gorp.file
359 list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
362 test exec-15.2 {standard error redirection} {
363 list [exec sh -c "echo foo bar 1>&2" \
364 | echo biz baz >gorp.file 2> gorp.file2] \
365 [exec cat gorp.file] \
366 [exec cat gorp.file2]
367 } {{} {biz baz} {foo bar}}
368 test exec-15.3 {standard error redirection} {
369 list [exec sh -c "echo foo bar 1>&2" \
370 | echo biz baz 2>gorp.file > gorp.file2] \
371 [exec cat gorp.file] \
372 [exec cat gorp.file2]
373 } {{} {foo bar} {biz baz}}
374 test exec-15.4 {standard error redirection} {
375 set f [open gorp.file w]
378 exec sh -c "echo foo bar 1>&2" 2>@ $f
385 test exec-15.5 {standard error redirection} {
386 exec echo "First line" > gorp.file
387 exec sh -c "echo foo bar 1>&2" 2>> gorp.file
391 test exec-15.6 {standard error redirection} {
392 exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
393 >& gorp.file 2> gorp.file2 | echo biz baz
394 list [exec cat gorp.file] [exec cat gorp.file2]
395 } {{biz baz} {foo bar}}
396 test exec-15.7 {combine standard output/standard error} -body {
397 exec sh -c "echo foo bar 1>&2" > gorp.file 2>@1
400 file delete gorp.file gorp.file2
403 test exec-16.1 {flush output before exec} -body {
404 set f [open gorp.file w]
406 exec echo "Second line" >@ $f
411 file delete gorp.file
412 } -result {First line
416 test exec-17.1 {redirecting from command pipeline} -setup {
417 makeFile "abc\nghi\njkl" gorp.file
419 set f [open "|cat gorp.file | wc -l" r]
420 set result [lindex [exec cat <@$f] 0]
424 file delete gorp.file
427 test exec-17.2 {redirecting to command pipeline} -setup {
428 makeFile "abc\nghi\njkl" gorp.file
430 set f [open "|wc -l >gorp2.file" w]
431 exec cat gorp.file >@$f
434 lindex [exec cat gorp2.file] 0
436 file delete gorp.file gorp2.file