1 # These tests are design especially for the vfork() implementation
2 # of exec where sh -c must be used and thus we must take extra care
3 # in quoting arguments to exec.
5 source [file dirname [info script]]/testing.tcl
8 testCmdConstraints pipe signal wait alarm
10 # Some Windows platforms (e.g. AppVeyor) produce ENOSPC rather than killing
11 # the child with SIGPIPE). So turn off this test for that platform
12 if {[info exists env(MSYSTEM)] && $env(MSYSTEM) eq "MINGW32"} {
13 testConstraint nomingw32 0
15 testConstraint nomingw32 1
22 array set saveenv [array get env]
24 test exec2-1.1 "Quoting - Result" {
25 exec echo ${d}double quoted${d} ${s}single quoted${s} ${b}backslash quoted${b}
26 } "\"double\ quoted\"\ 'single quoted'\ \\backslash\ quoted\\"
28 test exec2-1.2 "Quoting - Word Grouping" {
29 string trim [exec echo ${d}double quoted${d} ${s}single quoted${s} ${b}backslash quoted${b} | wc -w]
32 test exec2-2.1 "Add to exec environment" {
33 set env(TESTENV) "the value"
34 exec printenv | sed -n -e /^TESTENV=/p
37 test exec2-2.2 "Remove from exec environment" {
38 set env(TESTENV2) "new value"
40 exec printenv | sed -n -e /^TESTENV=/p
44 test exec2-2.3 "Remove all exec environment" {
46 exec printenv | sed -n -e /^TESTENV2=/p
49 test exec2-2.4 "Remove all env var" {
51 exec printenv | sed -n -e /^TESTENV2=/p
54 array set env [array get saveenv]
56 test exec2-3.1 "close pipeline return value" {
58 set rc [catch {close $f} msg opts]
59 lassign [dict get $opts -errorcode] status pid exitcode
60 list $rc $msg $status $exitcode
61 } {1 {child process exited abnormally} CHILDSTATUS 1}
63 test exec2-3.2 "close pipeline return value" -constraints {pipe nomingw32} -body {
64 # Create a pipe and immediately close the read end
67 # Write more than 64KB which is maximum size of the pipe buffers
68 # on all systems we have seen
69 set bigstring [string repeat a 100000]
70 set f [open [list |cat << $bigstring >$@w]]
71 set rc [catch {close $f} msg opts]
72 lassign [dict get $opts -errorcode] status pid exitcode
73 list $rc $msg $status $exitcode
74 } -match glob -result {1 {child killed*} CHILDKILLED SIGPIPE}
76 test exec2-3.3 "close pipeline with SIGPIPE blocked" -constraints {pipe signal nomingw32} -body {
77 # Create a pipe and immediately close the read end
81 # Write more than 64KB which is maximum size of the pipe buffers
82 # on all systems we have seen
83 set bigstring [string repeat a 100000]
84 set f [open [list |cat << $bigstring >$@w 2>/dev/null]]
85 set rc [catch {close $f} msg opts]
86 lassign [dict get $opts -errorcode] status pid exitcode
87 list $rc $msg $status $exitcode
88 } -match glob -result {1 {child process exited*} CHILDSTATUS 1} -cleanup {
89 signal default SIGPIPE
92 test exec2-3.4 "wait for background task" -constraints wait -body {
93 set pid [exec sleep 0.1 &]
94 lassign [wait $pid] status newpid exitcode
95 if {$pid != $newpid} {
96 error "Got wrong pid from wait"
98 list $status $exitcode
100 } -result {CHILDSTATUS 0}
102 test exec2-4.1 {redirect from invalid filehandle} -body {
104 } -returnCodes error -result {invalid command name "bogus"}
106 test exec2-4.2 {env is invalid dict} -constraints jim -body {
110 } -result {0} -cleanup {
114 test exec2-4.3 {signalled process during foreground exec} -constraints {jim alarm} -body {
115 # We need to exec a pipeline and then have one process
116 # be killed by a signal
117 exec [info nameofexecutable] -e {alarm 0.1; sleep 0.5}
118 } -returnCodes error -result {child killed by signal SIGALRM}
120 test exec2-4.4 {exec - consecutive |} -body {
122 } -returnCodes error -result {illegal use of | or |& in command}
124 test exec2-4.5 {exec - consecutive | with &} -body {
126 } -returnCodes error -result {illegal use of | or |& in command}
128 test exec2-4.6 {exec - illegal channel} -body {
129 exec echo hello >@nonexistent
130 } -returnCodes error -result {invalid command name "nonexistent"}
132 test exec2-5.1 {wait with invalid pid} wait {
136 test exec2-5.2 {wait with invalid pid} -constraints wait -body {
138 } -returnCodes error -result {expected integer but got "blah"}
140 test exec2-5.3 {wait - bad args} -constraints wait -body {
142 } -returnCodes error -result {wrong # args: should be "wait ?-nohang? ?pid?"}
144 test exec2-5.4 {wait -nohang} -constraints wait -body {
145 set pid [exec sleep 0.2 &]
146 # first wait will do nothing as the process is not finished
149 } -match glob -result {CHILDSTATUS * 0}
151 test exec2-5.5 {wait for all children} -body {
152 # We want to have children finish at different times
153 # so that we test the handling of the wait table
154 foreach i {0.1 0.2 0.6 0.5 0.4 0.3} {
157 # reap zombies, there should not be any
160 # reap zombies, 2-3 should be finished now
163 # reap zombies, all processes should be finished now