build: Fix out-of-tree build with json ext
[jimtcl.git] / tests / exec.test
blob0eb218ab6899015ada542b821fa4da67efc35f28
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
21 testConstraint unix [expr {$tcl_platform(platform) eq {unix}}]
23 # Sleep which supports fractions of a second
24 if {[info commands sleep] eq {}} {
25     proc sleep {n} {
26         exec {*}$::sleepx $n
27     }
30 set f [open sleepx w]
31 puts $f {
32     sleep "$@"
34 close $f
35 #catch {exec chmod +x sleepx}
36 set sleepx [list sh sleepx]
38 # Basic operations.
40 test exec-1.1 {basic exec operation} {
41     exec echo a b c
42 } "a b c"
43 test exec-1.2 {pipelining} {
44     exec echo a b c d | cat | cat
45 } "a b c d"
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
49 } {3 1 4}
50 set arg {12345678901234567890123456789012345678901234567890}
51 set arg "$arg$arg$arg$arg$arg$arg"
52 test exec-1.4 {long command lines} {
53     exec echo $arg
54 } $arg
55 set arg {}
57 # I/O redirection: input from Tcl command.
59 test exec-2.1 {redirecting input from immediate source} {
60     exec cat << "Sample text"
61 } {Sample text}
62 test exec-2.2 {redirecting input from immediate source} {
63     exec << "Sample text" cat | cat
64 } {Sample text}
65 test exec-2.3 {redirecting input from immediate source} {
66     exec cat << "Sample text" | cat
67 } {Sample text}
68 test exec-2.4 {redirecting input from immediate source} {
69     exec cat | cat << "Sample text"
70 } {Sample text}
71 test exec-2.5 {redirecting input from immediate source} {
72     exec cat "<<Joined to arrows"
73 } {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"
80 } "\uE9\uE0\uFC\uF1"
81 test exec-2.7 {redirecting input from immediate source with nulls} {
82     exec cat << "Sample\0text"
83 } "Sample\0text"
85 # I/O redirection: output to file.
87 file delete gorp.file
88 test exec-3.1 {redirecting output to file} {
89     exec echo "Some simple words" > gorp.file
90     exec cat gorp.file
91 } "Some simple words"
92 test exec-3.2 {redirecting output to file} {
93     exec echo "More simple words" | >gorp.file cat | cat
94     exec cat gorp.file
95 } "More simple words"
96 test exec-3.3 {redirecting output to file} {
97     exec > gorp.file echo "Different simple words" | cat | cat
98     exec cat gorp.file
99 } "Different simple words"
100 test exec-3.4 {redirecting output to file} {
101     exec echo "Some simple words" >gorp.file
102     exec cat 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
107     exec cat 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
112     exec cat gorp.file
113 } "First line\nSecond line"
114 test exec-3.7 {redirecting output to file} {
115     set f [open gorp.file w]
116     puts $f "Line 1"
117     flush $f
118     exec echo "More text" >@ $f
119     exec echo >@$f "Even more"
120     puts $f "Line 3"
121     close $f
122     exec cat gorp.file
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
130     exec cat gorp.file
131 } "test output"
132 test exec-4.2 {redirecting output and stderr to file} {
133     list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
134             [exec cat gorp.file]
135 } {{} {foo bar}}
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] \
139             [exec cat 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]
143     puts $f "Line 1"
144     flush $f
145     exec echo "More text" >&@ $f
146     exec echo >&@$f "Even more"
147     puts $f "Line 3"
148     close $f
149     exec cat gorp.file
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]
153     puts $f "Line 1"
154     flush $f
155     exec >&@ $f sh -c "echo foo bar 1>&2"
156     exec >&@$f sh -c "echo xyzzy 1>&2"
157     puts $f "Line 3"
158     close $f
159     exec cat gorp.file
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} {
167     exec cat < gorp.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} {
179     exec cat <gorp.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]
184     close $f
185     set result
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]
190     close $f
191     set result
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
198 } "foo bar"
199 test exec-6.2 {redirecting stderr through a pipeline} {
200     exec sh -c "echo foo bar 1>&2" |& cat
201 } "foo bar"
202 test exec-6.3 {redirecting stderr through a pipeline} {
203     exec sh -c "echo foo bar 1>&2" \
204         |& cat |& cat
205 } "foo bar"
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
212     exec cat gorp.file2
213 } {Just a few thoughts}
214 test exec-7.2 {multiple I/O redirections} {
215     exec < gorp.file << "command input" cat
216 } {command input}
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]
223 } 1000000
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} {
234     catch {exec gorp456}
235 } {1}
236 test exec-9.2 {commands returning errors} {
237     catch {exec echo foo | foo123} msg
238 } {1}
239 test exec-9.3 {commands returning errors} {
240     list [catch {exec {*}$sleepx 0.1 | false | {*}$sleepx 0.1} msg]
241 } {1}
242 test exec-9.4 {commands returning errors} jim {
243     list [catch {exec false | echo "foo bar"} msg] $msg
244 } {1 {foo bar}}
245 test exec-9.5 {commands returning errors} {
246     list [catch {exec gorp456 | echo a b c} msg]
247 } {1}
248 test exec-9.6 {commands returning errors} jim {
249     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
250 } {0 {error 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
255 } {0 {error msg 1
256 error msg 2}}
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]
263 } {1}
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]
315 } 1
316 close $f
317 set f [open gorp.file r]
318 test exec-10.19 {errors in exec invocation} {
319     list [catch {exec cat <<test >@ $f} msg]
320 } 1
321 close $f
323 # Commands in background.
325 test exec-11.1 {commands in background} {
326     set x [lindex [time {exec {*}$sleepx 0.2 &}] 0]
327     expr $x<1000000
328 } 1
329 test exec-11.2 {commands in background} {
330     list [catch {exec echo a &b} msg] $msg
331 } {0 {a &b}}
332 test exec-11.3 {commands in background} {
333     llength [exec {*}$sleepx 0.1 &]
334 } 1
335 test exec-11.4 {commands in background} {
336     llength [exec {*}$sleepx 0.1 | {*}$sleepx 0.1 | {*}$sleepx 0.1 &]
337 } 3
339 # Make sure that background commands are properly reaped when
340 # they eventually die.
342 exec {*}$sleepx 0.3
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 &
347     }
348     exec {*}$sleepx 0.1
349     catch {exec ps | fgrep "echo foo" | fgrep -v grep | wc} msg
350     lindex $msg 0
351 } -cleanup {
352     file delete exec.tmp1
353 } -result 0
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] \
360             [exec cat gorp.file]
361 } {{} {foo bar}}
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]
376     puts $f "Line 1"
377     flush $f
378     exec sh -c "echo foo bar 1>&2" 2>@ $f
379     puts $f "Line 3"
380     close $f
381     exec cat gorp.file
382 } {Line 1
383 foo bar
384 Line 3}
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
388     exec cat gorp.file
389 } {First line
390 foo bar}
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
398     exec cat gorp.file
399 } -cleanup {
400     file delete gorp.file gorp.file2
401 } -result {foo bar}
403 test exec-16.1 {flush output before exec} -body {
404     set f [open gorp.file w]
405     puts $f "First line"
406     exec echo "Second line" >@ $f
407     puts $f "Third line"
408     close $f
409     exec cat gorp.file
410 } -cleanup {
411     file delete gorp.file
412 } -result {First line
413 Second line
414 Third line}
416 test exec-17.1 {redirecting from command pipeline} -setup {
417     makeFile "abc\nghi\njkl" gorp.file
418 } -body {
419     set f [open "|cat gorp.file | wc -l" r]
420     set result [lindex [exec cat <@$f] 0]
421     close $f
422     set result
423 } -cleanup {
424     file delete gorp.file
425 } -result {3}
427 test exec-17.2 {redirecting to command pipeline} -setup {
428     makeFile "abc\nghi\njkl" gorp.file
429 } -body {
430     set f [open "|wc -l >gorp2.file" w]
431     exec cat gorp.file >@$f
432     flush $f
433     close $f
434     lindex [exec cat gorp2.file] 0
435 } -cleanup {
436     file delete gorp.file gorp2.file
437 } -result {3}
439 file delete sleepx
441 testreport