Debian Codegraph @

tests/cmdAH.test

raw
1 # The file tests the tclCmdAH.c file.
2 #
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
6 #
7 # Copyright (c) 1996-1998 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 Scriptics Corporation.
9 #
10 # See the file "license.terms" for information on usage and redistribution of
11 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
13 if {"::tcltest" ni [namespace children]} {
14 package require tcltest 2.1
15 namespace import -force ::tcltest::*
16 }
17
18 ::tcltest::loadTestedCommands
19 catch [list package require -exact Tcltest [info patchlevel]]
20
21 testConstraint testchmod [llength [info commands testchmod]]
22 testConstraint testsetplatform [llength [info commands testsetplatform]]
23 testConstraint testvolumetype [llength [info commands testvolumetype]]
24 testConstraint time64bit [expr {
25 ([llength [info command testsize]] ?
26 [testsize st_mtime] : $::tcl_platform(pointerSize)) >= 8
27 }]
28 testConstraint filetime64bit [expr {
29 [testConstraint time64bit] && (
30 ![testConstraint unix] || [apply {{} {
31 # check whether disk may have 2038 problem, see [fd91b0ca09cb171f]:
32 set fn [makeFile "" foo.text]
33 if {[catch {
34 exec sh -c "TZ=:UTC LC_TIME=en_US touch -ma -t '207006290000' '$fn' && TZ=:UTC LC_TIME=en_US ls -l '$fn'"
35 } res]} {
36 #puts "Check constraint failed:\t$res"
37 set res {}
38 }
39 removeFile $fn
40 regexp {\mJun\s+29\s+2070\M} $res
41 }}]
42 )
43 }]
44 testConstraint linkDirectory [expr {
45 ![testConstraint win] ||
46 ($::tcl_platform(osVersion) >= 5.0
47 && [lindex [file system [temporaryDirectory]] 1] eq "NTFS")
48 }]
49 testConstraint notWsl [expr {[llength [array names ::env *WSL*]] == 0}]
50
51 global env
52 set cmdAHwd [pwd]
53 catch {set platform [testgetplatform]}
54
55 proc waitForEvenSecondForFAT {} {
56 # Windows 9x uses filesystems (the FAT* family of FSes) without enough
57 # data in its timestamps for even per-second-accurate timings. :^(
58 # This procedure based on work by Helmut Giese
59 if {
60 [testConstraint win] &&
61 [lindex [file system [temporaryDirectory]] 1] ne "NTFS"
62 } then {
63 # Assume non-NTFS means FAT{12,16,32} and hence in need of special
64 # help...
65 set start [clock seconds]
66 while {1} {
67 set now [clock seconds]
68 if {$now!=$start && !($now & 1)} {
69 break
70 }
71 after 50
72 }
73 }
74 }
75
76 test cmdAH-0.1 {Tcl_BreakObjCmd, errors} -body {
77 break foo
78 } -returnCodes error -result {wrong # args: should be "break"}
79 test cmdAH-0.2 {Tcl_BreakObjCmd, success} {
80 list [catch {break} msg] $msg
81 } {3 {}}
82
83 # Tcl_CaseObjCmd is tested in case.test
84
85 test cmdAH-1.1 {Tcl_CatchObjCmd, errors} -returnCodes error -body {
86 catch
87 } -result {wrong # args: should be "catch script ?resultVarName? ?optionVarName?"}
88 test cmdAH-1.2 {Tcl_CatchObjCmd, errors} {
89 list [catch {catch foo bar baz} msg] $msg
90 } {0 1}
91 test cmdAH-1.3 {Tcl_CatchObjCmd, errors} -returnCodes error -body {
92 catch foo bar baz spaz
93 } -result {wrong # args: should be "catch script ?resultVarName? ?optionVarName?"}
94 test cmdAH-1.4 {Bug 3595576} {
95 catch {catch {} -> noSuchNs::var}
96 } 1
97 test cmdAH-1.5 {Bug 3595576} {
98 catch {catch error -> noSuchNs::var}
99 } 1
100
101 test cmdAH-2.1 {Tcl_CdObjCmd} -returnCodes error -body {
102 cd foo bar
103 } -result {wrong # args: should be "cd ?dirName?"}
104 set foodir [file join [temporaryDirectory] foo]
105 test cmdAH-2.2 {Tcl_CdObjCmd} -setup {
106 file delete -force $foodir
107 set oldpwd [pwd]
108 } -body {
109 file mkdir $foodir
110 cd $foodir
111 file tail [pwd]
112 } -cleanup {
113 cd $oldpwd
114 file delete $foodir
115 } -result foo
116 test cmdAH-2.3 {Tcl_CdObjCmd} -setup {
117 global env
118 set oldpwd [pwd]
119 set temp $env(HOME)
120 file delete -force $foodir
121 } -body {
122 set env(HOME) $oldpwd
123 file mkdir $foodir
124 cd $foodir
125 cd ~
126 string equal [pwd] $oldpwd
127 } -cleanup {
128 cd $oldpwd
129 file delete $foodir
130 set env(HOME) $temp
131 } -result 1
132 test cmdAH-2.4 {Tcl_CdObjCmd} -setup {
133 global env
134 set oldpwd [pwd]
135 set temp $env(HOME)
136 file delete -force $foodir
137 } -body {
138 set env(HOME) $oldpwd
139 file mkdir $foodir
140 cd $foodir
141 cd
142 string equal [pwd] $oldpwd
143 } -cleanup {
144 cd $oldpwd
145 file delete $foodir
146 set env(HOME) $temp
147 } -result 1
148 test cmdAH-2.5 {Tcl_CdObjCmd} -returnCodes error -body {
149 cd ~~
150 } -result {user "~" doesn't exist}
151 test cmdAH-2.6 {Tcl_CdObjCmd} -returnCodes error -body {
152 cd _foobar
153 } -result {couldn't change working directory to "_foobar": no such file or directory}
154 test cmdAH-2.6.1 {Tcl_CdObjCmd} -returnCodes error -body {
155 cd ""
156 } -result {couldn't change working directory to "": no such file or directory}
157 test cmdAH-2.6.2 {cd} -constraints {unix nonPortable} -setup {
158 set dir [pwd]
159 } -body {
160 cd /
161 pwd
162 } -cleanup {
163 cd $dir
164 } -result {/}
165 test cmdAH-2.6.3 {Tcl_CdObjCmd, bug #3118489} -setup {
166 set dir [pwd]
167 } -returnCodes error -body {
168 cd .\x00
169 } -cleanup {
170 cd $dir
171 } -match glob -result "couldn't change working directory to \".\x00\": *"
172 test cmdAH-2.7 {Tcl_ConcatObjCmd} {
173 concat
174 } {}
175 test cmdAH-2.8 {Tcl_ConcatObjCmd} {
176 concat a
177 } a
178 test cmdAH-2.9 {Tcl_ConcatObjCmd} {
179 concat a {b c}
180 } {a b c}
181
182 test cmdAH-3.1 {Tcl_ContinueObjCmd, errors} -returnCodes error -body {
183 continue foo
184 } -result {wrong # args: should be "continue"}
185 test cmdAH-3.2 {Tcl_ContinueObjCmd, success} {
186 list [catch {continue} msg] $msg
187 } {4 {}}
188
189 test cmdAH-4.1 {Tcl_EncodingObjCmd} -returnCodes error -body {
190 encoding
191 } -result {wrong # args: should be "encoding subcommand ?arg ...?"}
192 test cmdAH-4.2 {Tcl_EncodingObjCmd} -returnCodes error -body {
193 encoding foo
194 } -result {unknown or ambiguous subcommand "foo": must be convertfrom, convertto, dirs, names, or system}
195 test cmdAH-4.3 {Tcl_EncodingObjCmd} -returnCodes error -body {
196 encoding convertto
197 } -result {wrong # args: should be "encoding convertto ?encoding? data"}
198 test cmdAH-4.4 {Tcl_EncodingObjCmd} -returnCodes error -body {
199 encoding convertto foo bar
200 } -result {unknown encoding "foo"}
201 test cmdAH-4.5 {Tcl_EncodingObjCmd} -setup {
202 set system [encoding system]
203 } -body {
204 encoding system jis0208
205 encoding convertto \u4e4e
206 } -cleanup {
207 encoding system $system
208 } -result 8C
209 test cmdAH-4.6 {Tcl_EncodingObjCmd} -setup {
210 set system [encoding system]
211 } -body {
212 encoding system iso8859-1
213 encoding convertto jis0208 \u4e4e
214 } -cleanup {
215 encoding system $system
216 } -result 8C
217 test cmdAH-4.7 {Tcl_EncodingObjCmd} -returnCodes error -body {
218 encoding convertfrom
219 } -result {wrong # args: should be "encoding convertfrom ?encoding? data"}
220 test cmdAH-4.8 {Tcl_EncodingObjCmd} -returnCodes error -body {
221 encoding convertfrom foo bar
222 } -result {unknown encoding "foo"}
223 test cmdAH-4.9 {Tcl_EncodingObjCmd} -setup {
224 set system [encoding system]
225 } -body {
226 encoding system jis0208
227 encoding convertfrom 8C
228 } -cleanup {
229 encoding system $system
230 } -result \u4e4e
231 test cmdAH-4.10 {Tcl_EncodingObjCmd} -setup {
232 set system [encoding system]
233 } -body {
234 encoding system iso8859-1
235 encoding convertfrom jis0208 8C
236 } -cleanup {
237 encoding system $system
238 } -result \u4e4e
239 test cmdAH-4.11 {Tcl_EncodingObjCmd} -returnCodes error -body {
240 encoding names foo
241 } -result {wrong # args: should be "encoding names"}
242 test cmdAH-4.12 {Tcl_EncodingObjCmd} -returnCodes error -body {
243 encoding system foo bar
244 } -result {wrong # args: should be "encoding system ?encoding?"}
245 test cmdAH-4.13 {Tcl_EncodingObjCmd} -setup {
246 set system [encoding system]
247 } -body {
248 encoding system iso8859-1
249 encoding system
250 } -cleanup {
251 encoding system $system
252 } -result iso8859-1
253
254 test cmdAH-5.1 {Tcl_FileObjCmd} -returnCodes error -body {
255 file
256 } -result {wrong # args: should be "file subcommand ?arg ...?"}
257 test cmdAH-5.2 {Tcl_FileObjCmd} -returnCodes error -body {
258 file x
259 } -result {unknown or ambiguous subcommand "x": must be atime, attributes, channels, copy, delete, dirname, executable, exists, extension, isdirectory, isfile, join, link, lstat, mkdir, mtime, nativename, normalize, owned, pathtype, readable, readlink, rename, rootname, separator, size, split, stat, system, tail, tempfile, type, volumes, or writable}
260 test cmdAH-5.3 {Tcl_FileObjCmd} -returnCodes error -body {
261 file exists
262 } -result {wrong # args: should be "file exists name"}
263 test cmdAH-5.4 {Tcl_FileObjCmd} {
264 file exists ""
265 } 0
266
267 # volume
268 test cmdAH-6.1 {Tcl_FileObjCmd: volumes} -returnCodes error -body {
269 file volumes x
270 } -result {wrong # args: should be "file volumes"}
271 test cmdAH-6.2 {Tcl_FileObjCmd: volumes} -body {
272 lindex [file volumes] 0
273 } -match glob -result ?*
274 test cmdAH-6.3 {Tcl_FileObjCmd: volumes} -constraints unix -body {
275 set volumeList [file volumes]
276 glob -nocomplain [lindex $volumeList 0]*
277 } -match glob -result *
278 test cmdAH-6.4 {Tcl_FileObjCmd: volumes} -constraints win -body {
279 set volumeList [string tolower [file volumes]]
280 set element [lsearch -exact $volumeList "c:/"]
281 list [expr {$element>=0}] [glob -nocomplain [lindex $volumeList $element]*]
282 } -match glob -result {1 *}
283
284 # attributes
285 test cmdAH-7.1 {Tcl_FileObjCmd - file attrs} -setup {
286 set foofile [makeFile abcde foo.file]
287 catch {file delete -force $foofile}
288 } -body {
289 close [open $foofile w]
290 file attributes $foofile
291 } -cleanup {
292 # We used [makeFile] so we undo with [removeFile]
293 removeFile $foofile
294 } -match glob -result *
295
296 # dirname
297 test cmdAH-8.1 {Tcl_FileObjCmd: dirname} -returnCodes error -body {
298 file dirname a b
299 } -result {wrong # args: should be "file dirname name"}
300 test cmdAH-8.2 {Tcl_FileObjCmd: dirname} testsetplatform {
301 testsetplatform unix
302 file dirname /a/b
303 } /a
304 test cmdAH-8.3 {Tcl_FileObjCmd: dirname} testsetplatform {
305 testsetplatform unix
306 file dirname {}
307 } .
308 test cmdAH-8.5 {Tcl_FileObjCmd: dirname} testsetplatform {
309 testsetplatform win
310 file dirname {}
311 } .
312 test cmdAH-8.6 {Tcl_FileObjCmd: dirname} testsetplatform {
313 testsetplatform unix
314 file dirname .def
315 } .
316 test cmdAH-8.8 {Tcl_FileObjCmd: dirname} testsetplatform {
317 testsetplatform win
318 file dirname a
319 } .
320 test cmdAH-8.9 {Tcl_FileObjCmd: dirname} testsetplatform {
321 testsetplatform unix
322 file dirname a/b/c.d
323 } a/b
324 test cmdAH-8.10 {Tcl_FileObjCmd: dirname} testsetplatform {
325 testsetplatform unix
326 file dirname a/b.c/d
327 } a/b.c
328 test cmdAH-8.11 {Tcl_FileObjCmd: dirname} testsetplatform {
329 testsetplatform unix
330 file dirname /.
331 } /
332 test cmdAH-8.12 {Tcl_FileObjCmd: dirname} testsetplatform {
333 testsetplatform unix
334 file dirname /
335 } /
336 test cmdAH-8.13 {Tcl_FileObjCmd: dirname} testsetplatform {
337 testsetplatform unix
338 file dirname /foo
339 } /
340 test cmdAH-8.14 {Tcl_FileObjCmd: dirname} testsetplatform {
341 testsetplatform unix
342 file dirname //foo
343 } /
344 test cmdAH-8.15 {Tcl_FileObjCmd: dirname} testsetplatform {
345 testsetplatform unix
346 file dirname //foo/bar
347 } /foo
348 test cmdAH-8.16 {Tcl_FileObjCmd: dirname} testsetplatform {
349 testsetplatform unix
350 file dirname {//foo\/bar/baz}
351 } {/foo\/bar}
352 test cmdAH-8.17 {Tcl_FileObjCmd: dirname} testsetplatform {
353 testsetplatform unix
354 file dirname {//foo\/bar/baz/blat}
355 } {/foo\/bar/baz}
356 test cmdAH-8.18 {Tcl_FileObjCmd: dirname} testsetplatform {
357 testsetplatform unix
358 file dirname /foo//
359 } /
360 test cmdAH-8.19 {Tcl_FileObjCmd: dirname} testsetplatform {
361 testsetplatform unix
362 file dirname ./a
363 } .
364 test cmdAH-8.20 {Tcl_FileObjCmd: dirname} testsetplatform {
365 testsetplatform unix
366 file dirname a/.a
367 } a
368 test cmdAH-8.21 {Tcl_FileObjCmd: dirname} testsetplatform {
369 testsetplatform windows
370 file dirname c:foo
371 } c:
372 test cmdAH-8.22 {Tcl_FileObjCmd: dirname} testsetplatform {
373 testsetplatform windows
374 file dirname c:
375 } c:
376 test cmdAH-8.23 {Tcl_FileObjCmd: dirname} testsetplatform {
377 testsetplatform windows
378 file dirname c:/
379 } c:/
380 test cmdAH-8.24 {Tcl_FileObjCmd: dirname} testsetplatform {
381 testsetplatform windows
382 file dirname {c:\foo}
383 } c:/
384 test cmdAH-8.25 {Tcl_FileObjCmd: dirname} testsetplatform {
385 testsetplatform windows
386 file dirname {//foo/bar/baz}
387 } //foo/bar
388 test cmdAH-8.26 {Tcl_FileObjCmd: dirname} testsetplatform {
389 testsetplatform windows
390 file dirname {//foo/bar}
391 } //foo/bar
392 test cmdAH-8.38 {Tcl_FileObjCmd: dirname} testsetplatform {
393 testsetplatform unix
394 file dirname ~/foo
395 } ~
396 test cmdAH-8.39 {Tcl_FileObjCmd: dirname} testsetplatform {
397 testsetplatform unix
398 file dirname ~bar/foo
399 } ~bar
400 test cmdAH-8.43 {Tcl_FileObjCmd: dirname} -setup {
401 global env
402 set temp $env(HOME)
403 } -constraints testsetplatform -body {
404 set env(HOME) "/homewontexist/test"
405 testsetplatform unix
406 file dirname ~
407 } -cleanup {
408 set env(HOME) $temp
409 } -result /homewontexist
410 test cmdAH-8.44 {Tcl_FileObjCmd: dirname} -setup {
411 global env
412 set temp $env(HOME)
413 } -constraints testsetplatform -body {
414 set env(HOME) "~"
415 testsetplatform unix
416 file dirname ~
417 } -cleanup {
418 set env(HOME) $temp
419 } -result ~
420 test cmdAH-8.45 {Tcl_FileObjCmd: dirname} -setup {
421 set temp $::env(HOME)
422 } -constraints {win testsetplatform} -match regexp -body {
423 set ::env(HOME) "/homewontexist/test"
424 testsetplatform windows
425 file dirname ~
426 } -cleanup {
427 set ::env(HOME) $temp
428 } -result {([a-zA-Z]:?)/homewontexist}
429 test cmdAH-8.46 {Tcl_FileObjCmd: dirname} {
430 set f [file normalize [info nameof]]
431 file exists $f
432 set res1 [file dirname [file join $f foo/bar]]
433 set res2 [file dirname "${f}/foo/bar"]
434 if {$res1 eq $res2} {
435 return "ok"
436 }
437 return "file dirname problem, $res1, $res2 not equal"
438 } {ok}
439
440 # tail
441 test cmdAH-9.1 {Tcl_FileObjCmd: tail} -returnCodes error -body {
442 file tail a b
443 } -result {wrong # args: should be "file tail name"}
444 test cmdAH-9.2 {Tcl_FileObjCmd: tail} testsetplatform {
445 testsetplatform unix
446 file tail /a/b
447 } b
448 test cmdAH-9.3 {Tcl_FileObjCmd: tail} testsetplatform {
449 testsetplatform unix
450 file tail {}
451 } {}
452 test cmdAH-9.5 {Tcl_FileObjCmd: tail} testsetplatform {
453 testsetplatform win
454 file tail {}
455 } {}
456 test cmdAH-9.6 {Tcl_FileObjCmd: tail} testsetplatform {
457 testsetplatform unix
458 file tail .def
459 } .def
460 test cmdAH-9.8 {Tcl_FileObjCmd: tail} testsetplatform {
461 testsetplatform win
462 file tail a
463 } a
464 test cmdAH-9.9 {Tcl_FileObjCmd: tail} testsetplatform {
465 testsetplatform unix
466 file ta a/b/c.d
467 } c.d
468 test cmdAH-9.10 {Tcl_FileObjCmd: tail} testsetplatform {
469 testsetplatform unix
470 file tail a/b.c/d
471 } d
472 test cmdAH-9.11 {Tcl_FileObjCmd: tail} testsetplatform {
473 testsetplatform unix
474 file tail /.
475 } .
476 test cmdAH-9.12 {Tcl_FileObjCmd: tail} testsetplatform {
477 testsetplatform unix
478 file tail /
479 } {}
480 test cmdAH-9.13 {Tcl_FileObjCmd: tail} testsetplatform {
481 testsetplatform unix
482 file tail /foo
483 } foo
484 test cmdAH-9.14 {Tcl_FileObjCmd: tail} testsetplatform {
485 testsetplatform unix
486 file tail //foo
487 } foo
488 test cmdAH-9.15 {Tcl_FileObjCmd: tail} testsetplatform {
489 testsetplatform unix
490 file tail //foo/bar
491 } bar
492 test cmdAH-9.16 {Tcl_FileObjCmd: tail} testsetplatform {
493 testsetplatform unix
494 file tail {//foo\/bar/baz}
495 } baz
496 test cmdAH-9.17 {Tcl_FileObjCmd: tail} testsetplatform {
497 testsetplatform unix
498 file tail {//foo\/bar/baz/blat}
499 } blat
500 test cmdAH-9.18 {Tcl_FileObjCmd: tail} testsetplatform {
501 testsetplatform unix
502 file tail /foo//
503 } foo
504 test cmdAH-9.19 {Tcl_FileObjCmd: tail} testsetplatform {
505 testsetplatform unix
506 file tail ./a
507 } a
508 test cmdAH-9.20 {Tcl_FileObjCmd: tail} testsetplatform {
509 testsetplatform unix
510 file tail a/.a
511 } .a
512 test cmdAH-9.21 {Tcl_FileObjCmd: tail} testsetplatform {
513 testsetplatform windows
514 file tail c:foo
515 } foo
516 test cmdAH-9.22 {Tcl_FileObjCmd: tail} testsetplatform {
517 testsetplatform windows
518 file tail c:
519 } {}
520 test cmdAH-9.23 {Tcl_FileObjCmd: tail} testsetplatform {
521 testsetplatform windows
522 file tail c:/
523 } {}
524 test cmdAH-9.24 {Tcl_FileObjCmd: tail} testsetplatform {
525 testsetplatform windows
526 file tail {c:\foo}
527 } foo
528 test cmdAH-9.25 {Tcl_FileObjCmd: tail} testsetplatform {
529 testsetplatform windows
530 file tail {//foo/bar/baz}
531 } baz
532 test cmdAH-9.26 {Tcl_FileObjCmd: tail} testsetplatform {
533 testsetplatform windows
534 file tail {//foo/bar}
535 } {}
536 test cmdAH-9.42 {Tcl_FileObjCmd: tail} -constraints testsetplatform -setup {
537 global env
538 set temp $env(HOME)
539 } -body {
540 set env(HOME) "/home/test"
541 testsetplatform unix
542 file tail ~
543 } -cleanup {
544 set env(HOME) $temp
545 } -result test
546 test cmdAH-9.43 {Tcl_FileObjCmd: tail} -constraints testsetplatform -setup {
547 global env
548 set temp $env(HOME)
549 } -body {
550 set env(HOME) "~"
551 testsetplatform unix
552 file tail ~
553 } -cleanup {
554 set env(HOME) $temp
555 } -result {}
556 test cmdAH-9.44 {Tcl_FileObjCmd: tail} -constraints testsetplatform -setup {
557 global env
558 set temp $env(HOME)
559 } -body {
560 set env(HOME) "/home/test"
561 testsetplatform windows
562 file tail ~
563 } -cleanup {
564 set env(HOME) $temp
565 } -result test
566 test cmdAH-9.46 {Tcl_FileObjCmd: tail} testsetplatform {
567 testsetplatform unix
568 file tail {f.oo\bar/baz.bat}
569 } baz.bat
570 test cmdAH-9.47 {Tcl_FileObjCmd: tail} testsetplatform {
571 testsetplatform windows
572 file tail c:foo
573 } foo
574 test cmdAH-9.48 {Tcl_FileObjCmd: tail} testsetplatform {
575 testsetplatform windows
576 file tail c:
577 } {}
578 test cmdAH-9.49 {Tcl_FileObjCmd: tail} testsetplatform {
579 testsetplatform windows
580 file tail c:/foo
581 } foo
582 test cmdAH-9.50 {Tcl_FileObjCmd: tail} testsetplatform {
583 testsetplatform windows
584 file tail {c:/foo\bar}
585 } bar
586 test cmdAH-9.51 {Tcl_FileObjCmd: tail} testsetplatform {
587 testsetplatform windows
588 file tail {foo\bar}
589 } bar
590 test cmdAH-9.52 {Tcl_FileObjCmd: tail / normalize, bug 7a9dc52b29} {
591 list \
592 [file tail {~/~foo}] \
593 [file tail {~/test/~foo}] \
594 [file tail [file normalize {~/~foo}]] \
595 [file tail [file normalize {~/test/~foo}]]
596 } [lrepeat 4 ./~foo]
597
598 # rootname
599 test cmdAH-10.1 {Tcl_FileObjCmd: rootname} -returnCodes error -body {
600 file rootname a b
601 } -result {wrong # args: should be "file rootname name"}
602 test cmdAH-10.2 {Tcl_FileObjCmd: rootname} testsetplatform {
603 testsetplatform unix
604 file rootname {}
605 } {}
606 test cmdAH-10.3 {Tcl_FileObjCmd: rootname} testsetplatform {
607 testsetplatform unix
608 file ro foo
609 } foo
610 test cmdAH-10.4 {Tcl_FileObjCmd: rootname} testsetplatform {
611 testsetplatform unix
612 file rootname foo.
613 } foo
614 test cmdAH-10.5 {Tcl_FileObjCmd: rootname} testsetplatform {
615 testsetplatform unix
616 file rootname .foo
617 } {}
618 test cmdAH-10.6 {Tcl_FileObjCmd: rootname} testsetplatform {
619 testsetplatform unix
620 file rootname abc.def
621 } abc
622 test cmdAH-10.7 {Tcl_FileObjCmd: rootname} testsetplatform {
623 testsetplatform unix
624 file rootname abc.def.ghi
625 } abc.def
626 test cmdAH-10.8 {Tcl_FileObjCmd: rootname} testsetplatform {
627 testsetplatform unix
628 file rootname a/b/c.d
629 } a/b/c
630 test cmdAH-10.9 {Tcl_FileObjCmd: rootname} testsetplatform {
631 testsetplatform unix
632 file rootname a/b.c/d
633 } a/b.c/d
634 test cmdAH-10.10 {Tcl_FileObjCmd: rootname} testsetplatform {
635 testsetplatform unix
636 file rootname a/b.c/
637 } a/b.c/
638 test cmdAH-10.23 {Tcl_FileObjCmd: rootname} testsetplatform {
639 testsetplatform windows
640 file rootname {}
641 } {}
642 test cmdAH-10.24 {Tcl_FileObjCmd: rootname} testsetplatform {
643 testsetplatform windows
644 file ro foo
645 } foo
646 test cmdAH-10.25 {Tcl_FileObjCmd: rootname} testsetplatform {
647 testsetplatform windows
648 file rootname foo.
649 } foo
650 test cmdAH-10.26 {Tcl_FileObjCmd: rootname} testsetplatform {
651 testsetplatform windows
652 file rootname .foo
653 } {}
654 test cmdAH-10.27 {Tcl_FileObjCmd: rootname} testsetplatform {
655 testsetplatform windows
656 file rootname abc.def
657 } abc
658 test cmdAH-10.28 {Tcl_FileObjCmd: rootname} testsetplatform {
659 testsetplatform windows
660 file rootname abc.def.ghi
661 } abc.def
662 test cmdAH-10.29 {Tcl_FileObjCmd: rootname} testsetplatform {
663 testsetplatform windows
664 file rootname a/b/c.d
665 } a/b/c
666 test cmdAH-10.30 {Tcl_FileObjCmd: rootname} testsetplatform {
667 testsetplatform windows
668 file rootname a/b.c/d
669 } a/b.c/d
670 test cmdAH-10.31 {Tcl_FileObjCmd: rootname} testsetplatform {
671 testsetplatform windows
672 file rootname a\\b.c\\
673 } a\\b.c\\
674 test cmdAH-10.32 {Tcl_FileObjCmd: rootname} testsetplatform {
675 testsetplatform windows
676 file rootname a\\b\\c.d
677 } a\\b\\c
678 test cmdAH-10.33 {Tcl_FileObjCmd: rootname} testsetplatform {
679 testsetplatform windows
680 file rootname a\\b.c\\d
681 } a\\b.c\\d
682 test cmdAH-10.34 {Tcl_FileObjCmd: rootname} testsetplatform {
683 testsetplatform windows
684 file rootname a\\b.c\\
685 } a\\b.c\\
686 set num 35
687 foreach outer { {} a .a a. a.a } {
688 foreach inner { {} a .a a. a.a } {
689 set thing [format %s/%s $outer $inner]
690 ;test cmdAH-10.$num {Tcl_FileObjCmd: rootname and extension options} testsetplatform "
691 testsetplatform unix
692 [list format %s%s [file rootname $thing] [file ext $thing]]
693 " $thing
694 incr num
695 }
696 }
697
698 # extension
699 test cmdAH-11.1 {Tcl_FileObjCmd: extension} -returnCodes error -body {
700 file extension a b
701 } -result {wrong # args: should be "file extension name"}
702 test cmdAH-11.2 {Tcl_FileObjCmd: extension} testsetplatform {
703 testsetplatform unix
704 file extension {}
705 } {}
706 test cmdAH-11.3 {Tcl_FileObjCmd: extension} testsetplatform {
707 testsetplatform unix
708 file ext foo
709 } {}
710 test cmdAH-11.4 {Tcl_FileObjCmd: extension} testsetplatform {
711 testsetplatform unix
712 file extension foo.
713 } .
714 test cmdAH-11.5 {Tcl_FileObjCmd: extension} testsetplatform {
715 testsetplatform unix
716 file extension .foo
717 } .foo
718 test cmdAH-11.6 {Tcl_FileObjCmd: extension} testsetplatform {
719 testsetplatform unix
720 file extension abc.def
721 } .def
722 test cmdAH-11.7 {Tcl_FileObjCmd: extension} testsetplatform {
723 testsetplatform unix
724 file extension abc.def.ghi
725 } .ghi
726 test cmdAH-11.8 {Tcl_FileObjCmd: extension} testsetplatform {
727 testsetplatform unix
728 file extension a/b/c.d
729 } .d
730 test cmdAH-11.9 {Tcl_FileObjCmd: extension} testsetplatform {
731 testsetplatform unix
732 file extension a/b.c/d
733 } {}
734 test cmdAH-11.10 {Tcl_FileObjCmd: extension} testsetplatform {
735 testsetplatform unix
736 file extension a/b.c/
737 } {}
738 test cmdAH-11.23 {Tcl_FileObjCmd: extension} testsetplatform {
739 testsetplatform windows
740 file extension {}
741 } {}
742 test cmdAH-11.24 {Tcl_FileObjCmd: extension} testsetplatform {
743 testsetplatform windows
744 file ext foo
745 } {}
746 test cmdAH-11.25 {Tcl_FileObjCmd: extension} testsetplatform {
747 testsetplatform windows
748 file extension foo.
749 } .
750 test cmdAH-11.26 {Tcl_FileObjCmd: extension} testsetplatform {
751 testsetplatform windows
752 file extension .foo
753 } .foo
754 test cmdAH-11.27 {Tcl_FileObjCmd: extension} testsetplatform {
755 testsetplatform windows
756 file extension abc.def
757 } .def
758 test cmdAH-11.28 {Tcl_FileObjCmd: extension} testsetplatform {
759 testsetplatform windows
760 file extension abc.def.ghi
761 } .ghi
762 test cmdAH-11.29 {Tcl_FileObjCmd: extension} testsetplatform {
763 testsetplatform windows
764 file extension a/b/c.d
765 } .d
766 test cmdAH-11.30 {Tcl_FileObjCmd: extension} testsetplatform {
767 testsetplatform windows
768 file extension a/b.c/d
769 } {}
770 test cmdAH-11.31 {Tcl_FileObjCmd: extension} testsetplatform {
771 testsetplatform windows
772 file extension a\\b.c\\
773 } {}
774 test cmdAH-11.32 {Tcl_FileObjCmd: extension} testsetplatform {
775 testsetplatform windows
776 file extension a\\b\\c.d
777 } .d
778 test cmdAH-11.33 {Tcl_FileObjCmd: extension} testsetplatform {
779 testsetplatform windows
780 file extension a\\b.c\\d
781 } {}
782 test cmdAH-11.34 {Tcl_FileObjCmd: extension} testsetplatform {
783 testsetplatform windows
784 file extension a\\b.c\\
785 } {}
786 foreach {test onPlatform value result} {
787 cmdAH-11.35 unix a..b .b
788 cmdAH-11.36 windows a..b .b
789 cmdAH-11.37 unix a...b .b
790 cmdAH-11.38 windows a...b .b
791 cmdAH-11.39 unix a.c..b .b
792 cmdAH-11.40 windows a.c..b .b
793 cmdAH-11.41 unix ..b .b
794 cmdAH-11.42 windows ..b .b
795 } {
796 test $test {Tcl_FileObjCmd: extension} testsetplatform "
797 testsetplatform $onPlatform
798 file extension $value
799 " $result
800 }
801
802 # pathtype
803 test cmdAH-12.1 {Tcl_FileObjCmd: pathtype} -returnCodes error -body {
804 file pathtype a b
805 } -result {wrong # args: should be "file pathtype name"}
806 test cmdAH-12.2 {Tcl_FileObjCmd: pathtype} testsetplatform {
807 testsetplatform unix
808 file pathtype /a
809 } absolute
810 test cmdAH-12.3 {Tcl_FileObjCmd: pathtype} testsetplatform {
811 testsetplatform unix
812 file p a
813 } relative
814 test cmdAH-12.4 {Tcl_FileObjCmd: pathtype} testsetplatform {
815 testsetplatform windows
816 file pathtype c:a
817 } volumerelative
818
819 # split
820 test cmdAH-13.1 {Tcl_FileObjCmd: split} -returnCodes error -body {
821 file split a b
822 } -result {wrong # args: should be "file split name"}
823 test cmdAH-13.2 {Tcl_FileObjCmd: split} testsetplatform {
824 testsetplatform unix
825 file split a
826 } a
827 test cmdAH-13.3 {Tcl_FileObjCmd: split} testsetplatform {
828 testsetplatform unix
829 file split a/b
830 } {a b}
831
832 # join
833 test cmdAH-14.1 {Tcl_FileObjCmd: join} testsetplatform {
834 testsetplatform unix
835 file join a
836 } a
837 test cmdAH-14.2 {Tcl_FileObjCmd: join} testsetplatform {
838 testsetplatform unix
839 file join a b
840 } a/b
841 test cmdAH-14.3 {Tcl_FileObjCmd: join} testsetplatform {
842 testsetplatform unix
843 file join a b c d
844 } a/b/c/d
845
846 # error handling of Tcl_TranslateFileName
847 test cmdAH-15.1 {Tcl_FileObjCmd} -constraints testsetplatform -body {
848 testsetplatform unix
849 file atime ~_bad_user
850 } -returnCodes error -result {user "_bad_user" doesn't exist}
851
852 catch {testsetplatform $platform}
853
854 # readable
855 set gorpfile [makeFile abcde g\xF3rp.file]
856 set dirfile [makeDirectory dir.file]
857 test cmdAH-16.1 {Tcl_FileObjCmd: readable} {
858 -returnCodes error
859 -body {file readable a b}
860 -result {wrong # args: should be "file readable name"}
861 }
862 test cmdAH-16.2 {Tcl_FileObjCmd: readable} {
863 -constraints testchmod
864 -setup {testchmod 0o444 $gorpfile}
865 -body {file readable $gorpfile}
866 -result 1
867 }
868 test cmdAH-16.3 {Tcl_FileObjCmd: readable} {
869 -constraints {unix notRoot testchmod notWsl}
870 -setup {testchmod 0o333 $gorpfile}
871 -body {file readable $gorpfile}
872 -result 0
873 }
874
875 # writable
876 test cmdAH-17.1 {Tcl_FileObjCmd: writable} {
877 -returnCodes error
878 -body {file writable a b}
879 -result {wrong # args: should be "file writable name"}
880 }
881 test cmdAH-17.2 {Tcl_FileObjCmd: writable} {
882 -constraints {notRoot testchmod}
883 -setup {testchmod 0o555 $gorpfile}
884 -body {file writable $gorpfile}
885 -result 0
886 }
887 test cmdAH-17.3 {Tcl_FileObjCmd: writable} {
888 -constraints testchmod
889 -setup {testchmod 0o222 $gorpfile}
890 -body {file writable $gorpfile}
891 -result 1
892 }
893
894 # executable
895 removeFile $gorpfile
896 removeDirectory $dirfile
897 set dirfile [makeDirectory dir.file]
898 set gorpfile [makeFile abcde g\xF3rp.file]
899 test cmdAH-18.1 {Tcl_FileObjCmd: executable} -returnCodes error -body {
900 file executable a b
901 } -result {wrong # args: should be "file executable name"}
902 test cmdAH-18.2 {Tcl_FileObjCmd: executable} {notRoot notWsl} {
903 file executable $gorpfile
904 } 0
905 test cmdAH-18.3 {Tcl_FileObjCmd: executable} {unix testchmod} {
906 # Only on unix will setting the execute bit on a regular file cause that
907 # file to be executable.
908 testchmod 0o775 $gorpfile
909 file exe $gorpfile
910 } 1
911 test cmdAH-18.5 {Tcl_FileObjCmd: executable} -constraints {win} -body {
912 # On windows, must be a .exe, .com, etc.
913 set x {}
914 set gorpexes {}
915 foreach ext {exe com cmd bat} {
916 lappend x [file exe nosuchfile.$ext]
917 set gorpexe [makeFile foo gorp.$ext]
918 lappend gorpexes $gorpexe
919 lappend x [file exe $gorpexe] [file exe [string toupper $gorpexe]]
920 }
921 set x
922 } -cleanup {
923 foreach gorpexe $gorpexes {
924 removeFile $gorpexe
925 }
926 } -result {0 1 1 0 1 1 0 1 1 0 1 1}
927 test cmdAH-18.6 {Tcl_FileObjCmd: executable} {} {
928 # Directories are always executable.
929 file exe $dirfile
930 } 1
931
932 removeDirectory $dirfile
933 removeFile $gorpfile
934 set linkfile [file join [temporaryDirectory] link.file]
935 file delete $linkfile
936
937 # exists
938 test cmdAH-19.1 {Tcl_FileObjCmd: exists} -returnCodes error -body {
939 file exists a b
940 } -result {wrong # args: should be "file exists name"}
941 test cmdAH-19.2 {Tcl_FileObjCmd: exists} {file exists $gorpfile} 0
942 test cmdAH-19.3 {Tcl_FileObjCmd: exists} {
943 file exists [file join [temporaryDirectory] dir.file g\xF3rp.file]
944 } 0
945 catch {
946 set gorpfile [makeFile abcde g\xF3rp.file]
947 set dirfile [makeDirectory dir.file]
948 set subgorp [makeFile 12345 [file join $dirfile g\xF3rp.file]]
949 }
950 test cmdAH-19.4 {Tcl_FileObjCmd: exists} {
951 file exists $gorpfile
952 } 1
953 test cmdAH-19.5 {Tcl_FileObjCmd: exists} {
954 file exists $subgorp
955 } 1
956 # nativename
957 test cmdAH-19.6 {Tcl_FileObjCmd: nativename} -body {
958 testsetplatform unix
959 file nativename a/b
960 } -constraints testsetplatform -cleanup {
961 testsetplatform $platform
962 } -result a/b
963 test cmdAH-19.7 {Tcl_FileObjCmd: nativename} -body {
964 testsetplatform windows
965 file nativename a/b
966 } -constraints testsetplatform -cleanup {
967 testsetplatform $platform
968 } -result {a\b}
969 test cmdAH-19.9 {Tcl_FileObjCmd: ~ : exists} {
970 file exists ~nOsUcHuSeR
971 } 0
972 test cmdAH-19.10 {Tcl_FileObjCmd: ~ : nativename} -body {
973 # should probably be a non-error in fact...
974 file nativename ~nOsUcHuSeR
975 } -returnCodes error -match glob -result *
976 # The test below has to be done in /tmp rather than the current directory in
977 # order to guarantee (?) a local file system: some NFS file systems won't do
978 # the stuff below correctly.
979 test cmdAH-19.11 {Tcl_FileObjCmd: exists} -constraints {unix notRoot} -setup {
980 file delete -force /tmp/tcl.foo.dir/file
981 file delete -force /tmp/tcl.foo.dir
982 } -body {
983 makeDirectory /tmp/tcl.foo.dir
984 makeFile 12345 /tmp/tcl.foo.dir/file
985 file attributes /tmp/tcl.foo.dir -permissions 0o000
986 file exists /tmp/tcl.foo.dir/file
987 } -cleanup {
988 file attributes /tmp/tcl.foo.dir -permissions 0o775
989 removeFile /tmp/tcl.foo.dir/file
990 removeDirectory /tmp/tcl.foo.dir
991 } -result 0
992 test cmdAH-19.12 {Bug 3608360: [file exists] mustn't do globbing} -setup {
993 set newdirfile [makeDirectory newdir.file]
994 set cwd [pwd]
995 cd $newdirfile
996 # Content of file is totally unimportant; name is *not*
997 set innocentBystander [makeFile "abc" [file join $newdirfile foo.bar]]
998 } -body {
999 list [file exists foo.bar] [file exists *.bar]
1000 } -cleanup {
1001 cd $cwd
1002 removeFile $innocentBystander
1003 removeDirectory $newdirfile
1004 } -result {1 0}
1005
1006 # Stat related commands
1007
1008 catch {testsetplatform $platform}
1009 removeFile $gorpfile
1010 set gorpfile [makeFile "Test string" g\xF3rp.file]
1011 catch {file attributes $gorpfile -permissions 0o765}
1012
1013 # avoid problems with non-local filesystems
1014 if {[testConstraint unix] && [file exists /tmp]} {
1015 set file [makeFile "data" touch.me /tmp]
1016 } else {
1017 set file [makeFile "data" touch.me]
1018 }
1019
1020 # atime
1021 test cmdAH-20.1 {Tcl_FileObjCmd: atime} -returnCodes error -body {
1022 file atime a b c
1023 } -result {wrong # args: should be "file atime name ?time?"}
1024 test cmdAH-20.2 {Tcl_FileObjCmd: atime} -setup {
1025 unset -nocomplain stat
1026 } -body {
1027 file stat $gorpfile stat
1028 list [expr {[file mtime $gorpfile] == $stat(mtime)}] \
1029 [expr {[file atime $gorpfile] == $stat(atime)}]
1030 } -result {1 1}
1031 test cmdAH-20.3 {Tcl_FileObjCmd: atime} {
1032 list [catch {file atime _bogus_} msg] [string tolower $msg] $errorCode
1033 } {1 {could not read "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1034 test cmdAH-20.4 {Tcl_FileObjCmd: atime} -returnCodes error -body {
1035 file atime $file notint
1036 } -result {expected integer but got "notint"}
1037 test cmdAH-20.5 {Tcl_FileObjCmd: atime touch} {unix} {
1038 set atime [file atime $file]
1039 after 1100; # pause a sec to notice change in atime
1040 set newatime [clock seconds]
1041 set modatime [file atime $file $newatime]
1042 expr {$newatime == $modatime ? 1 : "$newatime != $modatime"}
1043 } 1
1044 test cmdAH-20.6 {Tcl_FileObjCmd: atime touch} -setup {
1045 set old [pwd]
1046 cd $::tcltest::temporaryDirectory
1047 set volumetype [testvolumetype]
1048 cd $old
1049 } -constraints {win testvolumetype} -body {
1050 if {"NTFS" ne $volumetype} {
1051 # Windows FAT doesn't understand atime, but NTFS does. May also fail
1052 # for Windows on NFS mounted disks.
1053 return 1
1054 }
1055 cd $old
1056 set atime [file atime $file]
1057 after 1100; # pause a sec to notice change in atime
1058 set newatime [clock seconds]
1059 set modatime [file atime $file $newatime]
1060 expr {$newatime == $modatime ? 1 : "$newatime != $modatime"}
1061 } -result 1
1062 test cmdAH-20.7 {
1063 Tcl_FileObjCmd: atime (built-in Windows names)
1064 } -constraints {win} -body {
1065 file atime con
1066 } -result "could not get access time for file \"con\"" -returnCodes error
1067 test cmdAH-20.7.1 {
1068 Tcl_FileObjCmd: atime (built-in Windows names with dir path and extension)
1069 } -constraints {win} -body {
1070 file atime [file join [temporaryDirectory] CON.txt]
1071 } -match regexp -result {could not (?:get access time|read)} -returnCodes error
1072
1073 if {[testConstraint unix] && [file exists /tmp]} {
1074 removeFile touch.me /tmp
1075 } else {
1076 removeFile touch.me
1077 }
1078
1079 # isdirectory
1080 test cmdAH-21.1 {Tcl_FileObjCmd: isdirectory} -returnCodes error -body {
1081 file isdirectory a b
1082 } -result {wrong # args: should be "file isdirectory name"}
1083 test cmdAH-21.2 {Tcl_FileObjCmd: isdirectory} {file isdirectory $gorpfile} 0
1084 test cmdAH-21.3 {Tcl_FileObjCmd: isdirectory} {file isdirectory $dirfile} 1
1085
1086 # isfile
1087 test cmdAH-22.1 {Tcl_FileObjCmd: isfile} -returnCodes error -body {
1088 file isfile a b
1089 } -result {wrong # args: should be "file isfile name"}
1090 test cmdAH-22.2 {Tcl_FileObjCmd: isfile} {file isfile $gorpfile} 1
1091 test cmdAH-22.3 {Tcl_FileObjCmd: isfile} {file isfile $dirfile} 0
1092
1093 # lstat and readlink: don't run these tests everywhere, since not all sites
1094 # will have symbolic links
1095 catch {file link -symbolic $linkfile $gorpfile}
1096 test cmdAH-23.1 {Tcl_FileObjCmd: lstat} -returnCodes error -body {
1097 file lstat a
1098 } -result {wrong # args: should be "file lstat name varName"}
1099 test cmdAH-23.2 {Tcl_FileObjCmd: lstat} -returnCodes error -body {
1100 file lstat a b c
1101 } -result {wrong # args: should be "file lstat name varName"}
1102 test cmdAH-23.3 {Tcl_FileObjCmd: lstat} -setup {
1103 unset -nocomplain stat
1104 } -constraints {unix nonPortable} -body {
1105 file lstat $linkfile stat
1106 lsort [array names stat]
1107 } -result {atime ctime dev gid ino mode mtime nlink size type uid}
1108 test cmdAH-23.4 {Tcl_FileObjCmd: lstat} -setup {
1109 unset -nocomplain stat
1110 } -constraints {unix nonPortable} -body {
1111 file lstat $linkfile stat
1112 list $stat(nlink) [expr {$stat(mode) & 0o777}] $stat(type)
1113 } -result {1 511 link}
1114 test cmdAH-23.5 {Tcl_FileObjCmd: lstat errors} {nonPortable} {
1115 list [catch {file lstat _bogus_ stat} msg] [string tolower $msg] \
1116 $errorCode
1117 } {1 {could not read "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1118 test cmdAH-23.6 {Tcl_FileObjCmd: lstat errors} -setup {
1119 unset -nocomplain x
1120 } -body {
1121 set x 44
1122 list [catch {file lstat $gorpfile x} msg] $msg $errorCode
1123 } -result {1 {can't set "x(dev)": variable isn't array} {TCL LOOKUP VARNAME x}}
1124 unset -nocomplain stat
1125 # mkdir
1126 set dirA [file join [temporaryDirectory] a]
1127 set dirB [file join [temporaryDirectory] a]
1128 test cmdAH-23.7 {Tcl_FileObjCmd: mkdir} -setup {
1129 catch {file delete -force $dirA}
1130 } -body {
1131 file mkdir $dirA
1132 file isdirectory $dirA
1133 } -cleanup {
1134 file delete $dirA
1135 } -result {1}
1136 test cmdAH-23.8 {Tcl_FileObjCmd: mkdir} -setup {
1137 catch {file delete -force $dirA}
1138 } -body {
1139 file mkdir $dirA/b
1140 file isdirectory $dirA/b
1141 } -cleanup {
1142 file delete -force $dirA
1143 } -result {1}
1144 test cmdAH-23.9 {Tcl_FileObjCmd: mkdir} -setup {
1145 catch {file delete -force $dirA}
1146 } -body {
1147 file mkdir $dirA/b/c
1148 file isdirectory $dirA/b/c
1149 } -cleanup {
1150 file delete -force $dirA
1151 } -result {1}
1152 test cmdAH-23.10 {Tcl_FileObjCmd: mkdir} -setup {
1153 catch {file delete -force $dirA}
1154 catch {file delete -force $dirB}
1155 } -body {
1156 file mkdir $dirA/b $dirB/a/c
1157 list [file isdirectory $dirA/b] [file isdirectory $dirB/a/c]
1158 } -cleanup {
1159 file delete -force $dirA
1160 file delete -force $dirB
1161 } -result {1 1}
1162 test cmdAH-23.11 {Tcl_FileObjCmd: mkdir} {
1163 # Allow zero arguments (TIP 323)
1164 file mkdir
1165 } {}
1166
1167 set file [makeFile "data" touch.me]
1168 # mtime
1169 test cmdAH-24.1 {Tcl_FileObjCmd: mtime} -returnCodes error -body {
1170 file mtime a b c
1171 } -result {wrong # args: should be "file mtime name ?time?"}
1172 test cmdAH-24.2 {Tcl_FileObjCmd: mtime} -setup {
1173 # Check (allowing for clock-skew and OS interrupts as best we can) that
1174 # the change in mtime on a file being written is the time elapsed between
1175 # writes. Note that this can still fail on very busy systems if there are
1176 # long preemptions between the writes and the reading of the clock, but
1177 # there's not much you can do about that other than the completely
1178 # horrible "keep on trying to write until you managed to do it all in less
1179 # than a second." - DKF
1180 waitForEvenSecondForFAT
1181 } -body {
1182 set f [open $gorpfile w]
1183 puts $f "More text"
1184 close $f
1185 set clockOld [clock seconds]
1186 set fileOld [file mtime $gorpfile]
1187 after 2000
1188 set f [open $gorpfile w]
1189 puts $f "More text"
1190 close $f
1191 set clockNew [clock seconds]
1192 set fileNew [file mtime $gorpfile]
1193 expr {
1194 (($fileNew > $fileOld) && ($clockNew > $clockOld) &&
1195 (abs(($fileNew-$fileOld) - ($clockNew-$clockOld)) <= 1)) ? "1" :
1196 "file:($fileOld=>$fileNew) clock:($clockOld=>$clockNew)"
1197 }
1198 } -result {1}
1199 test cmdAH-24.3 {Tcl_FileObjCmd: mtime} -setup {
1200 unset -nocomplain stat
1201 } -body {
1202 file stat $gorpfile stat
1203 list [expr {[file mtime $gorpfile] == $stat(mtime)}] \
1204 [expr {[file atime $gorpfile] == $stat(atime)}]
1205 } -result {1 1}
1206 test cmdAH-24.4 {Tcl_FileObjCmd: mtime} {
1207 list [catch {file mtime _bogus_} msg] [string tolower $msg] $errorCode
1208 } {1 {could not read "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1209 test cmdAH-24.5 {Tcl_FileObjCmd: mtime} -setup {
1210 # Under Unix, use a file in /tmp to avoid clock skew due to NFS. On other
1211 # platforms, just use a file in the local directory.
1212 if {[testConstraint unix]} {
1213 set name /tmp/tcl.test.[pid]
1214 } else {
1215 set name [file join [temporaryDirectory] tf]
1216 }
1217 } -body {
1218 # Make sure that a new file's time is correct. 10 seconds variance is
1219 # allowed used due to slow networks or clock skew on a network drive.
1220 file delete -force $name
1221 close [open $name w]
1222 expr {abs([clock seconds]-[file mtime $name])<10}
1223 } -cleanup {
1224 file delete $name
1225 } -result {1}
1226 test cmdAH-24.7 {Tcl_FileObjCmd: mtime} -returnCodes error -body {
1227 file mtime $file notint
1228 } -result {expected integer but got "notint"}
1229 test cmdAH-24.8 {Tcl_FileObjCmd: mtime touch} unix {
1230 set mtime [file mtime $file]
1231 after 1100; # pause a sec to notice change in mtime
1232 set newmtime [clock seconds]
1233 set modmtime [file mtime $file $newmtime]
1234 expr {$newmtime == $modmtime ? 1 : "$newmtime != $modmtime"}
1235 } 1
1236 test cmdAH-24.9 {Tcl_FileObjCmd: mtime touch with non-ascii chars} -setup {
1237 set oldfile $file
1238 } -constraints unix -body {
1239 # introduce some non-ascii characters.
1240 append file \u2022
1241 file delete -force $file
1242 file rename $oldfile $file
1243 set mtime [file mtime $file]
1244 after 1100; # pause a sec to notice change in mtime
1245 set newmtime [clock seconds]
1246 set modmtime [file mtime $file $newmtime]
1247 expr {$newmtime == $modmtime ? 1 : "$newmtime != $modmtime"}
1248 } -cleanup {
1249 file rename $file $oldfile
1250 } -result 1
1251 test cmdAH-24.10 {Tcl_FileObjCmd: mtime touch} -constraints win -setup {
1252 waitForEvenSecondForFAT
1253 } -body {
1254 set mtime [file mtime $file]
1255 after 2100; # pause two secs to notice change in mtime on FAT fs'es
1256 set newmtime [clock seconds]
1257 set modmtime [file mtime $file $newmtime]
1258 expr {$newmtime == $modmtime ? 1 : "$newmtime != $modmtime"}
1259 } -result 1
1260 test cmdAH-24.11 {Tcl_FileObjCmd: mtime touch with non-ascii chars} -setup {
1261 waitForEvenSecondForFAT
1262 set oldfile $file
1263 } -constraints win -body {
1264 # introduce some non-ascii characters.
1265 append file \u2022
1266 file delete -force $file
1267 file rename $oldfile $file
1268 set mtime [file mtime $file]
1269 after 2100; # pause two secs to notice change in mtime on FAT fs'es
1270 set newmtime [clock seconds]
1271 set modmtime [file mtime $file $newmtime]
1272 expr {$newmtime == $modmtime ? 1 : "$newmtime != $modmtime"}
1273 } -cleanup {
1274 file rename $file $oldfile
1275 } -result 1
1276 removeFile touch.me
1277 rename waitForEvenSecondForFAT {}
1278 test cmdAH-24.12 {Tcl_FileObjCmd: mtime and daylight savings} -setup {
1279 set name [file join [temporaryDirectory] clockchange]
1280 file delete -force $name
1281 close [open $name w]
1282 } -body {
1283 set time [clock scan "21:00:00 October 30 2004 GMT"]
1284 file mtime $name $time
1285 set newmtime [file mtime $name]
1286 expr {$newmtime == $time ? 1 : "$newmtime != $time"}
1287 } -cleanup {
1288 file delete $name
1289 } -result {1}
1290 # bug 1420432: setting mtime fails for directories on windows.
1291 test cmdAH-24.13 {Tcl_FileObjCmd: directory mtime} -setup {
1292 set dirname [file join [temporaryDirectory] tmp[pid]]
1293 file delete -force $dirname
1294 } -constraints tempNotWin -body {
1295 file mkdir $dirname
1296 set old [file mtime $dirname]
1297 file mtime $dirname 0
1298 set new [file mtime $dirname]
1299 list $new [expr {$old != $new}]
1300 } -cleanup {
1301 file delete -force $dirname
1302 } -result {0 1}
1303 test cmdAH-24.14 {
1304 Tcl_FileObjCmd: mtime (built-in Windows names)
1305 } -constraints {win} -body {
1306 file mtime con
1307 } -result "could not get modification time for file \"con\"" -returnCodes error
1308 test cmdAH-24.14.1 {
1309 Tcl_FileObjCmd: mtime (built-in Windows names with dir path and extension)
1310 } -constraints {win} -body {
1311 file mtime [file join [temporaryDirectory] CON.txt]
1312 } -match regexp -result {could not (?:get modification time|read)} -returnCodes error
1313
1314 # 3155760000 is 64-bit Unix time, Wed Jan 01 00:00:00 GMT 2070:
1315 test cmdAH-24.20.1 {Tcl_FileObjCmd: atime 64-bit time_t, bug [4718b41c56]} -constraints {time64bit filetime64bit} -setup {
1316 set filename [makeFile "" foo.text]
1317 } -body {
1318 list [file atime $filename 3155760000] [file atime $filename]
1319 } -cleanup {
1320 removeFile $filename
1321 } -result {3155760000 3155760000}
1322 test cmdAH-24.20.2 {Tcl_FileObjCmd: mtime 64-bit time_t, bug [4718b41c56]} -constraints {time64bit filetime64bit} -setup {
1323 set filename [makeFile "" foo.text]
1324 } -body {
1325 list [file mtime $filename 3155760000] [file mtime $filename]
1326 } -cleanup {
1327 file delete -force $filename
1328 } -result {3155760000 3155760000}
1329
1330 # owned
1331 test cmdAH-25.1 {Tcl_FileObjCmd: owned} -returnCodes error -body {
1332 file owned a b
1333 } -result {wrong # args: should be "file owned name"}
1334 test cmdAH-25.2 {Tcl_FileObjCmd: owned} -constraints win -setup {
1335 set fn $gorpfile
1336 # prefer temp file to check owner (try to avoid bug [7de2d722bd]):
1337 if {
1338 [info exists ::env(TEMP)] && [file isdirectory $::env(TEMP)] &&
1339 [file dirname $fn] ne [file normalize $::env(TEMP)]
1340 } {
1341 set fn [file join $::env(TEMP)/test-owner-from-tcl.txt]
1342 set fn [makeFile "data" test-owner-from-tcl.txt $::env(TEMP)]
1343 }
1344 # be sure we have really owned this file before trying to check that
1345 # (avoid dependency on admin with UAC and the setting "System objects:
1346 # Default owner for objects created by members of the Administrators group"):
1347 catch {
1348 exec takeown /F [file nativename $fn]
1349 }
1350 } -body {
1351 file owned $fn
1352 } -cleanup {
1353 if {$fn ne $gorpfile} {
1354 removeFile $fn
1355 }
1356 } -result 1
1357 test cmdAH-25.2.1 {Tcl_FileObjCmd: owned} -constraints unix -setup {
1358 # Avoid problems with AFS
1359 set tmpfile [makeFile "data" touch.me /tmp]
1360 } -body {
1361 file owned $tmpfile
1362 } -cleanup {
1363 removeFile touch.me /tmp
1364 } -result 1
1365 test cmdAH-25.3 {Tcl_FileObjCmd: owned} {unix notRoot} {
1366 file owned /
1367 } 0
1368 test cmdAH-25.3.1 {Tcl_FileObjCmd: owned} -constraints win -body {
1369 if {[info exists env(SystemRoot)]} {
1370 file owned $env(SystemRoot)
1371 } else {
1372 file owned $env(windir)
1373 }
1374 } -result 0
1375 test cmdAH-25.4 {Tcl_FileObjCmd: owned} -body {
1376 file owned nosuchfile
1377 } -result 0
1378
1379 # readlink
1380 test cmdAH-26.1 {Tcl_FileObjCmd: readlink} -returnCodes error -body {
1381 file readlink a b
1382 } -result {wrong # args: should be "file readlink name"}
1383 test cmdAH-26.2 {Tcl_FileObjCmd: readlink} {unix nonPortable} {
1384 file readlink $linkfile
1385 } $gorpfile
1386 test cmdAH-26.3 {Tcl_FileObjCmd: readlink errors} {unix nonPortable} {
1387 list [catch {file readlink _bogus_} msg] [string tolower $msg] $errorCode
1388 } {1 {could not readlink "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1389 test cmdAH-26.5 {Tcl_FileObjCmd: readlink errors} {win nonPortable} {
1390 list [catch {file readlink _bogus_} msg] [string tolower $msg] $errorCode
1391 } {1 {could not readlink "_bogus_": invalid argument} {POSIX EINVAL {invalid argument}}}
1392
1393 # size
1394 test cmdAH-27.1 {Tcl_FileObjCmd: size} -returnCodes error -body {
1395 file size a b
1396 } -result {wrong # args: should be "file size name"}
1397 test cmdAH-27.2 {Tcl_FileObjCmd: size} {
1398 set oldsize [file size $gorpfile]
1399 set f [open $gorpfile a]
1400 fconfigure $f -translation lf -eofchar {}
1401 puts $f "More text"
1402 close $f
1403 expr {[file size $gorpfile] - $oldsize}
1404 } {10}
1405 test cmdAH-27.3 {Tcl_FileObjCmd: size} {
1406 list [catch {file size _bogus_} msg] [string tolower $msg] $errorCode
1407 } {1 {could not read "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1408 test cmdAH-27.4 {
1409 Tcl_FileObjCmd: size (built-in Windows names)
1410 } -constraints {win} -body {
1411 file size con
1412 } -result 0
1413 test cmdAH-27.4.1 {
1414 Tcl_FileObjCmd: size (built-in Windows names with dir path and extension)
1415 } -constraints {win} -body {
1416 try {
1417 set res [file size [file join [temporaryDirectory] con.txt]]
1418 } trap {POSIX ENOENT} {} {
1419 set res 0
1420 }
1421 set res
1422 } -result 0
1423
1424 catch {testsetplatform $platform}
1425 removeFile $gorpfile
1426 set gorpfile [makeFile "Test string" g\xF3rp.file]
1427 catch {file attributes $gorpfile -permissions 0o765}
1428
1429 # stat
1430 test cmdAH-28.1 {Tcl_FileObjCmd: stat} -returnCodes error -body {
1431 file stat _bogus_
1432 } -result {wrong # args: should be "file stat name varName"}
1433 test cmdAH-28.2 {Tcl_FileObjCmd: stat} -returnCodes error -body {
1434 file stat _bogus_ a b
1435 } -result {wrong # args: should be "file stat name varName"}
1436 test cmdAH-28.3 {Tcl_FileObjCmd: stat} -setup {
1437 unset -nocomplain stat
1438 set stat(blocks) [set stat(blksize) {}]
1439 } -body {
1440 file stat $gorpfile stat
1441 unset stat(blocks) stat(blksize); # Ignore these fields; not always set
1442 lsort [array names stat]
1443 } -result {atime ctime dev gid ino mode mtime nlink size type uid}
1444 test cmdAH-28.4 {Tcl_FileObjCmd: stat} -setup {
1445 unset -nocomplain stat
1446 } -body {
1447 file stat $gorpfile stat
1448 list $stat(nlink) $stat(size) $stat(type)
1449 } -result {1 12 file}
1450 test cmdAH-28.5 {Tcl_FileObjCmd: stat} -constraints {unix notWsl} -setup {
1451 unset -nocomplain stat
1452 } -body {
1453 file stat $gorpfile stat
1454 format 0o%03o [expr {$stat(mode) & 0o777}]
1455 } -result 0o765
1456 test cmdAH-28.6 {Tcl_FileObjCmd: stat} {
1457 list [catch {file stat _bogus_ stat} msg] [string tolower $msg] $errorCode
1458 } {1 {could not read "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1459 test cmdAH-28.7 {Tcl_FileObjCmd: stat} -setup {
1460 unset -nocomplain x
1461 } -returnCodes error -body {
1462 set x 44
1463 file stat $gorpfile x
1464 } -result {can't set "x(dev)": variable isn't array}
1465 test cmdAH-28.8 {Tcl_FileObjCmd: stat} -setup {
1466 set filename [makeFile "" foo.text]
1467 } -body {
1468 # Sign extension of purported unsigned short to int.
1469 file stat $filename stat
1470 expr {$stat(mode) > 0}
1471 } -cleanup {
1472 removeFile $filename
1473 } -result 1
1474 test cmdAH-28.9 {Tcl_FileObjCmd: stat} win {
1475 # stat of root directory was failing. Don't care about answer, just that
1476 # test runs. Relative paths that resolve to root
1477 set old [pwd]
1478 cd c:/
1479 file stat c: stat
1480 file stat c:. stat
1481 file stat . stat
1482 cd $old
1483 file stat / stat
1484 file stat c:/ stat
1485 file stat c:/. stat
1486 } {}
1487 test cmdAH-28.10 {Tcl_FileObjCmd: stat} {win nonPortable} {
1488 # stat of root directory was failing. Don't care about answer, just that
1489 # test runs.
1490 file stat //pop/$env(USERNAME) stat
1491 file stat //pop/$env(USERNAME)/ stat
1492 file stat //pop/$env(USERNAME)/. stat
1493 } {}
1494 test cmdAH-28.11 {Tcl_FileObjCmd: stat} -setup {
1495 set old [pwd]
1496 } -constraints {win nonPortable} -body {
1497 # stat of network directory was returning id of current local drive.
1498 cd c:/
1499 file stat //pop/$env(USERNAME) stat
1500 expr {$stat(dev) == 2}
1501 } -cleanup {
1502 cd $old
1503 } -result 0
1504 test cmdAH-28.12 {Tcl_FileObjCmd: stat} -setup {
1505 set filename [makeFile "" foo.test]
1506 } -body {
1507 # stat(mode) with S_IFREG flag was returned as a negative number if mode_t
1508 # was a short instead of an unsigned short.
1509 file stat $filename stat
1510 expr {$stat(mode) > 0}
1511 } -cleanup {
1512 removeFile $filename
1513 } -result 1
1514 test cmdAH-28.13 {Tcl_FileObjCmd: stat (built-in Windows names)} -constraints {win} -setup {
1515 unset -nocomplain stat
1516 } -body {
1517 file stat con stat
1518 lmap elem {atime ctime dev gid ino mode mtime nlink size type uid} {set stat($elem)}
1519 } -result {0 0 -1 0 0 8630 0 0 0 characterSpecial 0}
1520 test cmdAH-28.13.1 {Tcl_FileObjCmd: stat (built-in Windows names)} -constraints {win} -setup {
1521 unset -nocomplain stat
1522 } -body {
1523 try {
1524 file stat [file join [temporaryDirectory] CON.txt] stat
1525 set res [lmap elem {atime ctime dev gid ino mode mtime nlink size type uid} {set stat($elem)}]
1526 } trap {POSIX ENOENT} {} {
1527 set res {0 0 -1 0 0 8630 0 0 0 characterSpecial 0}
1528 }
1529 set res
1530 } -result {0 0 -1 0 0 8630 0 0 0 characterSpecial 0}
1531 unset -nocomplain stat
1532
1533 # type
1534 test cmdAH-29.1 {Tcl_FileObjCmd: type} -returnCodes error -body {
1535 file type a b
1536 } -result {wrong # args: should be "file type name"}
1537 test cmdAH-29.2 {Tcl_FileObjCmd: type} {
1538 file type $dirfile
1539 } directory
1540 test cmdAH-29.3.0 {Tcl_FileObjCmd: delete removes link not file} {unix nonPortable} {
1541 set exists [list [file exists $linkfile] [file exists $gorpfile]]
1542 file delete $linkfile
1543 set exists2 [list [file exists $linkfile] [file exists $gorpfile]]
1544 list $exists $exists2
1545 } {{1 1} {0 1}}
1546 test cmdAH-29.3 {Tcl_FileObjCmd: type} {
1547 file type $gorpfile
1548 } file
1549 test cmdAH-29.4 {Tcl_FileObjCmd: type} -constraints {unix} -setup {
1550 catch {file delete $linkfile}
1551 } -body {
1552 # Unlike [exec ln -s], [file link] requires an existing target
1553 file link -symbolic $linkfile $gorpfile
1554 file type $linkfile
1555 } -cleanup {
1556 file delete $linkfile
1557 } -result link
1558 test cmdAH-29.4.1 {Tcl_FileObjCmd: type} -constraints {linkDirectory} -setup {
1559 set tempdir [makeDirectory temp]
1560 } -body {
1561 set linkdir [file join [temporaryDirectory] link.dir]
1562 file link -symbolic $linkdir $tempdir
1563 file type $linkdir
1564 } -cleanup {
1565 file delete $linkdir
1566 removeDirectory $tempdir
1567 } -result link
1568 test cmdAH-29.5 {Tcl_FileObjCmd: type} {
1569 list [catch {file type _bogus_} msg] [string tolower $msg] $errorCode
1570 } {1 {could not read "_bogus_": no such file or directory} {POSIX ENOENT {no such file or directory}}}
1571 test cmdAH-29.6 {
1572 Tcl_FileObjCmd: type (built-in Windows names)
1573 } -constraints {win} -body {
1574 file type con
1575 } -result "characterSpecial"
1576 test cmdAH-29.6.1 {
1577 Tcl_FileObjCmd: type (built-in Windows names, with dir path and extension)
1578 } -constraints {win} -body {
1579 try {
1580 set res [file type [file join [temporaryDirectory] CON.txt]]
1581 } trap {POSIX ENOENT} {} {
1582 set res {characterSpecial}
1583 }
1584 set res
1585 } -result "characterSpecial"
1586
1587 # Error conditions
1588 test cmdAH-30.1 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1589 file gorp x
1590 } -result {unknown or ambiguous subcommand "gorp": must be atime, attributes, channels, copy, delete, dirname, executable, exists, extension, isdirectory, isfile, join, link, lstat, mkdir, mtime, nativename, normalize, owned, pathtype, readable, readlink, rename, rootname, separator, size, split, stat, system, tail, tempfile, type, volumes, or writable}
1591 test cmdAH-30.2 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1592 file ex x
1593 } -match glob -result {unknown or ambiguous subcommand "ex": must be *}
1594 test cmdAH-30.3 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1595 file is x
1596 } -match glob -result {unknown or ambiguous subcommand "is": must be *}
1597 test cmdAH-30.4 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1598 file z x
1599 } -match glob -result {unknown or ambiguous subcommand "z": must be *}
1600 test cmdAH-30.5 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1601 file read x
1602 } -match glob -result {unknown or ambiguous subcommand "read": must be *}
1603 test cmdAH-30.6 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1604 file s x
1605 } -match glob -result {unknown or ambiguous subcommand "s": must be *}
1606 test cmdAH-30.7 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1607 file t x
1608 } -match glob -result {unknown or ambiguous subcommand "t": must be *}
1609 test cmdAH-30.8 {Tcl_FileObjCmd: error conditions} -returnCodes error -body {
1610 file dirname ~woohgy
1611 } -result {user "woohgy" doesn't exist}
1612
1613 # channels
1614 # In testing 'file channels', we need to make sure that a channel created in
1615 # one interp isn't visible in another.
1616
1617 interp create simpleInterp
1618 interp create -safe safeInterp
1619 interp create
1620 catch {safeInterp expose file file}
1621
1622 test cmdAH-31.1 {Tcl_FileObjCmd: channels, too many args} -body {
1623 file channels a b
1624 } -returnCodes error -result {wrong # args: should be "file channels ?pattern?"}
1625 test cmdAH-31.2 {Tcl_FileObjCmd: channels, too many args} {
1626 # Normal interps start out with only the standard channels
1627 lsort [simpleInterp eval [list file chan]]
1628 } {stderr stdin stdout}
1629 test cmdAH-31.3 {Tcl_FileObjCmd: channels, globbing} {
1630 string equal [file channels] [file channels *]
1631 } {1}
1632 test cmdAH-31.4 {Tcl_FileObjCmd: channels, globbing} {
1633 lsort [file channels std*]
1634 } {stderr stdin stdout}
1635 set newFileId [open $gorpfile w]
1636 test cmdAH-31.5 {Tcl_FileObjCmd: channels} {
1637 set res [file channels $newFileId]
1638 string equal $newFileId $res
1639 } {1}
1640 test cmdAH-31.6 {Tcl_FileObjCmd: channels in other interp} {
1641 # Safe interps start out with no channels
1642 safeInterp eval [list file channels]
1643 } {}
1644 test cmdAH-31.7 {Tcl_FileObjCmd: channels in other interp} -body {
1645 safeInterp eval [list puts $newFileId "hello"]
1646 } -returnCodes error -result "can not find channel named \"$newFileId\""
1647 interp share {} $newFileId safeInterp
1648 interp share {} stdout safeInterp
1649 test cmdAH-31.8 {Tcl_FileObjCmd: channels in other interp} {
1650 # $newFileId should now be visible in both interps
1651 list [file channels $newFileId] \
1652 [safeInterp eval [list file channels $newFileId]]
1653 } [list $newFileId $newFileId]
1654 test cmdAH-31.9 {Tcl_FileObjCmd: channels in other interp} {
1655 lsort [safeInterp eval [list file channels]]
1656 } [lsort [list stdout $newFileId]]
1657 test cmdAH-31.10 {Tcl_FileObjCmd: channels in other interp} {
1658 # we can now write to $newFileId from child
1659 safeInterp eval [list puts $newFileId "hello"]
1660 } {}
1661 interp transfer {} $newFileId safeInterp
1662 test cmdAH-31.11 {Tcl_FileObjCmd: channels in other interp} {
1663 # $newFileId should now be visible only in safeInterp
1664 list [file channels $newFileId] \
1665 [safeInterp eval [list file channels $newFileId]]
1666 } [list {} $newFileId]
1667 test cmdAH-31.12 {Tcl_FileObjCmd: channels in other interp} {
1668 lsort [safeInterp eval [list file channels]]
1669 } [lsort [list stdout $newFileId]]
1670 test cmdAH-31.13 {Tcl_FileObjCmd: channels in other interp} {
1671 safeInterp eval [list close $newFileId]
1672 safeInterp eval [list file channels]
1673 } {stdout}
1674
1675 # Temp files (TIP#210)
1676 test cmdAH-32.1 {file tempfile - usage} -returnCodes error -body {
1677 file tempfile a b c
1678 } -result {wrong # args: should be "file tempfile ?nameVar? ?template?"}
1679 test cmdAH-32.2 {file tempfile - returns a read/write channel} -body {
1680 set f [file tempfile]
1681 puts $f ok
1682 seek $f 0
1683 gets $f
1684 } -cleanup {
1685 catch {close $f}
1686 } -result ok
1687 test cmdAH-32.3 {file tempfile - makes filenames} -setup {
1688 unset -nocomplain name
1689 } -body {
1690 set result [info exists name]
1691 set f [file tempfile name]
1692 lappend result [info exists name] [file exists $name]
1693 close $f
1694 lappend result [file exists $name]
1695 } -cleanup {
1696 catch {close $f}
1697 catch {file delete $name}
1698 } -result {0 1 1 1}
1699 # We try to obey the template on Unix, but don't (currently) bother on Win
1700 test cmdAH-32.4 {file tempfile - templates} -constraints unix -body {
1701 close [file tempfile name foo]
1702 expr {[string match foo* [file tail $name]] ? "ok" : "foo produced $name"}
1703 } -cleanup {
1704 catch {file delete $name}
1705 } -result ok
1706 test cmdAH-32.5 {file tempfile - templates} -constraints unix -body {
1707 set template [file join $dirfile foo]
1708 close [file tempfile name $template]
1709 expr {[string match $template* $name] ? "ok" : "$template produced $name"}
1710 } -cleanup {
1711 catch {file delete $name}
1712 } -result ok
1713 # Not portable; not all Unix systems have mkstemps()
1714 test cmdAH-32.6 {file tempfile - templates} -body {
1715 set template [file join $dirfile foo]
1716 close [file tempfile name $template.bar]
1717 expr {[string match $template*.bar $name] ? "ok" :
1718 "$template.bar produced $name"}
1719 } -constraints {unix nonPortable} -cleanup {
1720 catch {file delete $name}
1721 } -result ok
1722
1723 # This shouldn't work, but just in case a test above failed...
1724 catch {close $newFileId}
1725
1726 interp delete safeInterp
1727 interp delete simpleInterp
1728
1729 # cleanup
1730 catch {testsetplatform $platform}
1731 unset -nocomplain platform
1732
1733 # Tcl_ForObjCmd is tested in for.test
1734
1735 catch {file attributes $dirfile -permissions 0o777}
1736 removeDirectory $dirfile
1737 removeFile $gorpfile
1738 # No idea how well [removeFile] copes with links...
1739 file delete $linkfile
1740
1741 cd $cmdAHwd
1742
1743 ::tcltest::cleanupTests
1744 return
1745
1746 # Local Variables:
1747 # mode: tcl
1748 # End: