home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / file.test < prev    next >
Encoding:
Text File  |  1994-12-18  |  11.6 KB  |  314 lines

  1. # Commands covered:  file
  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. # @(#) file.test 1.25 94/12/17 16:19:56
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. # rootname and ext
  18.  
  19. test file-1.1 {rootname and extension options} {file ext abc.def} .def
  20. test file-1.2 {rootname and extension options} {file ro abc.def} abc
  21. test file-1.3 {rootname and extension options} {file extension a/b/c.d} .d
  22. test file-1.4 {rootname and extension options} {file rootname a/b/c.d} a/b/c
  23. test file-1.5 {rootname and extension options} {file extension a/b.c/d} {}
  24. test file-1.6 {rootname and extension options} {file rootname a/b.c/d} a/b.c/d
  25. set num 7
  26. foreach outer { {} a .a a. a.a } {
  27.   foreach inner { {} a .a a. a.a } {
  28.     set thing [format %s/%s $outer $inner]
  29.     test file-1.$num {rootname and extension options} {
  30.     format %s%s [file rootname $thing] [file ext $thing]
  31.     } $thing
  32.     set num [expr $num+1]
  33.   }
  34. }
  35.  
  36. # dirname and tail
  37.  
  38. test file-2.1 {dirname and tail options} {file dirname .def} .
  39. test file-2.2 {dirname and tail options} {file tail abc.def} abc.def
  40. test file-2.3 {dirname and tail options} {file d a/b/c.d} a/b
  41. test file-2.4 {dirname and tail options} {file ta a/b/c.d} c.d
  42. test file-2.5 {dirname and tail options} {file dirname a/b.c/d} a/b.c
  43. test file-2.6 {dirname and tail options} {file tail a/b.c/d} d
  44. set num 7
  45. foreach outer { a .a a. a.a } {
  46.   foreach inner { {} a .a a. a.a } {
  47.     set thing [format %s/%s $outer $inner]
  48.     test file-2.$num {dirname and tail options} {
  49.     format %s/%s [file dirname $thing] [file tail $thing]
  50.     } $thing
  51.     set num [expr $num+1]
  52.   }
  53. }
  54.  
  55. # exists
  56.  
  57. catch {exec chmod 777 dir.file}
  58. catch {exec rm -f dir.file/gorp.file}
  59. catch {exec rm -f gorp.file}
  60. catch {exec rmdir dir.file}
  61. catch {exec rm -f link.file}
  62. test file-3.1 {exists option} {file exists gorp.file} 0
  63. test file-3.2 {exists option} {file exists dir.file/gorp.file} 0
  64. exec cat > gorp.file << abcde
  65. exec mkdir dir.file
  66. exec cat > dir.file/gorp.file << 12345
  67. test file-3.3 {exists option} {file exists gorp.file} 1
  68. test file-3.4 {exists option} {file exi dir.file/gorp.file} 1
  69.  
  70. # The test below has to be done in /tmp rather than the current
  71. # directory in order to guarantee (?) a local file system:  some
  72. # NFS file systems won't do the stuff below correctly.
  73.  
  74. catch {exec rm /tmp/tcl.foo.dir/file}
  75. catch {exec rmdir /tmp/tcl.foo.dir}
  76. exec mkdir /tmp/tcl.foo.dir
  77. exec cat > /tmp/tcl.foo.dir/file << 12345
  78. exec chmod 000 /tmp/tcl.foo.dir
  79. if {$user != "root"} {
  80.     test file-3.5 {exists option} {file exists /tmp/tcl.foo.dir/file} 0
  81. }
  82. exec chmod 775 /tmp/tcl.foo.dir
  83. exec rm /tmp/tcl.foo.dir/file
  84. exec rmdir /tmp/tcl.foo.dir
  85.  
  86. # executable
  87.  
  88. exec chmod 000 dir.file
  89. if {$user != "root"} {
  90.     test file-4.1 {executable option} {file executable gorp.file} 0
  91. }
  92. exec chmod 775 gorp.file
  93. test file-4.2 {executable option} {file exe gorp.file} 1
  94.  
  95. # isdirectory
  96.  
  97. test file-5.1 {isdirectory option} {file isdirectory gorp.file} 0
  98. test file-5.2 {isdirectory option} {file isd dir.file} 1
  99.  
  100. # isfile
  101.  
  102. test file-6.1 {isfile option} {file isfile gorp.file} 1
  103. test file-6.2 {isfile option} {file isfile dir.file} 0
  104.  
  105. # isowned
  106.  
  107. test file-7.1 {owned option} {file owned gorp.file} 1
  108. if {$user != "root"} {
  109.     test file-7.2 {owned option} {file owned /} 0
  110. }
  111.  
  112. # readable
  113.  
  114. exec chmod 444 gorp.file
  115. test file-8.1 {readable option} {file readable gorp.file} 1
  116. exec chmod 333 gorp.file
  117. if {$user != "root"} {
  118.     test file-8.2 {readable option} {file reada gorp.file} 0
  119. }
  120.  
  121. # writable
  122.  
  123. exec chmod 555 gorp.file
  124. if {$user != "root"} {
  125.     test file-9.1 {writable option} {file writable gorp.file} 0
  126. }
  127. exec chmod 222 gorp.file
  128. test file-9.2 {writable option} {file w gorp.file} 1
  129.  
  130. # stat
  131.  
  132. exec cat > gorp.file << "Test string"
  133. exec chmod 765 gorp.file
  134. test file-10.1 {stat option} {
  135.     catch {unset stat}
  136.     file stat gorp.file stat
  137.     lsort [array names stat]
  138. } {atime ctime dev gid ino mode mtime nlink size type uid}
  139. test file-10.2 {stat option} {
  140.     catch {unset stat}
  141.     file stat gorp.file stat
  142.     list $stat(nlink) $stat(size) [expr $stat(mode)&0777] $stat(type)
  143. } {1 11 501 file}
  144. test file-10.3 {stat option} {
  145.     string tolower [list [catch {file stat _bogus_ stat} msg] \
  146.         $msg $errorCode]
  147. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  148. test file-10.4 {stat option} {
  149.     list [catch {file stat _bogus_} msg] $msg $errorCode
  150. } {1 {wrong # args: should be "file stat name varName"} NONE}
  151. test file-10.5 {stat option} {
  152.     list [catch {file stat _bogus_ a b} msg] $msg $errorCode
  153. } {1 {wrong # args: should be "file stat name varName"} NONE}
  154. test file-10.6 {stat option} {
  155.     catch {unset x}
  156.     set x 44
  157.     list [catch {file stat gorp.file x} msg] $msg $errorCode
  158. } {1 {can't set "x(dev)": variable isn't array} NONE}
  159. catch {unset stat}
  160.  
  161. # mtime, and size (I've given up trying to find a test for "atime":  there
  162. # seem to be too many quirks in the way file systems handle this to come
  163. # up with a reproducible test).
  164.  
  165. test file-11.1 {mtime and atime and size options} {
  166.     catch {unset stat}
  167.     file stat gorp.file stat
  168.     list [expr {[file mtime gorp.file] == $stat(mtime)}] \
  169.         [expr {[file atime gorp.file] == $stat(atime)}] \
  170.         [file size gorp.file]
  171. } {1 1 11}
  172. test file-11.2 {mtime option} {
  173.     set old [file mtime gorp.file]
  174.     exec sleep 2
  175.     set f [open gorp.file w]
  176.     puts $f "More text"
  177.     close $f
  178.     set new [file mtime gorp.file]
  179.     expr {($new > $old) && ($new <= ($old+5))}
  180. } {1}
  181. test file-11.3 {size option} {
  182.     set oldsize [file size gorp.file]
  183.     set f [open gorp.file a]
  184.     puts $f "More text"
  185.     close $f
  186.     expr {[file size gorp.file] - $oldsize}
  187. } {10}
  188. test file-11.4 {errors in atime option} {
  189.     list [catch {file atime _bogus_ x} msg] $msg $errorCode
  190. } {1 {wrong # args: should be "file atime name"} NONE}
  191. test file-11.5 {errors in atime option} {
  192.     string tolower [list [catch {file atime _bogus_} msg] \
  193.         $msg $errorCode]
  194. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  195. test file-11.6 {errors in mtime option} {
  196.     list [catch {file mtime _bogus_ x} msg] $msg $errorCode
  197. } {1 {wrong # args: should be "file mtime name"} NONE}
  198. test file-11.7 {errors in mtime option} {
  199.     string tolower [list [catch {file mtime _bogus_} msg] $msg \
  200.         $errorCode]
  201. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  202. test file-11.8 {errors in size option} {
  203.     list [catch {file size _bogus_ x} msg] $msg $errorCode
  204. } {1 {wrong # args: should be "file size name"} NONE}
  205. test file-11.9 {errors in size option} {
  206.     string tolower [list [catch {file size _bogus_} msg] $msg \
  207.         $errorCode]
  208. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  209.  
  210. # type
  211.  
  212. test file-12.1 {type option} {
  213.     file type dir.file
  214. } directory
  215. test file-12.2 {type option} {
  216.     file type gorp.file
  217. } file
  218. if $doNonPortableTests {
  219.     exec ln -s a/b/c link.file
  220.     test file-12.3 {type option} {
  221.     file type link.file
  222.     } link
  223.     exec rm link.file
  224. }
  225. test file-12.4 {errors in type option} {
  226.     list [catch {file type a b} msg] $msg $errorCode
  227. } {1 {wrong # args: should be "file type name"} NONE}
  228. test file-12.5 {errors in type option} {
  229.     string tolower [list [catch {file type _bogus_} msg] $msg $errorCode]
  230. } {1 {couldn't stat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  231.  
  232. # lstat and readlink:  don't run these tests everywhere, since not all
  233. # sites will have symbolic links
  234.  
  235. if $doNonPortableTests {
  236.     exec ln -s gorp.file link.file
  237.     test file-13.1 {lstat option} {
  238.     catch {unset stat}
  239.     file lstat link.file stat
  240.     lsort [array names stat]
  241.     } {atime ctime dev gid ino mode mtime nlink size type uid}
  242.     test file-13.1 {lstat option} {
  243.     catch {unset stat}
  244.     file lstat link.file stat
  245.     list $stat(nlink) [expr $stat(mode)&0777] $stat(type)
  246.     } {1 511 link}
  247.     test file-13.3 {errors in lstat option} {
  248.     string tolower [list [catch {file lstat _bogus_ stat} msg] \
  249.         $msg $errorCode]
  250.     } {1 {couldn't lstat "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  251.     test file-13.4 {errors in lstat option} {
  252.     list [catch {file lstat _bogus_} msg] $msg $errorCode
  253.     } {1 {wrong # args: should be "file lstat name varName"} NONE}
  254.     test file-13.5 {errors in lstat option} {
  255.     list [catch {file lstat _bogus_ a b} msg] $msg $errorCode
  256.     } {1 {wrong # args: should be "file lstat name varName"} NONE}
  257.     test file-13.6 {errors in lstat option} {
  258.     catch {unset x}
  259.     set x 44
  260.     list [catch {file lstat gorp.file x} msg] $msg $errorCode
  261.     } {1 {can't set "x(dev)": variable isn't array} NONE}
  262.     catch {unset stat}
  263.  
  264.     test file-14.1 {readlink option} {
  265.     file readlink link.file
  266.     } gorp.file
  267.     test file-14.2 {errors in readlink option} {
  268.     list [catch {file readlink a b} msg] $msg $errorCode
  269.     } {1 {wrong # args: should be "file readlink name"} NONE}
  270.     test file-14.3 {errors in readlink option} {
  271.     list [catch {file readlink _bogus_} msg] [string tolower $msg] \
  272.         [string tolower $errorCode]
  273.     } {1 {couldn't readlink "_bogus_": no such file or directory} {posix enoent {no such file or directory}}}
  274.  
  275.     exec rm link.file
  276. }
  277.  
  278. # Error conditions
  279.  
  280. test file-15.1 {error conditions} {
  281.     list [catch file msg] $msg
  282. } {1 {wrong # args: should be "file option name ?arg ...?"}}
  283. test file-15.2 {error conditions} {
  284.     list [catch {file x} msg] $msg
  285. } {1 {wrong # args: should be "file option name ?arg ...?"}}
  286. test file-15.3 {error conditions} {
  287.     list [catch {file exists x too} msg] $msg
  288. } {1 {wrong # args: should be "file exists name"}}
  289. test file-15.4 {error conditions} {
  290.     list [catch {file gorp x} msg] $msg
  291. } {1 {bad option "gorp": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  292. test file-15.5 {error conditions} {
  293.     list [catch {file ex x} msg] $msg
  294. } {1 {bad option "ex": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  295. test file-15.6 {error conditions} {
  296.     list [catch {file is x} msg] $msg
  297. } {1 {bad option "is": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  298. test file-15.7 {error conditions} {
  299.     list [catch {file read x} msg] $msg
  300. } {1 {bad option "read": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  301. test file-15.8 {error conditions} {
  302.     list [catch {file s x} msg] $msg
  303. } {1 {bad option "s": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  304. test file-15.9 {error conditions} {
  305.     list [catch {file t x} msg] $msg
  306. } {1 {bad option "t": should be atime, dirname, executable, exists, extension, isdirectory, isfile, lstat, mtime, owned, readable, readlink, root, size, stat, tail, type, or writable}}
  307. test file-15.10 {error conditions} {
  308.     list [catch {file rootname ~woohgy} msg] $msg
  309. } {1 {user "woohgy" doesn't exist}}
  310.  
  311. exec chmod 777 dir.file
  312. exec rm dir.file/gorp.file gorp.file
  313. exec rmdir dir.file
  314.