regsub: fix substitution with a trailing backslash
[jimtcl.git] / tests / exec.test
blob50dc706a64df0ed209c3f9c878a2eb781cf4e0cf
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 # Sleep which supports fractions of a second
23 if {[info commands sleep] eq {}} {
24     proc sleep {n} {
25         after [expr {int($n * 1000)}]
26     }
29 set f [open sleepx w]
30 puts $f "#![info nameofexecutable]"
31 puts $f {
32     set seconds [lindex $argv 0]
33     after [expr {int($seconds * 1000)}]
35 close $f
36 #catch {exec chmod +x sleepx}
37 set sleepx [list [info nameofexecutable] sleepx]
39 # Basic operations.
41 test exec-1.1 {basic exec operation} {
42     exec echo a b c
43 } "a b c"
44 test exec-1.2 {pipelining} {
45     exec echo a b c d | cat | cat
46 } "a b c d"
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
50 } {3 1 4}
51 set arg {12345678901234567890123456789012345678901234567890}
52 set arg "$arg$arg$arg$arg$arg$arg"
53 test exec-1.4 {long command lines} {
54     exec echo $arg
55 } $arg
56 set arg {}
58 # I/O redirection: input from Tcl command.
60 test exec-2.1 {redirecting input from immediate source} {
61     exec cat << "Sample text"
62 } {Sample text}
63 test exec-2.2 {redirecting input from immediate source} {
64     exec << "Sample text" cat | cat
65 } {Sample text}
66 test exec-2.3 {redirecting input from immediate source} {
67     exec cat << "Sample text" | cat
68 } {Sample text}
69 test exec-2.4 {redirecting input from immediate source} {
70     exec cat | cat << "Sample text"
71 } {Sample text}
72 test exec-2.5 {redirecting input from immediate source} {
73     exec cat "<<Joined to arrows"
74 } {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"
81 } "\uE9\uE0\uFC\uF1"
82 test exec-2.7 {redirecting input from immediate source with nulls} {
83     exec cat << "Sample\0text"
84 } "Sample\0text"
86 # I/O redirection: output to file.
88 file delete gorp.file
89 test exec-3.1 {redirecting output to file} {
90     exec echo "Some simple words" > gorp.file
91     exec cat gorp.file
92 } "Some simple words"
93 test exec-3.2 {redirecting output to file} {
94     exec echo "More simple words" | >gorp.file cat | cat
95     exec cat gorp.file
96 } "More simple words"
97 test exec-3.3 {redirecting output to file} {
98     exec > gorp.file echo "Different simple words" | cat | cat
99     exec cat gorp.file
100 } "Different simple words"
101 test exec-3.4 {redirecting output to file} {
102     exec echo "Some simple words" >gorp.file
103     exec cat 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
108     exec cat 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
113     exec cat gorp.file
114 } "First line\nSecond line"
115 test exec-3.7 {redirecting output to file} {
116     set f [open gorp.file w]
117     puts $f "Line 1"
118     flush $f
119     exec echo "More text" >@ $f
120     exec echo >@$f "Even more"
121     puts $f "Line 3"
122     close $f
123     exec cat gorp.file
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
131     exec cat gorp.file
132 } "test output"
133 test exec-4.2 {redirecting output and stderr to file} {
134     list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
135             [exec cat gorp.file]
136 } {{} {foo bar}}
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] \
140             [exec cat 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]
144     puts $f "Line 1"
145     flush $f
146     exec echo "More text" >&@ $f
147     exec echo >&@$f "Even more"
148     puts $f "Line 3"
149     close $f
150     exec cat gorp.file
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]
154     puts $f "Line 1"
155     flush $f
156     exec >&@ $f sh -c "echo foo bar 1>&2"
157     exec >&@$f sh -c "echo xyzzy 1>&2"
158     puts $f "Line 3"
159     close $f
160     exec cat gorp.file
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} {
168     exec cat < gorp.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} {
180     exec cat <gorp.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]
185     close $f
186     set result
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]
191     close $f
192     set result
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
199 } "foo bar"
200 test exec-6.2 {redirecting stderr through a pipeline} {
201     exec sh -c "echo foo bar 1>&2" |& cat
202 } "foo bar"
203 test exec-6.3 {redirecting stderr through a pipeline} {
204     exec sh -c "echo foo bar 1>&2" \
205         |& cat |& cat
206 } "foo bar"
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
213     exec cat gorp.file2
214 } {Just a few thoughts}
215 test exec-7.2 {multiple I/O redirections} {
216     exec < gorp.file << "command input" cat
217 } {command input}
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} {
227     exec cat << $a
228 } $a
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} {
239     catch {exec gorp456}
240 } {1}
241 test exec-9.2 {commands returning errors} {
242     catch {exec echo foo | foo123} msg
243 } {1}
244 test exec-9.3 {commands returning errors} {
245     list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
246 } {1}
247 test exec-9.4 {commands returning errors} jim {
248     list [catch {exec false | echo "foo bar"} msg] $msg
249 } {1 {foo bar}}
250 test exec-9.5 {commands returning errors} {
251     list [catch {exec gorp456 | echo a b c} msg]
252 } {1}
253 test exec-9.6 {commands returning errors} jim {
254     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
255 } {0 {error 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
260 } {0 {error msg 1
261 error msg 2}}
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]
268 } {1}
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]
320 } 1
321 close $f
322 set f [open gorp.file r]
323 test exec-10.19 {errors in exec invocation} {
324     list [catch {exec cat <<test >@ $f} msg]
325 } 1
326 close $f
328 # Commands in background.
330 test exec-11.1 {commands in background} {
331     set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
332     expr $x<1000000
333 } 1
334 test exec-11.2 {commands in background} {
335     list [catch {exec echo a &b} msg] $msg
336 } {0 {a &b}}
337 test exec-11.3 {commands in background} {
338     llength [exec {*}$sleepx 0.1 &]
339 } 1
340 test exec-11.4 {commands in background} {
341     llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
342 } 3
344 # Make sure that background commands are properly reaped when
345 # they eventually die.
347 exec {*}$sleepx 0.3
349 test exec-12.1 {reaping background processes} -body {
350     for {set i 0} {$i < 20} {incr i} {
351                 exec echo foo > exec.tmp1 &
352     }
353     exec {*}$sleepx 0.1
354     catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
355     lindex $msg 0
356 } -cleanup {
357     file delete exec.tmp1
358 } -result 0
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] \
365             [exec cat gorp.file]
366 } {{} {foo bar}}
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]
381     puts $f "Line 1"
382     flush $f
383     exec sh -c "echo foo bar 1>&2" 2>@ $f
384     puts $f "Line 3"
385     close $f
386     exec cat gorp.file
387 } {Line 1
388 foo bar
389 Line 3}
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
393     exec cat gorp.file
394 } {First line
395 foo bar}
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
403     exec cat gorp.file
404 } -cleanup {
405     file delete gorp.file gorp.file2
406 } -result {foo bar}
408 test exec-16.1 {flush output before exec} -body {
409     set f [open gorp.file w]
410     puts $f "First line"
411     exec echo "Second line" >@ $f
412     puts $f "Third line"
413     close $f
414     exec cat gorp.file
415 } -cleanup {
416     file delete gorp.file
417 } -result {First line
418 Second line
419 Third line}
421 file delete sleepx
423 testreport