📄 exec.test
字号:
# 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 (c) 1991-1994 The Regents of the University of California.# Copyright (c) 1994-1997 Sun Microsystems, Inc.# Copyright (c) 1998-1999 by Scriptics Corporation.## See the file "license.terms" for information on usage and redistribution# of this file, and for a DISCLAIMER OF ALL WARRANTIES.## RCS: @(#) $Id: exec.test,v 1.16 2003/02/04 18:23:35 vincentdarley Exp $package require tcltest 2namespace import -force ::tcltest::*# All tests require the "exec" command.# Skip them if exec is not defined.testConstraint exec [llength [info commands exec]]catch {unset path}set path(echo) [makeFile { puts -nonewline [lindex $argv 0] foreach str [lrange $argv 1 end] { puts -nonewline " $str" } puts {} exit} echo]set path(cat) [makeFile { if {$argv == {}} { set argv - } foreach name $argv { if {$name == "-"} { set f stdin } elseif {[catch {open $name r} f] != 0} { puts stderr $f continue } while {[eof $f] == 0} { puts -nonewline [read $f] } if {$f != "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] != "-c"} { error "sh: unexpected arguments $argv" } set cmd [lindex $argv 1] lappend cmd ";" set newcmd {} foreach arg $cmd { if {$arg == ";"} { eval exec >@stdout 2>@stderr [list [info nameofexecutable]] $newcmd set newcmd {} continue } if {$arg == "1>&2"} { set arg >@stderr } lappend newcmd $arg } exit} sh]set path(sleep) [makeFile { after [expr $argv*1000] exit} sleep]set path(exit) [makeFile { exit $argv} exit]# 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} $argset arg {}# 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} {exec} { # If this fails, it may give back: # "\uC3\uA9\uC3\uA0\uC3\uBC\uC3\uB1" # If it does, this means that the UTF -> external conversion did not # occur before writing out the temp file. exec [interpreter] $path(cat) << "\uE9\uE0\uFC\uF1"} "\uE9\uE0\uFC\uF1"# I/O redirection: output to file.set path(gorp.file) [makeFile {} gorp.file]removeFile gorp.filetest 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"# I/O redirection: output and stderr to file.removeFile gorp.filetest 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 { [set ::tcltest::testConstraints(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} {exec} { set f [open $path(gorp.file) r] set result [exec [interpreter] $path(cat) <@ $f] close $f set result} {Just a few thoughts}test exec-5.7 {redirecting input from file} {exec} { set f [open $path(gorp.file) r] set result [exec <@$f [interpreter] $path(cat)] close $f set result} {Just a few thoughts}# 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]removeFile gorp.file2test 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} { 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} { 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} {exec stdio} {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -