zlib: Don't use PASTE for INTMAX error messages
[jimtcl.git] / tests / exec.test
blob441681eb7b050dcb2909cfe1061e9e671aed049a
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 [string repeat a 1000000]
222 test exec-8.1 {long input and output} {
223     string length [exec cat << $a]
224 } 1000000
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} {
235     catch {exec gorp456}
236 } {1}
237 test exec-9.2 {commands returning errors} {
238     catch {exec echo foo | foo123} msg
239 } {1}
240 test exec-9.3 {commands returning errors} {
241     list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
242 } {1}
243 test exec-9.4 {commands returning errors} jim {
244     list [catch {exec false | echo "foo bar"} msg] $msg
245 } {1 {foo bar}}
246 test exec-9.5 {commands returning errors} {
247     list [catch {exec gorp456 | echo a b c} msg]
248 } {1}
249 test exec-9.6 {commands returning errors} jim {
250     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
251 } {0 {error 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
256 } {0 {error msg 1
257 error msg 2}}
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]
264 } {1}
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]
316 } 1
317 close $f
318 set f [open gorp.file r]
319 test exec-10.19 {errors in exec invocation} {
320     list [catch {exec cat <<test >@ $f} msg]
321 } 1
322 close $f
324 # Commands in background.
326 test exec-11.1 {commands in background} {
327     set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
328     expr $x<1000000
329 } 1
330 test exec-11.2 {commands in background} {
331     list [catch {exec echo a &b} msg] $msg
332 } {0 {a &b}}
333 test exec-11.3 {commands in background} {
334     llength [exec {*}$sleepx 0.1 &]
335 } 1
336 test exec-11.4 {commands in background} {
337     llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
338 } 3
340 # Make sure that background commands are properly reaped when
341 # they eventually die.
343 exec {*}$sleepx 0.3
345 test exec-12.1 {reaping background processes} -body {
346     for {set i 0} {$i < 20} {incr i} {
347                 exec echo foo > exec.tmp1 &
348     }
349     exec {*}$sleepx 0.1
350     catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
351     lindex $msg 0
352 } -cleanup {
353     file delete exec.tmp1
354 } -result 0
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] \
361             [exec cat gorp.file]
362 } {{} {foo bar}}
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]
377     puts $f "Line 1"
378     flush $f
379     exec sh -c "echo foo bar 1>&2" 2>@ $f
380     puts $f "Line 3"
381     close $f
382     exec cat gorp.file
383 } {Line 1
384 foo bar
385 Line 3}
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
389     exec cat gorp.file
390 } {First line
391 foo bar}
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
399     exec cat gorp.file
400 } -cleanup {
401     file delete gorp.file gorp.file2
402 } -result {foo bar}
404 test exec-16.1 {flush output before exec} -body {
405     set f [open gorp.file w]
406     puts $f "First line"
407     exec echo "Second line" >@ $f
408     puts $f "Third line"
409     close $f
410     exec cat gorp.file
411 } -cleanup {
412     file delete gorp.file
413 } -result {First line
414 Second line
415 Third line}
417 file delete sleepx
419 testreport