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 [string repeat a 1000000]
222 test exec-8.1 {long input and output} {
223 string length [exec cat << $a]
226 # More than 20 arguments to exec.
228 test exec-8.1 {long input and output} {
229 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
230 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23}
232 # Commands that return errors.
234 test exec-9.1 {commands returning errors} {
237 test exec-9.2 {commands returning errors} {
238 catch {exec echo foo | foo123} msg
240 test exec-9.3 {commands returning errors} {
241 list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
243 test exec-9.4 {commands returning errors} jim {
244 list [catch {exec false | echo "foo bar"} msg] $msg
246 test exec-9.5 {commands returning errors} {
247 list [catch {exec gorp456 | echo a b c} msg]
249 test exec-9.6 {commands returning errors} jim {
250 list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
252 test exec-9.7 {commands returning errors} jim {
253 # Note: Use sleep here to ensure the order
254 list [catch {exec sh -c "echo error msg 1 1>&2" \
255 | sh -c "sleep 0.1; echo error msg 2 1>&2"} msg] $msg
259 # Errors in executing the Tcl command, as opposed to errors in the
260 # processes that are invoked.
262 test exec-10.1 {errors in exec invocation} {
263 list [catch {exec} msg]
265 test exec-10.2 {errors in exec invocation} {
266 list [catch {exec | cat} msg] $msg
267 } {1 {illegal use of | or |& in command}}
268 test exec-10.3 {errors in exec invocation} {
269 list [catch {exec cat |} msg] $msg
270 } {1 {illegal use of | or |& in command}}
271 test exec-10.4 {errors in exec invocation} {
272 list [catch {exec cat | | cat} msg] $msg
273 } {1 {illegal use of | or |& in command}}
274 test exec-10.5 {errors in exec invocation} {
275 list [catch {exec cat | |& cat} msg] $msg
276 } {1 {illegal use of | or |& in command}}
277 test exec-10.6 {errors in exec invocation} {
278 list [catch {exec cat |&} msg] $msg
279 } {1 {illegal use of | or |& in command}}
280 test exec-10.7 {errors in exec invocation} {
281 list [catch {exec cat <} msg] $msg
282 } {1 {can't specify "<" as last word in command}}
283 test exec-10.8 {errors in exec invocation} {
284 list [catch {exec cat >} msg] $msg
285 } {1 {can't specify ">" as last word in command}}
286 test exec-10.9 {errors in exec invocation} {
287 list [catch {exec cat <<} msg] $msg
288 } {1 {can't specify "<<" as last word in command}}
289 test exec-10.10 {errors in exec invocation} {
290 list [catch {exec cat >>} msg] $msg
291 } {1 {can't specify ">>" as last word in command}}
292 test exec-10.11 {errors in exec invocation} {
293 list [catch {exec cat >&} msg] $msg
294 } {1 {can't specify ">&" as last word in command}}
295 test exec-10.12 {errors in exec invocation} {
296 list [catch {exec cat >>&} msg] $msg
297 } {1 {can't specify ">>&" as last word in command}}
298 test exec-10.13 {errors in exec invocation} {
299 list [catch {exec cat >@} msg] $msg
300 } {1 {can't specify ">@" as last word in command}}
301 test exec-10.14 {errors in exec invocation} {
302 list [catch {exec cat <@} msg] $msg
303 } {1 {can't specify "<@" as last word in command}}
304 test exec-10.15 {errors in exec invocation} {
305 list [catch {exec cat < a/b/c} msg] [string tolower $msg]
306 } {1 {couldn't read file "a/b/c": no such file or directory}}
307 test exec-10.16 {errors in exec invocation} {
308 list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
309 } {1 {couldn't write file "a/b/c": no such file or directory}}
310 test exec-10.17 {errors in exec invocation} {
311 list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
312 } {1 {couldn't write file "a/b/c": no such file or directory}}
313 set f [open gorp.file w]
314 test exec-10.18 {errors in exec invocation} {
315 list [catch {exec cat <<test <@ $f} msg]
318 set f [open gorp.file r]
319 test exec-10.19 {errors in exec invocation} {
320 list [catch {exec cat <<test >@ $f} msg]
324 # Commands in background.
326 test exec-11.1 {commands in background} {
327 set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
330 test exec-11.2 {commands in background} {
331 list [catch {exec echo a &b} msg] $msg
333 test exec-11.3 {commands in background} {
334 llength [exec {*}$sleepx 0.1 &]
336 test exec-11.4 {commands in background} {
337 llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
340 # Make sure that background commands are properly reaped when
341 # they eventually die.
345 test exec-12.1 {reaping background processes} -body {
346 for {set i 0} {$i < 20} {incr i} {
347 exec echo foo > exec.tmp1 &
350 catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
353 file delete exec.tmp1
356 # Redirecting standard error separately from standard output
358 test exec-15.1 {standard error redirection} {
359 exec echo "First line" > gorp.file
360 list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
363 test exec-15.2 {standard error redirection} {
364 list [exec sh -c "echo foo bar 1>&2" \
365 | echo biz baz >gorp.file 2> gorp.file2] \
366 [exec cat gorp.file] \
367 [exec cat gorp.file2]
368 } {{} {biz baz} {foo bar}}
369 test exec-15.3 {standard error redirection} {
370 list [exec sh -c "echo foo bar 1>&2" \
371 | echo biz baz 2>gorp.file > gorp.file2] \
372 [exec cat gorp.file] \
373 [exec cat gorp.file2]
374 } {{} {foo bar} {biz baz}}
375 test exec-15.4 {standard error redirection} {
376 set f [open gorp.file w]
379 exec sh -c "echo foo bar 1>&2" 2>@ $f
386 test exec-15.5 {standard error redirection} {
387 exec echo "First line" > gorp.file
388 exec sh -c "echo foo bar 1>&2" 2>> gorp.file
392 test exec-15.6 {standard error redirection} {
393 exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
394 >& gorp.file 2> gorp.file2 | echo biz baz
395 list [exec cat gorp.file] [exec cat gorp.file2]
396 } {{biz baz} {foo bar}}
397 test exec-15.7 {combine standard output/standard error} -body {
398 exec sh -c "echo foo bar 1>&2" > gorp.file 2>@1
401 file delete gorp.file gorp.file2
404 test exec-16.1 {flush output before exec} -body {
405 set f [open gorp.file w]
407 exec echo "Second line" >@ $f
412 file delete gorp.file
413 } -result {First line