home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / exec.test < prev    next >
Encoding:
Text File  |  1995-06-23  |  14.3 KB  |  437 lines

  1. # Commands covered:  exec
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1994 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) exec.test 1.37 95/06/22 16:22:51
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. # Basic operations.
  18.  
  19. test exec-1.1 {basic exec operation} {
  20.     exec echo a b c
  21. } "a b c"
  22. test exec-1.2 {pipelining} {
  23.     exec echo a b c d | cat | cat
  24. } "a b c d"
  25. test exec-1.3 {pipelining} {
  26.     set a [exec echo a b c d | cat | wc]
  27.     list [scan $a "%d %d %d" b c d] $b $c $d
  28. } {3 1 4 8}
  29.  
  30. # I/O redirection: input from Tcl command.
  31.  
  32. test exec-2.1 {redirecting input from immediate source} {
  33.     exec cat << "Sample text"
  34. } {Sample text}
  35. test exec-2.2 {redirecting input from immediate source} {
  36.     exec << "Sample text" cat | cat
  37. } {Sample text}
  38. test exec-2.3 {redirecting input from immediate source} {
  39.     exec cat << "Sample text" | cat
  40. } {Sample text}
  41. test exec-2.4 {redirecting input from immediate source} {
  42.     exec  cat | cat << "Sample text"
  43. } {Sample text}
  44. test exec-2.5 {redirecting input from immediate source} {
  45.     exec cat "<<Joined to arrows"
  46. } {Joined to arrows}
  47.  
  48. # I/O redirection: output to file.
  49.  
  50. catch {exec rm -f gorp.file}
  51. test exec-3.1 {redirecting output to file} {
  52.     exec echo "Some simple words" > gorp.file
  53.     exec cat gorp.file
  54. } "Some simple words"
  55. test exec-3.2 {redirecting output to file} {
  56.     exec echo "More simple words" | >gorp.file cat | cat
  57.     exec cat gorp.file
  58. } "More simple words"
  59. test exec-3.3 {redirecting output to file} {
  60.     exec > gorp.file echo "Different simple words" | cat | cat
  61.     exec cat gorp.file
  62. } "Different simple words"
  63. test exec-3.4 {redirecting output to file} {
  64.     exec echo "Some simple words" >gorp.file
  65.     exec cat gorp.file
  66. } "Some simple words"
  67. test exec-3.5 {redirecting output to file} {
  68.     exec echo "First line" >gorp.file
  69.     exec echo "Second line" >> gorp.file
  70.     exec cat gorp.file
  71. } "First line\nSecond line"
  72. test exec-3.6 {redirecting output to file} {
  73.     exec echo "First line" >gorp.file
  74.     exec echo "Second line" >>gorp.file
  75.     exec cat gorp.file
  76. } "First line\nSecond line"
  77. test exec-3.7 {redirecting output to file} {
  78.     set f [open gorp.file w]
  79.     puts $f "Line 1"
  80.     flush $f
  81.     exec echo "More text" >@ $f
  82.     exec echo >@$f "Even more"
  83.     puts $f "Line 3"
  84.     close $f
  85.     exec cat gorp.file
  86. } "Line 1\nMore text\nEven more\nLine 3"
  87.  
  88. # I/O redirection: output and stderr to file.
  89.  
  90. catch {exec rm -f gorp.file}
  91. test exec-4.1 {redirecting output and stderr to file} {
  92.     exec echo "test output" >& gorp.file
  93.     exec cat gorp.file
  94. } "test output"
  95. test exec-4.2 {redirecting output and stderr to file} {
  96.     list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
  97.         [exec cat gorp.file]
  98. } {{} {foo bar}}
  99. test exec-4.3 {redirecting output and stderr to file} {
  100.     exec echo "first line" > gorp.file
  101.     list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
  102.         [exec cat gorp.file]
  103. } "{} {first line\nfoo bar}"
  104. test exec-4.4 {redirecting output and stderr to file} {
  105.     set f [open gorp.file w]
  106.     puts $f "Line 1"
  107.     flush $f
  108.     exec echo "More text" >&@ $f
  109.     exec echo >&@$f "Even more"
  110.     puts $f "Line 3"
  111.     close $f
  112.     exec cat gorp.file
  113. } "Line 1\nMore text\nEven more\nLine 3"
  114. test exec-4.5 {redirecting output and stderr to file} {
  115.     set f [open gorp.file w]
  116.     puts $f "Line 1"
  117.     flush $f
  118.     exec >&@ $f sh -c "echo foo bar 1>&2"
  119.     exec >&@$f sh -c "echo xyzzy 1>&2"
  120.     puts $f "Line 3"
  121.     close $f
  122.     exec cat gorp.file
  123. } "Line 1\nfoo bar\nxyzzy\nLine 3"
  124.  
  125. # I/O redirection: input from file.
  126.  
  127. exec echo "Just a few thoughts" > gorp.file
  128. test exec-5.1 {redirecting input from file} {
  129.     exec cat < gorp.file
  130. } {Just a few thoughts}
  131. test exec-5.2 {redirecting input from file} {
  132.     exec cat | cat < gorp.file
  133. } {Just a few thoughts}
  134. test exec-5.3 {redirecting input from file} {
  135.     exec cat < gorp.file | cat
  136. } {Just a few thoughts}
  137. test exec-5.4 {redirecting input from file} {
  138.     exec < gorp.file cat | cat
  139. } {Just a few thoughts}
  140. test exec-5.5 {redirecting input from file} {
  141.     exec cat <gorp.file
  142. } {Just a few thoughts}
  143. test exec-5.6 {redirecting input from file} {
  144.     set f [open gorp.file r]
  145.     set result [exec cat <@ $f]
  146.     close $f
  147.     set result
  148. } {Just a few thoughts}
  149. test exec-5.7 {redirecting input from file} {
  150.     set f [open gorp.file r]
  151.     set result [exec <@$f cat]
  152.     close $f
  153.     set result
  154. } {Just a few thoughts}
  155.  
  156. # I/O redirection: standard error through a pipeline.
  157.  
  158. test exec-6.1 {redirecting stderr through a pipeline} {
  159.     exec sh -c "echo foo bar" |& cat
  160. } "foo bar"
  161. test exec-6.2 {redirecting stderr through a pipeline} {
  162.     exec sh -c "echo foo bar 1>&2" |& cat
  163. } "foo bar"
  164. test exec-6.3 {redirecting stderr through a pipeline} {
  165.     exec sh -c "echo foo bar 1>&2" |& sh -c "echo second msg 1>& 2; cat" |& cat
  166. } "second msg\nfoo bar"
  167.  
  168. # I/O redirection: combinations.
  169.  
  170. catch {exec rm -f gorp.file2}
  171. test exec-7.1 {multiple I/O redirections} {
  172.     exec << "command input" > gorp.file2 cat < gorp.file
  173.     exec cat gorp.file2
  174. } {Just a few thoughts}
  175. test exec-7.2 {multiple I/O redirections} {
  176.     exec < gorp.file << "command input" cat
  177. } {command input}
  178.  
  179. # Long input to command and output from command.
  180.  
  181. set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n"
  182. set a [concat $a $a $a $a]
  183. set a [concat $a $a $a $a]
  184. set a [concat $a $a $a $a]
  185. set a [concat $a $a $a $a]
  186. test exec-8.1 {long input and output} {
  187.     exec cat << $a
  188. } $a
  189.  
  190. # Commands that return errors.
  191.  
  192. test exec-9.1 {commands returning errors} {
  193.     set x [catch {exec gorp456} msg]
  194.     list $x [string tolower $msg] [string tolower $errorCode]
  195. } {1 {couldn't execute "gorp456": no such file or directory} {posix enoent {no such file or directory}}}
  196. test exec-9.2 {commands returning errors} {
  197.     string tolower [list [catch {exec echo foo | foo123} msg] $msg $errorCode]
  198. } {1 {couldn't execute "foo123": no such file or directory} {posix enoent {no such file or directory}}}
  199. test exec-9.3 {commands returning errors} {
  200.     list [catch {exec sleep 1 | sh -c "exit 43" | sleep 1} msg] $msg
  201. } {1 {child process exited abnormally}}
  202. test exec-9.4 {commands returning errors} {
  203.     list [catch {exec gorp456 | echo a b c} msg] [string tolower $msg]
  204. } {1 {couldn't execute "gorp456": no such file or directory}}
  205. test exec-9.5 {commands returning errors} {
  206.     list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
  207. } {1 {error msg}}
  208. test exec-9.6 {commands returning errors} {
  209.     list [catch {exec sh -c "echo error msg 1>&2" | sh -c "echo error msg 1>&2"} msg] $msg
  210. } {1 {error msg
  211. error msg}}
  212.  
  213. # Errors in executing the Tcl command, as opposed to errors in the
  214. # processes that are invoked.
  215.  
  216. test exec-10.1 {errors in exec invocation} {
  217.     list [catch {exec} msg] $msg
  218. } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
  219. test exec-10.2 {errors in exec invocation} {
  220.     list [catch {exec | cat} msg] $msg
  221. } {1 {illegal use of | or |& in command}}
  222. test exec-10.3 {errors in exec invocation} {
  223.     list [catch {exec cat |} msg] $msg
  224. } {1 {illegal use of | or |& in command}}
  225. test exec-10.4 {errors in exec invocation} {
  226.     list [catch {exec cat | | cat} msg] $msg
  227. } {1 {illegal use of | or |& in command}}
  228. test exec-10.5 {errors in exec invocation} {
  229.     list [catch {exec cat | |& cat} msg] $msg
  230. } {1 {illegal use of | or |& in command}}
  231. test exec-10.6 {errors in exec invocation} {
  232.     list [catch {exec cat |&} msg] $msg
  233. } {1 {illegal use of | or |& in command}}
  234. test exec-10.7 {errors in exec invocation} {
  235.     list [catch {exec cat <} msg] $msg
  236. } {1 {can't specify "<" as last word in command}}
  237. test exec-10.8 {errors in exec invocation} {
  238.     list [catch {exec cat >} msg] $msg
  239. } {1 {can't specify ">" as last word in command}}
  240. test exec-10.9 {errors in exec invocation} {
  241.     list [catch {exec cat <<} msg] $msg
  242. } {1 {can't specify "<<" as last word in command}}
  243. test exec-10.10 {errors in exec invocation} {
  244.     list [catch {exec cat >>} msg] $msg
  245. } {1 {can't specify ">>" as last word in command}}
  246. test exec-10.11 {errors in exec invocation} {
  247.     list [catch {exec cat >&} msg] $msg
  248. } {1 {can't specify ">&" as last word in command}}
  249. test exec-10.12 {errors in exec invocation} {
  250.     list [catch {exec cat >>&} msg] $msg
  251. } {1 {can't specify ">>&" as last word in command}}
  252. test exec-10.13 {errors in exec invocation} {
  253.     list [catch {exec cat >@} msg] $msg
  254. } {1 {can't specify ">@" as last word in command}}
  255. test exec-10.14 {errors in exec invocation} {
  256.     list [catch {exec cat <@} msg] $msg
  257. } {1 {can't specify "<@" as last word in command}}
  258. test exec-10.15 {errors in exec invocation} {
  259.     list [catch {exec cat < a/b/c} msg] [string tolower $msg]
  260. } {1 {couldn't read file "a/b/c": no such file or directory}}
  261. test exec-10.16 {errors in exec invocation} {
  262.     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
  263. } {1 {couldn't write file "a/b/c": no such file or directory}}
  264. test exec-10.17 {errors in exec invocation} {
  265.     list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
  266. } {1 {couldn't write file "a/b/c": no such file or directory}}
  267. set f [open gorp.file w]
  268. test exec-10.18 {errors in exec invocation} {
  269.     list [catch {exec cat <@ $f} msg] $msg
  270. } "1 {\"$f\" wasn't opened for reading}"
  271. close $f
  272. set f [open gorp.file r]
  273. test exec-10.19 {errors in exec invocation} {
  274.     list [catch {exec cat >@ $f} msg] $msg
  275. } "1 {\"$f\" wasn't opened for writing}"
  276. close $f
  277.  
  278. # Commands in background.
  279.  
  280. test exec-11.1 {commands in background} {
  281.     set x [lindex [time {exec sleep 2 &}] 0]
  282.     expr $x<1000000
  283. } 1
  284. test exec-11.2 {commands in background} {
  285.     list [catch {exec echo a &b} msg] $msg
  286. } {0 {a &b}}
  287. test exec-11.3 {commands in background} {
  288.     llength [exec sleep 1 &]
  289. } 1
  290. test exec-11.4 {commands in background} {
  291.     llength [exec sleep 1 | sleep 1 | sleep 1 &]
  292. } 3
  293.  
  294. # Make sure that background commands are properly reaped when
  295. # they eventually die.
  296.  
  297. exec sleep 3
  298. if $doNonPortableTests {
  299.     test exec-12.1 {reaping background processes} {
  300.     for {set i 0} {$i < 20} {incr i} {
  301.         exec echo foo > /dev/null &
  302.     }
  303.     exec sleep 1
  304.     catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
  305.     lindex $msg 0
  306.     } 0
  307.     test exec-12.2 {reaping background processes} {
  308.     exec sleep 2 | sleep 2 | sleep 2 &
  309.     catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
  310.     set x [lindex $msg 0]
  311.     exec sleep 3
  312.     catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
  313.     list $x [lindex $msg 0]
  314.     } {3 0}
  315.     test exec-12.3 {reaping background processes} {
  316.     exec sleep 1000 &
  317.     exec sleep 1000 &
  318.     set x [exec ps | fgrep "sleep" | fgrep -v fgrep]
  319.     set pids {}
  320.     foreach i [split $x \n] {
  321.         lappend pids [lindex $i 0]
  322.     }
  323.     foreach i $pids {
  324.         catch {exec kill -STOP $i}
  325.     }
  326.     catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
  327.     set x [lindex $msg 0]
  328.     
  329.     foreach i $pids {
  330.         catch {exec kill -KILL $i}
  331.     }
  332.     catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
  333.     list $x [lindex $msg 0]
  334.     } {2 0}
  335. }
  336.  
  337. # Make sure "errorCode" is set correctly.
  338.  
  339. test exec-13.1 {setting errorCode variable} {
  340.     list [catch {exec cat < a/b/c} msg] [string tolower $errorCode]
  341. } {1 {posix enoent {no such file or directory}}}
  342. test exec-13.2 {setting errorCode variable} {
  343.     list [catch {exec cat > a/b/c} msg] [string tolower $errorCode]
  344. } {1 {posix enoent {no such file or directory}}}
  345. test exec-13.3 {setting errorCode variable} {
  346.     set x [catch {exec _weird_cmd_} msg]
  347.     list $x [string tolower $msg] [lindex $errorCode 0] \
  348.         [string tolower [lrange $errorCode 2 end]]
  349. } {1 {couldn't execute "_weird_cmd_": no such file or directory} POSIX {{no such file or directory}}}
  350.  
  351. # Switches before the first argument
  352.  
  353. test exec-14.1 {-keepnewline switch} {
  354.     exec -keepnewline echo foo
  355. } "foo\n"
  356. test exec-14.2 {-keepnewline switch} {
  357.     list [catch {exec -keepnewline} msg] $msg
  358. } {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
  359. test exec-14.3 {unknown switch} {
  360.     list [catch {exec -gorp} msg] $msg
  361. } {1 {bad switch "-gorp": must be -keepnewline or --}}
  362. test exec-14.4 {-- switch} {
  363.     list [catch {exec -- -gorp} msg] [string tolower $msg]
  364. } {1 {couldn't execute "-gorp": no such file or directory}}
  365.  
  366. # Redirecting standard error separately from standard output
  367.  
  368. test exec-15.1 {standard error redirection} {
  369.     exec echo "First line" > gorp.file
  370.     list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
  371.         [exec cat gorp.file]
  372. } {{} {foo bar}}
  373. test exec-15.2 {standard error redirection} {
  374.     list [exec sh -c "echo foo bar 1>&2" | echo biz baz >gorp.file \
  375.         2> gorp.file2] [exec cat gorp.file] \
  376.         [exec cat gorp.file2]
  377. } {{} {biz baz} {foo bar}}
  378. test exec-15.3 {standard error redirection} {
  379.     list [exec sh -c "echo foo bar 1>&2" | echo biz baz 2>gorp.file \
  380.         > gorp.file2] [exec cat gorp.file] \
  381.         [exec cat gorp.file2]
  382. } {{} {foo bar} {biz baz}}
  383. test exec-15.4 {standard error redirection} {
  384.     set f [open gorp.file w]
  385.     puts $f "Line 1"
  386.     flush $f
  387.     exec sh -c "echo foo bar 1>&2" 2>@ $f
  388.     puts $f "Line 3"
  389.     close $f
  390.     exec cat gorp.file
  391. } {Line 1
  392. foo bar
  393. Line 3}
  394. test exec-15.5 {standard error redirection} {
  395.     exec echo "First line" > gorp.file
  396.     exec sh -c "echo foo bar 1>&2" 2>> gorp.file
  397.     exec cat gorp.file
  398. } {First line
  399. foo bar}
  400. test exec-15.6 {standard error redirection} {
  401.     exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
  402.         >& gorp.file 2> gorp.file2 | echo biz baz
  403.     list [exec cat gorp.file] [exec cat gorp.file2]
  404. } {{biz baz} {foo bar}}
  405.  
  406. if $doNonPortableTests {
  407.     test exec-16.1 {restore signal settings before exec} {
  408.     set f [open {|cat exec.test} r]
  409.     list [catch {close $f} msg] [string tolower $msg]
  410.     } {1 {child killed: write on pipe with no readers}}
  411. }
  412.  
  413. test exec-17.1 {flush output before exec} {
  414.     set f [open gorp.file w]
  415.     puts $f "First line"
  416.     exec echo "Second line" >@ $f
  417.     puts $f "Third line"
  418.     close $f
  419.     exec cat gorp.file
  420. } {First line
  421. Second line
  422. Third line}
  423. test exec-17.2 {flush output before exec} {
  424.     set f [open gorp.file w]
  425.     puts $f "First line"
  426.     exec sh -c "echo Second line 1>&2" >&@ $f > gorp.file2
  427.     puts $f "Third line"
  428.     close $f
  429.     exec cat gorp.file
  430. } {First line
  431. Second line
  432. Third line}
  433.  
  434. catch {exec rm -f gorp.file}
  435. catch {exec rm -f gorp.file2}
  436. return {}
  437.