/
niceSOFT
/
tcl
Обзор
Документация
Войти
/
niceSOFT
/
tcl
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
tests/exec.test
1 755 строк
61 KB
dgp
file lifetime hygiene
18 июн 2026, 22:26
18 июн 2026, 22:26
437c6c5
Код
Авторство
О чём код?
# Commands covered: exec # # This file contains a collection of tests for one or more of the Tcl built-in # commands. Sourcing this file into Tcl runs the tests and generates output # for errors. No output means no errors were found. # # Copyright © 1991-1994 The Regents of the University of California. # Copyright © 1994-1997 Sun Microsystems, Inc. # Copyright © 1998-1999 Scriptics Corporation. # # See the file "license.terms" for information on usage and redistribution of # this file, and for a DISCLAIMER OF ALL WARRANTIES. # There is no point in running Valgrind on cases where [exec] forks but then # fails and the child process doesn't go through full cleanup. if {"::tcltest" ni [namespace children]} { package require tcltest 2.5 namespace import -force ::tcltest::* } source [file join [file dirname [info script]] tcltests.tcl] # Some skips when running in a macOS CI environment testConstraint noosxCI [expr {![info exists ::env(MAC_CI)]}] testConstraint testhandlecount [expr {[llength [info commands testhandlecount]] != 0}] unset -nocomplain path # Utilities that are like Bourne shell stalwarts, but cross-platform. set path(echo) [makeFile { puts -nonewline [lindex $argv 0] foreach str [lrange $argv 1 end] { puts -nonewline " $str" } puts {} exit } echo] set path(echo2) [makeFile { puts stdout [join $argv] puts stderr [lindex $argv 1] exit } echo2] set path(echobin) [makeFile { fconfigure stdout -translation binary puts -nonewline [binary decode hex [join $argv ""]] exit } echobin] set path(cat) [makeFile { if {$argv eq ""} { set argv - } fconfigure stdout -translation binary foreach name $argv { if {$name eq "-"} { set f stdin } elseif {[catch {open $name r} f] != 0} { puts stderr $f continue } fconfigure $f -translation binary while {[eof $f] == 0} { puts -nonewline [read $f] } if {$f ne "stdin"} { close $f } } exit } cat] set path(wc) [makeFile { set data [read stdin] set lines [regsub -all "\n" $data {} dummy] set words [regsub -all "\[^ \t\n]+" $data {} dummy] set chars [string length $data] puts [format "%8.d%8.d%8.d" $lines $words $chars] exit } wc] set path(sh) [makeFile { if {[lindex $argv 0] ne "-c"} { error "sh: unexpected arguments $argv" } set cmd [lindex $argv 1] lappend cmd ";" set newcmd {} foreach arg $cmd { if {$arg eq ";"} { exec >@stdout 2>@stderr [info nameofexecutable] {*}$newcmd set newcmd {} continue } if {$arg eq "1>&2"} { set arg >@stderr } lappend newcmd $arg } exit } sh] set path(sh2) [makeFile { if {[lindex $argv 0] ne "-c"} { error "sh: unexpected arguments $argv" } set cmd [lindex $argv 1] lappend cmd ";" set newcmd {} foreach arg $cmd { if {$arg eq ";"} { exec -ignorestderr >@stdout [info nameofexecutable] {*}$newcmd set newcmd {} continue } lappend newcmd $arg } exit } sh2] set path(sleep) [makeFile { after [expr {$argv*1000}] exit } sleep] set path(exit) [makeFile { exit $argv } exit] proc readfile filename { set f [open $filename] set d [read $f] close $f return [string trimright $d \n] } # ---------------------------------------------------------------------- # Basic operations. test exec-1.1 {basic exec operation} {exec} { exec [interpreter] $path(echo) a b c } "a b c" test exec-1.2 {pipelining} {exec stdio} { exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(cat) } "a b c d" test exec-1.3 {pipelining} {exec stdio} { set a [exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(wc)] list [scan $a "%d %d %d" b c d] $b $c } {3 1 4} set arg {12345678901234567890123456789012345678901234567890} set arg "$arg$arg$arg$arg$arg$arg" test exec-1.4 {long command lines} {exec} { exec [interpreter] $path(echo) $arg } $arg set arg {} test exec-1.5 {pipelining - handle leaks} -constraints {exec stdio testhandlecount} -body { set numHandles [testhandlecount] set a [exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(wc)] list [scan $a "%d %d %d" b c d] $b $c [expr {[testhandlecount] - $numHandles}] } -result {3 1 4 0} # I/O redirection: input from Tcl command. test exec-2.1 {redirecting input from immediate source} {exec stdio} { exec [interpreter] $path(cat) << "Sample text" } {Sample text} test exec-2.2 {redirecting input from immediate source} {exec stdio} { exec << "Sample text" [interpreter] $path(cat) | [interpreter] $path(cat) } {Sample text} test exec-2.3 {redirecting input from immediate source} {exec stdio} { exec [interpreter] $path(cat) << "Sample text" | [interpreter] $path(cat) } {Sample text} test exec-2.4 {redirecting input from immediate source} {exec stdio} { exec [interpreter] $path(cat) | [interpreter] $path(cat) << "Sample text" } {Sample text} test exec-2.5 {redirecting input from immediate source} {exec} { exec [interpreter] $path(cat) "<<Joined to arrows" } {Joined to arrows} test exec-2.6 {redirecting input from immediate source, with UTF} -setup { set sysenc [encoding system] encoding system iso8859-1 proc quotenonascii s { regsub -all {\[|\\|\]} $s {\\&} s regsub -all "\[\x7F-\xFF\]" $s \ {[apply {c {format {\x%02X} [scan $c %c]}} &]} s return [subst -novariables $s] } } -constraints {exec} -body { # If this fails, it may give back: "\xC3\xA9\xC3\xA0\xC3\xBC\xC3\xB1" # If it does, this means that the UTF -> external conversion did not occur # before writing out the temp file. quotenonascii [exec [interpreter] $path(cat) << "\xE9\xE0\xFC\xF1"] } -cleanup { encoding system $sysenc rename quotenonascii {} } -result {\xE9\xE0\xFC\xF1} test exec-2.7 {handle count redirecting input from immediate source} -constraints { exec stdio testhandlecount } -body { set numHandles [testhandlecount] list [exec [interpreter] $path(cat) | [interpreter] $path(cat) << "Sample text"] \ [expr {[testhandlecount] - $numHandles}] } -result [list {Sample text} 0] # I/O redirection: output to file. set path(gorp.file) [makeFile {} gorp.file] file delete $path(gorp.file) test exec-3.1 {redirecting output to file} {exec} { exec [interpreter] $path(echo) "Some simple words" > $path(gorp.file) exec [interpreter] $path(cat) $path(gorp.file) } "Some simple words" test exec-3.2 {redirecting output to file} {exec stdio} { exec [interpreter] $path(echo) "More simple words" | >$path(gorp.file) [interpreter] $path(cat) | [interpreter] $path(cat) exec [interpreter] $path(cat) $path(gorp.file) } "More simple words" test exec-3.3 {redirecting output to file} {exec stdio} { exec > $path(gorp.file) [interpreter] $path(echo) "Different simple words" | [interpreter] $path(cat) | [interpreter] $path(cat) exec [interpreter] $path(cat) $path(gorp.file) } "Different simple words" test exec-3.4 {redirecting output to file} {exec} { exec [interpreter] $path(echo) "Some simple words" >$path(gorp.file) exec [interpreter] $path(cat) $path(gorp.file) } "Some simple words" test exec-3.5 {redirecting output to file} {exec} { exec [interpreter] $path(echo) "First line" >$path(gorp.file) exec [interpreter] $path(echo) "Second line" >> $path(gorp.file) exec [interpreter] $path(cat) $path(gorp.file) } "First line\nSecond line" test exec-3.6 {redirecting output to file} {exec} { exec [interpreter] $path(echo) "First line" >$path(gorp.file) exec [interpreter] $path(echo) "Second line" >>$path(gorp.file) exec [interpreter] $path(cat) $path(gorp.file) } "First line\nSecond line" test exec-3.7 {redirecting output to file} {exec} { set f [open $path(gorp.file) w] puts $f "Line 1" flush $f exec [interpreter] $path(echo) "More text" >@ $f exec [interpreter] $path(echo) >@$f "Even more" puts $f "Line 3" close $f exec [interpreter] $path(cat) $path(gorp.file) } "Line 1\nMore text\nEven more\nLine 3" test exec-3.8 {handle count redirecting output to file} -constraints { exec stdio testhandlecount } -body { set numHandles [testhandlecount] exec > $path(gorp.file) [interpreter] $path(echo) "Different simple words" | [interpreter] $path(cat) | [interpreter] $path(cat) list [exec [interpreter] $path(cat) $path(gorp.file)] \ [expr {[testhandlecount] - $numHandles}] } -result [list "Different simple words" 0] # I/O redirection: output and stderr to file. file delete $path(gorp.file) test exec-4.1 {redirecting output and stderr to file} {exec} { exec [interpreter] $path(echo) "test output" >& $path(gorp.file) exec [interpreter] $path(cat) $path(gorp.file) } "test output" test exec-4.2 {redirecting output and stderr to file} {exec} { list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" >&$path(gorp.file)] \ [exec [interpreter] $path(cat) $path(gorp.file)] } {{} {foo bar}} test exec-4.3 {redirecting output and stderr to file} {exec} { exec [interpreter] $path(echo) "first line" > $path(gorp.file) list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" >>&$path(gorp.file)] \ [exec [interpreter] $path(cat) $path(gorp.file)] } "{} {first line\nfoo bar}" test exec-4.4 {redirecting output and stderr to file} {exec} { set f [open $path(gorp.file) w] puts $f "Line 1" flush $f exec [interpreter] $path(echo) "More text" >&@ $f exec [interpreter] $path(echo) >&@$f "Even more" puts $f "Line 3" close $f exec [interpreter] $path(cat) $path(gorp.file) } "Line 1\nMore text\nEven more\nLine 3" test exec-4.5 {redirecting output and stderr to file} {exec} { set f [open $path(gorp.file) w] puts $f "Line 1" flush $f exec >&@ $f [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" exec >&@$f [interpreter] $path(sh) -c "\"$path(echo)\" xyzzy 1>&2" puts $f "Line 3" close $f exec [interpreter] $path(cat) $path(gorp.file) } "Line 1\nfoo bar\nxyzzy\nLine 3" # I/O redirection: input from file. if {[testConstraint exec]} { exec [interpreter] $path(echo) "Just a few thoughts" > $path(gorp.file) } test exec-5.1 {redirecting input from file} {exec} { exec [interpreter] $path(cat) < $path(gorp.file) } {Just a few thoughts} test exec-5.2 {redirecting input from file} {exec stdio} { exec [interpreter] $path(cat) | [interpreter] $path(cat) < $path(gorp.file) } {Just a few thoughts} test exec-5.3 {redirecting input from file} {exec stdio} { exec [interpreter] $path(cat) < $path(gorp.file) | [interpreter] $path(cat) } {Just a few thoughts} test exec-5.4 {redirecting input from file} {exec stdio} { exec < $path(gorp.file) [interpreter] $path(cat) | [interpreter] $path(cat) } {Just a few thoughts} test exec-5.5 {redirecting input from file} {exec} { exec [interpreter] $path(cat) <$path(gorp.file) } {Just a few thoughts} test exec-5.6 {redirecting input from file} -constraints {exec} -body { set f [open $path(gorp.file) r] exec [interpreter] $path(cat) <@ $f } -cleanup { close $f } -result {Just a few thoughts} test exec-5.7 {redirecting input from file} -constraints {exec} -body { set f [open $path(gorp.file) r] exec <@$f [interpreter] $path(cat) } -cleanup { close $f } -result {Just a few thoughts} test exec-5.8 {handle count redirecting input from file} -constraints { exec stdio testhandlecount } -body { set numHandles [testhandlecount] list [exec [interpreter] $path(cat) | [interpreter] $path(cat) < $path(gorp.file)] \ [expr {[testhandlecount] - $numHandles}] } -result [list {Just a few thoughts} 0] # I/O redirection: standard error through a pipeline. test exec-6.1 {redirecting stderr through a pipeline} {exec stdio} { exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar" |& [interpreter] $path(cat) } "foo bar" test exec-6.2 {redirecting stderr through a pipeline} {exec stdio} { exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" |& [interpreter] $path(cat) } "foo bar" test exec-6.3 {redirecting stderr through a pipeline} {exec stdio} { exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" \ |& [interpreter] $path(sh) -c "\"$path(echo)\" second msg 1>&2 ; \"$path(cat)\"" |& [interpreter] $path(cat) } "second msg\nfoo bar" # I/O redirection: combinations. set path(gorp.file2) [makeFile {} gorp.file2] test exec-7.1 {multiple I/O redirections} {exec} { exec << "command input" > $path(gorp.file2) [interpreter] $path(cat) < $path(gorp.file) exec [interpreter] $path(cat) $path(gorp.file2) } {Just a few thoughts} test exec-7.2 {multiple I/O redirections} {exec} { exec < $path(gorp.file) << "command input" [interpreter] $path(cat) } {command input} # Long input to command and output from command. set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n" set a [concat $a $a $a $a] set a [concat $a $a $a $a] set a [concat $a $a $a $a] set a [concat $a $a $a $a] test exec-8.1 {long input and output} {exec} { exec [interpreter] $path(cat) << $a } $a # More than 20 arguments to exec. test exec-8.2 {long input and output} {exec} { 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 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23} # Commands that return errors. test exec-9.1 {commands returning errors} {exec notValgrind} { set x [catch {exec gorp456} msg] list $x [string tolower $msg] [string tolower $errorCode] } {1 {couldn't execute "gorp456": no such file or directory} {posix enoent {no such file or directory}}} test exec-9.2 {commands returning errors} {exec notValgrind} { string tolower [list [catch {exec [interpreter] echo foo | foo123} msg] $msg $errorCode] } {1 {couldn't execute "foo123": no such file or directory} {posix enoent {no such file or directory}}} test exec-9.3 {commands returning errors} -constraints {exec stdio} -body { exec [interpreter] $path(sleep) 1 | [interpreter] $path(exit) 43 | [interpreter] $path(sleep) 1 } -returnCodes error -result {child process exited abnormally} test exec-9.4 {commands returning errors} -constraints {exec stdio} -body { exec [interpreter] $path(exit) 43 | [interpreter] $path(echo) "foo bar" } -returnCodes error -result {foo bar child process exited abnormally} test exec-9.5 {commands returning errors} -constraints {exec stdio notValgrind} -body { exec gorp456 | [interpreter] echo a b c } -returnCodes error -result {couldn't execute "gorp456": no such file or directory} test exec-9.6 {commands returning errors} -constraints {exec} -body { exec [interpreter] $path(sh) -c "\"$path(echo)\" error msg 1>&2" } -returnCodes error -result {error msg} test exec-9.7 {commands returning errors} -constraints {exec stdio nonPortable} -body { # This test can fail easily on multiprocessor machines exec [interpreter] $path(sh) -c "\"$path(echo)\" error msg 1>&2 ; \"$path(sleep)\" 1" \ | [interpreter] $path(sh) -c "\"$path(echo)\" error msg 1>&2 ; \"$path(sleep)\" 1" } -returnCodes error -result {error msg error msg} set path(err) [makeFile {} err] test exec-9.8 {commands returning errors} -constraints {exec} -setup { set f [open $path(err) w] puts $f { puts stdout out puts stderr err } close $f } -body { exec [interpreter] $path(err) } -returnCodes error -result {out err} test exec-9.9 "exec - empty executable path" -body { exec "" } -result {couldn't execute "": no such file or directory} -returnCodes error # Errors in executing the Tcl command, as opposed to errors in the processes # that are invoked. test exec-10.1 {errors in exec invocation} -constraints {exec} -body { exec } -returnCodes error -result {wrong # args: should be "exec ?-option ...? arg ?arg ...?"} test exec-10.2 {errors in exec invocation} -constraints {exec} -body { exec | cat } -returnCodes error -result {illegal use of | or |& in command} test exec-10.3 {errors in exec invocation} -constraints {exec} -body { exec cat | } -returnCodes error -result {illegal use of | or |& in command} test exec-10.4 {errors in exec invocation} -constraints {exec} -body { exec cat | | cat } -returnCodes error -result {illegal use of | or |& in command} test exec-10.5 {errors in exec invocation} -constraints {exec} -body { exec cat | |& cat } -returnCodes error -result {illegal use of | or |& in command} test exec-10.6 {errors in exec invocation} -constraints {exec} -body { exec cat |& } -returnCodes error -result {illegal use of | or |& in command} test exec-10.7 {errors in exec invocation} -constraints {exec} -body { exec cat < } -returnCodes error -result {can't specify "<" as last word in command} test exec-10.8 {errors in exec invocation} -constraints {exec} -body { exec cat > } -returnCodes error -result {can't specify ">" as last word in command} test exec-10.9 {errors in exec invocation} -constraints {exec} -body { exec cat << } -returnCodes error -result {can't specify "<<" as last word in command} test exec-10.10 {errors in exec invocation} -constraints {exec} -body { exec cat >> } -returnCodes error -result {can't specify ">>" as last word in command} test exec-10.11 {errors in exec invocation} -constraints {exec} -body { exec cat >& } -returnCodes error -result {can't specify ">&" as last word in command} test exec-10.12 {errors in exec invocation} -constraints {exec} -body { exec cat >>& } -returnCodes error -result {can't specify ">>&" as last word in command} test exec-10.13 {errors in exec invocation} -constraints {exec} -body { exec cat >@ } -returnCodes error -result {can't specify ">@" as last word in command} test exec-10.14 {errors in exec invocation} -constraints {exec} -body { exec cat <@ } -returnCodes error -result {can't specify "<@" as last word in command} test exec-10.15 {errors in exec invocation} -constraints {exec} -body { exec cat < a/b/c } -returnCodes error -result {couldn't read file "a/b/c": no such file or directory} test exec-10.16 {errors in exec invocation} -constraints {exec} -body { exec cat << foo > a/b/c } -returnCodes error -result {couldn't write file "a/b/c": no such file or directory} test exec-10.17 {errors in exec invocation} -constraints {exec} -body { exec cat << foo > a/b/c } -returnCodes error -result {couldn't write file "a/b/c": no such file or directory} set f [open $path(gorp.file) w] test exec-10.18 {errors in exec invocation} -constraints {exec} -body { exec cat <@ $f } -returnCodes error -result "channel \"$f\" wasn't opened for reading" close $f set f [open $path(gorp.file) r] test exec-10.19 {errors in exec invocation} -constraints {exec} -body { exec cat >@ $f } -returnCodes error -result "channel \"$f\" wasn't opened for writing" close $f test exec-10.20.1 {errors in exec invocation} -constraints {unix exec notValgrind} -body { exec ~non_existent_user/foo/bar } -returnCodes error -result {couldn't execute "~non_existent_user/foo/bar": no such file or directory} test exec-10.20.2 {errors in exec invocation} -constraints {win exec notValgrind} -body { exec ~non_existent_user/foo/bar } -returnCodes error -result {couldn't execute "~non_existent_user\foo\bar": no such file or directory} test exec-10.21.1 {errors in exec invocation} -constraints {unix exec notValgrind} -body { exec [interpreter] true | ~xyzzy_bad_user/x | false } -returnCodes error -result {couldn't execute "~xyzzy_bad_user/x": no such file or directory} test exec-10.21.2 {errors in exec invocation} -constraints {win exec notValgrind} -body { exec [interpreter] true | ~xyzzy_bad_user/x | false } -returnCodes error -result {couldn't execute "~xyzzy_bad_user\x": no such file or directory} test exec-10.22 {errors in exec invocation} -constraints {exec notValgrind} -body { exec echo test > ~non_existent_user/foo/bar } -returnCodes error -result {couldn't write file "~non_existent_user/foo/bar": no such file or directory} # Commands in background. test exec-11.1 {commands in background} {exec} { set time [time {exec [interpreter] $path(sleep) 2 &}] expr {[lindex $time 0] < 1000000} } 1 test exec-11.2 {commands in background} -constraints {exec} -body { exec [interpreter] $path(echo) a &b } -result {a &b} test exec-11.3 {commands in background} {exec} { llength [exec [interpreter] $path(sleep) 1 &] } 1 test exec-11.4 {commands in background} {exec stdio} { llength [exec [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 &] } 3 test exec-11.5 {commands in background} {exec} { set f [open $path(gorp.file) w] puts $f [list catch [list exec [info nameofexecutable] $path(echo) foo &]] close $f exec [interpreter] $path(gorp.file) } foo test exec-11.6 {commands in background} -constraints { exec stdio testhandlecount } -body { set numHandles [testhandlecount] set n [llength [exec [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 &]] after 1100 tcl::process::purge list $n [expr {([testhandlecount] - $numHandles) <= 0}]; # Could be < 0 if prior processes were reaped } -result {3 1} # Make sure that background commands are properly reaped when they # eventually die. if {[testConstraint exec] && [testConstraint nonPortable]} { after 1300 exec [interpreter] $path(sleep) 1 } test exec-12.1 {reaping background processes} {exec unix nonPortable} { for {set i 0} {$i < 20} {incr i} { exec echo foo > /dev/null & } after 1000 catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg lindex $msg 0 } 0 test exec-12.2 {reaping background processes} {exec unix nonPortable} { exec sleep 2 | sleep 2 | sleep 2 & catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg set x [lindex $msg 0] after 3000 catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg list $x [lindex $msg 0] } {3 0} test exec-12.3 {reaping background processes} {exec unix nonPortable} { exec sleep 1000 & exec sleep 1000 & set x [exec ps | fgrep "sleep" | fgrep -v fgrep] set pids {} foreach i [split $x \n] { lappend pids [lindex $i 0] } foreach i $pids { catch {exec kill -STOP $i} } catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg set x [lindex $msg 0] foreach i $pids { catch {exec kill -KILL $i} } catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg list $x [lindex $msg 0] } {2 0} # Make sure "errorCode" is set correctly. test exec-13.1 {setting errorCode variable} {exec} { list [catch {exec [interpreter] $path(cat) < a/b/c} msg] [string tolower $errorCode] } {1 {posix enoent {no such file or directory}}} test exec-13.2 {setting errorCode variable} {exec} { list [catch {exec [interpreter] $path(cat) > a/b/c} msg] [string tolower $errorCode] } {1 {posix enoent {no such file or directory}}} test exec-13.3 {setting errorCode variable} {exec notValgrind} { set x [catch {exec _weird_cmd_} msg] list $x [string tolower $msg] [lindex $errorCode 0] \ [string tolower [lrange $errorCode 2 end]] } {1 {couldn't execute "_weird_cmd_": no such file or directory} POSIX {{no such file or directory}}} test exec-13.4 {extended exit result codes} -setup { set tmp [makeFile {exit 0x00000101} tmpfile.exec-13.4] } -constraints {win} -body { list [catch {exec [interpreter] $tmp} err] [lreplace $::errorCode 1 1 {}] } -cleanup { removeFile $tmp } -result {1 {CHILDSTATUS {} 257}} test exec-13.5 {extended exit result codes: max value} -setup { set tmp [makeFile {exit 0x3fffffff} tmpfile.exec-13.5] } -constraints {win} -body { list [catch {exec [interpreter] $tmp} err] [lreplace $::errorCode 1 1 {}] } -cleanup { removeFile $tmp } -result {1 {CHILDSTATUS {} 1073741823}} test exec-13.6 {extended exit result codes: signalled} -setup { set tmp [makeFile {exit 0xC0000016} tmpfile.exec-13.6] } -constraints {win} -body { list [catch {exec [interpreter] $tmp} err] [lreplace $::errorCode 1 1 {}] } -cleanup { removeFile $tmp } -result {1 {CHILDKILLED {} SIGABRT SIGABRT}} # Switches before the first argument test exec-14.1 {-keepnewline switch} {exec} { exec -keepnewline [interpreter] $path(echo) foo } "foo\n" test exec-14.2 {-keepnewline switch} -constraints {exec} -body { exec -keepnewline } -returnCodes error -result {wrong # args: should be "exec ?-option ...? arg ?arg ...?"} test exec-14.3 {unknown switch} -constraints {exec} -body { exec -gorp } -returnCodes error -result {bad option "-gorp": must be -ignorestderr, -keepnewline, -encoding, or --} test exec-14.4 {-- switch} -constraints {exec notValgrind} -body { exec -- -gorp } -returnCodes error -result {couldn't execute "-gorp": no such file or directory} test exec-14.5 {-ignorestderr switch} {exec} { # Alas, the use of -ignorestderr is buried here :-( exec [interpreter] $path(sh2) -c [list $path(echo2) foo bar] 2>@1 } "foo bar\nbar" # Redirecting standard error separately from standard output test exec-15.1 {standard error redirection} {exec} { exec [interpreter] $path(echo) "First line" > $path(gorp.file) list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" 2> $path(gorp.file)] \ [exec [interpreter] $path(cat) $path(gorp.file)] } {{} {foo bar}} test exec-15.2 {standard error redirection} {exec stdio} { list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" \ | [interpreter] $path(echo) biz baz >$path(gorp.file) 2> $path(gorp.file2)] \ [exec [interpreter] $path(cat) $path(gorp.file)] \ [exec [interpreter] $path(cat) $path(gorp.file2)] } {{} {biz baz} {foo bar}} test exec-15.3 {standard error redirection} {exec stdio} { list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" \ | [interpreter] $path(echo) biz baz 2>$path(gorp.file) > $path(gorp.file2)] \ [exec [interpreter] $path(cat) $path(gorp.file)] \ [exec [interpreter] $path(cat) $path(gorp.file2)] } {{} {foo bar} {biz baz}} test exec-15.4 {standard error redirection} {exec} { set f [open $path(gorp.file) w] puts $f "Line 1" flush $f exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" 2>@ $f puts $f "Line 3" close $f readfile $path(gorp.file) } {Line 1 foo bar Line 3} test exec-15.5 {standard error redirection} {exec} { exec [interpreter] $path(echo) "First line" > "$path(gorp.file)" exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>> "$path(gorp.file)" readfile $path(gorp.file) } {First line foo bar} test exec-15.6 {standard error redirection} {exec stdio} { exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" > "$path(gorp.file2)" 2> "$path(gorp.file)" \ >& "$path(gorp.file)" 2> "$path(gorp.file2)" | [interpreter] $path(echo) biz baz list [readfile $path(gorp.file)] [readfile $path(gorp.file2)] } {{biz baz} {foo bar}} test exec-15.7 {standard error redirection 2>@1} {exec stdio} { # This redirects stderr output into normal result output from exec exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>@1 } {foo bar} test exec-16.1 {flush output before exec} {exec} { set f [open $path(gorp.file) w] puts $f "First line" exec [interpreter] $path(echo) "Second line" >@ $f puts $f "Third line" close $f readfile $path(gorp.file) } {First line Second line Third line} test exec-16.2 {flush output before exec} {exec} { set f [open $path(gorp.file) w] puts $f "First line" exec [interpreter] << {puts stderr {Second line}} >&@ $f > $path(gorp.file2) puts $f "Third line" close $f readfile $path(gorp.file) } {First line Second line Third line} test exec-17.1 {inheriting standard I/O} -constraints {exec} -setup { set path(script) [makeFile {} script] set f [open $path(script) w] puts $f [list lassign [list \ [info nameofexecutable] $path(gorp.file) $path(echo) $path(sleep) \ ] exe file echo sleep] puts $f { close stdout set f [open $file w] catch {exec $exe $echo foobar &} exec $exe $sleep 2 close $f } close $f } -body { catch {exec [interpreter] $path(script)} result list $result [readfile $path(gorp.file)] } -cleanup { removeFile $path(script) } -result {{} foobar} test exec-18.1 {exec deals with weird file names} -body { set path(fooblah) [makeFile {contents} "foo\[\{blah"] exec [interpreter] $path(cat) $path(fooblah) } -constraints {exec} -cleanup { removeFile $path(fooblah) } -result contents test exec-18.2 {exec cat deals with weird file names} -body { # This is cross-platform, but the cat isn't predictably correct on # Windows. set path(fooblah) [makeFile {contents} "foo\[\{blah"] exec cat $path(fooblah) } -constraints {exec tempNotWin} -cleanup { removeFile $path(fooblah) } -result contents # Note that this test cannot be adapted to work on Windows; that platform has # no kernel support for an analog of O_APPEND. OTOH, that means we can assume # that there is a POSIX shell... # # This test also fails in some cases when building with macOS test exec-19.1 {exec >> uses O_APPEND} -constraints {exec unix notValgrind noosxCI} -setup { set tmpfile [makeFile {0} tmpfile.exec-19.1] } -body { # Note that we have to allow for the current contents of the temporary # file, which is why the result is 14 and not 12 exec /bin/sh -c \ {for a in 1 2 3; do sleep 1; echo $a; done} >>$tmpfile & exec /bin/sh -c \ {for a in 4 5 6; do sleep 1; echo $a >&2; done} 2>>$tmpfile & exec /bin/sh -c \ {for a in a b c; do sleep 1; echo $a; done} >>$tmpfile & exec /bin/sh -c \ {for a in d e f; do sleep 1; echo $a >&2; done} 2>>$tmpfile & # The above four shell invocations take about 3 seconds to finish, so allow # 5s (in case the machine is busy) after 5000 # Check that no bytes have got lost through mixups with overlapping # appends, which is only guaranteed to work when we set O_APPEND on the # file descriptor in the [exec >>...] file size $tmpfile } -cleanup { removeFile $tmpfile } -result 26 # Tests to ensure batch files and .CMD (Bug 9ece99d58b) # can be executed on Windows test exec-20.1 {exec .bat file} -constraints {win} -body { set log [makeFile {} exec20.log] exec [makeFile "echo %1> \"$log\"" exec20.bat] "Testing exec-20.0" viewFile $log } -result "\"Testing exec-20.0\"" test exec-20.2 {exec .CMD file} -constraints {win} -body { set log [makeFile {} exec201.log] exec [makeFile "echo %1> \"$log\"" exec201.CMD] "Testing exec-20.1" viewFile $log } -result "\"Testing exec-20.1\"" # Test with encoding mismatches (Bug 0f1ddc0df7fb7) test exec-21.1 {exec encoding mismatch on stdout} -setup { set path(script) [makeFile { fconfigure stdout -translation binary puts a\xe9b } script] set enc [encoding system] encoding system utf-8 } -cleanup { removeFile $path(script) encoding system $enc } -body { exec [info nameofexecutable] $path(script) } -result a\uFFFDb test exec-21.2 {exec encoding mismatch on stderr} -setup { set path(script) [makeFile { fconfigure stderr -translation binary puts stderr a\xe9b } script] set enc [encoding system] encoding system utf-8 } -cleanup { removeFile $path(script) encoding system $enc } -body { list [catch {exec [info nameofexecutable] $path(script)} r] $r } -result [list 1 a\uFFFDb] # TIP 716 -encoding option test exec-22.0 {exec -encoding} -body { set enc [expr {[encoding system] eq "utf-8" ? "iso2022-jp" : "utf-8"}] exec -encoding $enc -- [interpreter] $path(echobin) [binary encode hex [encoding convertto $enc \u4e4e\u68d9]] } -result \u4e4e\u68d9 test exec-22.1 {exec -encoding invalid encoding} -body { exec -encoding nosuchencoding -- [interpreter] $path(echobin) abc } -result {unknown encoding "nosuchencoding"} -returnCodes error # ---------------------------------------------------------------------- # Tests specific to Windows which has more varied application types and # search mechanism. namespace eval tcl::test::exec { # Extensions directly understood by exec on Windows variable execExts {com bat cmd exe} variable ext variable envStack {} variable dirStack {} variable windir [expr {[info exists ::env(WINDIR)] ? $::env(WINDIR) : "C:/Windows"}] variable wingetPath variable testApp [file join [file dirname [interpreter]] execTestApp.exe] # Need a App Exec Alias for testing exec of reparse points, according to MS, # winget can only be used on servers with an installed desktop interface, no # idea how to check it in GHA programmatically, so simply disable it (todo: # rewrite with better check later) if {[testConstraint win]} { if {[file exists $testApp]} { testConstraint haveTestApp 1 } if {![info exists ::env(CI)] && [info exists ::env(LOCALAPPDATA)]} { set wingetPath [file join $::env(LOCALAPPDATA) "Microsoft" "WindowsApps" "winget.exe"] if {[file exists $wingetPath]} { # Ensure it is on the PATH testConstraint haveWinget 1 } } if {[file executable [file join $::env(WINDIR) system32 wscript.exe]] && [info exists ::env(PATHEXT)] && [lsearch -nocase -ascii [split $::env(PATHEXT) ";"] ".VBS"] >= 0 } { testConstraint haveWScript 1 } } proc rmdirs {args} { foreach arg $args { removeDirectory $arg } } proc nativeTestPath {args} { return [file nativename [file join [temporaryDirectory] {*}$args]] } # Save env vars specified by args proc pushEnv {args} { global env variable envStack set saved [dict create] foreach envVar $args { if {[info exists env($envVar)]} { dict set saved $envVar $env($envVar) } else { dict set saved $envVar "" } } lappend envStack $saved } # Restore env vars saved by pushEnv proc popEnv {} { global env variable envStack if {[llength $envStack] == 0} { error "Test implementation error: mismatched push/pops for envStack." } dict for {envVar envValue} [lpop envStack] { if {$envValue ne ""} { set env($envVar) $envValue } else { unset -nocomplain env($envVar) } } } # Save cwd, and env vars specified by args, makeDirectory $dir, cd $dir # Returns path to dir proc pushContext {dir args} { global env variable dirStack lappend dirStack [pwd] pushEnv {*}$args set dir [makeDirectory $dir] cd $dir return $dir } # Restore last context saved by pushContext proc popContext {} { global env variable dirStack if {[llength $dirStack] == 0} { error "Test implementation error: mismatched push/pops for dirStack." } popEnv cd [lpop dirStack] } # Content of batch files variable batContent { @echo off setlocal enabledelayedexpansion :parse if "%~1"=="" goto :eof if /i "%~1"=="-path" ( rem fullname of this script echo %~dpnx0 shift goto parse ) if /i "%~1"=="-cwd" ( rem Current working directory echo %CD% shift goto parse ) if /i "%~1"=="-argv0" ( rem Script name as passed echo %0 shift goto parse ) if /i "%~1"=="-env" ( rem Next argument is environment variable name if "%~2"=="" ( echo Error: -env requires a variable name goto :eof ) call echo %%%~2%% shift shift goto parse ) rem Unknown option echo Unknown option: %~1 shift goto parse } foreach ext $execExts { set path nosuchfile.$ext test exec-win-1.1-$ext "Exec missing $ext file" -constraints win -body { exec $path } -result "couldn't execute \"$path\": no such file or directory" \ -returnCodes error set path [makeDirectory exec-win-2.0.$ext] test exec-win-2.1-$ext "Exec directories with extension $ext" \ -constraints win \ -body { exec $path } -result "couldn't execute \"[file nativename $path]\": no such file or directory" \ -returnCodes error rmdirs $path } # Junk content - only test for .exe as bat,cmd may have any content test exec-win-3.1 "exec - junk .exe files" \ -constraints win -setup { set prog [makeFile [string repeat x 1000] exec-win-3.1.exe] } -body { exec $prog } -cleanup { removeFile $prog } -result "couldn't execute \"[nativeTestPath exec-win-3.1.exe]\": no such file or directory" \ -returnCodes error test exec-win-3.2 "exec - ignore invalid exe files along path without aborting" \ -constraints haveTestApp \ -setup { set dir1 [makeDirectory exec-win-3.2-1] set dir2 [makeDirectory exec-win-3.2-2] writeFile [file join $dir1 testApp.exe] [string repeat x 200] file copy $testApp [file join $dir2 testApp.exe] pushEnv PATH append ::env(PATH) ";" [file nativename $dir1] ";" [file nativename $dir2] } -cleanup { popEnv rmdirs $dir1 $dir2 } -body { exec testApp -path } -result [nativeTestPath exec-win-3.2-2 testApp.exe] test exec-win-4.1 "exec - verify exe priority over bat, cmd" \ -constraints haveTestApp \ -setup { set dir [pushContext exec-win-4.1] writeFile [file join $dir testApp.bat] $batContent writeFile [file join $dir testApp.cmd] $batContent file copy $testApp [file join $dir testApp.exe] } -cleanup { popContext rmdirs $dir } -body { exec testApp -path } -result [nativeTestPath exec-win-4.1 testApp.exe] test exec-win-4.2 "Verify bat priority over cmd" \ -constraints haveTestApp \ -setup { set dir [pushContext exec-win-4.2] writeFile [file join $dir testApp.cmd] $batContent writeFile [file join $dir testApp.bat] $batContent } -cleanup { popContext rmdirs $dir } -body { exec testApp -path } -result [nativeTestPath exec-win-4.2 testApp.bat] test exec-win-4.3 "Exec explicit extensions" \ -constraints haveTestApp \ -setup { set dir [pushContext exec-win-4.1] writeFile [file join $dir testApp.bat] $batContent writeFile [file join $dir testApp.cmd] $batContent file copy $testApp [file join $dir testApp.exe] } -cleanup { popContext rmdirs $dir } -body { list \ [exec testApp.cmd -path] \ [exec testApp.bat -path] \ [exec testApp.exe -path] } -result [list \ [nativeTestPath exec-win-4.1 testApp.cmd] \ [nativeTestPath exec-win-4.1 testApp.bat] \ [nativeTestPath exec-win-4.1 testApp.exe]] test exec-win-5.1 "exec - verify search path prioritized over extension" \ -constraints haveTestApp \ -setup { set dir1 [makeDirectory exec-win-5.1-1] set dir2 [makeDirectory exec-win-5.1-2] writeFile [file join $dir1 testApp.bat] $batContent file copy $testApp [file join $dir2 testApp.exe] pushEnv PATH append ::env(PATH) ";" [file nativename $dir1] ";" [file nativename $dir2] } -cleanup { popEnv rmdirs $dir1 $dir2 } -body { exec testApp -path } -result [nativeTestPath exec-win-5.1-1 testApp.bat] test exec-win-6.1 "exec - verify execution irrespective of random extension" \ -constraints haveTestApp \ -setup { set fooFile [file join [temporaryDirectory] testApp.foo] file copy $testApp $fooFile } -cleanup { file delete $fooFile unset fooFile } -body { exec $fooFile -path } -result [nativeTestPath testApp.foo] test exec-win-6.2 "exec - verify execution of exe masquerading as .bat" \ -constraints haveTestApp \ -setup { set batFile [file join [temporaryDirectory] testApp.bat] file copy $testApp $batFile } -cleanup { file delete $batFile unset batFile } -body { exec $batFile -path } -result [nativeTestPath testApp.bat] test exec-win-6.3 "exec - verify execution of exe in path masquerading as .com" \ -constraints win \ -body { exec chcp } -result "Active code*" -match glob test exec-win-7.1 "exec - verify Windows directory checked before PATH" \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-7.1] # copy to a name that is present in the Windows dir set dummyAttribExe [file join $dir attrib.exe] file copy $testApp [file join $dir testApp.exe] file copy $testApp $dummyAttribExe pushEnv PATH set ::env(PATH) [file nativename $dir] } -cleanup { rmdirs $dir popEnv } -body { # Verify path is correct by calling testApp and then # the real attrib is picked up though not on path list [exec testApp.exe -path] [exec attrib.exe /?] } -result {*testApp.exe*Displays or changes*} -match glob test exec-win-7.2.1 "exec - verify cwd checked before Windows and PATH" \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-7.2.1] set newCwd [pushContext exec-win-7.2.1-cwd PATH] # copy to a name that is present in the Windows dir to cwd and PATH file copy $testApp [file join $newCwd attrib.exe] file copy $testApp [file join $dir attrib.exe] set ::env(PATH) [file nativename $dir] } -cleanup { popContext file delete [file join [pwd] attrib.exe] rmdirs $dir $newCwd unset -nocomplain fp } -body { # The dummy attrib.exe in cwd should be executed # file normalize because some commands return lower case drive letters # and some return upper case lmap fp [split [exec attrib.exe -path -env PATH] \n] { file normalize $fp } } -result [list [file normalize [file join [temporaryDirectory] exec-win-7.2.1-cwd attrib.exe]] \ [file normalize [file join [temporaryDirectory] exec-win-7.2.1]]] test exec-win-7.2.2 "exec - verify cwd NOT checked if env NoDefaultCurrentDirectoryInExePath exists " \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-7.2.2] set newCwd [pushContext exec-win-7.2.2-cwd \ PATH NoDefaultCurrentDirectoryInExePath] file copy $testApp [file join $dir testApp.exe] file copy $testApp [file join $newCwd testApp.exe] set ::env(PATH) [file nativename $dir] set ::env(NoDefaultCurrentDirectoryInExePath) 1 } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain fp } -body { # The testApp.exe in PATH, not cwd should be executed # file normalize because some commands return lower case drive letters # and some return upper case file normalize [exec testApp.exe -path] } -result [file normalize [file join [temporaryDirectory] exec-win-7.2.2 testApp.exe]] test exec-win-7.2.3 "exec - verify *explicit* cwd checked even if env NoDefaultCurrentDirectoryInExePath exists " \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-7.2.3] set newCwd [pushContext exec-win-7.2.3-cwd \ PATH NoDefaultCurrentDirectoryInExePath] file copy $testApp [file join $dir testApp.exe] file copy $testApp [file join $newCwd testApp.exe] set ::env(PATH) [string cat ".;" [file nativename $dir]] set ::env(NoDefaultCurrentDirectoryInExePath) 1 } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain fp } -body { # The testApp.exe in PATH, not cwd should be executed # file normalize because some commands return lower case drive letters # and some return upper case file normalize [exec testApp.exe -path] } -result [file normalize [file join [temporaryDirectory] exec-win-7.2.3-cwd testApp.exe]] # We really want to test an empty path but Tcl is broken with unsetting # PATH to empty. Bug b492c33154. test exec-win-7.3 "exec - verify Windows program with dummy PATH" \ -constraints win \ -setup { pushEnv PATH set ::env(PATH) "C:\\NoSuchDir" } -cleanup { popEnv } -body { exec attrib /? } -result {*Displays or changes file attributes*} -match glob test exec-win-7.4 "exec - verify cwd works with dummy PATH" \ -constraints haveTestApp \ -setup { file copy $testApp [file join [pwd] testApp.exe] pushEnv PATH set ::env(PATH) "C:\\NoSuchDir" } -cleanup { file delete [file join [pwd] testApp.exe] popEnv } -body { exec testApp -path } -result [file nativename [file join [pwd] testApp.exe]] test exec-win-7.5 "exec - ignore empty elements in PATH" \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-7.5] file copy $testApp [file join $dir testApp.exe] pushEnv PATH set ::env(PATH) [string cat \ ";;;" \ $::env(PATH) \ ";" [file nativename $dir] \ ";;;"] } -cleanup { popEnv rmdirs $dir } -body { exec testApp -path } -result [nativeTestPath exec-win-7.5 testApp.exe] test exec-win-7.6 "exec - ignore non-existent dirs in PATH" \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-7.5] file copy $testApp [file join $dir testApp.exe] pushEnv PATH set ::env(PATH) [string cat \ $::env(PATH) \ ";" [file nativename [file join $dir nosuchdir]] \ ";" [file nativename $dir]] } -cleanup { popEnv rmdirs $dir } -body { exec testApp -path } -result [nativeTestPath exec-win-7.5 testApp.exe] test exec-win-7.7 "exec - verify dir of current executable checked first" \ -constraints haveTestApp \ -setup { # copy to a name that is present in the Windows dir (attrib) to # directory of current exe, cwd and PATH set newCwd [pushContext exec-win-7.7-1 PATH] set dir2 [makeDirectory exec-win-7.7-2]; # Will be added to PATH file copy $testApp [file join [file dirname [info nameofexecutable]] attrib.exe] file copy $testApp [file join $newCwd attrib.exe] file copy $testApp [file join $dir2 attrib.exe] set ::env(PATH) [file nativename $dir2] } -cleanup { file delete [file join [file dirname [info nameofexecutable]] attrib.exe] rmdirs $newCwd $dir2 popContext } -body { # The dummy attrib.exe in the program's directory should be executed # file normalize because some commands return lower case drive letters # and some return upper case exec attrib -path } -result [file nativename \ [file join [file dirname [info nameofexecutable]] \ attrib.exe]] test exec-win-8.1 "exec - verify / separator" \ -constraints haveTestApp \ -setup { set fn [file join [pwd] testApp.exe] file copy $testApp $fn } -cleanup { file delete $fn unset -nocomplain fn } -body { exec $fn -path } -result [file nativename [file join [pwd] testApp.exe]] test exec-win-8.2 "exec - verify \ separator" \ -constraints haveTestApp \ -setup { set fn [file join [pwd] testApp.exe] file copy $testApp $fn } -cleanup { file delete $fn } -body { exec [file nativename $fn] -path } -result [file nativename [file join [pwd] testApp.exe]] test exec-win-9.1 "exec - verify relative paths work relative to cwd" \ -constraints haveTestApp \ -setup { set dir [pushContext exec-win-9.1 PATH] file copy $testApp [file join $dir testApp.exe] set newCwd [file dirname $dir] cd $newCwd set ::env(PATH) [file nativename $newCwd] } -cleanup { rmdirs $dir popContext } -body { file normalize [exec exec-win-9.1/testApp.exe -path] } -result [file normalize [nativeTestPath exec-win-9.1 testApp.exe]] test exec-win-9.2 "exec - verify relative paths are not searched in PATH" \ -constraints haveTestApp \ -setup { set dir [makeDirectory exec-win-9.2] file copy $testApp [file join $dir testApp.exe] set newCwd [pushContext exec-win-9.2-cwd PATH] set ::env(PATH) [file nativename $dir] unset -nocomplain result } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain dir err } -body { # Verify can be executed if no directory separator # but not if directory separator is present lappend result [file normalize [exec testApp.exe -path]] lappend result [catch {file normalize [exec ./testApp.exe -path]} err] $err } -result [list [file normalize [nativeTestPath exec-win-9.2 testApp.exe]] \ 1 \ {couldn't execute ".\testApp.exe": no such file or directory}] test exec-win-10.1 {exec App Execution Alias - Bug 4f0b5767ac} -setup { # Ensure winget is on the PATH pushEnv PATH append ::env(PATH) ";" [file nativename [file dirname $wingetPath]] } -cleanup { popEnv } -constraints haveWinget -body { exec winget --info } -result "*Windows*" -match glob # # auto_execok tests for Windows variable cmdBuiltin foreach cmdBuiltin { assoc call cd cls color copy date del dir echo erase exit ftype for if md mkdir mklink move path pause prompt rd ren rename rmdir set start time title type ver vol } { test auto_execok-win-1.1-$cmdBuiltin "auto_execok cmd built-in $cmdBuiltin" \ -constraints win \ -cleanup { unset -nocomplain rest cmd } -body { set rest [lassign [auto_execok $cmdBuiltin] cmd] set cmd [file normalize $cmd] list [file normalize $cmd] {*}$rest } -result [list [file normalize [file join $windir system32 cmd.exe]] /c $cmdBuiltin] } unset cmdBuiltin test auto_execok-win-2.1 "auto_execok - verify PATHEXT order of priority and explicit extensions" \ -constraints win \ -setup { # Assumes .bat and .cmd are in configured in the registry set dir [pushContext auto_execok-win-2.1 PATHEXT] writeFile [file join $dir testApp.bat] "" writeFile [file join $dir testApp.cmd] "" } -cleanup { popContext rmdirs $dir } -body { set result [list ] set ::env(PATHEXT) ".bat;.cmd" lappend result [auto_execok testApp] set ::env(PATHEXT) ".cmd;.bat" lappend result [auto_execok testApp] lappend result [auto_execok testApp.cmd] lappend result [auto_execok testApp.bat] } -result [list \ [list {.\testApp.bat}] \ [list {.\testApp.cmd}] \ [list {.\testApp.cmd}] \ [list {.\testApp.bat}]] test auto_execok-win-2.2 "auto_execok - verify cmd built-ins prioritized" \ -constraints haveTestApp \ -setup { set dir [pushContext auto_execok-win-2.2] writeFile [file join $dir echo.exe] "" } -cleanup { popContext rmdirs $dir } -body { list [string tolower [auto_execok echo]] [auto_execok echo.exe] } -result [list \ [list [string tolower $windir\\system32\\cmd.exe] \ /c echo] \ [list {.\echo.exe}]] test auto_execok-win-2.3 "auto_execok - verify search path prioritized over extension" \ -constraints haveTestApp \ -setup { set dir1 [makeDirectory auto_execok-win-2.3-1] set dir2 [makeDirectory auto_execok-win-2.3-2] writeFile [file join $dir1 testApp.bat] $batContent writeFile [file join $dir2 testApp.exe] "" pushEnv PATH PATHEXT append ::env(PATH) ";" [file nativename $dir1] ";" [file nativename $dir2] set ::env(PATHEXT) ".exe;.bat" } -cleanup { popEnv rmdirs $dir1 $dir2 } -body { auto_execok testApp } -result [list [nativeTestPath auto_execok-win-2.3-1 testApp.bat]] test auto_execok-win-2.4 { auto_execok - verify files without extension are skipped (Bug 596936dd) } -constraints win -setup { set newCwd [pushContext auto_execok-win-2.4] writeFile [file join $newCwd testApp] "" writeFile [file join $newCwd testApp.bat] "" } -body { list [auto_execok testApp] [auto_execok testApp.bat] } -cleanup { popContext rmdirs $newCwd } -result {{{.\testApp.bat}} {{.\testApp.bat}}} test auto_execok-win-3.1 "auto_execok - verify Windows directory checked before PATH" \ -constraints haveTestApp \ -setup { set dir [makeDirectory auto_execok-win-3.0] # copy to a name that is present in the Windows dir set dummyAttribExe [file join $dir attrib.exe] writeFile $dummyAttribExe "" pushEnv PATH set ::env(PATH) [file nativename $dir] } -cleanup { popEnv rmdirs $dir } -body { string tolower [auto_execok attrib] } -result [list [string tolower $windir\\system32\\attrib.exe]] test auto_execok-win-3.2.1 { auto_execok - verify cwd checked before Windows and PATH } -constraints win -setup { set dir [makeDirectory auto_execok-win-3.2.1] set newCwd [pushContext auto_execok-win-3.2.1-cwd PATH] # copy to a name that is present in the Windows dir to cwd and PATH writeFile [file join $newCwd attrib.exe] "" writeFile [file join $dir attrib.exe] "" set ::env(PATH) [file nativename $dir] } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain fp } -body { auto_execok attrib } -result [list .\\attrib.exe] test auto_execok-win-3.2.2 { auto_execok - verify cwd NOT checked if env NoDefaultCurrentDirectoryInExePath exists } -constraints win -setup { set dir [makeDirectory auto_execok-win-3.2.2] set newCwd [pushContext auto_execok-win-3.2.2-cwd \ PATH NoDefaultCurrentDirectoryInExePath] # copy to a name that is present in the cwd and PATH writeFile [file join $newCwd testApp.exe] "" writeFile [file join $dir testApp.exe] "" set ::env(PATH) [file nativename $dir] set ::env(NoDefaultCurrentDirectoryInExePath) 1 } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain fp } -body { string tolower [auto_execok testApp] } -result [list [string tolower [nativeTestPath auto_execok-win-3.2.2 testApp.exe]]] test auto_execok-win-3.2.3 { auto_execok - verify explicit cwd checked even if env NoDefaultCurrentDirectoryInExePath exists } -constraints win -setup { set dir [makeDirectory auto_execok-win-3.2.3] set newCwd [pushContext auto_execok-win-3.2.3-cwd \ PATH NoDefaultCurrentDirectoryInExePath] # copy to a name that is present in cwd and PATH writeFile [file join $newCwd testApp.exe] "" writeFile [file join $dir testApp.exe] "" set ::env(PATH) [file nativename $dir] set ::env(NoDefaultCurrentDirectoryInExePath) 1 } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain fp } -body { auto_execok ./testApp } -result [list .\\testApp.exe] # We really want to test an empty path but Tcl is broken with unsetting # PATH to empty. Bug b492c33154. test auto_execok-win-3.3 { auto_execok - verify Windows program with dummy PATH } -constraints win -setup { pushEnv PATH set ::env(PATH) "C:\\NoSuchDir" } -cleanup { popEnv } -body { string tolower [auto_execok attrib] } -result [list [string tolower $windir\\system32\\attrib.exe]] test auto_execok-win-3.4 { auto_execok - verify cwd with dummy PATH } -constraints win -setup { set dir [pushContext auto_execok-win-3.4 PATH] # copy to a name that is present in cwd and PATH writeFile [file join $dir testApp.exe] "" set ::env(PATH) "C:\\NoSuchDir" } -cleanup { popContext } -body { auto_execok testApp } -result [list .\\testApp.exe] test auto_execok-win-3.5 { auto_execok - ignore empty elements in PATH } -constraints win -setup { set dir [makeDirectory auto_execok-win-3.5] writeFile [file join $dir testApp.exe] "" pushEnv PATH set ::env(PATH) [string cat \ ";;;" \ $::env(PATH) \ ";" [file nativename $dir] \ ";;;"] } -cleanup { popEnv rmdirs $dir } -body { auto_execok testApp } -result [list [nativeTestPath auto_execok-win-3.5 testApp.exe]] test auto_execok-win-3.6 { auto_execok - ignore non-existent elements in PATH } -constraints win -setup { set dir [makeDirectory auto_execok-win-3.6] writeFile [file join $dir testApp.exe] "" pushEnv PATH set ::env(PATH) [string cat \ $::env(PATH) \ ";" [file nativename [file join $dir nosuchdir]] \ ";" [file nativename $dir]] } -cleanup { popEnv rmdirs $dir } -body { auto_execok testApp } -result [list [nativeTestPath auto_execok-win-3.6 testApp.exe]] test auto_execok-win-3.7 { auto_execok - verify dir of current executable checked first } -constraints win -setup { # copy to a name that is present in the Windows dir (attrib) to # directory of current exe, cwd and PATH set newCwd [pushContext exec-win-7.7-1 PATH] set dir2 [makeDirectory exec-win-7.7-2]; # Will be added to PATH writeFile [file join [file dirname [info nameofexecutable]] attrib.exe] "" writeFile [file join $newCwd attrib.exe] "" writeFile [file join $dir2 attrib.exe] "" set ::env(PATH) [file nativename $dir2] } -cleanup { file delete [file join [file dirname [info nameofexecutable]] attrib.exe] rmdirs $newCwd $dir2 popContext } -body { string tolower [auto_execok attrib] } -result [list [string tolower \ [file nativename \ [file join [file dirname [info nameofexecutable]] \ attrib.exe]]]] test auto_execok-win-4.1 { auto_execok - verify / separator } -constraints win -setup { set fn [file join [temporaryDirectory] testApp.exe] writeFile $fn "" } -cleanup { file delete $fn unset -nocomplain fn } -body { # File join ensures / separator string tolower [auto_execok [file join $fn]] } -result [list [string tolower \ [file nativename \ [file join [temporaryDirectory] testApp.exe]]]] test auto_execok-win-4.2 { auto_execok - verify \ separator } -constraints win -setup { set fn [file join [temporaryDirectory] testApp.exe] writeFile $fn "" } -cleanup { file delete $fn unset -nocomplain fn } -body { string tolower [auto_execok [file nativename $fn]] } -result [list [string tolower \ [file nativename \ [file join [temporaryDirectory] testApp.exe]]]] test auto_execok-win-5.1 { auto_execok - verify relative paths work relative to cwd } -constraints haveTestApp -setup { set dir [pushContext auto_execok-win-5.1 PATH] writeFile [file join $dir testApp.exe] "" set newCwd [file dirname $dir] cd $newCwd set ::env(PATH) [file nativename $newCwd] } -cleanup { rmdirs $dir popContext } -body { auto_execok auto_execok-win-5.1/testApp.exe } -result [list auto_execok-win-5.1\\testApp.exe] test auto_execok-win-5.2 { auto_execok - verify relative paths are not searched in PATH } -constraints win -setup { set dir [makeDirectory auto_execok-win-5.2] writeFile [file join $dir testApp.exe] "" set newCwd [pushContext auto_execok-win-5.2-cwd PATH] set ::env(PATH) [file nativename $dir] unset -nocomplain result } -cleanup { popContext rmdirs $dir $newCwd unset -nocomplain dir result } -body { # Verify can be found if no directory separator # but not if directory separator is present lappend result [auto_execok testApp.exe] lappend result [auto_execok ./testApp.exe] } -result [list [list [nativeTestPath auto_execok-win-5.2 testApp.exe]] \ {}] test auto_execok-win-6.1 { auto_execok - verify paths are not cached. Bugs 1244733 and 4dc35e0c0c } -constraints win -setup { set dir [pushContext auto_execok-win-6.1] writeFile [file join $dir testApp.exe] "" unset -nocomplain result } -cleanup { rmdirs $dir popContext } -body { lappend result [lindex [auto_execok testApp.exe] 0] cd .. lappend result [lindex [auto_execok testApp.exe] 0] } -result [list .\\testApp.exe {}] test auto_execok-unix-6.1 { auto_execok - verify paths are not cached. Bugs 1244733 and 4dc35e0c0c } -constraints unix -setup { set dir [pushContext auto_execok-win-6.1 PATH] set fn [file join $dir testApp.sh] writeFile $fn "" file attributes $fn -permissions u+x unset -nocomplain result append ::env(PATH) ":." } -cleanup { rmdirs $dir popContext unset -nocomplain fn } -body { lappend result [lindex [auto_execok testApp.sh] 0] cd .. lappend result [lindex [auto_execok testApp.sh] 0] } -result [list ./testApp.sh {}] test auto_execok-win-7.1 { auto_execok - verify execution of PATHEXT extension in current directory } -constraints haveWScript -setup { set dir [pushContext auto_execok-win-7.1] writeFile [file join $dir testApp.vbs] "" unset -nocomplain result } -cleanup { rmdirs $dir popContext } -body { set cmd [auto_execok testApp] # The exec passes an empty file to wscript so no output expected # Primarily ensures wscript is found and run list [string tolower $cmd] [exec {*}$cmd] } -result [list \ [list \ [string tolower $windir\\system32\\wscript.exe] \ .\\testapp.vbs] \ {}] test auto_execok-win-7.2 { auto_execok - verify execution of PATHEXT extension in PATH } -constraints haveWScript -setup { set dir [makeDirectory auto_execok-win-7.2] writeFile [file join $dir testApp.vbs] "" pushEnv PATH append ::env(PATH) ";[file nativename $dir]" unset -nocomplain result } -cleanup { rmdirs $dir popEnv } -body { set cmd [auto_execok testApp] # The exec passes an empty file to wscript so no output expected # Primarily ensures wscript is found and run list [string tolower $cmd] [exec {*}$cmd] } -result [list \ [list \ [string tolower $windir\\system32\\wscript.exe] \ [string tolower \ [nativeTestPath auto_execok-win-7.2 testApp.vbs]]] \ {}] test auto_execok-win-8.1 { auto_execok - verify execution of App Paths } -constraints win -setup { set dir [makeDirectory auto_execok-win-8.1] set fn [file join $dir testApp.tcl] writeFile $fn { puts [info nameofexecutable] puts [pwd] } set key [string cat \ {HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths} \ "\\" [file tail [info nameofexecutable]]] tcl::registry set $key "" [file nativename [info nameofexecutable]] } -cleanup { tcl::registry delete $key rmdirs $dir unset -nocomplain fn } -body { set cmd [auto_execok [file tail [info nameofexecutable]]] list [string tolower $cmd] [split [exec {*}$cmd $fn] \n] } -result [list \ [list [string tolower [file nativename [info nameofexecutable]]] ] \ [list [info nameofexecutable] [pwd]]] } namespace delete tcl::test::exec # ---------------------------------------------------------------------- # cleanup foreach file {gorp.file gorp.file2 echo echo2 echobin cat wc sh sh2 sleep exit err} { removeFile $file } unset -nocomplain path tmp wingetPath savedPath savedCwd newCwd ::tcltest::cleanupTests return # Local Variables: # mode: tcl # End: