📄 exec.test
字号:
list [catch {exec [interpreter] $path(sleep) 1 | [interpreter] $path(exit) 43 | [interpreter] $path(sleep) 1} msg] $msg} {1 {child process exited abnormally}}test exec-9.4 {commands returning errors} {exec stdio} { list [catch {exec [interpreter] $path(exit) 43 | [interpreter] $path(echo) "foo bar"} msg] $msg} {1 {foo barchild process exited abnormally}}test exec-9.5 {commands returning errors} {exec stdio} { list [catch {exec gorp456 | [interpreter] echo a b c} msg] [string tolower $msg]} {1 {couldn't execute "gorp456": no such file or directory}}test exec-9.6 {commands returning errors} {exec} { list [catch {exec [interpreter] "$path(sh)" -c "\"$path(echo)\" error msg 1>&2"} msg] $msg} {1 {error msg}}test exec-9.7 {commands returning errors} {exec stdio} { list [catch {exec [interpreter] "$path(sh)" -c "\"$path(echo)\" error msg 1>&2" \ | [interpreter] "$path(sh)" -c "\"$path(echo)\" error msg 1>&2"} msg] $msg} {1 {error msgerror msg}}set path(err) [makeFile {} err]test exec-9.8 {commands returning errors} {exec} { set f [open $path(err) w] puts $f { puts stdout out puts stderr err } close $f list [catch {exec [interpreter] $path(err)} msg] $msg} {1 {outerr}}# Errors in executing the Tcl command, as opposed to errors in the# processes that are invoked.test exec-10.1 {errors in exec invocation} {exec} { list [catch {exec} msg] $msg} {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}test exec-10.2 {errors in exec invocation} {exec} { list [catch {exec | cat} msg] $msg} {1 {illegal use of | or |& in command}}test exec-10.3 {errors in exec invocation} {exec} { list [catch {exec cat |} msg] $msg} {1 {illegal use of | or |& in command}}test exec-10.4 {errors in exec invocation} {exec} { list [catch {exec cat | | cat} msg] $msg} {1 {illegal use of | or |& in command}}test exec-10.5 {errors in exec invocation} {exec} { list [catch {exec cat | |& cat} msg] $msg} {1 {illegal use of | or |& in command}}test exec-10.6 {errors in exec invocation} {exec} { list [catch {exec cat |&} msg] $msg} {1 {illegal use of | or |& in command}}test exec-10.7 {errors in exec invocation} {exec} { list [catch {exec cat <} msg] $msg} {1 {can't specify "<" as last word in command}}test exec-10.8 {errors in exec invocation} {exec} { list [catch {exec cat >} msg] $msg} {1 {can't specify ">" as last word in command}}test exec-10.9 {errors in exec invocation} {exec} { list [catch {exec cat <<} msg] $msg} {1 {can't specify "<<" as last word in command}}test exec-10.10 {errors in exec invocation} {exec} { list [catch {exec cat >>} msg] $msg} {1 {can't specify ">>" as last word in command}}test exec-10.11 {errors in exec invocation} {exec} { list [catch {exec cat >&} msg] $msg} {1 {can't specify ">&" as last word in command}}test exec-10.12 {errors in exec invocation} {exec} { list [catch {exec cat >>&} msg] $msg} {1 {can't specify ">>&" as last word in command}}test exec-10.13 {errors in exec invocation} {exec} { list [catch {exec cat >@} msg] $msg} {1 {can't specify ">@" as last word in command}}test exec-10.14 {errors in exec invocation} {exec} { list [catch {exec cat <@} msg] $msg} {1 {can't specify "<@" as last word in command}}test exec-10.15 {errors in exec invocation} {exec} { list [catch {exec cat < a/b/c} msg] [string tolower $msg]} {1 {couldn't read file "a/b/c": no such file or directory}}test exec-10.16 {errors in exec invocation} {exec} { list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]} {1 {couldn't write file "a/b/c": no such file or directory}}test exec-10.17 {errors in exec invocation} {exec} { list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]} {1 {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} {exec} { list [catch {exec cat <@ $f} msg] $msg} "1 {channel \"$f\" wasn't opened for reading}"close $fset f [open $path(gorp.file) r]test exec-10.19 {errors in exec invocation} {exec} { list [catch {exec cat >@ $f} msg] $msg} "1 {channel \"$f\" wasn't opened for writing}"close $ftest exec-10.20 {errors in exec invocation} {exec} { list [catch {exec ~non_existent_user/foo/bar} msg] $msg} {1 {user "non_existent_user" doesn't exist}}test exec-10.21 {errors in exec invocation} {exec} { list [catch {exec [interpreter] true | ~xyzzy_bad_user/x | false} msg] $msg} {1 {user "xyzzy_bad_user" doesn't exist}}# Commands in background.test exec-11.1 {commands in background} {exec} { set x [lindex [time {exec [interpreter] $path(sleep) 2 &}] 0] expr $x<1000000} 1test exec-11.2 {commands in background} {exec} { list [catch {exec [interpreter] $path(echo) a &b} msg] $msg} {0 {a &b}}test exec-11.3 {commands in background} {exec} { llength [exec [interpreter] $path(sleep) 1 &]} 1test exec-11.4 {commands in background} {exec stdio} { llength [exec [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 &]} 3test exec-11.5 {commands in background} {exec} { set f [open $path(gorp.file) w] puts $f [format { catch { exec [info nameofexecutable] {%s} foo & } } $path(echo)] close $f string compare "foo" [exec [interpreter] $path(gorp.file)]} 0# Make sure that background commands are properly reaped when# they eventually die.if { [set ::tcltest::testConstraints(exec)] } {exec [interpreter] $path(sleep) 3}test exec-12.1 {reaping background processes} \ {exec unixOnly nonPortable} { for {set i 0} {$i < 20} {incr i} { exec echo foo > /dev/null & } exec sleep 1 catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg lindex $msg 0} 0test exec-12.2 {reaping background processes} \ {exec unixOnly 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] exec sleep 3 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 unixOnly 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} { 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}}}# Switches before the first argumenttest exec-14.1 {-keepnewline switch} {exec} { exec -keepnewline [interpreter] $path(echo) foo} "foo\n"test exec-14.2 {-keepnewline switch} {exec} { list [catch {exec -keepnewline} msg] $msg} {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}test exec-14.3 {unknown switch} {exec} { list [catch {exec -gorp} msg] $msg} {1 {bad switch "-gorp": must be -keepnewline or --}}test exec-14.4 {-- switch} {exec} { list [catch {exec -- -gorp} msg] [string tolower $msg]} {1 {couldn't execute "-gorp": no such file or directory}}# Redirecting standard error separately from standard outputtest 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 exec [interpreter] "$path(cat)" "$path(gorp.file)"} {Line 1foo barLine 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)" exec [interpreter] "$path(cat)" "$path(gorp.file)"} {First linefoo 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 [exec [interpreter] "$path(cat)" "$path(gorp.file)"] [exec [interpreter] "$path(cat)" "$path(gorp.file2)"]} {{biz baz} {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 exec [interpreter] $path(cat) $path(gorp.file)} {First lineSecond lineThird 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 exec [interpreter] $path(cat) $path(gorp.file)} {First lineSecond lineThird line}set path(script) [makeFile {} script]test exec-17.1 { inheriting standard I/O } {exec} { set f [open $path(script) w] puts $f [format {close stdout set f [open {%s} w] catch {exec [info nameofexecutable] {%s} foobar &} exec [info nameofexecutable] {%s} 2 close $f } $path(gorp.file) $path(echo) $path(sleep)] close $f catch {exec [interpreter] $path(script)} result set f [open $path(gorp.file) r] lappend result [read $f] close $f set result} {{foobar}}test exec-18.1 { exec cat deals with weird file names} {exec tempNotWin} { # This is cross-platform, but the cat isn't predictably correct on # Windows. set f "foo\[\{blah" set path(fooblah) [makeFile {} $f] set fout [open $path(fooblah) w] puts $fout "contents" close $fout set res [list [catch {exec cat $path(fooblah)} msg] $msg] removeFile $f set res} {0 contents}# cleanupforeach file {script gorp.file gorp.file2 echo cat wc sh sleep exit err} { removeFile $file}::tcltest::cleanupTestsreturn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -