sl@0: # Commands covered: exec sl@0: # sl@0: # This file contains a collection of tests for one or more of the Tcl sl@0: # built-in commands. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1991-1994 The Regents of the University of California. sl@0: # Copyright (c) 1994-1997 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-1999 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: exec.test,v 1.16.2.7 2006/01/16 19:31:19 rmax Exp $ sl@0: sl@0: package require tcltest 2 sl@0: namespace import -force ::tcltest::* sl@0: sl@0: # All tests require the "exec" command. sl@0: # Skip them if exec is not defined. sl@0: testConstraint exec [llength [info commands exec]] sl@0: unset -nocomplain path sl@0: set path(echo) [makeFile { sl@0: puts -nonewline [lindex $argv 0] sl@0: foreach str [lrange $argv 1 end] { sl@0: puts -nonewline " $str" sl@0: } sl@0: puts {} sl@0: exit sl@0: } echo] sl@0: sl@0: set path(cat) [makeFile { sl@0: if {$argv == {}} { sl@0: set argv - sl@0: } sl@0: foreach name $argv { sl@0: if {$name == "-"} { sl@0: set f stdin sl@0: } elseif {[catch {open $name r} f] != 0} { sl@0: puts stderr $f sl@0: continue sl@0: } sl@0: while {[eof $f] == 0} { sl@0: puts -nonewline [read $f] sl@0: } sl@0: if {$f != "stdin"} { sl@0: close $f sl@0: } sl@0: } sl@0: exit sl@0: } cat] sl@0: sl@0: set path(wc) [makeFile { sl@0: set data [read stdin] sl@0: set lines [regsub -all "\n" $data {} dummy] sl@0: set words [regsub -all "\[^ \t\n]+" $data {} dummy] sl@0: set chars [string length $data] sl@0: puts [format "%8.d%8.d%8.d" $lines $words $chars] sl@0: exit sl@0: } wc] sl@0: sl@0: set path(sh) [makeFile { sl@0: if {[lindex $argv 0] != "-c"} { sl@0: error "sh: unexpected arguments $argv" sl@0: } sl@0: set cmd [lindex $argv 1] sl@0: lappend cmd ";" sl@0: sl@0: set newcmd {} sl@0: sl@0: foreach arg $cmd { sl@0: if {$arg == ";"} { sl@0: eval exec >@stdout 2>@stderr [list [info nameofexecutable]] $newcmd sl@0: set newcmd {} sl@0: continue sl@0: } sl@0: if {$arg == "1>&2"} { sl@0: set arg >@stderr sl@0: } sl@0: lappend newcmd $arg sl@0: } sl@0: exit sl@0: } sh] sl@0: sl@0: set path(sleep) [makeFile { sl@0: after [expr $argv*1000] sl@0: exit sl@0: } sleep] sl@0: sl@0: set path(exit) [makeFile { sl@0: exit $argv sl@0: } exit] sl@0: sl@0: # Basic operations. sl@0: sl@0: test exec-1.1 {basic exec operation} {exec} { sl@0: exec [interpreter] $path(echo) a b c sl@0: } "a b c" sl@0: test exec-1.2 {pipelining} {exec stdio} { sl@0: exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(cat) sl@0: } "a b c d" sl@0: test exec-1.3 {pipelining} {exec stdio} { sl@0: set a [exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(wc)] sl@0: list [scan $a "%d %d %d" b c d] $b $c sl@0: } {3 1 4} sl@0: set arg {12345678901234567890123456789012345678901234567890} sl@0: set arg "$arg$arg$arg$arg$arg$arg" sl@0: test exec-1.4 {long command lines} {exec} { sl@0: exec [interpreter] $path(echo) $arg sl@0: } $arg sl@0: set arg {} sl@0: sl@0: # I/O redirection: input from Tcl command. sl@0: sl@0: test exec-2.1 {redirecting input from immediate source} {exec stdio} { sl@0: exec [interpreter] $path(cat) << "Sample text" sl@0: } {Sample text} sl@0: test exec-2.2 {redirecting input from immediate source} {exec stdio} { sl@0: exec << "Sample text" [interpreter] $path(cat) | [interpreter] $path(cat) sl@0: } {Sample text} sl@0: test exec-2.3 {redirecting input from immediate source} {exec stdio} { sl@0: exec [interpreter] $path(cat) << "Sample text" | [interpreter] $path(cat) sl@0: } {Sample text} sl@0: test exec-2.4 {redirecting input from immediate source} {exec stdio} { sl@0: exec [interpreter] $path(cat) | [interpreter] $path(cat) << "Sample text" sl@0: } {Sample text} sl@0: test exec-2.5 {redirecting input from immediate source} {exec} { sl@0: exec [interpreter] $path(cat) "< external conversion did not sl@0: # occur before writing out the temp file. sl@0: exec [interpreter] $path(cat) << "\uE9\uE0\uFC\uF1" sl@0: } "\uE9\uE0\uFC\uF1" sl@0: sl@0: # I/O redirection: output to file. sl@0: sl@0: set path(gorp.file) [makeFile {} gorp.file] sl@0: file delete $path(gorp.file) sl@0: sl@0: test exec-3.1 {redirecting output to file} {exec} { sl@0: exec [interpreter] $path(echo) "Some simple words" > $path(gorp.file) sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "Some simple words" sl@0: test exec-3.2 {redirecting output to file} {exec stdio} { sl@0: exec [interpreter] $path(echo) "More simple words" | >$path(gorp.file) [interpreter] $path(cat) | [interpreter] $path(cat) sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "More simple words" sl@0: test exec-3.3 {redirecting output to file} {exec stdio} { sl@0: exec > $path(gorp.file) [interpreter] $path(echo) "Different simple words" | [interpreter] $path(cat) | [interpreter] $path(cat) sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "Different simple words" sl@0: test exec-3.4 {redirecting output to file} {exec} { sl@0: exec [interpreter] $path(echo) "Some simple words" >$path(gorp.file) sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "Some simple words" sl@0: test exec-3.5 {redirecting output to file} {exec} { sl@0: exec [interpreter] $path(echo) "First line" >$path(gorp.file) sl@0: exec [interpreter] $path(echo) "Second line" >> $path(gorp.file) sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "First line\nSecond line" sl@0: test exec-3.6 {redirecting output to file} {exec} { sl@0: exec [interpreter] $path(echo) "First line" >$path(gorp.file) sl@0: exec [interpreter] $path(echo) "Second line" >>$path(gorp.file) sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "First line\nSecond line" sl@0: test exec-3.7 {redirecting output to file} {exec} { sl@0: set f [open $path(gorp.file) w] sl@0: puts $f "Line 1" sl@0: flush $f sl@0: exec [interpreter] $path(echo) "More text" >@ $f sl@0: exec [interpreter] $path(echo) >@$f "Even more" sl@0: puts $f "Line 3" sl@0: close $f sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } "Line 1\nMore text\nEven more\nLine 3" sl@0: sl@0: # I/O redirection: output and stderr to file. sl@0: sl@0: file delete $path(gorp.file) sl@0: sl@0: test exec-4.1 {redirecting output and stderr to file} {exec} { sl@0: exec [interpreter] "$path(echo)" "test output" >& $path(gorp.file) sl@0: exec [interpreter] "$path(cat)" "$path(gorp.file)" sl@0: } "test output" sl@0: test exec-4.2 {redirecting output and stderr to file} {exec} { sl@0: list [exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" >&$path(gorp.file)] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file)"] sl@0: } {{} {foo bar}} sl@0: test exec-4.3 {redirecting output and stderr to file} {exec} { sl@0: exec [interpreter] $path(echo) "first line" > $path(gorp.file) sl@0: list [exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" >>&$path(gorp.file)] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file)"] sl@0: } "{} {first line\nfoo bar}" sl@0: test exec-4.4 {redirecting output and stderr to file} {exec} { sl@0: set f [open "$path(gorp.file)" w] sl@0: puts $f "Line 1" sl@0: flush $f sl@0: exec [interpreter] "$path(echo)" "More text" >&@ $f sl@0: exec [interpreter] "$path(echo)" >&@$f "Even more" sl@0: puts $f "Line 3" sl@0: close $f sl@0: exec [interpreter] "$path(cat)" "$path(gorp.file)" sl@0: } "Line 1\nMore text\nEven more\nLine 3" sl@0: test exec-4.5 {redirecting output and stderr to file} {exec} { sl@0: set f [open "$path(gorp.file)" w] sl@0: puts $f "Line 1" sl@0: flush $f sl@0: exec >&@ $f [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" sl@0: exec >&@$f [interpreter] "$path(sh)" -c "\"$path(echo)\" xyzzy 1>&2" sl@0: puts $f "Line 3" sl@0: close $f sl@0: exec [interpreter] "$path(cat)" "$path(gorp.file)" sl@0: } "Line 1\nfoo bar\nxyzzy\nLine 3" sl@0: sl@0: # I/O redirection: input from file. sl@0: sl@0: if { [set ::tcltest::testConstraints(exec)] } { sl@0: exec [interpreter] $path(echo) "Just a few thoughts" > $path(gorp.file) sl@0: } sl@0: test exec-5.1 {redirecting input from file} {exec} { sl@0: exec [interpreter] $path(cat) < $path(gorp.file) sl@0: } {Just a few thoughts} sl@0: test exec-5.2 {redirecting input from file} {exec stdio} { sl@0: exec [interpreter] $path(cat) | [interpreter] $path(cat) < $path(gorp.file) sl@0: } {Just a few thoughts} sl@0: test exec-5.3 {redirecting input from file} {exec stdio} { sl@0: exec [interpreter] $path(cat) < $path(gorp.file) | [interpreter] $path(cat) sl@0: } {Just a few thoughts} sl@0: test exec-5.4 {redirecting input from file} {exec stdio} { sl@0: exec < $path(gorp.file) [interpreter] $path(cat) | [interpreter] $path(cat) sl@0: } {Just a few thoughts} sl@0: test exec-5.5 {redirecting input from file} {exec} { sl@0: exec [interpreter] $path(cat) <$path(gorp.file) sl@0: } {Just a few thoughts} sl@0: test exec-5.6 {redirecting input from file} {exec} { sl@0: set f [open $path(gorp.file) r] sl@0: set result [exec [interpreter] $path(cat) <@ $f] sl@0: close $f sl@0: set result sl@0: } {Just a few thoughts} sl@0: test exec-5.7 {redirecting input from file} {exec} { sl@0: set f [open $path(gorp.file) r] sl@0: set result [exec <@$f [interpreter] $path(cat)] sl@0: close $f sl@0: set result sl@0: } {Just a few thoughts} sl@0: sl@0: # I/O redirection: standard error through a pipeline. sl@0: sl@0: test exec-6.1 {redirecting stderr through a pipeline} {exec stdio} { sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar" |& [interpreter] "$path(cat)" sl@0: } "foo bar" sl@0: test exec-6.2 {redirecting stderr through a pipeline} {exec stdio} { sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" |& [interpreter] "$path(cat)" sl@0: } "foo bar" sl@0: test exec-6.3 {redirecting stderr through a pipeline} {exec stdio} { sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" \ sl@0: |& [interpreter] "$path(sh)" -c "\"$path(echo)\" second msg 1>&2 ; \"$path(cat)\"" |& [interpreter] "$path(cat)" sl@0: } "second msg\nfoo bar" sl@0: sl@0: # I/O redirection: combinations. sl@0: sl@0: set path(gorp.file2) [makeFile {} gorp.file2] sl@0: file delete $path(gorp.file2) sl@0: sl@0: test exec-7.1 {multiple I/O redirections} {exec} { sl@0: exec << "command input" > $path(gorp.file2) [interpreter] $path(cat) < $path(gorp.file) sl@0: exec [interpreter] $path(cat) $path(gorp.file2) sl@0: } {Just a few thoughts} sl@0: test exec-7.2 {multiple I/O redirections} {exec} { sl@0: exec < $path(gorp.file) << "command input" [interpreter] $path(cat) sl@0: } {command input} sl@0: sl@0: # Long input to command and output from command. sl@0: sl@0: set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n" sl@0: set a [concat $a $a $a $a] sl@0: set a [concat $a $a $a $a] sl@0: set a [concat $a $a $a $a] sl@0: set a [concat $a $a $a $a] sl@0: test exec-8.1 {long input and output} {exec} { sl@0: exec [interpreter] $path(cat) << $a sl@0: } $a sl@0: sl@0: # More than 20 arguments to exec. sl@0: sl@0: test exec-8.2 {long input and output} {exec} { sl@0: exec [interpreter] $path(echo) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 sl@0: } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23} sl@0: sl@0: # Commands that return errors. sl@0: sl@0: test exec-9.1 {commands returning errors} {exec} { sl@0: set x [catch {exec gorp456} msg] sl@0: list $x [string tolower $msg] [string tolower $errorCode] sl@0: } {1 {couldn't execute "gorp456": no such file or directory} {posix enoent {no such file or directory}}} sl@0: test exec-9.2 {commands returning errors} {exec} { sl@0: string tolower [list [catch {exec [interpreter] echo foo | foo123} msg] $msg $errorCode] sl@0: } {1 {couldn't execute "foo123": no such file or directory} {posix enoent {no such file or directory}}} sl@0: test exec-9.3 {commands returning errors} {exec stdio} { sl@0: list [catch {exec [interpreter] $path(sleep) 1 | [interpreter] $path(exit) 43 | [interpreter] $path(sleep) 1} msg] $msg sl@0: } {1 {child process exited abnormally}} sl@0: test exec-9.4 {commands returning errors} {exec stdio} { sl@0: list [catch {exec [interpreter] $path(exit) 43 | [interpreter] $path(echo) "foo bar"} msg] $msg sl@0: } {1 {foo bar sl@0: child process exited abnormally}} sl@0: test exec-9.5 {commands returning errors} {exec stdio} { sl@0: list [catch {exec gorp456 | [interpreter] echo a b c} msg] [string tolower $msg] sl@0: } {1 {couldn't execute "gorp456": no such file or directory}} sl@0: test exec-9.6 {commands returning errors} {exec} { sl@0: list [catch {exec [interpreter] "$path(sh)" -c "\"$path(echo)\" error msg 1>&2"} msg] $msg sl@0: } {1 {error msg}} sl@0: test exec-9.7 {commands returning errors} {exec stdio} { sl@0: list [catch {exec [interpreter] "$path(sh)" -c "\"$path(echo)\" error msg 1>&2" \ sl@0: | [interpreter] "$path(sh)" -c "\"$path(echo)\" error msg 1>&2"} msg] $msg sl@0: } {1 {error msg sl@0: error msg}} sl@0: sl@0: set path(err) [makeFile {} err] sl@0: sl@0: test exec-9.8 {commands returning errors} {exec} { sl@0: set f [open $path(err) w] sl@0: puts $f { sl@0: puts stdout out sl@0: puts stderr err sl@0: } sl@0: close $f sl@0: list [catch {exec [interpreter] $path(err)} msg] $msg sl@0: } {1 {out sl@0: err}} sl@0: sl@0: # Errors in executing the Tcl command, as opposed to errors in the sl@0: # processes that are invoked. sl@0: sl@0: test exec-10.1 {errors in exec invocation} {exec} { sl@0: list [catch {exec} msg] $msg sl@0: } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}} sl@0: test exec-10.2 {errors in exec invocation} {exec} { sl@0: list [catch {exec | cat} msg] $msg sl@0: } {1 {illegal use of | or |& in command}} sl@0: test exec-10.3 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat |} msg] $msg sl@0: } {1 {illegal use of | or |& in command}} sl@0: test exec-10.4 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat | | cat} msg] $msg sl@0: } {1 {illegal use of | or |& in command}} sl@0: test exec-10.5 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat | |& cat} msg] $msg sl@0: } {1 {illegal use of | or |& in command}} sl@0: test exec-10.6 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat |&} msg] $msg sl@0: } {1 {illegal use of | or |& in command}} sl@0: test exec-10.7 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat <} msg] $msg sl@0: } {1 {can't specify "<" as last word in command}} sl@0: test exec-10.8 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat >} msg] $msg sl@0: } {1 {can't specify ">" as last word in command}} sl@0: test exec-10.9 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat <<} msg] $msg sl@0: } {1 {can't specify "<<" as last word in command}} sl@0: test exec-10.10 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat >>} msg] $msg sl@0: } {1 {can't specify ">>" as last word in command}} sl@0: test exec-10.11 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat >&} msg] $msg sl@0: } {1 {can't specify ">&" as last word in command}} sl@0: test exec-10.12 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat >>&} msg] $msg sl@0: } {1 {can't specify ">>&" as last word in command}} sl@0: test exec-10.13 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat >@} msg] $msg sl@0: } {1 {can't specify ">@" as last word in command}} sl@0: test exec-10.14 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat <@} msg] $msg sl@0: } {1 {can't specify "<@" as last word in command}} sl@0: test exec-10.15 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat < a/b/c} msg] [string tolower $msg] sl@0: } {1 {couldn't read file "a/b/c": no such file or directory}} sl@0: test exec-10.16 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg] sl@0: } {1 {couldn't write file "a/b/c": no such file or directory}} sl@0: test exec-10.17 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg] sl@0: } {1 {couldn't write file "a/b/c": no such file or directory}} sl@0: set f [open $path(gorp.file) w] sl@0: test exec-10.18 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat <@ $f} msg] $msg sl@0: } "1 {channel \"$f\" wasn't opened for reading}" sl@0: close $f sl@0: set f [open $path(gorp.file) r] sl@0: test exec-10.19 {errors in exec invocation} {exec} { sl@0: list [catch {exec cat >@ $f} msg] $msg sl@0: } "1 {channel \"$f\" wasn't opened for writing}" sl@0: close $f sl@0: test exec-10.20 {errors in exec invocation} {exec} { sl@0: list [catch {exec ~non_existent_user/foo/bar} msg] $msg sl@0: } {1 {user "non_existent_user" doesn't exist}} sl@0: test exec-10.21 {errors in exec invocation} {exec} { sl@0: list [catch {exec [interpreter] true | ~xyzzy_bad_user/x | false} msg] $msg sl@0: } {1 {user "xyzzy_bad_user" doesn't exist}} sl@0: test exec-10.22 {errors in exec invocation} \ sl@0: -constraints exec \ sl@0: -returnCodes 1 \ sl@0: -body {exec echo test > ~non_existent_user/foo/bar} \ sl@0: -result {user "non_existent_user" doesn't exist} sl@0: # Commands in background. sl@0: sl@0: test exec-11.1 {commands in background} {exec} { sl@0: set x [lindex [time {exec [interpreter] $path(sleep) 2 &}] 0] sl@0: expr $x<1000000 sl@0: } 1 sl@0: test exec-11.2 {commands in background} {exec} { sl@0: list [catch {exec [interpreter] $path(echo) a &b} msg] $msg sl@0: } {0 {a &b}} sl@0: test exec-11.3 {commands in background} {exec} { sl@0: llength [exec [interpreter] $path(sleep) 1 &] sl@0: } 1 sl@0: test exec-11.4 {commands in background} {exec stdio} { sl@0: llength [exec [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 &] sl@0: } 3 sl@0: test exec-11.5 {commands in background} {exec} { sl@0: set f [open $path(gorp.file) w] sl@0: puts $f [list catch [list exec [info nameofexecutable] $path(echo) foo &]] sl@0: close $f sl@0: string compare "foo" [exec [interpreter] $path(gorp.file)] sl@0: } 0 sl@0: sl@0: # Make sure that background commands are properly reaped when sl@0: # they eventually die. sl@0: sl@0: if { [set ::tcltest::testConstraints(exec)] } { sl@0: exec [interpreter] $path(sleep) 3 sl@0: } sl@0: test exec-12.1 {reaping background processes} \ sl@0: {exec unixOnly nonPortable} { sl@0: for {set i 0} {$i < 20} {incr i} { sl@0: exec echo foo > /dev/null & sl@0: } sl@0: exec sleep 1 sl@0: catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg sl@0: lindex $msg 0 sl@0: } 0 sl@0: test exec-12.2 {reaping background processes} \ sl@0: {exec unixOnly nonPortable} { sl@0: exec sleep 2 | sleep 2 | sleep 2 & sl@0: catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg sl@0: set x [lindex $msg 0] sl@0: exec sleep 3 sl@0: catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg sl@0: list $x [lindex $msg 0] sl@0: } {3 0} sl@0: test exec-12.3 {reaping background processes} \ sl@0: {exec unixOnly nonPortable} { sl@0: exec sleep 1000 & sl@0: exec sleep 1000 & sl@0: set x [exec ps | fgrep "sleep" | fgrep -v fgrep] sl@0: set pids {} sl@0: foreach i [split $x \n] { sl@0: lappend pids [lindex $i 0] sl@0: } sl@0: foreach i $pids { sl@0: catch {exec kill -STOP $i} sl@0: } sl@0: catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg sl@0: set x [lindex $msg 0] sl@0: sl@0: foreach i $pids { sl@0: catch {exec kill -KILL $i} sl@0: } sl@0: catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg sl@0: list $x [lindex $msg 0] sl@0: } {2 0} sl@0: sl@0: # Make sure "errorCode" is set correctly. sl@0: sl@0: test exec-13.1 {setting errorCode variable} {exec} { sl@0: list [catch {exec [interpreter] $path(cat) < a/b/c} msg] [string tolower $errorCode] sl@0: } {1 {posix enoent {no such file or directory}}} sl@0: test exec-13.2 {setting errorCode variable} {exec} { sl@0: list [catch {exec [interpreter] $path(cat) > a/b/c} msg] [string tolower $errorCode] sl@0: } {1 {posix enoent {no such file or directory}}} sl@0: test exec-13.3 {setting errorCode variable} {exec} { sl@0: set x [catch {exec _weird_cmd_} msg] sl@0: list $x [string tolower $msg] [lindex $errorCode 0] \ sl@0: [string tolower [lrange $errorCode 2 end]] sl@0: } {1 {couldn't execute "_weird_cmd_": no such file or directory} POSIX {{no such file or directory}}} sl@0: sl@0: test exec-13.4 {extended exit result codes} { sl@0: -constraints {win} sl@0: -setup { sl@0: set tmp [makeFile {exit 0x00000101} tmpfile.exec-13.4] sl@0: } sl@0: -body { sl@0: list [catch {exec [interpreter] $tmp} err]\ sl@0: [lreplace $::errorCode 1 1 {}] sl@0: } sl@0: -cleanup { sl@0: removeFile $tmp sl@0: } sl@0: -result {1 {CHILDSTATUS {} 257}} sl@0: } sl@0: sl@0: test exec-13.5 {extended exit result codes: max value} { sl@0: -constraints {win} sl@0: -setup { sl@0: set tmp [makeFile {exit 0x3fffffff} tmpfile.exec-13.5] sl@0: } sl@0: -body { sl@0: list [catch {exec [interpreter] $tmp} err]\ sl@0: [lreplace $::errorCode 1 1 {}] sl@0: } sl@0: -cleanup { sl@0: removeFile $tmp sl@0: } sl@0: -result {1 {CHILDSTATUS {} 1073741823}} sl@0: } sl@0: sl@0: test exec-13.6 {extended exit result codes: signalled} { sl@0: -constraints {win} sl@0: -setup { sl@0: set tmp [makeFile {exit 0xffffffff} tmpfile.exec-13.6] sl@0: } sl@0: -body { sl@0: list [catch {exec [interpreter] $tmp} err]\ sl@0: [lreplace $::errorCode 1 1 {}] sl@0: } sl@0: -cleanup { sl@0: removeFile $tmp sl@0: } sl@0: -result {1 {CHILDKILLED {} SIGABRT SIGABRT}} sl@0: } sl@0: sl@0: # Switches before the first argument sl@0: sl@0: test exec-14.1 {-keepnewline switch} {exec} { sl@0: exec -keepnewline [interpreter] $path(echo) foo sl@0: } "foo\n" sl@0: test exec-14.2 {-keepnewline switch} {exec} { sl@0: list [catch {exec -keepnewline} msg] $msg sl@0: } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}} sl@0: test exec-14.3 {unknown switch} {exec} { sl@0: list [catch {exec -gorp} msg] $msg sl@0: } {1 {bad switch "-gorp": must be -keepnewline or --}} sl@0: test exec-14.4 {-- switch} {exec} { sl@0: list [catch {exec -- -gorp} msg] [string tolower $msg] sl@0: } {1 {couldn't execute "-gorp": no such file or directory}} sl@0: sl@0: # Redirecting standard error separately from standard output sl@0: sl@0: test exec-15.1 {standard error redirection} {exec} { sl@0: exec [interpreter] "$path(echo)" "First line" > "$path(gorp.file)" sl@0: list [exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2> "$path(gorp.file)"] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file)"] sl@0: } {{} {foo bar}} sl@0: test exec-15.2 {standard error redirection} {exec stdio} { sl@0: list [exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" \ sl@0: | [interpreter] "$path(echo)" biz baz >$path(gorp.file) 2> "$path(gorp.file2)"] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file)"] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file2)"] sl@0: } {{} {biz baz} {foo bar}} sl@0: test exec-15.3 {standard error redirection} {exec stdio} { sl@0: list [exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" \ sl@0: | [interpreter] "$path(echo)" biz baz 2>$path(gorp.file) > "$path(gorp.file2)"] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file)"] \ sl@0: [exec [interpreter] "$path(cat)" "$path(gorp.file2)"] sl@0: } {{} {foo bar} {biz baz}} sl@0: test exec-15.4 {standard error redirection} {exec} { sl@0: set f [open "$path(gorp.file)" w] sl@0: puts $f "Line 1" sl@0: flush $f sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>@ $f sl@0: puts $f "Line 3" sl@0: close $f sl@0: exec [interpreter] "$path(cat)" "$path(gorp.file)" sl@0: } {Line 1 sl@0: foo bar sl@0: Line 3} sl@0: test exec-15.5 {standard error redirection} {exec} { sl@0: exec [interpreter] "$path(echo)" "First line" > "$path(gorp.file)" sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>> "$path(gorp.file)" sl@0: exec [interpreter] "$path(cat)" "$path(gorp.file)" sl@0: } {First line sl@0: foo bar} sl@0: test exec-15.6 {standard error redirection} {exec stdio} { sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" > "$path(gorp.file2)" 2> "$path(gorp.file)" \ sl@0: >& "$path(gorp.file)" 2> "$path(gorp.file2)" | [interpreter] "$path(echo)" biz baz sl@0: list [exec [interpreter] "$path(cat)" "$path(gorp.file)"] [exec [interpreter] "$path(cat)" "$path(gorp.file2)"] sl@0: } {{biz baz} {foo bar}} sl@0: test exec-15.7 {standard error redirection 2>@1} {exec stdio} { sl@0: # This redirects stderr output into normal result output from exec sl@0: exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>@1 sl@0: } {foo bar} sl@0: sl@0: test exec-16.1 {flush output before exec} {exec} { sl@0: set f [open $path(gorp.file) w] sl@0: puts $f "First line" sl@0: exec [interpreter] $path(echo) "Second line" >@ $f sl@0: puts $f "Third line" sl@0: close $f sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } {First line sl@0: Second line sl@0: Third line} sl@0: test exec-16.2 {flush output before exec} {exec} { sl@0: set f [open $path(gorp.file) w] sl@0: puts $f "First line" sl@0: exec [interpreter] << {puts stderr {Second line}} >&@ $f > $path(gorp.file2) sl@0: puts $f "Third line" sl@0: close $f sl@0: exec [interpreter] $path(cat) $path(gorp.file) sl@0: } {First line sl@0: Second line sl@0: Third line} sl@0: sl@0: set path(script) [makeFile {} script] sl@0: sl@0: test exec-17.1 { inheriting standard I/O } {exec} { sl@0: set f [open $path(script) w] sl@0: puts -nonewline $f {close stdout sl@0: set f [} sl@0: puts $f [list open $path(gorp.file) w]] sl@0: puts $f [list catch \ sl@0: [list exec [info nameofexecutable] $path(echo) foobar &]] sl@0: puts $f [list exec [info nameofexecutable] $path(sleep) 2] sl@0: puts $f {close $f} sl@0: close $f sl@0: catch {exec [interpreter] $path(script)} result sl@0: set f [open $path(gorp.file) r] sl@0: lappend result [read $f] sl@0: close $f sl@0: set result sl@0: } {{foobar sl@0: }} sl@0: sl@0: test exec-18.1 { exec cat deals with weird file names} {exec tempNotWin} { sl@0: # This is cross-platform, but the cat isn't predictably correct on sl@0: # Windows. sl@0: set f "foo\[\{blah" sl@0: set path(fooblah) [makeFile {} $f] sl@0: set fout [open $path(fooblah) w] sl@0: puts $fout "contents" sl@0: close $fout sl@0: set res [list [catch {exec cat $path(fooblah)} msg] $msg] sl@0: removeFile $f sl@0: set res sl@0: } {0 contents} sl@0: sl@0: # Note that this test cannot be adapted to work on Windows; that platform has sl@0: # no kernel support for an analog of O_APPEND. sl@0: test exec-19.1 {exec >> uses O_APPEND} { sl@0: -constraints {exec unix} sl@0: -setup { sl@0: set tmpfile [makeFile {0} tmpfile.exec-19.1] sl@0: } sl@0: -body { sl@0: # Note that we have to allow for the current contents of the sl@0: # temporary file, which is why the result is 14 and not 12 sl@0: exec /bin/sh -c \ sl@0: {for a in 1 2 3; do sleep 1; echo $a; done} >>$tmpfile & sl@0: exec /bin/sh -c \ sl@0: {for a in a b c; do sleep 1; echo $a; done} >>$tmpfile & sl@0: # The above two shell invokations take about 3 seconds to sl@0: # finish, so allow 5s (in case the machine is busy) sl@0: after 5000 sl@0: # Check that no bytes have got lost through mixups with sl@0: # overlapping appends, which is only guaranteed to work when sl@0: # we set O_APPEND on the file descriptor in the [exec >>...] sl@0: file size $tmpfile sl@0: } sl@0: -cleanup { sl@0: removeFile $tmpfile sl@0: } sl@0: -result 14 sl@0: } sl@0: sl@0: # cleanup sl@0: sl@0: foreach file {script gorp.file gorp.file2 echo cat wc sh sleep exit err} { sl@0: removeFile $file sl@0: } sl@0: unset -nocomplain path sl@0: sl@0: ::tcltest::cleanupTests sl@0: return