# These tests are design especially for the vfork() implementation
# of exec where sh -c must be used and thus we must take extra care
# in quoting arguments to exec.

source [file dirname [info script]]/testing.tcl

needs cmd exec
foreach i {pipe signal wait} {
	testConstraint $i [expr {[info commands $i] ne ""}]
}
# Some Windows platforms (e.g. AppVeyor) produce ENOSPC rather than killing
# the child with SIGPIPE). So turn off this test for that platform
if {[info exists env(MSYSTEM)] && $env(MSYSTEM) eq "MINGW32"} {
	testConstraint nomingw32 0
} else {
	testConstraint nomingw32 1
}

set d \"
set s '
set b \\

array set saveenv [array get env]

test exec2-1.1 "Quoting - Result" {
	exec echo ${d}double quoted${d} ${s}single quoted${s} ${b}backslash quoted${b}
} "\"double\ quoted\"\ 'single quoted'\ \\backslash\ quoted\\"

test exec2-1.2 "Quoting - Word Grouping" {
	string trim [exec echo ${d}double quoted${d} ${s}single quoted${s} ${b}backslash quoted${b} | wc -w]
} {6}

test exec2-2.1 "Add to exec environment" {
	set env(TESTENV) "the value"
	exec printenv | sed -n -e /^TESTENV=/p
} {TESTENV=the value}

test exec2-2.2 "Remove from exec environment" {
	set env(TESTENV2) "new value"
	unset env(TESTENV)
	exec printenv | sed -n -e /^TESTENV=/p
} {}


test exec2-2.3 "Remove all exec environment" {
	array unset env *
	exec printenv | sed -n -e /^TESTENV2=/p
} {}

test exec2-2.4 "Remove all env var" {
	unset -nocomplain env
	exec printenv | sed -n -e /^TESTENV2=/p
} {}

array set env [array get saveenv]

test exec2-3.1 "close pipeline return value" {
	set f [open |false]
	set rc [catch {close $f} msg opts]
	lassign [dict get $opts -errorcode] status pid exitcode
	list $rc $msg $status $exitcode
} {1 {child process exited abnormally} CHILDSTATUS 1}

test exec2-3.2 "close pipeline return value" -constraints {pipe nomingw32} -body {
	# Create a pipe and immediately close the read end
	lassign [pipe] r w
	close $r
	# Write more than 64KB which is maximum size of the pipe buffers
	# on all systems we have seen
	set bigstring [string repeat a 100000]
	set f [open [list |cat << $bigstring >$@w]]
	set rc [catch {close $f} msg opts]
	lassign [dict get $opts -errorcode] status pid exitcode
	list $rc $msg $status $exitcode
} -match glob -result {1 {child killed*} CHILDKILLED SIGPIPE}

test exec2-3.3 "close pipeline with SIGPIPE blocked" -constraints {pipe signal nomingw32} -body {
	# Create a pipe and immediately close the read end
	lassign [pipe] r w
	close $r
	signal block SIGPIPE
	# Write more than 64KB which is maximum size of the pipe buffers
	# on all systems we have seen
	set bigstring [string repeat a 100000]
	set f [open [list |cat << $bigstring >$@w 2>/dev/null]]
	set rc [catch {close $f} msg opts]
	lassign [dict get $opts -errorcode] status pid exitcode
	list $rc $msg $status $exitcode
} -match glob -result {1 {child process exited*} CHILDSTATUS 1} -cleanup {
	signal default SIGPIPE
}

test exec2-3.4 "wait for background task" -constraints wait -body {
	set pid [exec sleep 0.1 &]
	lassign [wait $pid] status newpid exitcode
	if {$pid != $newpid} {
		error "Got wrong pid from wait"
	} else {
		list $status $exitcode
	}
} -result {CHILDSTATUS 0}

testreport
