build: Fix out-of-tree build with json ext
[jimtcl.git] / tests / exec2.test
blobb4b42ccef134a72936e4f95735bb537b3ec4f5ab
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
7 needs cmd exec
8 foreach i {pipe signal wait} {
9         testConstraint $i [expr {[info commands $i] ne ""}]
11 # Some Windows platforms (e.g. AppVeyor) produce ENOSPC rather than killing
12 # the child with SIGPIPE). So turn off this test for that platform
13 if {[info exists env(MSYSTEM)] && $env(MSYSTEM) eq "MINGW32"} {
14         testConstraint nomingw32 0
15 } else {
16         testConstraint nomingw32 1
19 set d \"
20 set s '
21 set b \\
23 array set saveenv [array get env]
25 test exec2-1.1 "Quoting - Result" {
26         exec echo ${d}double quoted${d} ${s}single quoted${s} ${b}backslash quoted${b}
27 } "\"double\ quoted\"\ 'single quoted'\ \\backslash\ quoted\\"
29 test exec2-1.2 "Quoting - Word Grouping" {
30         string trim [exec echo ${d}double quoted${d} ${s}single quoted${s} ${b}backslash quoted${b} | wc -w]
31 } {6}
33 test exec2-2.1 "Add to exec environment" {
34         set env(TESTENV) "the value"
35         exec printenv | sed -n -e /^TESTENV=/p
36 } {TESTENV=the value}
38 test exec2-2.2 "Remove from exec environment" {
39         set env(TESTENV2) "new value"
40         unset env(TESTENV)
41         exec printenv | sed -n -e /^TESTENV=/p
42 } {}
45 test exec2-2.3 "Remove all exec environment" {
46         array unset env *
47         exec printenv | sed -n -e /^TESTENV2=/p
48 } {}
50 test exec2-2.4 "Remove all env var" {
51         unset -nocomplain env
52         exec printenv | sed -n -e /^TESTENV2=/p
53 } {}
55 array set env [array get saveenv]
57 test exec2-3.1 "close pipeline return value" {
58         set f [open |false]
59         set rc [catch {close $f} msg opts]
60         lassign [dict get $opts -errorcode] status pid exitcode
61         list $rc $msg $status $exitcode
62 } {1 {child process exited abnormally} CHILDSTATUS 1}
64 test exec2-3.2 "close pipeline return value" -constraints {pipe nomingw32} -body {
65         # Create a pipe and immediately close the read end
66         lassign [pipe] r w
67         close $r
68         # Write more than 64KB which is maximum size of the pipe buffers
69         # on all systems we have seen
70         set bigstring [string repeat a 100000]
71         set f [open [list |cat << $bigstring >$@w]]
72         set rc [catch {close $f} msg opts]
73         lassign [dict get $opts -errorcode] status pid exitcode
74         list $rc $msg $status $exitcode
75 } -match glob -result {1 {child killed*} CHILDKILLED SIGPIPE}
77 test exec2-3.3 "close pipeline with SIGPIPE blocked" -constraints {pipe signal nomingw32} -body {
78         # Create a pipe and immediately close the read end
79         lassign [pipe] r w
80         close $r
81         signal block SIGPIPE
82         # Write more than 64KB which is maximum size of the pipe buffers
83         # on all systems we have seen
84         set bigstring [string repeat a 100000]
85         set f [open [list |cat << $bigstring >$@w 2>/dev/null]]
86         set rc [catch {close $f} msg opts]
87         lassign [dict get $opts -errorcode] status pid exitcode
88         list $rc $msg $status $exitcode
89 } -match glob -result {1 {child process exited*} CHILDSTATUS 1} -cleanup {
90         signal default SIGPIPE
93 test exec2-3.4 "wait for background task" -constraints wait -body {
94         set pid [exec sleep 0.1 &]
95         lassign [wait $pid] status newpid exitcode
96         if {$pid != $newpid} {
97                 error "Got wrong pid from wait"
98         } else {
99                 list $status $exitcode
100         }
101 } -result {CHILDSTATUS 0}
103 testreport