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 testConstraint unix [expr {$tcl_platform(platform) eq {unix}}]
24 # Sleep which supports fractions of a second
25 if {[info commands sleep] eq {}} {
27 after [expr {int($n * 1000)}]
32 puts $f "#![info nameofexecutable]"
34 set seconds [lindex $argv 0]
35 after [expr {int($seconds * 1000)}]
38 #catch {exec chmod +x sleepx}
39 set sleepx [list [info nameofexecutable] sleepx]
43 test exec-1.1 {basic exec operation} {
46 test exec-1.2 {pipelining} {
47 exec echo a b c d | cat | cat
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
53 set arg {12345678901234567890123456789012345678901234567890}
54 set arg "$arg$arg$arg$arg$arg$arg"
55 test exec-1.4 {long command lines} {
60 # I/O redirection: input from Tcl command.
62 test exec-2.1 {redirecting input from immediate source} {
63 exec cat << "Sample text"
65 test exec-2.2 {redirecting input from immediate source} {
66 exec << "Sample text" cat | cat
68 test exec-2.3 {redirecting input from immediate source} {
69 exec cat << "Sample text" | cat
71 test exec-2.4 {redirecting input from immediate source} {
72 exec cat | cat << "Sample text"
74 test exec-2.5 {redirecting input from immediate source} {
75 exec cat "<<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"
84 test exec-2.7 {redirecting input from immediate source with nulls} {
85 exec cat << "Sample\0text"
88 # I/O redirection: output to file.
91 test exec-3.1 {redirecting output to file} {
92 exec echo "Some simple words" > gorp.file
95 test exec-3.2 {redirecting output to file} {
96 exec echo "More simple words" | >gorp.file cat | cat
99 test exec-3.3 {redirecting output to file} {
100 exec > gorp.file echo "Different simple words" | cat | cat
102 } "Different simple words"
103 test exec-3.4 {redirecting output to file} {
104 exec echo "Some simple words" >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
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
116 } "First line\nSecond line"
117 test exec-3.7 {redirecting output to file} {
118 set f [open gorp.file w]
121 exec echo "More text" >@ $f
122 exec echo >@$f "Even more"
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
135 test exec-4.2 {redirecting output and stderr to file} {
136 list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
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] \
143 } "{} {first line\nfoo bar}"
144 test exec-4.4 {redirecting output and stderr to file} {
145 set f [open gorp.file w]
148 exec echo "More text" >&@ $f
149 exec echo >&@$f "Even more"
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]
158 exec >&@ $f sh -c "echo foo bar 1>&2"
159 exec >&@$f sh -c "echo xyzzy 1>&2"
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} {
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} {
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]
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]
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
202 test exec-6.2 {redirecting stderr through a pipeline} {
203 exec sh -c "echo foo bar 1>&2" |& cat
205 test exec-6.3 {redirecting stderr through a pipeline} {
206 exec sh -c "echo foo bar 1>&2" \
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
216 } {Just a few thoughts}
217 test exec-7.2 {multiple I/O redirections} {
218 exec < gorp.file << "command input" cat
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]
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} {
239 test exec-9.2 {commands returning errors} {
240 catch {exec echo foo | foo123} msg
242 test exec-9.3 {commands returning errors} {
243 list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
245 test exec-9.4 {commands returning errors} jim {
246 list [catch {exec false | echo "foo bar"} msg] $msg
248 test exec-9.5 {commands returning errors} {
249 list [catch {exec gorp456 | echo a b c} msg]
251 test exec-9.6 {commands returning errors} jim {
252 list [catch {exec sh -c "echo error msg 1>&2"} msg] $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
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]
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]
320 set f [open gorp.file r]
321 test exec-10.19 {errors in exec invocation} {
322 list [catch {exec cat <<test >@ $f} msg]
326 # Commands in background.
328 test exec-11.1 {commands in background} {
329 set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
332 test exec-11.2 {commands in background} {
333 list [catch {exec echo a &b} msg] $msg
335 test exec-11.3 {commands in background} {
336 llength [exec {*}$sleepx 0.1 &]
338 test exec-11.4 {commands in background} {
339 llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
342 # Make sure that background commands are properly reaped when
343 # they eventually die.
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 &
352 catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
355 file delete exec.tmp1
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] \
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]
381 exec sh -c "echo foo bar 1>&2" 2>@ $f
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
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
403 file delete gorp.file gorp.file2
406 test exec-16.1 {flush output before exec} -body {
407 set f [open gorp.file w]
409 exec echo "Second line" >@ $f
414 file delete gorp.file
415 } -result {First line