File: fileutil.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (499 lines) | stat: -rw-r--r-- 13,323 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# -*- tcl -*-
# Tests for the find function.
#
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
# Copyright (c) 2001 by ActiveState Tool Corp.
# Copyright (c) 2005-2013 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: fileutil.test,v 1.56 2009/10/06 20:07:18 andreas_kupries Exp $

# TODO: Bug [8b317b4a63]: Create test cases for this bug. This
# requires the use of a custom VFS as the native filesystem does not
# contain the bug we are guarding ourselves against.

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5 ;# See fumagic.testsupport
testsNeedTcltest 1.0

support {
    useTcllibFile fumagic/fumagic.testsupport ;# 8.5+ (dict, apply)
    use           cmdline/cmdline.tcl cmdline
}
testing {
    useLocal fileutil.tcl fileutil
}

# -------------------------------------------------------------------------

proc gt_setup {} {
    global tcl_platform gt gtfa gtfb

    set gt   [makeDirectory grepTest]
    set gtfa [makeFile "zoop" [file join $gt {file [1]}]]
    set gtfb {}

    if {[string equal $::tcl_platform(platform) windows]} return

    set gtfb [makeFile "zoo\nbart" [file join $gt {file* 2}]]
    return
}

proc gt_cleanup {} {
    removeDirectory grepTest


    rename gt_setup   {}
    rename gt_cleanup {}
    unset  ::gt ::gtfa ::gtfb
    return
}

# -------------------------------------------------------------------------

gt_setup

test grep-1.1 {normal grep} {macOrUnix} {
    lsort [fileutil::grep "zoo" [glob [file join $gt *]]]
} [list "$gtfa:1:zoop" "$gtfb:1:zoo"]

test grep-1.2 {more restrictive grep} {
    lsort [fileutil::grep "zoo." [glob [file join $gt *]]]
} [list "$gtfa:1:zoop"]

test grep-1.3 {more restrictive grep} {macOrUnix} {
    lsort [fileutil::grep "bar" [glob [file join $gt *]]]
} [list "$gtfb:2:bart"]

gt_cleanup

# -------------------------------------------------------------------------

test foreachline-1.0 {foreachLine} {
    set path [makeFile "foo\nbar\nbaz\n" {cat [1]}]

    set res ""
    ::fileutil::foreachLine line $path {
	append res /$line
    }

    removeFile {cat [1]}
    set res
} {/foo/bar/baz}

# -------------------------------------------------------------------------

proc t_setup {} {
    global tt

    set tt [makeDirectory touchTest]
    makeFile "blah" [file join touchTest {file [1]}]
}

proc t_cleanup {} {
    removeDirectory touchTest
    rename t_setup   {}
    rename t_cleanup {}
    unset ::tt
    catch { unset ::a1 }
    catch { unset ::m1}
    catch { unset ::a2}
    catch { unset ::m2}
    catch { unset ::f}
    catch { unset ::r}
    return
}

# -------------------------------------------------------------------------

t_setup

test touch-1.1 {create file} tcl8.3plus {
    set f [file join $tt here]
    fileutil::touch $f
    file exists $f
} 1

test touch-1.2 {'-c' prevents file creation} tcl8.3plus {
    set f [file join $tt nothere]
    fileutil::touch -c $f
    file exists $f
} 0

test touch-1.3 {'-c' has no effect on existing files} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch -c $f
    file exists $f
} 1

test touch-1.4 {test relative times} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    after 1001
    fileutil::touch $f
    set a2 [file atime $f]
    set m2 [file mtime $f]
    list [expr {$a1 == $m1}] [expr {$a2 == $m2}] [expr {$a1 < $a2}] [expr {$m1 < $m2}]
} {1 1 1 1}

test touch-1.5 {test relative times using -a} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    after 1001
    fileutil::touch -a $f
    set a2 [file atime $f]
    set m2 [file mtime $f]
    list [expr {$a1 == $m1}] [expr {$a2 == $m2}] [expr {$a1 < $a2}] [expr {$m1 < $m2}]
} {1 0 1 0}

test touch-1.6 {test relative times using -m} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    after 1001
    fileutil::touch -m $f
    set a2 [file atime $f]
    set m2 [file mtime $f]
    list [expr {$a1 == $m1}] [expr {$a2 == $m2}] [expr {$a1 < $a2}] [expr {$m1 < $m2}]
} {1 0 0 1}

test touch-1.7 {test relative times using -a and -m} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    after 1001
    fileutil::touch -a -m $f
    set a2 [file atime $f]
    set m2 [file mtime $f]
    list [expr {$a1 == $m1}] [expr {$a2 == $m2}] [expr {$a1 < $a2}] [expr {$m1 < $m2}]
} {1 1 1 1}

test touch-1.8 {test -t} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -t 42 $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == 42}] [expr {$m1 == 42}]
} {1 1}

test touch-1.9 {test -t with -a} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -t 42 -a $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == 42}] [expr {$m1 == 42}]
} [list 1 0]

test touch-1.10 {test -t with -m} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -t 42 -m $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == 42}] [expr {$m1 == 42}]
} [list 0 1]

test touch-1.11 {test -t with -a and -m} tcl8.3plus {
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -t 42 -a -m $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == 42}] [expr {$m1 == 42}]
} {1 1}

test touch-1.12 {test -r} tcl8.3plus {
    set r [info script]
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -r $r $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == [file atime $r]}] [expr {$m1 == [file mtime $r]}]
} {1 1}

test touch-1.13 {test -r with -a} tcl8.3plus {
    set r [info script]
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -r $r -a $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == [file atime $r]}] [expr {$m1 == [file mtime $r]}]
} {1 0}

test touch-1.14 {test -r with -m} tcl8.3plus {
    set r [info script]
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -r $r -m $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == [file atime $r]}] [expr {$m1 == [file mtime $r]}]
} {0 1}

test touch-1.15 {test -r with -a and -m} tcl8.3plus {
    set r [info script]
    set f [file join $tt {file [1]}]
    fileutil::touch $f
    after 1001
    fileutil::touch -r $r -m -a $f
    set a1 [file atime $f]
    set m1 [file mtime $f]
    list [expr {$a1 == [file atime $r]}] [expr {$m1 == [file mtime $r]}]
} {1 1}

t_cleanup

# ----------------------------------------------------------------

proc i_setup {} {
    global tcl_platform
    makeDirectory installDst
    makeDirectory installSrc

    makeDirectory   [file join installSrc subdir]
    makeFile "blah" [file join installSrc {file [1]}]

    # Make a second subdirectory to install, unix-only
    if {$tcl_platform(platform) != "unix" } return

    makeDirectory   [file join installSrc subdir2]
    makeFile "blah" [file join installSrc subdir subfile1]
    makeFile "blah" [file join installSrc subdir subfile2]
    makeFile "blah" [file join installSrc subdir subfile3]

    foreach fl {1 2 3} {
	set fn [file join installSrc subdir2 subfile$fl]
	makeFile "blah" $fn
	    
	# Give it some "bad" permissions.
	file attributes $fn -permissions 0600
    }
    return
}

proc i_cleanup {} {
    removeDirectory installDst
    removeDirectory installSrc

    rename i_setup   {}
    rename i_cleanup {}
    return
}

# ----------------------------------------------------------------

i_setup

test install-1.1 {install a file} {
    fileutil::install [file join installSrc {file [1]}] installDst
    file exists       [file join installDst {file [1]}]
} {1}

makeDirectory installDst

test install-2.1 {install a directory} {tcl8.4plus} {
    list [catch {
        fileutil::install                         [file join installSrc subdir] installDst
        set result [lsort [glob -tails -directory [file join installDst subdir] [file join . / *]]]
        file delete -force installDst
        set result
    } err] $err
} {0 {subfile1 subfile2 subfile3}}

makeDirectory installDst

test install-2.2 {install a directory} {tcl8.3plus} {
    list [catch {
        fileutil::install                  [file join installSrc subdir] installDst
        set result [lsort [glob -directory [file join installDst subdir] [file join . / *]]]
        file delete -force installDst
        set result
    } err] $err
} {0 {installDst/subdir/subfile1 installDst/subdir/subfile2 installDst/subdir/subfile3}}

makeDirectory installDst

test install-3.1 {install a directory, set permissions} {unix tcl8.3plus} {
    set res {}
    fileutil::install -m go+rw [file join installSrc subdir2] installDst
    foreach fl [glob [file join installDst subdir2 *]] {
	append res [file attributes $fl -permissions]
    }
    set res
} {006660066600666}

i_cleanup

# -------------------------------------------------------------------------

proc tmp_setup {} {
    global xpath res

    # Set up an exclusive directory to search. This cannot be unset,
    # hence the location of these tests after the regular
    # tempdir/tempfile tests.

    removeDirectory x
    set xpath [makeDirectory x]
    set res {}
    removeDirectory x
    return
}

proc tmp_cleanup {} {
    rename tmp_setup {}
    rename tmp_cleanup {}
    removeDirectory x
    unset ::xpath
    unset ::res
    return
}

# -------------------------------------------------------------------------

tmp_setup

test tempdir-1.1 {return the correct directorary for temporary files} {unix} {
    set ::env(TMPDIR) [pwd] ;# Most high-priority source, and existing directory!
    set res [::fileutil::tempdir]
    unset ::env(TMPDIR)
    set res
} [pwd]

test tempdir-1.2 {return the correct directorary for temporary files} {unix} {
    catch {unset ::env(TMPDIR)}
    catch {unset ::env(TEMPDIR)}
    catch {unset ::env(TMP)}
    catch {unset ::env(TEMP)}
    ::fileutil::tempdir
} {/tmp}

test tempfile-1.1 {generate temporary file name and file} {
    set filename [::fileutil::tempfile]
    set res [file exists $filename]
    file delete $filename
    unset filename
    set res
} {1}

test tempfile-1.2 {generate writable temporary file name} {
    set filename [::fileutil::tempfile]
    set res [file writable $filename]
    file delete $filename
    unset filename
    set res
} {1}

test tempfile-1.3 {generate 100 unique temporary filenames} {
    set filenames [list]
    for {set i 0} {$i<100} {incr i} {
	lappend filenames [::fileutil::tempfile]
    }
    foreach f $filenames {
	file delete $f
    }
    set i
} {100}

test tempdir-1.3 {tempdir, user-specified, bad} {
    catch {::fileutil::tempdir x y} msg
    set msg
} {wrong#args: should be "::fileutil::tempdir ?path?"}

test tempdir-1.4 {tempdir, user-specified, bad} {
    ::fileutil::tempdir [makeDirectory x]
    removeDirectory x

    catch {::fileutil::tempdir} msg
    removeDirectory x

    lindex [split $msg \n] 0 ; # First line only.
} {Unable to determine a proper directory for temporary files}

test tempdir-1.5 {tempdir, user-specified, ok} {
    ::fileutil::tempdir [makeDirectory x]

    set res [::fileutil::tempdir]
    removeDirectory x
    set res
} $xpath

test tempfile-1.4 {temp file in user specified directory} {
    ::fileutil::tempdir [makeDirectory x]

    set          filename [::fileutil::tempfile TEST]
    file delete $filename
    ::fileutil::tempdirReset

    removeDirectory x
    string match $xpath/TEST* $filename
} 1

# -------------------------------------------------------------------------

test maketempdir-1.1 {generate temporary directory} {
    set filename [::fileutil::maketempdir]
    set res [file exists $filename]
    file delete $filename
    unset filename
    set res
} {1}

test maketempdir-1.2 {generate writable temporary directory} {
    set filename [::fileutil::maketempdir]
    set res [file writable $filename]
    file delete $filename
    unset filename
    set res
} {1}

test maketempdir-1.3 {generate 100 unique temporary directories} {
    set filenames [list]
    for {set i 0} {$i<100} {incr i} {
	lappend filenames [::fileutil::maketempdir]
    }
    foreach f $filenames {
	file delete $f
    }
    set i
} {100}

test maketempdir-1.4 {temp directory in user specified directory} {
    set          filename [::fileutil::maketempdir -dir $xpath -prefix TEST]
    file delete $filename
    string match $xpath/TEST* $filename
} 1

# -------------------------------------------------------------------------

tmp_cleanup

# -------------------------------------------------------------------------

testsuiteCleanup
return