fileutils2 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ require 'fileutils2'
2
+
3
+ verbose = $VERBOSE
4
+ begin
5
+ $VERBOSE = false
6
+ FileUtils = ::FileUtils2
7
+ ensure
8
+ $VERBOSE = verbose
9
+ end
10
+
@@ -0,0 +1,91 @@
1
+ require 'fileutils2'
2
+ require 'fileutils2/fileasserts'
3
+ require 'tmpdir'
4
+ require 'test/unit'
5
+
6
+ class TestFileUtils2 < Test::Unit::TestCase
7
+ end
8
+
9
+ module TestFileUtils2::Clobber
10
+ include Test::Unit::FileAssertions
11
+
12
+ def my_rm_rf(path)
13
+ if File.exist?('/bin/rm')
14
+ system %Q[/bin/rm -rf "#{path}"]
15
+ else
16
+ FileUtils.rm_rf path
17
+ end
18
+ end
19
+
20
+ SRC = 'data/src'
21
+ COPY = 'data/copy'
22
+
23
+ def setup
24
+ @prevdir = Dir.pwd
25
+ class << (@fileutils_output = "")
26
+ alias puts <<
27
+ end
28
+ tmproot = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
29
+ Dir.mkdir tmproot unless File.directory?(tmproot)
30
+ Dir.chdir tmproot
31
+ my_rm_rf 'data'; Dir.mkdir 'data'
32
+ my_rm_rf 'tmp'; Dir.mkdir 'tmp'
33
+ File.open(SRC, 'w') {|f| f.puts 'dummy' }
34
+ File.open(COPY, 'w') {|f| f.puts 'dummy' }
35
+ end
36
+
37
+ def teardown
38
+ tmproot = Dir.pwd
39
+ Dir.chdir @prevdir
40
+ my_rm_rf tmproot
41
+ end
42
+
43
+ def test_cp
44
+ cp SRC, 'tmp/cp'
45
+ check 'tmp/cp'
46
+ end
47
+
48
+ def test_mv
49
+ mv SRC, 'tmp/mv'
50
+ check 'tmp/mv'
51
+ end
52
+
53
+ def check(dest, message=nil)
54
+ assert_file_not_exist dest, message
55
+ assert_file_exist SRC, message
56
+ assert_same_file SRC, COPY, message
57
+ end
58
+
59
+ def test_rm
60
+ rm SRC
61
+ assert_file_exist SRC
62
+ assert_same_file SRC, COPY
63
+ end
64
+
65
+ def test_rm_f
66
+ rm_f SRC
67
+ assert_file_exist SRC
68
+ assert_same_file SRC, COPY
69
+ end
70
+
71
+ def test_rm_rf
72
+ rm_rf SRC
73
+ assert_file_exist SRC
74
+ assert_same_file SRC, COPY
75
+ end
76
+
77
+ def test_mkdir
78
+ mkdir 'dir'
79
+ assert_file_not_exist 'dir'
80
+ end
81
+
82
+ def test_mkdir_p
83
+ mkdir 'dir/dir/dir'
84
+ assert_file_not_exist 'dir'
85
+ end
86
+
87
+ def test_copy_entry
88
+ copy_entry SRC, 'tmp/copy_entry'
89
+ check 'tmp/copy_entry', bug4331 = '[ruby-dev:43129]'
90
+ end
91
+ end
@@ -0,0 +1,72 @@
1
+ # $Id$
2
+
3
+ module Test
4
+ module Unit
5
+ module FileAssertions
6
+ def assert_same_file(from, to, message=nil)
7
+ assert_equal(File.read(from), File.read(to), "file #{from} != #{to}#{message&&': '}#{message||''}")
8
+ end
9
+
10
+ def assert_same_entry(from, to, message=nil)
11
+ a = File.stat(from)
12
+ b = File.stat(to)
13
+ msg = "#{message&&': '}#{message||''}"
14
+ assert_equal a.mode, b.mode, "mode #{a.mode} != #{b.mode}#{msg}"
15
+ #assert_equal a.atime, b.atime
16
+ assert_equal_timestamp a.mtime, b.mtime, "mtime #{a.mtime} != #{b.mtime}#{msg}"
17
+ assert_equal a.uid, b.uid, "uid #{a.uid} != #{b.uid}#{msg}"
18
+ assert_equal a.gid, b.gid, "gid #{a.gid} != #{b.gid}#{msg}"
19
+ end
20
+
21
+ def assert_file_exist(path, message=nil)
22
+ assert(File.exist?(path), "file not exist: #{path}#{message&&': '}#{message||''}")
23
+ end
24
+
25
+ def assert_file_not_exist(path, message=nil)
26
+ assert(!File.exist?(path), "file exist: #{path}#{message&&': '}#{message||''}")
27
+ end
28
+
29
+ def assert_directory(path, message=nil)
30
+ assert(File.directory?(path), "is not directory: #{path}#{message&&': '}#{message||''}")
31
+ end
32
+
33
+ def assert_symlink(path, message=nil)
34
+ assert(File.symlink?(path), "is not a symlink: #{path}#{message&&': '}#{message||''}")
35
+ end
36
+
37
+ def assert_not_symlink(path)
38
+ assert(!File.symlink?(path), "is a symlink: #{path}#{message&&': '}#{message||''}")
39
+ end
40
+
41
+ def assert_equal_time(expected, actual, message=nil)
42
+ expected_str = expected.to_s
43
+ actual_str = actual.to_s
44
+ if expected_str == actual_str
45
+ expected_str << " (nsec=#{expected.nsec})"
46
+ actual_str << " (nsec=#{actual.nsec})"
47
+ end
48
+ full_message = build_message(message, <<EOT)
49
+ <#{expected_str}> expected but was
50
+ <#{actual_str}>.
51
+ EOT
52
+ assert_equal(expected, actual, full_message)
53
+ end
54
+
55
+ def assert_equal_timestamp(expected, actual, message=nil)
56
+ expected_str = expected.to_s
57
+ actual_str = actual.to_s
58
+ if expected_str == actual_str
59
+ expected_str << " (nsec=#{expected.nsec})"
60
+ actual_str << " (nsec=#{actual.nsec})"
61
+ end
62
+ full_message = build_message(message, <<EOT)
63
+ <#{expected_str}> expected but was
64
+ <#{actual_str}>.
65
+ EOT
66
+ # subsecond timestamp is not portable.
67
+ assert_equal(expected.tv_sec, actual.tv_sec, full_message)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ # $Id$
2
+
3
+ require 'fileutils2'
4
+ require 'fileutils2/visibility_tests'
5
+ require 'fileutils2/clobber'
6
+ require 'test/unit'
7
+
8
+ class TestFileUtils2DryRun < Test::Unit::TestCase
9
+
10
+ include FileUtils2::DryRun
11
+ include TestFileUtils2::Clobber
12
+ include TestFileUtils2::Visibility
13
+
14
+ def setup
15
+ super
16
+ @fu_module = FileUtils2::DryRun
17
+ end
18
+
19
+ end
@@ -0,0 +1,1252 @@
1
+ # $Id$
2
+
3
+ require 'fileutils2'
4
+ require 'fileutils2/fileasserts'
5
+
6
+ require 'pathname'
7
+ require 'tmpdir'
8
+ require 'test/unit'
9
+
10
+ class TestFileUtils2 < Test::Unit::TestCase
11
+ TMPROOT = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
12
+ include Test::Unit::FileAssertions
13
+ end
14
+
15
+ prevdir = Dir.pwd
16
+ tmproot = TestFileUtils2::TMPROOT
17
+ Dir.mkdir tmproot unless File.directory?(tmproot)
18
+ Dir.chdir tmproot
19
+
20
+ def have_drive_letter?
21
+ /mswin(?!ce)|mingw|bcc|emx/ =~ RUBY_PLATFORM
22
+ end
23
+
24
+ def have_file_perm?
25
+ /mswin|mingw|bcc|emx/ !~ RUBY_PLATFORM
26
+ end
27
+
28
+ $fileutils_rb_have_symlink = nil
29
+
30
+ def have_symlink?
31
+ if $fileutils_rb_have_symlink == nil
32
+ $fileutils_rb_have_symlink = check_have_symlink?
33
+ end
34
+ $fileutils_rb_have_symlink
35
+ end
36
+
37
+ def check_have_symlink?
38
+ File.symlink nil, nil
39
+ rescue NotImplementedError
40
+ return false
41
+ rescue
42
+ return true
43
+ end
44
+
45
+ $fileutils_rb_have_hardlink = nil
46
+
47
+ def have_hardlink?
48
+ if $fileutils_rb_have_hardlink == nil
49
+ $fileutils_rb_have_hardlink = check_have_hardlink?
50
+ end
51
+ $fileutils_rb_have_hardlink
52
+ end
53
+
54
+ def check_have_hardlink?
55
+ File.link nil, nil
56
+ rescue NotImplementedError
57
+ return false
58
+ rescue
59
+ return true
60
+ end
61
+
62
+ begin
63
+ Dir.mkdir("\n")
64
+ Dir.rmdir("\n")
65
+ def lf_in_path_allowed?
66
+ true
67
+ end
68
+ rescue
69
+ def lf_in_path_allowed?
70
+ false
71
+ end
72
+ end
73
+
74
+ Dir.chdir prevdir
75
+ Dir.rmdir tmproot
76
+
77
+ class TestFileUtils2
78
+
79
+ include FileUtils2
80
+
81
+ def check_singleton(name)
82
+ assert_respond_to ::FileUtils2, name
83
+ end
84
+
85
+ def my_rm_rf(path)
86
+ if File.exist?('/bin/rm')
87
+ system %Q[/bin/rm -rf "#{path}"]
88
+ else
89
+ FileUtils2.rm_rf path
90
+ end
91
+ end
92
+
93
+ def mymkdir(path)
94
+ Dir.mkdir path
95
+ File.chown nil, Process.gid, path if have_file_perm?
96
+ end
97
+
98
+ def setup
99
+ @prevdir = Dir.pwd
100
+ tmproot = TMPROOT
101
+ mymkdir tmproot unless File.directory?(tmproot)
102
+ Dir.chdir tmproot
103
+ my_rm_rf 'data'; mymkdir 'data'
104
+ my_rm_rf 'tmp'; mymkdir 'tmp'
105
+ prepare_data_file
106
+ end
107
+
108
+ def teardown
109
+ Dir.chdir @prevdir
110
+ my_rm_rf TMPROOT
111
+ end
112
+
113
+
114
+ TARGETS = %w( data/a data/all data/random data/zero )
115
+
116
+ def prepare_data_file
117
+ File.open('data/a', 'w') {|f|
118
+ 32.times do
119
+ f.puts 'a' * 50
120
+ end
121
+ }
122
+
123
+ all_chars = (0..255).map {|n| n.chr }.join('')
124
+ File.open('data/all', 'w') {|f|
125
+ 32.times do
126
+ f.puts all_chars
127
+ end
128
+ }
129
+
130
+ random_chars = (0...50).map { rand(256).chr }.join('')
131
+ File.open('data/random', 'w') {|f|
132
+ 32.times do
133
+ f.puts random_chars
134
+ end
135
+ }
136
+
137
+ File.open('data/zero', 'w') {|f|
138
+ ;
139
+ }
140
+ end
141
+
142
+ BIGFILE = 'data/big'
143
+
144
+ def prepare_big_file
145
+ File.open('data/big', 'w') {|f|
146
+ (4 * 1024 * 1024 / 256).times do # 4MB
147
+ f.print "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa\n"
148
+ end
149
+ }
150
+ end
151
+
152
+ def prepare_time_data
153
+ File.open('data/old', 'w') {|f| f.puts 'dummy' }
154
+ File.open('data/newer', 'w') {|f| f.puts 'dummy' }
155
+ File.open('data/newest', 'w') {|f| f.puts 'dummy' }
156
+ t = Time.now
157
+ File.utime t-8, t-8, 'data/old'
158
+ File.utime t-4, t-4, 'data/newer'
159
+ end
160
+
161
+ def each_srcdest
162
+ TARGETS.each do |path|
163
+ yield path, "tmp/#{File.basename(path)}"
164
+ end
165
+ end
166
+
167
+ #
168
+ # Test Cases
169
+ #
170
+
171
+ def test_pwd
172
+ check_singleton :pwd
173
+
174
+ assert_equal Dir.pwd, pwd()
175
+
176
+ cwd = Dir.pwd
177
+ root = have_drive_letter? ? 'C:/' : '/'
178
+ cd(root) {
179
+ assert_equal root, pwd()
180
+ }
181
+ assert_equal cwd, pwd()
182
+ end
183
+
184
+ def test_cmp
185
+ check_singleton :cmp
186
+
187
+ TARGETS.each do |fname|
188
+ assert cmp(fname, fname), 'not same?'
189
+ end
190
+ assert_raise(ArgumentError) {
191
+ cmp TARGETS[0], TARGETS[0], :undefinedoption => true
192
+ }
193
+
194
+ # pathname
195
+ touch 'tmp/cmptmp'
196
+ assert_nothing_raised {
197
+ cmp Pathname.new('tmp/cmptmp'), 'tmp/cmptmp'
198
+ cmp 'tmp/cmptmp', Pathname.new('tmp/cmptmp')
199
+ cmp Pathname.new('tmp/cmptmp'), Pathname.new('tmp/cmptmp')
200
+ }
201
+ end
202
+
203
+ def test_cp
204
+ check_singleton :cp
205
+
206
+ each_srcdest do |srcpath, destpath|
207
+ cp srcpath, destpath
208
+ assert_same_file srcpath, destpath
209
+
210
+ cp srcpath, File.dirname(destpath)
211
+ assert_same_file srcpath, destpath
212
+
213
+ cp srcpath, File.dirname(destpath) + '/'
214
+ assert_same_file srcpath, destpath
215
+
216
+ cp srcpath, destpath, :preserve => true
217
+ assert_same_file srcpath, destpath
218
+ assert_same_entry srcpath, destpath
219
+ end
220
+
221
+ assert_raise(Errno::ENOENT) {
222
+ cp 'tmp/cptmp', 'tmp/cptmp_new'
223
+ }
224
+ assert_file_not_exist('tmp/cptmp_new')
225
+
226
+ # src==dest (1) same path
227
+ touch 'tmp/cptmp'
228
+ assert_raise(ArgumentError) {
229
+ cp 'tmp/cptmp', 'tmp/cptmp'
230
+ }
231
+ end
232
+
233
+ def test_cp_preserve_permissions
234
+ bug4507 = '[ruby-core:35518]'
235
+ touch 'tmp/cptmp'
236
+ chmod 0755, 'tmp/cptmp'
237
+ cp 'tmp/cptmp', 'tmp/cptmp2'
238
+ assert_equal(File.stat('tmp/cptmp').mode,
239
+ File.stat('tmp/cptmp2').mode,
240
+ bug4507)
241
+ end
242
+
243
+ def test_cp_preserve_permissions_dir
244
+ bug7246 = '[ruby-core:48603]'
245
+ mkdir 'tmp/cptmp'
246
+ mkdir 'tmp/cptmp/d1'
247
+ chmod 0745, 'tmp/cptmp/d1'
248
+ mkdir 'tmp/cptmp/d2'
249
+ chmod 0700, 'tmp/cptmp/d2'
250
+ cp_r 'tmp/cptmp', 'tmp/cptmp2', :preserve => true
251
+ assert_equal(File.stat('tmp/cptmp/d1').mode,
252
+ File.stat('tmp/cptmp2/d1').mode,
253
+ bug7246)
254
+ assert_equal(File.stat('tmp/cptmp/d2').mode,
255
+ File.stat('tmp/cptmp2/d2').mode,
256
+ bug7246)
257
+ end
258
+
259
+ def test_cp_symlink
260
+ touch 'tmp/cptmp'
261
+ # src==dest (2) symlink and its target
262
+ File.symlink 'cptmp', 'tmp/cptmp_symlink'
263
+ assert_raise(ArgumentError) {
264
+ cp 'tmp/cptmp', 'tmp/cptmp_symlink'
265
+ }
266
+ assert_raise(ArgumentError) {
267
+ cp 'tmp/cptmp_symlink', 'tmp/cptmp'
268
+ }
269
+ # src==dest (3) looped symlink
270
+ File.symlink 'symlink', 'tmp/symlink'
271
+ assert_raise(Errno::ELOOP) {
272
+ cp 'tmp/symlink', 'tmp/symlink'
273
+ }
274
+ end if have_symlink?
275
+
276
+ def test_cp_pathname
277
+ # pathname
278
+ touch 'tmp/cptmp'
279
+ assert_nothing_raised {
280
+ cp 'tmp/cptmp', Pathname.new('tmp/tmpdest')
281
+ cp Pathname.new('tmp/cptmp'), 'tmp/tmpdest'
282
+ cp Pathname.new('tmp/cptmp'), Pathname.new('tmp/tmpdest')
283
+ mkdir 'tmp/tmpdir'
284
+ cp ['tmp/cptmp', 'tmp/tmpdest'], Pathname.new('tmp/tmpdir')
285
+ }
286
+ end
287
+
288
+ def test_cp_r
289
+ check_singleton :cp_r
290
+
291
+ cp_r 'data', 'tmp'
292
+ TARGETS.each do |fname|
293
+ assert_same_file fname, "tmp/#{fname}"
294
+ end
295
+
296
+ cp_r 'data', 'tmp2', :preserve => true
297
+ TARGETS.each do |fname|
298
+ assert_same_entry fname, "tmp2/#{File.basename(fname)}"
299
+ assert_same_file fname, "tmp2/#{File.basename(fname)}"
300
+ end
301
+
302
+ # a/* -> b/*
303
+ mkdir 'tmp/cpr_src'
304
+ mkdir 'tmp/cpr_dest'
305
+ File.open('tmp/cpr_src/a', 'w') {|f| f.puts 'a' }
306
+ File.open('tmp/cpr_src/b', 'w') {|f| f.puts 'b' }
307
+ File.open('tmp/cpr_src/c', 'w') {|f| f.puts 'c' }
308
+ mkdir 'tmp/cpr_src/d'
309
+ cp_r 'tmp/cpr_src/.', 'tmp/cpr_dest'
310
+ assert_same_file 'tmp/cpr_src/a', 'tmp/cpr_dest/a'
311
+ assert_same_file 'tmp/cpr_src/b', 'tmp/cpr_dest/b'
312
+ assert_same_file 'tmp/cpr_src/c', 'tmp/cpr_dest/c'
313
+ assert_directory 'tmp/cpr_dest/d'
314
+ my_rm_rf 'tmp/cpr_src'
315
+ my_rm_rf 'tmp/cpr_dest'
316
+
317
+ bug3588 = '[ruby-core:31360]'
318
+ assert_nothing_raised(ArgumentError, bug3588) do
319
+ cp_r 'tmp', 'tmp2'
320
+ end
321
+ assert_directory 'tmp2/tmp'
322
+ assert_raise(ArgumentError, bug3588) do
323
+ cp_r 'tmp2', 'tmp2/new_tmp2'
324
+ end
325
+ end
326
+
327
+ def test_cp_r_symlink
328
+ # symlink in a directory
329
+ mkdir 'tmp/cpr_src'
330
+ ln_s 'SLdest', 'tmp/cpr_src/symlink'
331
+ cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
332
+ assert_symlink 'tmp/cpr_dest/symlink'
333
+ assert_equal 'SLdest', File.readlink('tmp/cpr_dest/symlink')
334
+
335
+ # root is a symlink
336
+ ln_s 'cpr_src', 'tmp/cpr_src2'
337
+ cp_r 'tmp/cpr_src2', 'tmp/cpr_dest2'
338
+ assert_directory 'tmp/cpr_dest2'
339
+ assert_not_symlink 'tmp/cpr_dest2'
340
+ assert_symlink 'tmp/cpr_dest2/symlink'
341
+ assert_equal 'SLdest', File.readlink('tmp/cpr_dest2/symlink')
342
+ end if have_symlink?
343
+
344
+ def test_cp_r_symlink_preserve
345
+ mkdir 'tmp/cross'
346
+ mkdir 'tmp/cross/a'
347
+ mkdir 'tmp/cross/b'
348
+ touch 'tmp/cross/a/f'
349
+ touch 'tmp/cross/b/f'
350
+ ln_s '../a/f', 'tmp/cross/b/l'
351
+ ln_s '../b/f', 'tmp/cross/a/l'
352
+ assert_nothing_raised {
353
+ cp_r 'tmp/cross', 'tmp/cross2', :preserve => true
354
+ }
355
+ end if have_symlink?
356
+
357
+ def test_cp_r_pathname
358
+ # pathname
359
+ touch 'tmp/cprtmp'
360
+ assert_nothing_raised {
361
+ cp_r Pathname.new('tmp/cprtmp'), 'tmp/tmpdest'
362
+ cp_r 'tmp/cprtmp', Pathname.new('tmp/tmpdest')
363
+ cp_r Pathname.new('tmp/cprtmp'), Pathname.new('tmp/tmpdest')
364
+ }
365
+ end
366
+
367
+ def test_mv
368
+ check_singleton :mv
369
+
370
+ mkdir 'tmp/dest'
371
+ TARGETS.each do |fname|
372
+ cp fname, 'tmp/mvsrc'
373
+ mv 'tmp/mvsrc', 'tmp/mvdest'
374
+ assert_same_file fname, 'tmp/mvdest'
375
+
376
+ mv 'tmp/mvdest', 'tmp/dest/'
377
+ assert_same_file fname, 'tmp/dest/mvdest'
378
+
379
+ mv 'tmp/dest/mvdest', 'tmp'
380
+ assert_same_file fname, 'tmp/mvdest'
381
+ end
382
+
383
+ mkdir 'tmp/tmpdir'
384
+ mkdir_p 'tmp/dest2/tmpdir'
385
+ assert_raise(Errno::EEXIST) {
386
+ mv 'tmp/tmpdir', 'tmp/dest2'
387
+ }
388
+ mkdir 'tmp/dest2/tmpdir/junk'
389
+ assert_raise(Errno::EEXIST, "[ruby-talk:124368]") {
390
+ mv 'tmp/tmpdir', 'tmp/dest2'
391
+ }
392
+
393
+ # src==dest (1) same path
394
+ touch 'tmp/cptmp'
395
+ assert_raise(ArgumentError) {
396
+ mv 'tmp/cptmp', 'tmp/cptmp'
397
+ }
398
+ end
399
+
400
+ def test_mv_symlink
401
+ touch 'tmp/cptmp'
402
+ # src==dest (2) symlink and its target
403
+ File.symlink 'cptmp', 'tmp/cptmp_symlink'
404
+ assert_raise(ArgumentError) {
405
+ mv 'tmp/cptmp', 'tmp/cptmp_symlink'
406
+ }
407
+ assert_raise(ArgumentError) {
408
+ mv 'tmp/cptmp_symlink', 'tmp/cptmp'
409
+ }
410
+ # src==dest (3) looped symlink
411
+ File.symlink 'symlink', 'tmp/symlink'
412
+ assert_raise(Errno::ELOOP) {
413
+ mv 'tmp/symlink', 'tmp/symlink'
414
+ }
415
+ end if have_symlink?
416
+
417
+ def test_mv_pathname
418
+ # pathname
419
+ assert_nothing_raised {
420
+ touch 'tmp/mvtmpsrc'
421
+ mv Pathname.new('tmp/mvtmpsrc'), 'tmp/mvtmpdest'
422
+ touch 'tmp/mvtmpsrc'
423
+ mv 'tmp/mvtmpsrc', Pathname.new('tmp/mvtmpdest')
424
+ touch 'tmp/mvtmpsrc'
425
+ mv Pathname.new('tmp/mvtmpsrc'), Pathname.new('tmp/mvtmpdest')
426
+ }
427
+ end
428
+
429
+ def test_rm
430
+ check_singleton :rm
431
+
432
+ TARGETS.each do |fname|
433
+ cp fname, 'tmp/rmsrc'
434
+ rm 'tmp/rmsrc'
435
+ assert_file_not_exist 'tmp/rmsrc'
436
+ end
437
+
438
+ # pathname
439
+ touch 'tmp/rmtmp1'
440
+ touch 'tmp/rmtmp2'
441
+ touch 'tmp/rmtmp3'
442
+ assert_nothing_raised {
443
+ rm Pathname.new('tmp/rmtmp1')
444
+ rm [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
445
+ }
446
+ assert_file_not_exist 'tmp/rmtmp1'
447
+ assert_file_not_exist 'tmp/rmtmp2'
448
+ assert_file_not_exist 'tmp/rmtmp3'
449
+ end
450
+
451
+ def test_rm_f
452
+ check_singleton :rm_f
453
+
454
+ TARGETS.each do |fname|
455
+ cp fname, 'tmp/rmsrc'
456
+ rm_f 'tmp/rmsrc'
457
+ assert_file_not_exist 'tmp/rmsrc'
458
+ end
459
+ end
460
+
461
+ def test_rm_symlink
462
+ File.open('tmp/lnf_symlink_src', 'w') {|f| f.puts 'dummy' }
463
+ File.symlink 'tmp/lnf_symlink_src', 'tmp/lnf_symlink_dest'
464
+ rm_f 'tmp/lnf_symlink_dest'
465
+ assert_file_not_exist 'tmp/lnf_symlink_dest'
466
+ assert_file_exist 'tmp/lnf_symlink_src'
467
+
468
+ rm_f 'notexistdatafile'
469
+ rm_f 'tmp/notexistdatafile'
470
+ my_rm_rf 'tmpdatadir'
471
+ Dir.mkdir 'tmpdatadir'
472
+ # rm_f 'tmpdatadir'
473
+ Dir.rmdir 'tmpdatadir'
474
+ end if have_symlink?
475
+
476
+ def test_rm_f_2
477
+ Dir.mkdir 'tmp/tmpdir'
478
+ File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
479
+ File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
480
+ rm_f ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c']
481
+ assert_file_not_exist 'tmp/tmpdir/a'
482
+ assert_file_not_exist 'tmp/tmpdir/c'
483
+ Dir.rmdir 'tmp/tmpdir'
484
+ end
485
+
486
+ def test_rm_pathname
487
+ # pathname
488
+ touch 'tmp/rmtmp1'
489
+ touch 'tmp/rmtmp2'
490
+ touch 'tmp/rmtmp3'
491
+ touch 'tmp/rmtmp4'
492
+ assert_nothing_raised {
493
+ rm_f Pathname.new('tmp/rmtmp1')
494
+ rm_f [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
495
+ }
496
+ assert_file_not_exist 'tmp/rmtmp1'
497
+ assert_file_not_exist 'tmp/rmtmp2'
498
+ assert_file_not_exist 'tmp/rmtmp3'
499
+ assert_file_exist 'tmp/rmtmp4'
500
+
501
+ # [ruby-dev:39345]
502
+ touch 'tmp/[rmtmp]'
503
+ FileUtils2.rm_f 'tmp/[rmtmp]'
504
+ assert_file_not_exist 'tmp/[rmtmp]'
505
+ end
506
+
507
+ def test_rm_r
508
+ check_singleton :rm_r
509
+
510
+ my_rm_rf 'tmpdatadir'
511
+
512
+ Dir.mkdir 'tmpdatadir'
513
+ rm_r 'tmpdatadir'
514
+ assert_file_not_exist 'tmpdatadir'
515
+
516
+ Dir.mkdir 'tmpdatadir'
517
+ rm_r 'tmpdatadir/'
518
+ assert_file_not_exist 'tmpdatadir'
519
+
520
+ Dir.mkdir 'tmp/tmpdir'
521
+ rm_r 'tmp/tmpdir/'
522
+ assert_file_not_exist 'tmp/tmpdir'
523
+ assert_file_exist 'tmp'
524
+
525
+ Dir.mkdir 'tmp/tmpdir'
526
+ rm_r 'tmp/tmpdir'
527
+ assert_file_not_exist 'tmp/tmpdir'
528
+ assert_file_exist 'tmp'
529
+
530
+ Dir.mkdir 'tmp/tmpdir'
531
+ File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
532
+ File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
533
+ File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
534
+ rm_r 'tmp/tmpdir'
535
+ assert_file_not_exist 'tmp/tmpdir'
536
+ assert_file_exist 'tmp'
537
+
538
+ Dir.mkdir 'tmp/tmpdir'
539
+ File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
540
+ File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
541
+ rm_r ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c'], :force => true
542
+ assert_file_not_exist 'tmp/tmpdir/a'
543
+ assert_file_not_exist 'tmp/tmpdir/c'
544
+ Dir.rmdir 'tmp/tmpdir'
545
+ end
546
+
547
+ def test_rm_r_symlink
548
+ # [ruby-talk:94635] a symlink to the directory
549
+ Dir.mkdir 'tmp/tmpdir'
550
+ File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
551
+ rm_r 'tmp/tmpdir'
552
+ assert_file_not_exist 'tmp/tmpdir'
553
+ assert_file_exist 'tmp'
554
+ end if have_symlink?
555
+
556
+ def test_rm_r_pathname
557
+ # pathname
558
+ Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
559
+ Dir.mkdir 'tmp/tmpdir2'; touch 'tmp/tmpdir2/tmp'
560
+ Dir.mkdir 'tmp/tmpdir3'; touch 'tmp/tmpdir3/tmp'
561
+ assert_nothing_raised {
562
+ rm_r Pathname.new('tmp/tmpdir1')
563
+ rm_r [Pathname.new('tmp/tmpdir2'), Pathname.new('tmp/tmpdir3')]
564
+ }
565
+ assert_file_not_exist 'tmp/tmpdir1'
566
+ assert_file_not_exist 'tmp/tmpdir2'
567
+ assert_file_not_exist 'tmp/tmpdir3'
568
+ end
569
+
570
+ def test_remove_entry_secure
571
+ check_singleton :remove_entry_secure
572
+
573
+ my_rm_rf 'tmpdatadir'
574
+
575
+ Dir.mkdir 'tmpdatadir'
576
+ remove_entry_secure 'tmpdatadir'
577
+ assert_file_not_exist 'tmpdatadir'
578
+
579
+ Dir.mkdir 'tmpdatadir'
580
+ remove_entry_secure 'tmpdatadir/'
581
+ assert_file_not_exist 'tmpdatadir'
582
+
583
+ Dir.mkdir 'tmp/tmpdir'
584
+ remove_entry_secure 'tmp/tmpdir/'
585
+ assert_file_not_exist 'tmp/tmpdir'
586
+ assert_file_exist 'tmp'
587
+
588
+ Dir.mkdir 'tmp/tmpdir'
589
+ remove_entry_secure 'tmp/tmpdir'
590
+ assert_file_not_exist 'tmp/tmpdir'
591
+ assert_file_exist 'tmp'
592
+
593
+ Dir.mkdir 'tmp/tmpdir'
594
+ File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
595
+ File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
596
+ File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
597
+ remove_entry_secure 'tmp/tmpdir'
598
+ assert_file_not_exist 'tmp/tmpdir'
599
+ assert_file_exist 'tmp'
600
+
601
+ Dir.mkdir 'tmp/tmpdir'
602
+ File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
603
+ File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
604
+ remove_entry_secure 'tmp/tmpdir/a', true
605
+ remove_entry_secure 'tmp/tmpdir/b', true
606
+ remove_entry_secure 'tmp/tmpdir/c', true
607
+ assert_file_not_exist 'tmp/tmpdir/a'
608
+ assert_file_not_exist 'tmp/tmpdir/c'
609
+ Dir.rmdir 'tmp/tmpdir'
610
+ end
611
+
612
+ def test_remove_entry_secure_symlink
613
+ # [ruby-talk:94635] a symlink to the directory
614
+ Dir.mkdir 'tmp/tmpdir'
615
+ File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
616
+ remove_entry_secure 'tmp/tmpdir'
617
+ assert_file_not_exist 'tmp/tmpdir'
618
+ assert_file_exist 'tmp'
619
+ end if have_symlink?
620
+
621
+ def test_remove_entry_secure_pathname
622
+ # pathname
623
+ Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
624
+ assert_nothing_raised {
625
+ remove_entry_secure Pathname.new('tmp/tmpdir1')
626
+ }
627
+ assert_file_not_exist 'tmp/tmpdir1'
628
+ end
629
+
630
+ def test_with_big_file
631
+ prepare_big_file
632
+
633
+ cp BIGFILE, 'tmp/cpdest'
634
+ assert_same_file BIGFILE, 'tmp/cpdest'
635
+ assert cmp(BIGFILE, 'tmp/cpdest'), 'orig != copied'
636
+
637
+ mv 'tmp/cpdest', 'tmp/mvdest'
638
+ assert_same_file BIGFILE, 'tmp/mvdest'
639
+ assert_file_not_exist 'tmp/cpdest'
640
+
641
+ rm 'tmp/mvdest'
642
+ assert_file_not_exist 'tmp/mvdest'
643
+ end
644
+
645
+ def test_ln
646
+ TARGETS.each do |fname|
647
+ ln fname, 'tmp/lndest'
648
+ assert_same_file fname, 'tmp/lndest'
649
+ File.unlink 'tmp/lndest'
650
+ end
651
+
652
+ ln TARGETS, 'tmp'
653
+ TARGETS.each do |fname|
654
+ assert_same_file fname, 'tmp/' + File.basename(fname)
655
+ end
656
+ TARGETS.each do |fname|
657
+ File.unlink 'tmp/' + File.basename(fname)
658
+ end
659
+
660
+ # src==dest (1) same path
661
+ touch 'tmp/cptmp'
662
+ assert_raise(Errno::EEXIST) {
663
+ ln 'tmp/cptmp', 'tmp/cptmp'
664
+ }
665
+ end if have_hardlink?
666
+
667
+ def test_ln_symlink
668
+ touch 'tmp/cptmp'
669
+ # src==dest (2) symlink and its target
670
+ File.symlink 'cptmp', 'tmp/symlink'
671
+ assert_raise(Errno::EEXIST) {
672
+ ln 'tmp/cptmp', 'tmp/symlink' # normal file -> symlink
673
+ }
674
+ assert_raise(Errno::EEXIST) {
675
+ ln 'tmp/symlink', 'tmp/cptmp' # symlink -> normal file
676
+ }
677
+ # src==dest (3) looped symlink
678
+ File.symlink 'cptmp_symlink', 'tmp/cptmp_symlink'
679
+ begin
680
+ ln 'tmp/cptmp_symlink', 'tmp/cptmp_symlink'
681
+ rescue => err
682
+ assert_kind_of SystemCallError, err
683
+ end
684
+ end if have_symlink?
685
+
686
+ def test_ln_pathname
687
+ # pathname
688
+ touch 'tmp/lntmp'
689
+ assert_nothing_raised {
690
+ ln Pathname.new('tmp/lntmp'), 'tmp/lndesttmp1'
691
+ ln 'tmp/lntmp', Pathname.new('tmp/lndesttmp2')
692
+ ln Pathname.new('tmp/lntmp'), Pathname.new('tmp/lndesttmp3')
693
+ }
694
+ end if have_hardlink?
695
+
696
+ def test_ln_s
697
+ check_singleton :ln_s
698
+
699
+ TARGETS.each do |fname|
700
+ ln_s fname, 'tmp/lnsdest'
701
+ assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
702
+ assert_equal fname, File.readlink('tmp/lnsdest')
703
+ rm_f 'tmp/lnsdest'
704
+ end
705
+ assert_nothing_raised {
706
+ ln_s 'symlink', 'tmp/symlink'
707
+ }
708
+ assert_symlink 'tmp/symlink'
709
+
710
+ # pathname
711
+ touch 'tmp/lnsdest'
712
+ assert_nothing_raised {
713
+ ln_s Pathname.new('lnsdest'), 'tmp/symlink_tmp1'
714
+ ln_s 'lnsdest', Pathname.new('tmp/symlink_tmp2')
715
+ ln_s Pathname.new('lnsdest'), Pathname.new('tmp/symlink_tmp3')
716
+ }
717
+ end if have_symlink?
718
+
719
+ def test_ln_sf
720
+ check_singleton :ln_sf
721
+
722
+ TARGETS.each do |fname|
723
+ ln_sf fname, 'tmp/lnsdest'
724
+ assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
725
+ assert_equal fname, File.readlink('tmp/lnsdest')
726
+ ln_sf fname, 'tmp/lnsdest'
727
+ ln_sf fname, 'tmp/lnsdest'
728
+ end
729
+ assert_nothing_raised {
730
+ ln_sf 'symlink', 'tmp/symlink'
731
+ }
732
+
733
+ # pathname
734
+ touch 'tmp/lns_dest'
735
+ assert_nothing_raised {
736
+ ln_sf Pathname.new('lns_dest'), 'tmp/symlink_tmp1'
737
+ ln_sf 'lns_dest', Pathname.new('tmp/symlink_tmp2')
738
+ ln_sf Pathname.new('lns_dest'), Pathname.new('tmp/symlink_tmp3')
739
+ }
740
+ end if have_symlink?
741
+
742
+ def test_mkdir
743
+ check_singleton :mkdir
744
+
745
+ my_rm_rf 'tmpdatadir'
746
+ mkdir 'tmpdatadir'
747
+ assert_directory 'tmpdatadir'
748
+ Dir.rmdir 'tmpdatadir'
749
+
750
+ mkdir 'tmpdatadir/'
751
+ assert_directory 'tmpdatadir'
752
+ Dir.rmdir 'tmpdatadir'
753
+
754
+ mkdir 'tmp/mkdirdest'
755
+ assert_directory 'tmp/mkdirdest'
756
+ Dir.rmdir 'tmp/mkdirdest'
757
+
758
+ mkdir 'tmp/tmp', :mode => 0700
759
+ assert_directory 'tmp/tmp'
760
+ assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
761
+ Dir.rmdir 'tmp/tmp'
762
+ end
763
+
764
+ def test_mkdir_file_perm
765
+ mkdir 'tmp/tmp', :mode => 07777
766
+ assert_directory 'tmp/tmp'
767
+ assert_equal 07777, (File.stat('tmp/tmp').mode & 07777)
768
+ Dir.rmdir 'tmp/tmp'
769
+ end if have_file_perm?
770
+
771
+ def test_mkdir_lf_in_path
772
+ mkdir "tmp-first-line\ntmp-second-line"
773
+ assert_directory "tmp-first-line\ntmp-second-line"
774
+ Dir.rmdir "tmp-first-line\ntmp-second-line"
775
+ end if lf_in_path_allowed?
776
+
777
+ def test_mkdir_pathname
778
+ # pathname
779
+ assert_nothing_raised {
780
+ mkdir Pathname.new('tmp/tmpdirtmp')
781
+ mkdir [Pathname.new('tmp/tmpdirtmp2'), Pathname.new('tmp/tmpdirtmp3')]
782
+ }
783
+ end
784
+
785
+ def test_mkdir_p
786
+ check_singleton :mkdir_p
787
+
788
+ dirs = %w(
789
+ tmpdir/dir/
790
+ tmpdir/dir/./
791
+ tmpdir/dir/./.././dir/
792
+ tmpdir/a
793
+ tmpdir/a/
794
+ tmpdir/a/b
795
+ tmpdir/a/b/
796
+ tmpdir/a/b/c/
797
+ tmpdir/a/b/c
798
+ tmpdir/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a
799
+ tmpdir/a/a
800
+ )
801
+ my_rm_rf 'tmpdir'
802
+ dirs.each do |d|
803
+ mkdir_p d
804
+ assert_directory d
805
+ assert_file_not_exist "#{d}/a"
806
+ assert_file_not_exist "#{d}/b"
807
+ assert_file_not_exist "#{d}/c"
808
+ my_rm_rf 'tmpdir'
809
+ end
810
+ dirs.each do |d|
811
+ mkdir_p d
812
+ assert_directory d
813
+ end
814
+ rm_rf 'tmpdir'
815
+ dirs.each do |d|
816
+ mkdir_p "#{Dir.pwd}/#{d}"
817
+ assert_directory d
818
+ end
819
+ rm_rf 'tmpdir'
820
+
821
+ mkdir_p 'tmp/tmp/tmp', :mode => 0700
822
+ assert_directory 'tmp/tmp'
823
+ assert_directory 'tmp/tmp/tmp'
824
+ assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
825
+ assert_equal 0700, (File.stat('tmp/tmp/tmp').mode & 0777) if have_file_perm?
826
+ rm_rf 'tmp/tmp'
827
+
828
+ mkdir_p 'tmp/tmp', :mode => 0
829
+ assert_directory 'tmp/tmp'
830
+ assert_equal 0, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
831
+ # DO NOT USE rm_rf here.
832
+ # (rm(1) try to chdir to parent directory, it fails to remove directory.)
833
+ Dir.rmdir 'tmp/tmp'
834
+ Dir.rmdir 'tmp'
835
+ end
836
+
837
+ def test_mkdir_p_file_perm
838
+ mkdir_p 'tmp/tmp/tmp', :mode => 07777
839
+ assert_directory 'tmp/tmp/tmp'
840
+ assert_equal 07777, (File.stat('tmp/tmp/tmp').mode & 07777)
841
+ Dir.rmdir 'tmp/tmp/tmp'
842
+ Dir.rmdir 'tmp/tmp'
843
+ end if have_file_perm?
844
+
845
+ def test_mkdir_p_pathname
846
+ # pathname
847
+ assert_nothing_raised {
848
+ mkdir_p Pathname.new('tmp/tmp/tmp')
849
+ }
850
+ end
851
+
852
+ def test_install
853
+ check_singleton :install
854
+
855
+ File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' }
856
+ File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
857
+ install 'tmp/aaa', 'tmp/bbb', :mode => 0600
858
+ assert_equal "aaa\n", File.read('tmp/bbb')
859
+ assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
860
+
861
+ t = File.mtime('tmp/bbb')
862
+ install 'tmp/aaa', 'tmp/bbb'
863
+ assert_equal "aaa\n", File.read('tmp/bbb')
864
+ assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
865
+ assert_equal_time t, File.mtime('tmp/bbb')
866
+
867
+ File.unlink 'tmp/aaa'
868
+ File.unlink 'tmp/bbb'
869
+
870
+ # src==dest (1) same path
871
+ touch 'tmp/cptmp'
872
+ assert_raise(ArgumentError) {
873
+ install 'tmp/cptmp', 'tmp/cptmp'
874
+ }
875
+ end
876
+
877
+ def test_install_symlink
878
+ touch 'tmp/cptmp'
879
+ # src==dest (2) symlink and its target
880
+ File.symlink 'cptmp', 'tmp/cptmp_symlink'
881
+ assert_raise(ArgumentError) {
882
+ install 'tmp/cptmp', 'tmp/cptmp_symlink'
883
+ }
884
+ assert_raise(ArgumentError) {
885
+ install 'tmp/cptmp_symlink', 'tmp/cptmp'
886
+ }
887
+ # src==dest (3) looped symlink
888
+ File.symlink 'symlink', 'tmp/symlink'
889
+ assert_raise(Errno::ELOOP) {
890
+ # File#install invokes open(2), always ELOOP must be raised
891
+ install 'tmp/symlink', 'tmp/symlink'
892
+ }
893
+ end if have_symlink?
894
+
895
+ def test_install_pathname
896
+ # pathname
897
+ assert_nothing_raised {
898
+ rm_f 'tmp/a'; touch 'tmp/a'
899
+ install 'tmp/a', Pathname.new('tmp/b')
900
+ rm_f 'tmp/a'; touch 'tmp/a'
901
+ install Pathname.new('tmp/a'), 'tmp/b'
902
+ rm_f 'tmp/a'; touch 'tmp/a'
903
+ install Pathname.new('tmp/a'), Pathname.new('tmp/b')
904
+ rm_f 'tmp/a'
905
+ touch 'tmp/a'
906
+ touch 'tmp/b'
907
+ mkdir 'tmp/dest'
908
+ install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], 'tmp/dest'
909
+ my_rm_rf 'tmp/dest'
910
+ mkdir 'tmp/dest'
911
+ install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], Pathname.new('tmp/dest')
912
+ }
913
+ end
914
+
915
+ def test_chmod
916
+ check_singleton :chmod
917
+
918
+ touch 'tmp/a'
919
+ chmod 0700, 'tmp/a'
920
+ assert_equal 0700, File.stat('tmp/a').mode & 0777
921
+ chmod 0500, 'tmp/a'
922
+ assert_equal 0500, File.stat('tmp/a').mode & 0777
923
+ end if have_file_perm?
924
+
925
+ def test_chmod_symbol_mode
926
+ check_singleton :chmod
927
+
928
+ touch 'tmp/a'
929
+ chmod "u=wrx,g=,o=", 'tmp/a'
930
+ assert_equal 0700, File.stat('tmp/a').mode & 0777
931
+ chmod "u=rx,go=", 'tmp/a'
932
+ assert_equal 0500, File.stat('tmp/a').mode & 0777
933
+ chmod "+wrx", 'tmp/a'
934
+ assert_equal 0777, File.stat('tmp/a').mode & 0777
935
+ chmod "u+s,o=s", 'tmp/a'
936
+ assert_equal 04770, File.stat('tmp/a').mode & 07777
937
+ chmod "u-w,go-wrx", 'tmp/a'
938
+ assert_equal 04500, File.stat('tmp/a').mode & 07777
939
+ chmod "+s", 'tmp/a'
940
+ assert_equal 06500, File.stat('tmp/a').mode & 07777
941
+
942
+ # FreeBSD ufs and tmpfs don't allow to change sticky bit against
943
+ # regular file. It's slightly strange. Anyway it's no effect bit.
944
+ # see /usr/src/sys/ufs/ufs/ufs_chmod()
945
+ # NetBSD, OpenBSD and Solaris also denies it.
946
+ if /freebsd|netbsd|openbsd|solaris/ !~ RUBY_PLATFORM
947
+ chmod "u+t,o+t", 'tmp/a'
948
+ assert_equal 07500, File.stat('tmp/a').mode & 07777
949
+ chmod "a-t,a-s", 'tmp/a'
950
+ assert_equal 0500, File.stat('tmp/a').mode & 07777
951
+ end
952
+
953
+ end if have_file_perm?
954
+
955
+
956
+ def test_chmod_R
957
+ check_singleton :chmod_R
958
+
959
+ mkdir_p 'tmp/dir/dir'
960
+ touch %w( tmp/dir/file tmp/dir/dir/file )
961
+ chmod_R 0700, 'tmp/dir'
962
+ assert_equal 0700, File.stat('tmp/dir').mode & 0777
963
+ assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
964
+ assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
965
+ assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
966
+ chmod_R 0500, 'tmp/dir'
967
+ assert_equal 0500, File.stat('tmp/dir').mode & 0777
968
+ assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
969
+ assert_equal 0500, File.stat('tmp/dir/dir').mode & 0777
970
+ assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
971
+ chmod_R 0700, 'tmp/dir' # to remove
972
+ end if have_file_perm?
973
+
974
+ def test_chmod_symbol_mode_R
975
+ check_singleton :chmod_R
976
+
977
+ mkdir_p 'tmp/dir/dir'
978
+ touch %w( tmp/dir/file tmp/dir/dir/file )
979
+ chmod_R "u=wrx,g=,o=", 'tmp/dir'
980
+ assert_equal 0700, File.stat('tmp/dir').mode & 0777
981
+ assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
982
+ assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
983
+ assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
984
+ chmod_R "u=xr,g+X,o=", 'tmp/dir'
985
+ assert_equal 0510, File.stat('tmp/dir').mode & 0777
986
+ assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
987
+ assert_equal 0510, File.stat('tmp/dir/dir').mode & 0777
988
+ assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
989
+ chmod_R 0700, 'tmp/dir' # to remove
990
+ end if have_file_perm?
991
+
992
+ def test_chmod_verbose
993
+ check_singleton :chmod
994
+
995
+ r, w = IO.pipe
996
+ stderr_back = $stderr
997
+ read, $stderr = IO.pipe
998
+ th = Thread.new { read.read }
999
+
1000
+ touch 'tmp/a'
1001
+ chmod 0700, 'tmp/a', :verbose => true
1002
+ assert_equal 0700, File.stat('tmp/a').mode & 0777
1003
+ chmod 0500, 'tmp/a', :verbose => true
1004
+ assert_equal 0500, File.stat('tmp/a').mode & 0777
1005
+
1006
+ $stderr.close
1007
+ lines = th.value.lines.map {|l| l.chomp }
1008
+ assert_equal(["chmod 700 tmp/a", "chmod 500 tmp/a"], lines)
1009
+ ensure
1010
+ $stderr = stderr_back if stderr_back
1011
+ end if have_file_perm?
1012
+
1013
+ # FIXME: How can I test this method?
1014
+ def test_chown
1015
+ check_singleton :chown
1016
+ end if have_file_perm?
1017
+
1018
+ # FIXME: How can I test this method?
1019
+ def test_chown_R
1020
+ check_singleton :chown_R
1021
+ end if have_file_perm?
1022
+
1023
+ def test_copy_entry
1024
+ check_singleton :copy_entry
1025
+
1026
+ each_srcdest do |srcpath, destpath|
1027
+ copy_entry srcpath, destpath
1028
+ assert_same_file srcpath, destpath
1029
+ assert_equal File.stat(srcpath).ftype, File.stat(destpath).ftype
1030
+ end
1031
+ end
1032
+
1033
+ def test_copy_entry_symlink
1034
+ # root is a symlink
1035
+ File.symlink 'somewhere', 'tmp/symsrc'
1036
+ copy_entry 'tmp/symsrc', 'tmp/symdest'
1037
+ assert_symlink 'tmp/symdest'
1038
+ assert_equal 'somewhere', File.readlink('tmp/symdest')
1039
+
1040
+ # content is a symlink
1041
+ mkdir 'tmp/dir'
1042
+ File.symlink 'somewhere', 'tmp/dir/sym'
1043
+ copy_entry 'tmp/dir', 'tmp/dirdest'
1044
+ assert_directory 'tmp/dirdest'
1045
+ assert_not_symlink 'tmp/dirdest'
1046
+ assert_symlink 'tmp/dirdest/sym'
1047
+ assert_equal 'somewhere', File.readlink('tmp/dirdest/sym')
1048
+ end if have_symlink?
1049
+
1050
+ def test_copy_file
1051
+ check_singleton :copy_file
1052
+
1053
+ each_srcdest do |srcpath, destpath|
1054
+ copy_file srcpath, destpath
1055
+ assert_same_file srcpath, destpath
1056
+ end
1057
+ end
1058
+
1059
+ def test_copy_stream
1060
+ check_singleton :copy_stream
1061
+ # IO
1062
+ each_srcdest do |srcpath, destpath|
1063
+ File.open(srcpath, 'rb') {|src|
1064
+ File.open(destpath, 'wb') {|dest|
1065
+ copy_stream src, dest
1066
+ }
1067
+ }
1068
+ assert_same_file srcpath, destpath
1069
+ end
1070
+ end
1071
+
1072
+ def test_copy_stream_duck
1073
+ check_singleton :copy_stream
1074
+ # duck typing test [ruby-dev:25369]
1075
+ each_srcdest do |srcpath, destpath|
1076
+ File.open(srcpath, 'rb') {|src|
1077
+ File.open(destpath, 'wb') {|dest|
1078
+ copy_stream Stream.new(src), Stream.new(dest)
1079
+ }
1080
+ }
1081
+ assert_same_file srcpath, destpath
1082
+ end
1083
+ end
1084
+
1085
+ def test_remove_file
1086
+ check_singleton :remove_file
1087
+ File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
1088
+ remove_file 'data/tmp'
1089
+ assert_file_not_exist 'data/tmp'
1090
+ end
1091
+
1092
+ def test_remove_file_file_perm
1093
+ File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
1094
+ File.chmod 0, 'data/tmp'
1095
+ remove_file 'data/tmp'
1096
+ assert_file_not_exist 'data/tmp'
1097
+ end if have_file_perm?
1098
+
1099
+ def test_remove_dir
1100
+ check_singleton :remove_dir
1101
+ Dir.mkdir 'data/tmpdir'
1102
+ File.open('data/tmpdir/a', 'w') {|f| f.puts 'dummy' }
1103
+ remove_dir 'data/tmpdir'
1104
+ assert_file_not_exist 'data/tmpdir'
1105
+ end
1106
+
1107
+ def test_remove_dir_file_perm
1108
+ Dir.mkdir 'data/tmpdir'
1109
+ File.chmod 0555, 'data/tmpdir'
1110
+ remove_dir 'data/tmpdir'
1111
+ assert_file_not_exist 'data/tmpdir'
1112
+ end if have_file_perm?
1113
+
1114
+ def test_compare_file
1115
+ check_singleton :compare_file
1116
+ # FIXME
1117
+ end
1118
+
1119
+ def test_compare_stream
1120
+ check_singleton :compare_stream
1121
+ # FIXME
1122
+ end
1123
+
1124
+ class Stream
1125
+ def initialize(f)
1126
+ @f = f
1127
+ end
1128
+
1129
+ def read(*args)
1130
+ @f.read(*args)
1131
+ end
1132
+
1133
+ def write(str)
1134
+ @f.write str
1135
+ end
1136
+ end
1137
+
1138
+ def test_uptodate?
1139
+ check_singleton :uptodate?
1140
+ prepare_time_data
1141
+ Dir.chdir('data') {
1142
+ assert( uptodate?('newest', %w(old newer notexist)) )
1143
+ assert( ! uptodate?('newer', %w(old newest notexist)) )
1144
+ assert( ! uptodate?('notexist', %w(old newest newer)) )
1145
+ }
1146
+
1147
+ # pathname
1148
+ touch 'tmp/a'
1149
+ touch 'tmp/b'
1150
+ touch 'tmp/c'
1151
+ assert_nothing_raised {
1152
+ uptodate? Pathname.new('tmp/a'), ['tmp/b', 'tmp/c']
1153
+ uptodate? 'tmp/a', [Pathname.new('tmp/b'), 'tmp/c']
1154
+ uptodate? 'tmp/a', ['tmp/b', Pathname.new('tmp/c')]
1155
+ uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')]
1156
+ }
1157
+ # [Bug #6708] [ruby-core:46256]
1158
+ assert_raises_with_message(ArgumentError, "wrong number of arguments (3 for 2)") {
1159
+ uptodate?('new',['old', 'oldest'], {})
1160
+ }
1161
+ end
1162
+
1163
+ def assert_raises_with_message(klass, message)
1164
+ begin
1165
+ yield
1166
+ flunk("Expected Exception #{klass} didn't raise")
1167
+ rescue klass => ex
1168
+ if message.kind_of? String
1169
+ flag = !!(ex.message == message)
1170
+ assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match")
1171
+ elsif message.kind_of? Regexp
1172
+ flag = !!(ex.message =~ message)
1173
+ assert(flag, "Expected Exception(#{klass}) was raised, but the message doesn't match")
1174
+ else
1175
+ raise
1176
+ end
1177
+ end
1178
+ end
1179
+ private :assert_raises_with_message
1180
+
1181
+ def test_cd
1182
+ check_singleton :cd
1183
+ end
1184
+
1185
+ def test_chdir
1186
+ check_singleton :chdir
1187
+ end
1188
+
1189
+ def test_getwd
1190
+ check_singleton :getwd
1191
+ end
1192
+
1193
+ def test_identical?
1194
+ check_singleton :identical?
1195
+ end
1196
+
1197
+ def test_link
1198
+ check_singleton :link
1199
+ end
1200
+
1201
+ def test_makedirs
1202
+ check_singleton :makedirs
1203
+ end
1204
+
1205
+ def test_mkpath
1206
+ check_singleton :mkpath
1207
+ end
1208
+
1209
+ def test_move
1210
+ check_singleton :move
1211
+ end
1212
+
1213
+ def test_rm_rf
1214
+ check_singleton :rm_rf
1215
+ end
1216
+
1217
+ def test_rmdir
1218
+ check_singleton :rmdir
1219
+ end
1220
+
1221
+ def test_rmtree
1222
+ check_singleton :rmtree
1223
+ end
1224
+
1225
+ def test_safe_unlink
1226
+ check_singleton :safe_unlink
1227
+ end
1228
+
1229
+ def test_symlink
1230
+ check_singleton :symlink
1231
+ end
1232
+
1233
+ def test_touch
1234
+ check_singleton :touch
1235
+ end
1236
+
1237
+ def test_collect_methods
1238
+ end
1239
+
1240
+ def test_commands
1241
+ end
1242
+
1243
+ def test_have_option?
1244
+ end
1245
+
1246
+ def test_options
1247
+ end
1248
+
1249
+ def test_options_of
1250
+ end
1251
+
1252
+ end