fakefs 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +0,0 @@
1
- require 'test_helper'
2
-
3
- # File SysWrite test class
4
- class FileSysWriteTest < Minitest::Test
5
- def setup
6
- FakeFS.activate!
7
- FakeFS::FileSystem.clear
8
- end
9
-
10
- def teardown
11
- FakeFS.deactivate!
12
- end
13
-
14
- def test_returns_one_byte_when_written
15
- f = File.open 'foo', 'w'
16
- result = f.syswrite 'a'
17
- assert_equal 1, result
18
- end
19
-
20
- def test_returns_two_bytes_when_two_written
21
- f = File.open 'foo', 'w'
22
- result = f.syswrite 'ab'
23
- assert_equal 2, result
24
- end
25
-
26
- def test_syswrite_writes_file
27
- f = File.open 'foo', 'w'
28
- f.syswrite 'abcdef'
29
- f.close
30
-
31
- assert_equal 'abcdef', File.read('foo')
32
- end
33
-
34
- def test_writes_to_the_actual_position_when_called_after_buffered_io_read
35
- File.open('foo', 'w') do |file|
36
- file.syswrite('012345678901234567890123456789')
37
- end
38
-
39
- file = File.open('foo', 'r+')
40
- file.read(5)
41
- file.syswrite('abcde')
42
-
43
- File.open('foo') do |f|
44
- assert_equal '01234abcde', f.sysread(10)
45
- end
46
- end
47
-
48
- def test_writes_all_of_the_strings_bytes_but_does_not_buffer_them
49
- File.open('foo', 'w') do |file|
50
- file.syswrite('012345678901234567890123456789')
51
- end
52
-
53
- file = File.open('foo', 'r+')
54
- file.syswrite('abcde')
55
-
56
- File.open('foo') do |f|
57
- assert_equal 'abcde56789', f.sysread(10)
58
- f.seek(0)
59
- f.fsync
60
- assert_equal 'abcde56789', f.sysread(10)
61
- end
62
- end
63
- end
@@ -1,112 +0,0 @@
1
- require 'test_helper'
2
-
3
- # Fake File test class
4
- class FakeFileTest < Minitest::Test
5
- include FakeFS
6
-
7
- def setup
8
- FileSystem.clear
9
-
10
- @file = FakeFile.new
11
- end
12
-
13
- def test_fake_file_has_empty_content_by_default
14
- assert_equal '', @file.content
15
- end
16
-
17
- def test_fake_file_can_read_and_write_to_content
18
- @file.content = 'foobar'
19
- assert_equal 'foobar', @file.content
20
- end
21
-
22
- def test_fake_file_has_1_link_by_default
23
- assert_equal [@file], @file.links
24
- end
25
-
26
- def test_fake_file_can_create_link
27
- other_file = FakeFile.new
28
-
29
- @file.link(other_file)
30
-
31
- assert_equal [@file, other_file], @file.links
32
- end
33
-
34
- def test_fake_file_wont_add_link_to_same_file_twice
35
- other_file = FakeFile.new
36
-
37
- @file.link other_file
38
- @file.link other_file
39
-
40
- assert_equal [@file, other_file], @file.links
41
- end
42
-
43
- def test_links_are_mutual
44
- other_file = FakeFile.new
45
-
46
- @file.link(other_file)
47
-
48
- assert_equal [@file, other_file], other_file.links
49
- end
50
-
51
- def test_can_link_multiple_files
52
- file_two = FakeFile.new
53
- file_three = FakeFile.new
54
-
55
- @file.link file_two
56
- @file.link file_three
57
-
58
- assert_equal [@file, file_two, file_three], @file.links
59
- assert_equal [@file, file_two, file_three], file_two.links
60
- assert_equal [@file, file_two, file_three], file_three.links
61
- end
62
-
63
- def test_links_share_same_content
64
- other_file = FakeFile.new
65
-
66
- @file.link other_file
67
-
68
- @file.content = 'foobar'
69
-
70
- assert_equal 'foobar', other_file.content
71
- end
72
-
73
- def test_clone_creates_new_inode
74
- clone = @file.clone
75
- refute clone.inode.equal?(@file.inode)
76
- end
77
-
78
- def test_cloning_does_not_use_same_content_object
79
- clone = @file.clone
80
-
81
- clone.content = 'foo'
82
- @file.content = 'bar'
83
-
84
- assert_equal 'foo', clone.content
85
- assert_equal 'bar', @file.content
86
- end
87
-
88
- def test_raises_an_error_with_the_correct_path
89
- path = '/some/non/existing/file'
90
- begin
91
- FakeFS::File.new path
92
- msg = nil
93
- rescue Errno::ENOENT => e
94
- msg = e.message
95
- end
96
- assert_equal "No such file or directory - #{path}", msg
97
- end
98
-
99
- def test_file_size_question_works
100
- assert_nil FileTest.size?('does-not-exist.txt')
101
-
102
- File.open('empty.txt', 'w') do |f|
103
- f << ''
104
- end
105
- assert_nil FileTest.size?('empty.txt')
106
-
107
- File.open('one-char.txt', 'w') do |f|
108
- f << 'a'
109
- end
110
- assert_equal 1, FileTest.size?('one-char.txt')
111
- end
112
- end
@@ -1,33 +0,0 @@
1
- require 'test_helper'
2
-
3
- # Fake symlink test class
4
- class FakeSymlinkTest < Minitest::Test
5
- include FakeFS
6
-
7
- def test_symlink_has_method_missing_as_private
8
- methods = FakeSymlink.private_instance_methods.map(&:to_s)
9
- assert methods.include?('method_missing')
10
- end
11
-
12
- def test_symlink_respond_to_accepts_multiple_params
13
- fake_symlink = FakeSymlink.new('foo')
14
- assert fake_symlink.respond_to?(:to_s, false),
15
- 'has public method \#to_s'
16
- assert fake_symlink.respond_to?(:to_s, true),
17
- 'has public or private method \#to_s'
18
- refute fake_symlink.respond_to?(:initialize, false),
19
- 'has private method \#initialize'
20
- assert fake_symlink.respond_to?(:initialize, true),
21
- 'has private method \#initialize'
22
- end
23
-
24
- def test_symlink_respond_to_uses_same_param_defaults
25
- fake_symlink = FakeSymlink.new('foo')
26
- assert_equal fake_symlink.respond_to?(:to_s),
27
- fake_symlink.entry.respond_to?(:to_s)
28
- refute_equal fake_symlink.respond_to?(:to_s),
29
- fake_symlink.entry.respond_to?(:initialize)
30
- assert_equal fake_symlink.respond_to?(:initialize),
31
- fake_symlink.entry.respond_to?(:initialize)
32
- end
33
- end
data/test/fakefs_test.rb DELETED
@@ -1,2769 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'test_helper'
3
-
4
- # FakeFS tests
5
- class FakeFSTest < Minitest::Test
6
- def setup
7
- act_on_real_fs do
8
- File.umask(0006)
9
- FileUtils.rm_rf(real_file_sandbox)
10
- FileUtils.mkdir_p(real_file_sandbox)
11
- FileUtils.chmod(0777, real_file_sandbox)
12
- end
13
-
14
- FakeFS.activate!
15
- FakeFS::FileSystem.clear
16
- # Create /tmp so that Minitest can create files for diffing when an
17
- # assertion fails. See https://github.com/defunkt/fakefs/issues/143
18
- FileUtils.mkdir_p('/tmp')
19
- end
20
-
21
- def teardown
22
- FakeFS.deactivate!
23
-
24
- act_on_real_fs do
25
- FileUtils.rm_rf(real_file_sandbox)
26
- end
27
- end
28
-
29
- def test_can_be_initialized_empty
30
- FakeFS::FileSystem.clear
31
- fs = FakeFS::FileSystem
32
- assert_equal 0, fs.files.size
33
- end
34
-
35
- def xtest_can_be_initialized_with_an_existing_directory
36
- fs = FakeFS::FileSystem
37
- fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
38
- assert_equal 1, fs.files.size
39
- end
40
-
41
- def test_can_create_directories_with_file_utils_mkdir_p
42
- FileUtils.mkdir_p('/path/to/dir')
43
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
44
- end
45
-
46
- def test_can_cd_to_directory_with_block
47
- FileUtils.mkdir_p('/path/to/dir')
48
- new_path = nil
49
- FileUtils.cd('/path/to') do
50
- new_path = Dir.getwd
51
- end
52
-
53
- assert_equal new_path, '/path/to'
54
- end
55
-
56
- def test_can_create_a_list_of_directories_with_file_utils_mkdir_p
57
- FileUtils.mkdir_p(%w(/path/to/dir1 /path/to/dir2))
58
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir1']
59
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir2']
60
- end
61
-
62
- def test_can_create_directories_with_options
63
- FileUtils.mkdir_p('/path/to/dir', mode: 0755)
64
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
65
- end
66
-
67
- def test_can_create_directories_with_file_utils_mkdir
68
- FileUtils.mkdir_p('/path/to/dir')
69
- FileUtils.mkdir('/path/to/dir/subdir')
70
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']['subdir']
71
- end
72
-
73
- def test_can_create_a_list_of_directories_with_file_utils_mkdir
74
- FileUtils.mkdir_p('/path/to/dir')
75
- FileUtils.mkdir(%w(/path/to/dir/subdir1 /path/to/dir/subdir2))
76
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']['subdir1']
77
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']['subdir2']
78
- end
79
-
80
- def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path
81
- assert_raises Errno::ENOENT do
82
- FileUtils.mkdir('/this/path/does/not/exists/newdir')
83
- end
84
- end
85
-
86
- def test_raises_error_when_creating_a_new_dir_over_existing_file
87
- File.open('file', 'w') { |f| f << 'This is a file, not a directory.' }
88
-
89
- assert_raises Errno::EEXIST do
90
- FileUtils.mkdir_p('file/subdir')
91
- end
92
-
93
- FileUtils.mkdir('dir')
94
- File.open('dir/subfile', 'w') { |f| f << 'This is a file inside a directory.' }
95
-
96
- assert_raises Errno::EEXIST do
97
- FileUtils.mkdir_p('dir/subfile/subdir')
98
- end
99
- end
100
-
101
- def test_can_create_directories_with_mkpath
102
- FileUtils.mkpath('/path/to/dir')
103
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
104
- end
105
-
106
- def test_can_create_directories_with_mkpath_and_options
107
- FileUtils.mkpath('/path/to/dir', mode: 0755)
108
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
109
- end
110
-
111
- def test_can_create_directories_with_mkdirs
112
- FileUtils.makedirs('/path/to/dir')
113
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
114
- end
115
-
116
- def test_can_create_directories_with_mkdirs_and_options
117
- FileUtils.makedirs('/path/to/dir', mode: 0755)
118
- assert_kind_of FakeFS::FakeDir, FakeFS::FileSystem.fs['path']['to']['dir']
119
- end
120
-
121
- def test_unlink_errors_on_file_not_found
122
- assert_raises Errno::ENOENT do
123
- FileUtils.rm('/foo')
124
- end
125
- end
126
-
127
- def test_unlink_doesnt_error_on_file_not_found_when_forced
128
- FileUtils.rm('/foo', force: true)
129
- end
130
-
131
- def test_unlink_doesnt_error_on_file_not_found_with_rm_rf
132
- FileUtils.rm_rf('/foo')
133
- end
134
-
135
- def test_can_delete_directories
136
- FileUtils.mkdir_p('/path/to/dir')
137
- FileUtils.rmdir('/path/to/dir')
138
- assert File.exist?('/path/to/')
139
- assert File.exist?('/path/to/dir') == false
140
- end
141
-
142
- def test_can_delete_multiple_files
143
- FileUtils.touch(%w(foo bar))
144
- FileUtils.rm(%w(foo bar))
145
- assert File.exist?('foo') == false
146
- assert File.exist?('bar') == false
147
- end
148
-
149
- def test_aliases_exist
150
- assert File.respond_to?(:unlink)
151
- assert FileUtils.respond_to?(:rm_f)
152
- assert FileUtils.respond_to?(:rm_r)
153
- assert FileUtils.respond_to?(:rm)
154
- assert FileUtils.respond_to?(:symlink)
155
- assert FileUtils.respond_to?(:move)
156
- assert FileUtils.respond_to?(:copy)
157
- assert FileUtils.respond_to?(:remove)
158
- assert FileUtils.respond_to?(:rmtree)
159
- assert FileUtils.respond_to?(:safe_unlink)
160
- assert FileUtils.respond_to?(:remove_entry_secure)
161
- assert FileUtils.respond_to?(:cmp)
162
- assert FileUtils.respond_to?(:identical?)
163
- end
164
-
165
- def test_knows_directories_exist
166
- FileUtils.mkdir_p(path = '/path/to/dir')
167
- assert File.exist?(path)
168
- end
169
-
170
- def test_knows_directories_are_directories
171
- FileUtils.mkdir_p(path = '/path/to/dir')
172
- assert File.directory?(path)
173
- end
174
-
175
- def test_knows_directories_are_directories_with_periods
176
- FileUtils.mkdir_p(period_path = '/path/to/periodfiles/one.one')
177
- FileUtils.mkdir('/path/to/periodfiles/one-one')
178
-
179
- assert File.directory?(period_path)
180
- end
181
-
182
- def test_knows_symlink_directories_are_directories
183
- FileUtils.mkdir_p(path = '/path/to/dir')
184
- FileUtils.ln_s path, sympath = '/sympath'
185
- assert File.directory?(sympath)
186
- end
187
-
188
- def test_knows_non_existent_directories_arent_directories
189
- path = 'does/not/exist/'
190
- assert_equal RealFile.directory?(path), File.directory?(path)
191
- end
192
-
193
- def test_doesnt_overwrite_existing_directories
194
- FileUtils.mkdir_p(path = '/path/to/dir')
195
- assert File.exist?(path)
196
- FileUtils.mkdir_p('/path/to')
197
- assert File.exist?(path)
198
- assert_raises Errno::EEXIST do
199
- FileUtils.mkdir('/path/to')
200
- end
201
- assert File.exist?(path)
202
- end
203
-
204
- def test_file_utils_mkdir_takes_options
205
- FileUtils.mkdir('/foo', some: :option)
206
- assert File.exist?('/foo')
207
- end
208
-
209
- def test_symlink_with_missing_refferent_does_not_exist
210
- File.symlink('/foo', '/bar')
211
- refute File.exist?('/bar')
212
- end
213
-
214
- def test_can_create_symlinks
215
- FileUtils.mkdir_p(target = '/path/to/target')
216
- FileUtils.ln_s(target, '/path/to/link')
217
- assert_kind_of FakeFS::FakeSymlink, FakeFS::FileSystem.fs['path']['to']['link']
218
-
219
- assert_raises(Errno::EEXIST) do
220
- FileUtils.ln_s(target, '/path/to/link')
221
- end
222
- end
223
-
224
- def test_can_force_creation_of_symlinks
225
- FileUtils.mkdir_p(target = '/path/to/first/target')
226
- FileUtils.ln_s(target, '/path/to/link')
227
- assert_kind_of FakeFS::FakeSymlink, FakeFS::FileSystem.fs['path']['to']['link']
228
- FileUtils.ln_s(target, '/path/to/link', force: true)
229
- end
230
-
231
- def test_create_symlink_using_ln_sf
232
- FileUtils.mkdir_p(target = '/path/to/first/target')
233
- FileUtils.ln_s(target, '/path/to/link')
234
- assert_kind_of FakeFS::FakeSymlink, FakeFS::FileSystem.fs['path']['to']['link']
235
- FileUtils.ln_sf(target, '/path/to/link')
236
- end
237
-
238
- def test_can_follow_symlinks
239
- FileUtils.mkdir_p(target = '/path/to/target')
240
- FileUtils.ln_s(target, link = '/path/to/symlink')
241
- assert_equal target, File.readlink(link)
242
- end
243
-
244
- def test_symlinks_in_different_directories
245
- FileUtils.mkdir_p('/path/to/bar')
246
- FileUtils.mkdir_p(target = '/path/to/foo/target')
247
-
248
- FileUtils.ln_s(target, link = '/path/to/bar/symlink')
249
- assert_equal target, File.readlink(link)
250
- end
251
-
252
- def test_symlink_with_relative_path_exists
253
- FileUtils.touch('/file')
254
- FileUtils.mkdir_p('/a/b')
255
- FileUtils.ln_s('../../file', '/a/b/symlink')
256
- assert File.exist?('/a/b/symlink')
257
- end
258
-
259
- def test_symlink_with_relative_path_and_nonexistant_file_does_not_exist
260
- FileUtils.touch('/file')
261
- FileUtils.mkdir_p('/a/b')
262
- FileUtils.ln_s('../../file_foo', '/a/b/symlink')
263
- refute File.exist?('/a/b/symlink')
264
- end
265
-
266
- def test_symlink_with_relative_path_has_correct_target
267
- FileUtils.touch('/file')
268
- FileUtils.mkdir_p('/a/b')
269
- FileUtils.ln_s('../../file', link = '/a/b/symlink')
270
- assert_equal '../../file', File.readlink(link)
271
- end
272
-
273
- def test_symlinks_to_symlinks
274
- FileUtils.mkdir_p(target = '/path/to/foo/target')
275
- FileUtils.mkdir_p('/path/to/bar')
276
- FileUtils.mkdir_p('/path/to/bar2')
277
-
278
- FileUtils.ln_s(target, link1 = '/path/to/bar/symlink')
279
- FileUtils.ln_s(link1, link2 = '/path/to/bar2/symlink')
280
- assert_equal link1, File.readlink(link2)
281
- end
282
-
283
- def test_symlink_to_symlinks_should_raise_error_if_dir_doesnt_exist
284
- FileUtils.mkdir_p(target = '/path/to/foo/target')
285
-
286
- refute Dir.exist?('/path/to/bar')
287
-
288
- assert_raises Errno::ENOENT do
289
- FileUtils.ln_s(target, '/path/to/bar/symlink')
290
- end
291
- end
292
-
293
- def test_knows_symlinks_are_symlinks
294
- FileUtils.mkdir_p(target = '/path/to/target')
295
- FileUtils.ln_s(target, link = '/path/to/symlink')
296
- assert File.symlink?(link)
297
- end
298
-
299
- def test_can_create_files_in_current_dir
300
- path = 'file.txt'
301
- File.open(path, 'w') do |f|
302
- f.write 'Yatta!'
303
- end
304
-
305
- assert File.exist?(path)
306
- assert File.readable?(path)
307
- assert File.writable?(path)
308
- end
309
-
310
- def test_nothing_is_sticky
311
- refute File.sticky?('/')
312
- end
313
-
314
- def test_can_create_files_in_existing_dir
315
- FileUtils.mkdir_p '/path/to'
316
- path = '/path/to/file.txt'
317
-
318
- File.open(path, 'w') do |f|
319
- f.write 'Yatta!'
320
- end
321
-
322
- assert File.exist?(path)
323
- assert File.readable?(path)
324
- assert File.writable?(path)
325
- end
326
-
327
- def test_raises_ENOENT_trying_to_create_files_in_nonexistent_dir
328
- path = '/path/to/file.txt'
329
-
330
- assert_raises(Errno::ENOENT) do
331
- File.open(path, 'w') do |f|
332
- f.write 'Yatta!'
333
- end
334
- end
335
- end
336
-
337
- def test_raises_ENOENT_trying_to_create_files_in_relative_nonexistent_dir
338
- FileUtils.mkdir_p '/some/path'
339
-
340
- Dir.chdir('/some/path') do
341
- assert_raises(Errno::ENOENT) do
342
- File.open('../foo') { |f| f.write 'moo' }
343
- end
344
- end
345
- end
346
-
347
- def test_raises_ENOENT_trying_to_create_files_in_obscured_nonexistent_dir
348
- FileUtils.mkdir_p '/some/path'
349
-
350
- assert_raises(Errno::ENOENT) do
351
- File.open('/some/path/../foo') { |f| f.write 'moo' }
352
- end
353
- end
354
-
355
- def test_raises_ENOENT_trying_to_create_tilde_referenced_nonexistent_dir
356
- path = "~/fakefs_test_#{$PID}_0000"
357
-
358
- path = path.succ while File.exist? path
359
-
360
- assert_raises(Errno::ENOENT) do
361
- File.open("#{path}/foo") { |f| f.write 'moo' }
362
- end
363
- end
364
-
365
- def test_raises_EISDIR_if_trying_to_open_existing_directory_name
366
- path = '/path/to'
367
-
368
- FileUtils.mkdir_p path
369
-
370
- assert_raises(Errno::EISDIR) do
371
- File.open(path, 'w') do |f|
372
- f.write 'Yatta!'
373
- end
374
- end
375
- end
376
-
377
- def test_can_create_files_with_bitmasks
378
- FileUtils.mkdir_p('/path/to')
379
-
380
- path = '/path/to/file.txt'
381
- File.open(path, File::RDWR | File::CREAT) do |f|
382
- f.write 'Yatta!'
383
- end
384
-
385
- assert File.exist?(path)
386
- assert File.readable?(path)
387
- assert File.writable?(path)
388
- end
389
-
390
- def test_file_opens_in_read_only_mode
391
- File.open('foo', 'w') { |f| f << 'foo' }
392
-
393
- f = File.open('foo')
394
-
395
- assert_raises(IOError) do
396
- f << 'bar'
397
- end
398
- end
399
-
400
- def test_file_opens_in_read_only_mode_with_bitmasks
401
- File.open('foo', 'w') { |f| f << 'foo' }
402
-
403
- f = File.open('foo', File::RDONLY)
404
-
405
- assert_raises(IOError) do
406
- f << 'bar'
407
- end
408
- end
409
-
410
- def test_file_opens_in_invalid_mode
411
- FileUtils.touch('foo')
412
-
413
- assert_raises(ArgumentError) do
414
- File.open('foo', 'an_illegal_mode')
415
- end
416
- end
417
-
418
- def test_raises_error_when_cannot_find_file_in_read_mode
419
- assert_raises(Errno::ENOENT) do
420
- File.open('does_not_exist', 'r')
421
- end
422
- end
423
-
424
- def test_raises_error_when_cannot_find_file_in_read_write_mode
425
- assert_raises(Errno::ENOENT) do
426
- File.open('does_not_exist', 'r+')
427
- end
428
- end
429
-
430
- def test_creates_files_in_write_only_mode
431
- File.open('foo', 'w')
432
- assert File.exist?('foo')
433
- end
434
-
435
- def test_creates_files_in_write_only_mode_with_bitmasks
436
- File.open('foo', File::WRONLY | File::CREAT)
437
- assert File.exist?('foo')
438
- end
439
-
440
- def test_raises_in_write_only_mode_without_create_bitmask
441
- assert_raises(Errno::ENOENT) do
442
- File.open('foo', File::WRONLY)
443
- end
444
- end
445
-
446
- def test_creates_files_in_read_write_truncate_mode
447
- File.open('foo', 'w+')
448
- assert File.exist?('foo')
449
- end
450
-
451
- def test_creates_files_in_append_write_only
452
- File.open('foo', 'a')
453
- assert File.exist?('foo')
454
- end
455
-
456
- def test_creates_files_in_append_read_write
457
- File.open('foo', 'a+')
458
- assert File.exist?('foo')
459
- end
460
-
461
- def test_file_in_write_only_raises_error_when_reading
462
- FileUtils.touch('foo')
463
-
464
- f = File.open('foo', 'w')
465
-
466
- assert_raises(IOError) do
467
- f.read
468
- end
469
- end
470
-
471
- def test_file_in_write_mode_truncates_existing_file
472
- File.open('foo', 'w') { |f| f << 'contents' }
473
- File.open('foo', 'w')
474
- assert_equal '', File.read('foo')
475
- end
476
-
477
- def test_file_in_read_write_truncation_mode_truncates_file
478
- File.open('foo', 'w') { |f| f << 'foo' }
479
- File.open('foo', 'w+')
480
- assert_equal '', File.read('foo')
481
- end
482
-
483
- def test_file_in_append_write_only_raises_error_when_reading
484
- FileUtils.touch('foo')
485
-
486
- f = File.open('foo', 'a')
487
-
488
- assert_raises(IOError) do
489
- f.read
490
- end
491
- end
492
-
493
- def test_can_read_files_once_written
494
- path = 'file.txt'
495
- File.open(path, 'w') do |f|
496
- f.write 'Yatta!'
497
- end
498
-
499
- assert_equal 'Yatta!', File.read(path)
500
- end
501
-
502
- def test_file_read_accepts_hashes
503
- path = 'file.txt'
504
- File.open(path, 'w') do |f|
505
- f.write 'Yatta!'
506
- end
507
-
508
- # nothing raised
509
- File.read(path, mode: 'r:UTF-8:-')
510
- end
511
-
512
- def test_file_read_respects_args
513
- path = 'file.txt'
514
- File.open(path, 'w') do |f|
515
- f.write 'Yatta!'
516
- end
517
-
518
- assert_equal 'Ya', File.read(path, 2)
519
- assert_equal 'at', File.read(path, 2, 1)
520
- assert_equal 'atta!', File.read(path, nil, 1)
521
- end
522
-
523
- def test_can_write_to_files
524
- path = 'file.txt'
525
- File.open(path, 'w') do |f|
526
- f << 'Yada Yada'
527
- end
528
- assert_equal 'Yada Yada', File.read(path)
529
- end
530
-
531
- def test_raises_error_when_opening_with_binary_mode_only
532
- assert_raises ArgumentError do
533
- File.open('/foo', 'b')
534
- end
535
- end
536
-
537
- def test_can_open_file_in_binary_mode
538
- File.open('foo', 'wb') { |x| x << 'a' }
539
- assert_equal 'a', File.read('foo')
540
- end
541
-
542
- def test_can_chunk_io_when_reading
543
- FileUtils.mkdir_p '/path/to'
544
- path = '/path/to/file.txt'
545
- File.open(path, 'w') do |f|
546
- f << 'Yada Yada'
547
- end
548
- file = File.new(path, 'r')
549
- assert_equal 'Yada', file.read(4)
550
- assert_equal ' Yada', file.read(5)
551
- assert_equal '', file.read
552
- file.rewind
553
- assert_equal 'Yada Yada', file.read
554
- end
555
-
556
- def test_can_get_size_of_files
557
- path = 'file.txt'
558
- File.open(path, 'w') do |f|
559
- f << 'Yada Yada'
560
- end
561
- assert_equal 9, File.size(path)
562
- end
563
-
564
- def test_can_get_correct_size_for_files_with_multibyte_characters
565
- path = 'file.txt'
566
- File.open(path, 'wb') do |f|
567
- f << "Y\xC3\xA1da"
568
- end
569
- assert_equal 5, File.size(path)
570
- end
571
-
572
- def test_can_check_if_file_has_size?
573
- path = 'file.txt'
574
- File.open(path, 'w') do |f|
575
- f << 'Yada Yada'
576
- end
577
- assert_equal 9, File.size?(path)
578
- assert_nil File.size?('other.txt')
579
- end
580
-
581
- def test_can_check_size_of_empty_file
582
- path = 'file.txt'
583
- File.open(path, 'w') do |f|
584
- f << ''
585
- end
586
- assert_nil File.size?('file.txt')
587
- end
588
-
589
- def test_zero_on_empty_file
590
- path = 'file.txt'
591
- File.open(path, 'w') do |f|
592
- f << ''
593
- end
594
- assert_equal true, File.zero?(path)
595
- end
596
-
597
- def test_zero_on_non_empty_file
598
- path = 'file.txt'
599
- File.open(path, 'w') do |f|
600
- f << 'Not empty'
601
- end
602
- assert_equal false, File.zero?(path)
603
- end
604
-
605
- def test_zero_on_non_existent_file
606
- path = 'file_does_not_exist.txt'
607
- assert_equal false, File.zero?(path)
608
- end
609
-
610
- def test_raises_error_on_mtime_if_file_does_not_exist
611
- assert_raises Errno::ENOENT do
612
- File.mtime('/path/to/file.txt')
613
- end
614
- end
615
-
616
- if RUBY_VERSION >= '1.9'
617
- def test_can_set_mtime_on_new_file_touch_with_param
618
- time = Time.new(2002, 10, 31, 2, 2, 2, '+02:00')
619
- FileUtils.touch('foo.txt', mtime: time)
620
-
621
- assert_equal File.mtime('foo.txt'), time
622
- end
623
-
624
- def test_can_set_mtime_on_existing_file_touch_with_param
625
- FileUtils.touch('foo.txt')
626
-
627
- time = Time.new(2002, 10, 31, 2, 2, 2, '+02:00')
628
- FileUtils.touch('foo.txt', mtime: time)
629
-
630
- assert_equal File.mtime('foo.txt'), time
631
- end
632
- end
633
-
634
- def test_can_return_mtime_on_existing_file
635
- path = 'file.txt'
636
- File.open(path, 'w') do |f|
637
- f << ''
638
- end
639
- assert File.mtime('file.txt').is_a?(Time)
640
- end
641
-
642
- def test_raises_error_on_ctime_if_file_does_not_exist
643
- assert_raises Errno::ENOENT do
644
- File.ctime('file.txt')
645
- end
646
- end
647
-
648
- def test_can_return_ctime_on_existing_file
649
- File.open('foo', 'w') { |f| f << 'some content' }
650
- assert File.ctime('foo').is_a?(Time)
651
- end
652
-
653
- def test_raises_error_on_atime_if_file_does_not_exist
654
- assert_raises Errno::ENOENT do
655
- File.atime('file.txt')
656
- end
657
- end
658
-
659
- def test_can_return_atime_on_existing_file
660
- File.open('foo', 'w') { |f| f << 'some content' }
661
- assert File.atime('foo').is_a?(Time)
662
- end
663
-
664
- def test_ctime_mtime_and_atime_are_equal_for_new_files
665
- FileUtils.touch('foo')
666
-
667
- ctime = File.ctime('foo')
668
- mtime = File.mtime('foo')
669
- atime = File.atime('foo')
670
- assert ctime.is_a?(Time)
671
- assert mtime.is_a?(Time)
672
- assert atime.is_a?(Time)
673
- assert_equal ctime, mtime
674
- assert_equal ctime, atime
675
-
676
- File.open('foo', 'r') do |f|
677
- assert_equal ctime, f.ctime
678
- assert_equal mtime, f.mtime
679
- assert_equal atime, f.atime
680
- end
681
- end
682
-
683
- def test_ctime_mtime_and_atime_are_equal_for_new_directories
684
- FileUtils.mkdir_p('foo')
685
- ctime = File.ctime('foo')
686
- mtime = File.mtime('foo')
687
- atime = File.atime('foo')
688
- assert ctime.is_a?(Time)
689
- assert mtime.is_a?(Time)
690
- assert atime.is_a?(Time)
691
- assert_equal ctime, mtime
692
- assert_equal ctime, atime
693
- end
694
-
695
- def test_file_ctime_is_equal_to_file_stat_ctime
696
- File.open('foo', 'w') { |f| f << 'some content' }
697
- assert_equal File.stat('foo').ctime, File.ctime('foo')
698
- end
699
-
700
- def test_directory_ctime_is_equal_to_directory_stat_ctime
701
- FileUtils.mkdir_p('foo')
702
- assert_equal File.stat('foo').ctime, File.ctime('foo')
703
- end
704
-
705
- def test_file_mtime_is_equal_to_file_stat_mtime
706
- File.open('foo', 'w') { |f| f << 'some content' }
707
- assert_equal File.stat('foo').mtime, File.mtime('foo')
708
- end
709
-
710
- def test_directory_mtime_is_equal_to_directory_stat_mtime
711
- FileUtils.mkdir_p('foo')
712
- assert_equal File.stat('foo').mtime, File.mtime('foo')
713
- end
714
-
715
- def test_file_atime_is_equal_to_file_stat_atime
716
- File.open('foo', 'w') { |f| f << 'some content' }
717
- assert_equal File.stat('foo').atime, File.atime('foo')
718
- end
719
-
720
- def test_directory_atime_is_equal_to_directory_stat_atime
721
- FileUtils.mkdir_p('foo')
722
- assert_equal File.stat('foo').atime, File.atime('foo')
723
- end
724
-
725
- def test_utime_raises_error_if_path_does_not_exist
726
- assert_raises Errno::ENOENT do
727
- File.utime(Time.now, Time.now, '/path/to/file.txt')
728
- end
729
- end
730
-
731
- def test_can_call_utime_on_an_existing_file
732
- time = Time.now - 300 # Not now
733
- path = 'file.txt'
734
- File.open(path, 'w') do |f|
735
- f << ''
736
- end
737
- File.utime(time, time, path)
738
- assert_equal time, File.mtime('file.txt')
739
- assert_equal time, File.atime('file.txt')
740
- end
741
-
742
- def test_utime_returns_number_of_paths
743
- path1, path2 = 'file.txt', 'another_file.txt'
744
- [path1, path2].each do |path|
745
- File.open(path, 'w') do |f|
746
- f << ''
747
- end
748
- end
749
- assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
750
- end
751
-
752
- def test_file_a_time_updated_when_file_is_read
753
- old_atime = Time.now - 300
754
-
755
- path = 'file.txt'
756
- File.open(path, 'w') do |f|
757
- f << 'Hello'
758
- end
759
-
760
- File.utime(old_atime, File.mtime(path), path)
761
-
762
- assert_equal File.atime(path), old_atime
763
- File.read(path)
764
- assert File.atime(path) != old_atime
765
- end
766
-
767
- def test_can_read_with_File_readlines
768
- path = 'file.txt'
769
- File.open(path, 'w') do |f|
770
- f.puts 'Yatta!', 'Gatta!'
771
- f.puts %w(woot toot)
772
- end
773
-
774
- assert_equal ["Yatta!\n", "Gatta!\n", "woot\n", "toot\n"], File.readlines(path)
775
- end
776
-
777
- def test_can_read_with_File_readlines_and_only_empty_lines
778
- path = 'file.txt'
779
- File.open(path, 'w') do |f|
780
- f.write "\n"
781
- end
782
-
783
- assert_equal ["\n"], File.readlines(path)
784
- end
785
-
786
- def test_can_read_with_File_readlines_and_new_lines
787
- path = 'file.txt'
788
- File.open(path, 'w') do |f|
789
- f.write "this\nis\na\ntest\n"
790
- end
791
-
792
- assert_equal ["this\n", "is\n", "a\n", "test\n"], File.readlines(path)
793
- end
794
-
795
- def test_File_close_disallows_further_access
796
- path = 'file.txt'
797
- file = File.open(path, 'w')
798
- file.write 'Yada'
799
- file.close
800
- assert_raises IOError do
801
- file.read
802
- end
803
- end
804
-
805
- def test_File_close_disallows_further_writes
806
- path = 'file.txt'
807
- file = File.open(path, 'w')
808
- file.write 'Yada'
809
- file.close
810
- assert_raises IOError do
811
- file << 'foo'
812
- end
813
- end
814
-
815
- def test_can_read_from_file_objects
816
- path = 'file.txt'
817
- File.open(path, 'w') do |f|
818
- f.write 'Yatta!'
819
- end
820
-
821
- assert_equal 'Yatta!', File.new(path).read
822
- end
823
-
824
- if RUBY_VERSION >= '1.9'
825
- def test_file_object_has_default_external_encoding
826
- Encoding.default_external = 'UTF-8'
827
- path = 'file.txt'
828
- File.open(path, 'w') { |f| f.write 'Yatta!' }
829
- assert_equal 'UTF-8', File.new(path).read.encoding.name
830
- end
831
- end
832
-
833
- def test_file_object_initialization_with_mode_in_hash_parameter
834
- File.open('file.txt', mode: 'w') { |f| f.write 'Yatta!' }
835
- end
836
-
837
- def test_file_object_initialization_with_brackets_in_filename
838
- skip 'TODO'
839
-
840
- filename = 'bracket[1](2).txt'
841
- expected_contents = 'Yokudekimashita'
842
- # nothing raised
843
- File.open(filename, mode: 'w') { |f| f.write "#{expected_contents}" }
844
- the_file = Dir['/*']
845
- assert_equal the_file.length, 1
846
- assert_equal the_file[0], "/#{filename}"
847
- contents = File.open("/#{filename}").read
848
- assert_equal contents, expected_contents
849
- end
850
-
851
- def test_file_object_initialization_with_utf_chars
852
- filename = "\u65e5\u672c\u8a9e.txt"
853
- expected_contents = 'Yokudekimashita'
854
- # nothing raised
855
- File.open(filename, mode: 'w') { |f| f.write "#{expected_contents}" }
856
- contents = File.open("/#{filename}").read
857
- assert_equal contents, expected_contents
858
- end
859
-
860
- def test_file_read_errors_appropriately
861
- assert_raises Errno::ENOENT do
862
- File.read('anything')
863
- end
864
- end
865
-
866
- def test_file_read_errors_on_directory
867
- FileUtils.mkdir_p('a_directory')
868
-
869
- assert_raises Errno::EISDIR do
870
- File.read('a_directory')
871
- end
872
- end
873
-
874
- def test_knows_files_are_files
875
- path = 'file.txt'
876
- File.open(path, 'w') do |f|
877
- f.write 'Yatta!'
878
- end
879
-
880
- assert File.file?(path)
881
- end
882
-
883
- def test_size_returns_size
884
- first_file = 'first.txt'
885
- File.open(first_file, 'w') do |f|
886
- f.write '12345678'
887
- end
888
-
889
- assert_equal File.size?(first_file), 8
890
-
891
- File.open(first_file, 'w') do |f|
892
- f.write 'abcd'
893
- end
894
-
895
- assert_equal File.size?(first_file), 4
896
-
897
- second_file = 'second.txt'
898
- File.open(second_file, 'w') do |f|
899
- f.write '1'
900
- end
901
- assert_equal File.size?(second_file), 1
902
- end
903
-
904
- def test_File_io_returns_self
905
- f = File.open('foo', 'w')
906
- assert_equal f, f.to_io
907
- end
908
-
909
- def test_File_to_i_is_alias_for_filno
910
- f = File.open('foo', 'w')
911
- assert_equal f.method(:to_i), f.method(:fileno)
912
- end
913
-
914
- def test_knows_symlink_files_are_files
915
- path = 'file.txt'
916
- File.open(path, 'w') do |f|
917
- f.write 'Yatta!'
918
- end
919
- FileUtils.ln_s path, sympath = '/sympath'
920
-
921
- assert File.file?(sympath)
922
- end
923
-
924
- def test_knows_non_existent_files_arent_files
925
- assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
926
- end
927
-
928
- def test_executable_returns_false_for_non_existent_files
929
- refute File.executable?('/does/not/exist')
930
- end
931
-
932
- def test_can_chown_files
933
- good = 'file.txt'
934
- bad = 'nofile.txt'
935
- File.open(good, 'w') { |f| f.write 'foo' }
936
- username = Etc.getpwuid(Process.uid).name
937
- groupname = Etc.getgrgid(Process.gid).name
938
-
939
- out = FileUtils.chown(1337, 1338, good, verbose: true)
940
- assert_equal [good], out
941
- assert_equal File.stat(good).uid, 1337
942
- assert_equal File.stat(good).gid, 1338
943
- assert_raises(Errno::ENOENT) do
944
- FileUtils.chown(username, groupname, bad, verbose: true)
945
- end
946
-
947
- assert_equal [good], FileUtils.chown(username, groupname, good)
948
- assert_equal File.stat(good).uid, Process.uid
949
- assert_equal File.stat(good).gid, Process.gid
950
- assert_raises(Errno::ENOENT) do
951
- FileUtils.chown(username, groupname, bad)
952
- end
953
-
954
- assert_equal [good], FileUtils.chown(username, groupname, [good])
955
- assert_equal File.stat(good).uid, Process.uid
956
- assert_equal File.stat(good).gid, Process.gid
957
- assert_raises(Errno::ENOENT) do
958
- FileUtils.chown(username, groupname, [good, bad])
959
- end
960
-
961
- # FileUtils.chown with nil user and nil group should not change anything
962
- FileUtils.chown(username, groupname, good)
963
- assert_equal File.stat(good).uid, Process.uid
964
- assert_equal File.stat(good).gid, Process.gid
965
- assert_equal [good], FileUtils.chown(nil, nil, [good])
966
- assert_equal File.stat(good).uid, Process.uid
967
- assert_equal File.stat(good).gid, Process.gid
968
- assert_raises(Errno::ENOENT) do
969
- FileUtils.chown(nil, nil, [good, bad])
970
- end
971
- end
972
-
973
- def test_can_chown_R_files
974
- username = Etc.getpwuid(Process.uid).name
975
- groupname = Etc.getgrgid(Process.gid).name
976
- FileUtils.mkdir_p '/path/'
977
- File.open('/path/foo', 'w') { |f| f.write 'foo' }
978
- File.open('/path/foobar', 'w') { |f| f.write 'foo' }
979
- assert_equal ['/path'], FileUtils.chown_R(username, groupname, '/path')
980
- %w(/path /path/foo /path/foobar).each do |f|
981
- assert_equal File.stat(f).uid, Process.uid
982
- assert_equal File.stat(f).gid, Process.gid
983
- end
984
- end
985
-
986
- def test_can_chmod_files
987
- good = 'file.txt'
988
- bad = 'nofile.txt'
989
- FileUtils.touch(good)
990
-
991
- assert_equal [good], FileUtils.chmod(0600, good, verbose: true)
992
- assert_equal File.stat(good).mode, 0100600
993
- assert_equal File.executable?(good), false
994
- assert_raises(Errno::ENOENT) do
995
- FileUtils.chmod(0600, bad)
996
- end
997
-
998
- assert_equal [good], FileUtils.chmod(0666, good)
999
- assert_equal File.stat(good).mode, 0100666
1000
- assert_raises(Errno::ENOENT) do
1001
- FileUtils.chmod(0666, bad)
1002
- end
1003
-
1004
- assert_equal [good], FileUtils.chmod(0644, [good])
1005
- assert_equal File.stat(good).mode, 0100644
1006
- assert_raises(Errno::ENOENT) do
1007
- FileUtils.chmod(0644, bad)
1008
- end
1009
-
1010
- assert_equal [good], FileUtils.chmod(0744, [good])
1011
- assert_equal File.executable?(good), true
1012
-
1013
- # This behaviour is unimplemented, the spec below is only to show that it
1014
- # is a deliberate YAGNI omission.
1015
- assert_equal [good], FileUtils.chmod(0477, [good])
1016
- assert_equal File.executable?(good), false
1017
- end
1018
-
1019
- def test_can_chmod_R_files
1020
- FileUtils.mkdir_p '/path/sub'
1021
- FileUtils.touch '/path/file1'
1022
- FileUtils.touch '/path/sub/file2'
1023
-
1024
- assert_equal ['/path'], FileUtils.chmod_R(0600, '/path')
1025
- assert_equal File.stat('/path').mode, 0100600
1026
- assert_equal File.stat('/path/file1').mode, 0100600
1027
- assert_equal File.stat('/path/sub').mode, 0100600
1028
- assert_equal File.stat('/path/sub/file2').mode, 0100600
1029
-
1030
- FileUtils.mkdir_p '/path2'
1031
- FileUtils.touch '/path2/hej'
1032
- assert_equal ['/path2'], FileUtils.chmod_R(0600, '/path2')
1033
- end
1034
-
1035
- def test_dir_globs_paths
1036
- FileUtils.mkdir_p '/path'
1037
- File.open('/path/foo', 'w') { |f| f.write 'foo' }
1038
- File.open('/path/foobar', 'w') { |f| f.write 'foo' }
1039
- File.open('/path/.bar', 'w') { |f| f.write 'foo' }
1040
-
1041
- FileUtils.mkdir_p '/path/bar'
1042
- File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
1043
-
1044
- FileUtils.cp_r '/path/bar', '/path/bar2'
1045
-
1046
- assert_equal ['/path'], Dir['/path']
1047
- assert_equal ['/path/.bar'], Dir['**/{.*}']
1048
- assert_equal ['/path/.bar'], Dir['/path**/{.*}']
1049
- assert_equal ['/path/.bar'], Dir['/path/{.*}']
1050
- assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
1051
-
1052
- assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
1053
- assert_equal ['/path/foo'], Dir['/path/foo']
1054
-
1055
- assert_equal ['/path'], Dir['/path*']
1056
- assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
1057
- assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
1058
-
1059
- assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
1060
- assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar', '/tmp'], Dir['/**/*']
1061
-
1062
- assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
1063
- assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
1064
-
1065
- assert_equal ['/path/bar/baz', '/path/bar2/baz'], Dir['/path/bar/**/*', '/path/bar2/**/*']
1066
- assert_equal ['/path/bar/baz', '/path/bar2/baz', '/path/bar/baz'], Dir['/path/ba*/**/*', '/path/bar/**/*']
1067
-
1068
- FileUtils.cp_r '/path', '/otherpath'
1069
-
1070
- assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
1071
-
1072
- assert_equal ['/path/bar', '/path/foo'], Dir['/path/{foo,bar}']
1073
-
1074
- assert_equal ['/path/bar', '/path/bar2'], Dir['/path/bar{2,}']
1075
-
1076
- assert_equal ['/path/bar', '/path/foo'], Dir['{/nowhere,/path/{foo,bar}}']
1077
- assert_equal ['/path/bar', '/path/foo'], Dir['{/nowhere,/path/{foo,{bar,bar2/baz}}}']
1078
-
1079
- Dir.chdir '/path' do
1080
- assert_equal ['foo'], Dir['foo']
1081
- end
1082
- end
1083
-
1084
- def test_file_utils_cp_allows_verbose_option
1085
- File.open('foo', 'w') { |f| f << 'TEST' }
1086
- assert_equal "cp foo bar\n", capture_stderr { FileUtils.cp 'foo', 'bar', verbose: true }
1087
- end
1088
-
1089
- def test_file_utils_cp_allows_noop_option
1090
- File.open('foo', 'w') { |f| f << 'TEST' }
1091
- FileUtils.cp 'foo', 'bar', noop: true
1092
- refute File.exist?('bar'), 'does not actually copy'
1093
- end
1094
-
1095
- def test_file_utils_cp_raises_on_invalid_option
1096
- assert_raises ArgumentError do
1097
- FileUtils.cp 'foo', 'bar', whatisthis: "I don't know"
1098
- end
1099
- end
1100
-
1101
- def test_file_utils_cp_r_allows_verbose_option
1102
- FileUtils.touch '/foo'
1103
- assert_equal "cp -r /foo /bar\n", capture_stderr { FileUtils.cp_r '/foo', '/bar', verbose: true }
1104
- end
1105
-
1106
- def test_file_utils_cp_r_allows_noop_option
1107
- FileUtils.touch '/foo'
1108
- FileUtils.cp_r '/foo', '/bar', noop: true
1109
- refute File.exist?('/bar'), 'does not actually copy'
1110
- end
1111
-
1112
- def test_dir_glob_handles_root
1113
- FileUtils.mkdir_p '/path'
1114
-
1115
- # this fails. the root dir should be named '/' but it is '.'
1116
- assert_equal ['/'], Dir['/']
1117
- end
1118
-
1119
- def test_dir_glob_takes_optional_flags
1120
- FileUtils.touch '/foo'
1121
- assert_equal Dir.glob('/*', 0), ['/foo', '/tmp']
1122
- end
1123
-
1124
- def test_dir_glob_handles_recursive_globs
1125
- FileUtils.mkdir_p '/one/two/three'
1126
- File.open('/one/two/three/four.rb', 'w')
1127
- File.open('/one/five.rb', 'w')
1128
- assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
1129
- assert_equal ['/one/two'], Dir['/one/**/two']
1130
- assert_equal ['/one/two/three'], Dir['/one/**/three']
1131
- end
1132
-
1133
- def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
1134
- FileUtils.mkdir_p '/one/two/three'
1135
- File.open('/one/two/three/four.rb', 'w')
1136
- File.open('/one/five.rb', 'w')
1137
- assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
1138
- assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**']
1139
- end
1140
-
1141
- def test_dir_glob_ending_in_group_and_wildcard
1142
- FileUtils.mkdir_p '/tmp/python-3.4.1'
1143
- FileUtils.mkdir_p '/tmp/python-2.7.8'
1144
- assert_equal ['/tmp/python-2.7.8', '/tmp/python-3.4.1'], Dir.glob('/tmp/python-[0-9]*')
1145
- end
1146
-
1147
- def test_dir_glob_with_block
1148
- FileUtils.touch('foo')
1149
- FileUtils.touch('bar')
1150
-
1151
- yielded = []
1152
- Dir.glob('*') { |file| yielded << file }
1153
-
1154
- assert_equal 3, yielded.size
1155
- end
1156
-
1157
- def test_copy_with_subdirectory
1158
- FileUtils.mkdir_p '/one/two/three/'
1159
- FileUtils.mkdir_p '/onebis/two/three/'
1160
- FileUtils.touch '/one/two/three/foo'
1161
- Dir.glob('/one/two/three/*') do |hook|
1162
- FileUtils.cp(hook, '/onebis/two/three/')
1163
- end
1164
- assert_equal ['/onebis/two/three/foo'], Dir['/onebis/two/three/*']
1165
- end
1166
-
1167
- if RUBY_VERSION >= '1.9'
1168
- def test_dir_home
1169
- assert_equal RealDir.home, Dir.home
1170
- end
1171
- end
1172
-
1173
- def test_should_report_pos_as_0_when_opening
1174
- File.open('foo', 'w') do |f|
1175
- f << 'foobar'
1176
- f.rewind
1177
-
1178
- assert_equal 0, f.pos
1179
- end
1180
- end
1181
-
1182
- def test_should_report_pos_as_1_when_seeking_one_char
1183
- File.open('foo', 'w') do |f|
1184
- f << 'foobar'
1185
-
1186
- f.rewind
1187
- f.seek(1)
1188
-
1189
- assert_equal 1, f.pos
1190
- end
1191
- end
1192
-
1193
- def test_should_set_pos
1194
- File.open('foo', 'w') do |f|
1195
- f << 'foo'
1196
- end
1197
-
1198
- fp = File.open('foo', 'r')
1199
- fp.pos = 1
1200
-
1201
- assert_equal 1, fp.pos
1202
- end
1203
-
1204
- def test_should_set_pos_with_tell_method
1205
- File.open('foo', 'w') do |f|
1206
- f << 'foo'
1207
- end
1208
-
1209
- fp = File.open('foo', 'r')
1210
- fp.tell = 1
1211
-
1212
- assert_equal 1, fp.pos
1213
- end
1214
-
1215
- OMITTED_FILE_METHODS = [
1216
- # omit methods from io/console
1217
- :raw, :raw!, :cooked, :cooked!,
1218
- :echo?, :echo=, :noecho,
1219
- :winsize, :winsize=,
1220
- :getch,
1221
- :iflush, :ioflush, :oflush,
1222
- :pathconf
1223
- ]
1224
-
1225
- def test_every_method_in_file_is_in_fake_fs_file
1226
- (RealFile.instance_methods - OMITTED_FILE_METHODS).each do |method_name|
1227
- assert File.instance_methods.include?(method_name), "#{method_name} method is not available in File :("
1228
- end
1229
- end
1230
-
1231
- def test_file_should_not_respond_to_string_io_unique_methods
1232
- uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
1233
- uniq_string_io_methods.each do |method_name|
1234
- refute File.instance_methods.include?(method_name), "File responds to #{method_name}"
1235
- end
1236
- end
1237
-
1238
- def test_does_not_remove_methods_from_stringio
1239
- stringio = StringIO.new('foo')
1240
- assert stringio.respond_to?(:size)
1241
- end
1242
-
1243
- def test_is_not_a_stringio
1244
- File.open('foo', 'w') do |f|
1245
- refute f.is_a?(StringIO), 'File is not a StringIO'
1246
- end
1247
- end
1248
-
1249
- def test_chdir_changes_directories_like_a_boss
1250
- # I know memes!
1251
- FileUtils.mkdir_p '/path'
1252
- assert_equal '/', FakeFS::FileSystem.fs.name
1253
- assert_equal [], Dir.glob('/path/*')
1254
- Dir.chdir '/path' do
1255
- File.open('foo', 'w') { |f| f.write 'foo' }
1256
- File.open('foobar', 'w') { |f| f.write 'foo' }
1257
- end
1258
-
1259
- assert_equal '/', FakeFS::FileSystem.fs.name
1260
- assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
1261
-
1262
- c = nil
1263
- Dir.chdir '/path' do
1264
- c = File.open('foo', 'r') { |f| f.read }
1265
- end
1266
-
1267
- assert_equal 'foo', c
1268
- end
1269
-
1270
- def test_chdir_shouldnt_keep_us_from_absolute_paths
1271
- FileUtils.mkdir_p '/path'
1272
-
1273
- Dir.chdir '/path' do
1274
- File.open('foo', 'w') { |f| f.write 'foo' }
1275
- File.open('/foobar', 'w') { |f| f.write 'foo' }
1276
- end
1277
- assert_equal ['/path/foo'], Dir.glob('/path/*').sort
1278
- assert_equal ['/foobar', '/path', '/tmp'], Dir.glob('/*').sort
1279
-
1280
- Dir.chdir '/path' do
1281
- FileUtils.rm('foo')
1282
- FileUtils.rm('/foobar')
1283
- end
1284
-
1285
- assert_equal [], Dir.glob('/path/*').sort
1286
- assert_equal ['/path', '/tmp'], Dir.glob('/*').sort
1287
- end
1288
-
1289
- def test_chdir_should_be_nestable
1290
- FileUtils.mkdir_p '/path/me'
1291
- Dir.chdir '/path' do
1292
- File.open('foo', 'w') { |f| f.write 'foo' }
1293
- Dir.chdir 'me' do
1294
- File.open('foobar', 'w') { |f| f.write 'foo' }
1295
- end
1296
- end
1297
-
1298
- assert_equal ['/path/foo', '/path/me'], Dir.glob('/path/*').sort
1299
- assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
1300
- end
1301
-
1302
- def test_chdir_should_be_nestable_with_absolute_paths
1303
- FileUtils.mkdir_p '/path/me'
1304
- Dir.chdir '/path' do
1305
- File.open('foo', 'w') { |f| f.write 'foo' }
1306
- Dir.chdir '/path/me' do
1307
- File.open('foobar', 'w') { |f| f.write 'foo' }
1308
- end
1309
- end
1310
-
1311
- assert_equal ['/path/foo', '/path/me'], Dir.glob('/path/*').sort
1312
- assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
1313
- end
1314
-
1315
- def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
1316
- assert_raises(Errno::ENOENT) do
1317
- Dir.chdir('/nope') do
1318
- 1
1319
- end
1320
- end
1321
- end
1322
-
1323
- def test_chdir_shouldnt_lose_state_because_of_errors
1324
- FileUtils.mkdir_p '/path'
1325
-
1326
- Dir.chdir '/path' do
1327
- File.open('foo', 'w') { |f| f.write 'foo' }
1328
- File.open('foobar', 'w') { |f| f.write 'foo' }
1329
- end
1330
-
1331
- begin
1332
- Dir.chdir('/path') do
1333
- fail Errno::ENOENT
1334
- end
1335
- rescue Errno::ENOENT => e # hardcore
1336
- 'Nothing to do'
1337
- end
1338
-
1339
- Dir.chdir('/path') do
1340
- begin
1341
- Dir.chdir('nope') {}
1342
- rescue Errno::ENOENT => e
1343
- 'Nothing to do'
1344
- end
1345
-
1346
- assert_equal ['/', '/path'], FakeFS::FileSystem.dir_levels
1347
- end
1348
-
1349
- assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
1350
- end
1351
-
1352
- def test_chdir_with_no_block_is_awesome
1353
- FileUtils.mkdir_p '/path'
1354
- Dir.chdir('/path')
1355
- FileUtils.mkdir_p 'subdir'
1356
- assert_equal ['subdir'], Dir.glob('*')
1357
- Dir.chdir('subdir')
1358
- File.open('foo', 'w') { |f| f.write 'foo' }
1359
- assert_equal ['foo'], Dir.glob('*')
1360
-
1361
- assert_raises(Errno::ENOENT) do
1362
- Dir.chdir('subsubdir')
1363
- end
1364
-
1365
- assert_equal ['foo'], Dir.glob('*')
1366
- end
1367
-
1368
- def test_current_dir_reflected_by_pwd
1369
- FileUtils.mkdir_p '/path'
1370
- Dir.chdir('/path')
1371
-
1372
- assert_equal '/path', Dir.pwd
1373
- assert_equal '/path', Dir.getwd
1374
-
1375
- FileUtils.mkdir_p 'subdir'
1376
- Dir.chdir('subdir')
1377
-
1378
- assert_equal '/path/subdir', Dir.pwd
1379
- assert_equal '/path/subdir', Dir.getwd
1380
- end
1381
-
1382
- def test_current_dir_reflected_by_expand_path_with_relative_paths
1383
- FileUtils.mkdir_p '/path'
1384
- Dir.chdir '/path'
1385
-
1386
- assert_equal '/path', File.expand_path('.')
1387
- assert_equal '/path/foo', File.expand_path('foo')
1388
-
1389
- FileUtils.mkdir_p 'subdir'
1390
- Dir.chdir 'subdir'
1391
-
1392
- assert_equal '/path/subdir', File.expand_path('.')
1393
- assert_equal '/path/subdir/foo', File.expand_path('foo')
1394
- end
1395
-
1396
- def test_expand_path_with_parent_dir
1397
- FakeFS.deactivate!
1398
- real = File.expand_path('../other.file', __FILE__)
1399
- FakeFS.activate!
1400
- fake = File.expand_path('../other.file', __FILE__)
1401
- assert_equal real, fake
1402
- end
1403
-
1404
- def test_expand_path_works_with_absolute_paths
1405
- FakeFS.deactivate!
1406
- home = File.expand_path('~')
1407
- FakeFS.activate!
1408
- assert_equal "#{home}/dir/subdir", File.expand_path('subdir', '~/dir')
1409
- assert_equal '/somewhere/else', File.expand_path('else', '/somewhere')
1410
- end
1411
-
1412
- def test_file_open_defaults_to_read
1413
- File.open('foo', 'w') { |f| f.write 'bar' }
1414
- assert_equal 'bar', File.open('foo') { |f| f.read }
1415
- end
1416
-
1417
- def test_flush_exists_on_file
1418
- r = File.open('foo', 'w') do |f|
1419
- f.write 'bar'
1420
- f.flush
1421
- end
1422
- assert_equal 'foo', r.path
1423
- end
1424
-
1425
- def test_mv_should_raise_error_on_missing_file
1426
- assert_raises(Errno::ENOENT) do
1427
- FileUtils.mv 'blafgag', 'foo'
1428
- end
1429
- exception = assert_raises(Errno::ENOENT) do
1430
- FileUtils.mv %w(foo bar), 'destdir'
1431
- end
1432
- assert_equal 'No such file or directory - foo', exception.message
1433
- end
1434
-
1435
- def test_mv_actually_works
1436
- File.open('foo', 'w') { |f| f.write 'bar' }
1437
- FileUtils.mv 'foo', 'baz'
1438
- assert_equal 'bar', File.open('baz') { |f| f.read }
1439
- end
1440
-
1441
- def test_mv_overwrites_existing_files
1442
- File.open('foo', 'w') { |f| f.write 'bar' }
1443
- File.open('baz', 'w') { |f| f.write 'qux' }
1444
- FileUtils.mv 'foo', 'baz'
1445
- assert_equal 'bar', File.read('baz')
1446
- end
1447
-
1448
- def test_mv_works_with_options
1449
- File.open('foo', 'w') { |f| f.write 'bar' }
1450
- FileUtils.mv 'foo', 'baz', force: true
1451
- assert_equal('bar', File.open('baz') { |f| f.read })
1452
- end
1453
-
1454
- def test_mv_to_directory
1455
- File.open('foo', 'w') { |f| f.write 'bar' }
1456
- FileUtils.mkdir_p 'destdir'
1457
- FileUtils.mv 'foo', 'destdir'
1458
- assert_equal('bar', File.open('destdir/foo') { |f| f.read })
1459
- assert File.directory?('destdir')
1460
- end
1461
-
1462
- def test_mv_array
1463
- File.open('foo', 'w') { |f| f.write 'bar' }
1464
- File.open('baz', 'w') { |f| f.write 'binky' }
1465
- FileUtils.mkdir_p 'destdir'
1466
- FileUtils.mv %w(foo baz), 'destdir'
1467
- assert_equal('bar', File.open('destdir/foo') { |f| f.read })
1468
- assert_equal('binky', File.open('destdir/baz') { |f| f.read })
1469
- end
1470
-
1471
- def test_mv_accepts_verbose_option
1472
- FileUtils.touch 'foo'
1473
- assert_equal "mv foo bar\n", capture_stderr { FileUtils.mv 'foo', 'bar', verbose: true }
1474
- end
1475
-
1476
- def test_mv_accepts_noop_option
1477
- FileUtils.touch 'foo'
1478
- FileUtils.mv 'foo', 'bar', noop: true
1479
- assert File.exist?('foo'), 'does not remove src'
1480
- refute File.exist?('bar'), 'does not create target'
1481
- end
1482
-
1483
- def test_mv_raises_when_moving_file_onto_directory
1484
- FileUtils.mkdir_p 'dir/stuff'
1485
- FileUtils.touch 'stuff'
1486
- assert_raises Errno::EEXIST do
1487
- FileUtils.mv 'stuff', 'dir'
1488
- end
1489
- end
1490
-
1491
- def test_mv_raises_when_moving_to_non_existent_directory
1492
- FileUtils.touch 'stuff'
1493
- assert_raises Errno::ENOENT do
1494
- FileUtils.mv 'stuff', '/this/path/is/not/here'
1495
- end
1496
- end
1497
-
1498
- def test_mv_ignores_failures_when_using_force
1499
- FileUtils.mkdir_p 'dir/stuff'
1500
- FileUtils.touch %w(stuff other)
1501
- FileUtils.mv %w(stuff other), 'dir', force: true
1502
- assert File.exist?('stuff'), 'failed move remains where it was'
1503
- assert File.exist?('dir/other'), 'successful one is moved'
1504
- refute File.exist?('other'), 'successful one is moved'
1505
-
1506
- FileUtils.mv 'stuff', '/this/path/is/not/here', force: true
1507
- assert File.exist?('stuff'), 'failed move remains where it was'
1508
- refute File.exist?('/this/path/is/not/here'), 'nothing is created for a failed move'
1509
- end
1510
-
1511
- def test_cp_actually_works
1512
- File.open('foo', 'w') { |f| f.write 'bar' }
1513
- FileUtils.cp('foo', 'baz')
1514
- assert_equal 'bar', File.read('baz')
1515
- end
1516
-
1517
- def test_cp_file_into_dir
1518
- File.open('foo', 'w') { |f| f.write 'bar' }
1519
- FileUtils.mkdir_p 'baz'
1520
-
1521
- FileUtils.cp('foo', 'baz')
1522
- assert_equal 'bar', File.read('baz/foo')
1523
- end
1524
-
1525
- def test_cp_array_of_files_into_directory
1526
- File.open('foo', 'w') { |f| f.write 'footext' }
1527
- File.open('bar', 'w') { |f| f.write 'bartext' }
1528
- FileUtils.mkdir_p 'destdir'
1529
- FileUtils.cp(%w(foo bar), 'destdir')
1530
-
1531
- assert_equal 'footext', File.read('destdir/foo')
1532
- assert_equal 'bartext', File.read('destdir/bar')
1533
- end
1534
-
1535
- def test_cp_fails_on_array_of_files_into_non_directory
1536
- File.open('foo', 'w') { |f| f.write 'footext' }
1537
-
1538
- exception = assert_raises(Errno::ENOTDIR) do
1539
- FileUtils.cp(%w(foo), 'baz')
1540
- end
1541
- assert_equal 'Not a directory - baz', exception.to_s
1542
- end
1543
-
1544
- def test_cp_overwrites_dest_file
1545
- File.open('foo', 'w') { |f| f.write 'FOO' }
1546
- File.open('bar', 'w') { |f| f.write 'BAR' }
1547
-
1548
- FileUtils.cp('foo', 'bar')
1549
- assert_equal 'FOO', File.read('bar')
1550
- end
1551
-
1552
- def test_cp_fails_on_no_source
1553
- assert_raises Errno::ENOENT do
1554
- FileUtils.cp('foo', 'baz')
1555
- end
1556
- end
1557
-
1558
- def test_cp_fails_on_directory_copy
1559
- FileUtils.mkdir_p 'baz'
1560
-
1561
- assert_raises Errno::EISDIR do
1562
- FileUtils.cp('baz', 'bar')
1563
- end
1564
- end
1565
-
1566
- def test_copy_file_works
1567
- File.open('foo', 'w') { |f| f.write 'bar' }
1568
- FileUtils.copy_file('foo', 'baz', :ignore_param_1, :ignore_param_2)
1569
- assert_equal 'bar', File.read('baz')
1570
- end
1571
-
1572
- def test_cp_r_doesnt_tangle_files_together
1573
- File.open('foo', 'w') { |f| f.write 'bar' }
1574
- FileUtils.cp_r('foo', 'baz')
1575
- File.open('baz', 'w') { |f| f.write 'quux' }
1576
- assert_equal 'bar', File.open('foo') { |f| f.read }
1577
- end
1578
-
1579
- def test_cp_r_should_raise_error_on_missing_file
1580
- # Yes, this error sucks, but it conforms to the original Ruby
1581
- # method.
1582
- assert_raises(RuntimeError) do
1583
- FileUtils.cp_r 'blafgag', 'foo'
1584
- end
1585
- end
1586
-
1587
- def test_cp_r_handles_copying_directories
1588
- FileUtils.mkdir_p 'subdir'
1589
- Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
1590
-
1591
- FileUtils.mkdir_p 'baz'
1592
-
1593
- # To a previously uncreated directory
1594
- FileUtils.cp_r('subdir', 'quux')
1595
- assert_equal 'footext', File.open('quux/foo') { |f| f.read }
1596
-
1597
- # To a directory that already exists
1598
- FileUtils.cp_r('subdir', 'baz')
1599
- assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
1600
-
1601
- # To a subdirectory of a directory that does not exist
1602
- assert_raises(Errno::ENOENT) do
1603
- FileUtils.cp_r('subdir', 'nope/something')
1604
- end
1605
- end
1606
-
1607
- def test_cp_r_array_of_files
1608
- FileUtils.mkdir_p 'subdir'
1609
- File.open('foo', 'w') { |f| f.write 'footext' }
1610
- File.open('bar', 'w') { |f| f.write 'bartext' }
1611
- FileUtils.cp_r(%w(foo bar), 'subdir')
1612
-
1613
- assert_equal 'footext', File.open('subdir/foo') { |f| f.read }
1614
- assert_equal 'bartext', File.open('subdir/bar') { |f| f.read }
1615
- end
1616
-
1617
- def test_cp_r_array_of_directories
1618
- %w(foo bar subdir).each { |d| FileUtils.mkdir_p d }
1619
- File.open('foo/baz', 'w') { |f| f.write 'baztext' }
1620
- File.open('bar/quux', 'w') { |f| f.write 'quuxtext' }
1621
-
1622
- FileUtils.cp_r(%w(foo bar), 'subdir')
1623
- assert_equal 'baztext', File.open('subdir/foo/baz') { |f| f.read }
1624
- assert_equal 'quuxtext', File.open('subdir/bar/quux') { |f| f.read }
1625
- end
1626
-
1627
- def test_cp_r_only_copies_into_directories
1628
- FileUtils.mkdir_p 'subdir'
1629
- Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
1630
-
1631
- File.open('bar', 'w') { |f| f.write 'bartext' }
1632
-
1633
- assert_raises(Errno::EEXIST) do
1634
- FileUtils.cp_r 'subdir', 'bar'
1635
- end
1636
-
1637
- FileUtils.mkdir_p 'otherdir'
1638
- FileUtils.ln_s 'otherdir', 'symdir'
1639
-
1640
- FileUtils.cp_r 'subdir', 'symdir'
1641
- assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
1642
- end
1643
-
1644
- def test_cp_r_sets_parent_correctly
1645
- FileUtils.mkdir_p '/path/foo'
1646
- File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
1647
- File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
1648
-
1649
- FileUtils.cp_r '/path/foo', '/path/bar'
1650
-
1651
- assert File.exist?('/path/bar/baz')
1652
- FileUtils.rm_rf '/path/bar/baz'
1653
- assert_equal %w( /path/bar/bar ), Dir['/path/bar/*']
1654
- end
1655
-
1656
- def test_clone_clones_normal_files
1657
- act_on_real_fs do
1658
- File.open(real_file_sandbox('foo'), 'w') do |f|
1659
- f.write 'bar'
1660
- end
1661
-
1662
- assert RealFile.file?(real_file_sandbox('foo'))
1663
- assert File.file?(real_file_sandbox('foo'))
1664
- end
1665
-
1666
- assert RealFile.file?(real_file_sandbox('foo'))
1667
-
1668
- refute File.exist?(real_file_sandbox('foo'))
1669
- FakeFS::FileSystem.clone(real_file_sandbox('foo'))
1670
- assert_equal 'bar', File.open(real_file_sandbox('foo')) { |f| f.read }
1671
- end
1672
-
1673
- def test_clone_clones_directories
1674
- act_on_real_fs { FileUtils.mkdir_p(real_file_sandbox('subdir')) }
1675
-
1676
- FakeFS::FileSystem.clone(real_file_sandbox('subdir'))
1677
-
1678
- assert File.exist?(real_file_sandbox('subdir')), 'subdir was cloned'
1679
- assert File.directory?(real_file_sandbox('subdir')), 'subdir is a directory'
1680
- end
1681
-
1682
- def test_clone_clones_dot_files_even_hard_to_find_ones
1683
- act_on_real_fs { FileUtils.mkdir_p(real_file_sandbox('subdir/.bar/baz/.quux/foo')) }
1684
-
1685
- refute File.exist?(real_file_sandbox('subdir'))
1686
-
1687
- FakeFS::FileSystem.clone(real_file_sandbox('subdir'))
1688
- assert_equal ['.', '..', '.bar'], Dir.entries(real_file_sandbox('subdir'))
1689
- assert_equal ['.', '..', 'foo'], Dir.entries(real_file_sandbox('subdir/.bar/baz/.quux'))
1690
- end
1691
-
1692
- def test_dir_glob_on_clone_with_absolute_path
1693
- act_on_real_fs { FileUtils.mkdir_p(real_file_sandbox('subdir/.bar/baz/.quux/foo')) }
1694
- FileUtils.mkdir_p '/path'
1695
- Dir.chdir('/path')
1696
- FakeFS::FileSystem.clone(real_file_sandbox('subdir'), '/foo')
1697
- assert Dir.glob '/foo/*'
1698
- end
1699
-
1700
- def test_clone_with_target_specified
1701
- act_on_real_fs do
1702
- assert FileUtils == RealFileUtils, 'using the real FileUtils in act_on_real_fs'
1703
- FileUtils.mkdir_p(real_file_sandbox('subdir/.bar/baz/.quux/foo'))
1704
- end
1705
-
1706
- refute File.exist?(real_file_sandbox('subdir'))
1707
-
1708
- FakeFS::FileSystem.clone(real_file_sandbox('subdir'), real_file_sandbox('subdir2'))
1709
- refute File.exist?(real_file_sandbox('subdir'))
1710
- assert_equal ['.', '..', '.bar'], Dir.entries(real_file_sandbox('subdir2'))
1711
- assert_equal ['.', '..', 'foo'], Dir.entries(real_file_sandbox('subdir2/.bar/baz/.quux'))
1712
- end
1713
-
1714
- def test_clone_with_file_symlinks
1715
- original = real_file_sandbox('subdir/test-file')
1716
- symlink = real_file_sandbox('subdir/test-file.txt')
1717
-
1718
- act_on_real_fs do
1719
- FileUtils.mkdir_p(File.dirname(original))
1720
- File.open(original, 'w') { |f| f << 'stuff' }
1721
- FileUtils.ln_s original, symlink
1722
- assert File.symlink?(symlink), 'real symlink is in place'
1723
- end
1724
-
1725
- refute File.exist?(original), 'file does not already exist'
1726
-
1727
- FakeFS::FileSystem.clone(File.dirname(original))
1728
- assert File.symlink?(symlink), 'symlinks are cloned as symlinks'
1729
- assert_equal 'stuff', File.read(symlink)
1730
- end
1731
-
1732
- def test_clone_with_dir_symlinks
1733
- original = real_file_sandbox('subdir/dir')
1734
- symlink = real_file_sandbox('subdir/dir.link')
1735
- original_file = File.join(original, 'test-file')
1736
- symlink_file = File.join(symlink, 'test-file')
1737
-
1738
- act_on_real_fs do
1739
- FileUtils.mkdir_p(original)
1740
- File.open(original_file, 'w') { |f| f << 'stuff' }
1741
- FileUtils.ln_s original, symlink
1742
- assert File.symlink?(symlink), 'real symlink is in place'
1743
- end
1744
-
1745
- refute File.exist?(original_file), 'file does not already exist'
1746
-
1747
- FakeFS::FileSystem.clone(File.dirname(original))
1748
- assert File.symlink?(symlink), 'symlinks are cloned as symlinks'
1749
- assert_equal 'stuff', File.read(symlink_file)
1750
- end
1751
-
1752
- def test_putting_a_dot_at_end_copies_the_contents
1753
- FileUtils.mkdir_p 'subdir'
1754
- Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
1755
-
1756
- FileUtils.mkdir_p 'newdir'
1757
- FileUtils.cp_r 'subdir/.', 'newdir'
1758
- assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
1759
- end
1760
-
1761
- def test_file_can_read_from_symlinks
1762
- File.open('first', 'w') { |f| f.write '1' }
1763
- FileUtils.ln_s 'first', 'one'
1764
- assert_equal '1', File.open('one') { |f| f.read }
1765
-
1766
- FileUtils.mkdir_p 'subdir'
1767
- File.open('subdir/nother', 'w') { |f| f.write 'works' }
1768
- FileUtils.ln_s 'subdir', 'new'
1769
- assert_equal 'works', File.open('new/nother') { |f| f.read }
1770
- end
1771
-
1772
- def test_can_symlink_through_file
1773
- FileUtils.touch('/foo')
1774
-
1775
- File.symlink('/foo', '/bar')
1776
-
1777
- assert File.symlink?('/bar')
1778
- end
1779
-
1780
- def test_files_can_be_touched
1781
- FileUtils.touch('touched_file')
1782
- assert File.exist?('touched_file')
1783
- list = %w(newfile another)
1784
- FileUtils.touch(list)
1785
- list.each { |fp| assert(File.exist?(fp)) }
1786
- end
1787
-
1788
- def test_touch_does_not_work_if_the_dir_path_cannot_be_found
1789
- assert_raises(Errno::ENOENT) do
1790
- FileUtils.touch('this/path/should/not/be/here')
1791
- end
1792
- FileUtils.mkdir_p('subdir')
1793
- list = ['subdir/foo', 'nosubdir/bar']
1794
-
1795
- assert_raises(Errno::ENOENT) do
1796
- FileUtils.touch(list)
1797
- end
1798
- end
1799
-
1800
- def test_extname
1801
- assert File.extname('test.doc') == '.doc'
1802
- end
1803
-
1804
- # Directory tests
1805
- def test_new_directory
1806
- FileUtils.mkdir_p('/this/path/should/be/here')
1807
-
1808
- # nothing raised
1809
- Dir.new('/this/path/should/be/here')
1810
- end
1811
-
1812
- def test_new_directory_does_not_work_if_dir_path_cannot_be_found
1813
- assert_raises(Errno::ENOENT) do
1814
- Dir.new('/this/path/should/not/be/here')
1815
- end
1816
- end
1817
-
1818
- def test_directory_close
1819
- FileUtils.mkdir_p('/this/path/should/be/here')
1820
- dir = Dir.new('/this/path/should/be/here')
1821
- assert dir.close.nil?
1822
-
1823
- assert_raises(IOError) do
1824
- dir.each { |d| d }
1825
- end
1826
- end
1827
-
1828
- def test_directory_each
1829
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1830
-
1831
- FileUtils.mkdir_p('/this/path/should/be/here')
1832
-
1833
- test.each do |f|
1834
- FileUtils.touch("/this/path/should/be/here/#{f}")
1835
- end
1836
-
1837
- dir = Dir.new('/this/path/should/be/here')
1838
-
1839
- yielded = []
1840
- dir.each do |d|
1841
- yielded << d
1842
- end
1843
-
1844
- assert yielded.size == test.size
1845
- test.each { |t| assert yielded.include?(t) }
1846
- end
1847
-
1848
- def test_directory_path
1849
- FileUtils.mkdir_p('/this/path/should/be/here')
1850
- good_path = '/this/path/should/be/here'
1851
- assert_equal good_path, Dir.new('/this/path/should/be/here').path
1852
- end
1853
-
1854
- def test_directory_pos
1855
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1856
- FileUtils.mkdir_p('/this/path/should/be/here')
1857
- test.each do |f|
1858
- FileUtils.touch("/this/path/should/be/here/#{f}")
1859
- end
1860
-
1861
- dir = Dir.new('/this/path/should/be/here')
1862
-
1863
- assert dir.pos == 0
1864
- dir.read
1865
- assert dir.pos == 1
1866
- dir.read
1867
- assert dir.pos == 2
1868
- dir.read
1869
- assert dir.pos == 3
1870
- dir.read
1871
- assert dir.pos == 4
1872
- dir.read
1873
- assert dir.pos == 5
1874
- end
1875
-
1876
- def test_directory_pos_assign
1877
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1878
-
1879
- FileUtils.mkdir_p('/this/path/should/be/here')
1880
- test.each do |f|
1881
- FileUtils.touch("/this/path/should/be/here/#{f}")
1882
- end
1883
-
1884
- dir = Dir.new('/this/path/should/be/here')
1885
-
1886
- assert dir.pos == 0
1887
- dir.pos = 2
1888
- assert dir.pos == 2
1889
- end
1890
-
1891
- def test_directory_read
1892
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1893
-
1894
- FileUtils.mkdir_p('/this/path/should/be/here')
1895
- test.each do |f|
1896
- FileUtils.touch("/this/path/should/be/here/#{f}")
1897
- end
1898
-
1899
- dir = Dir.new('/this/path/should/be/here')
1900
-
1901
- assert dir.pos == 0
1902
- d = dir.read
1903
- assert dir.pos == 1
1904
- assert d == '.'
1905
-
1906
- d = dir.read
1907
- assert dir.pos == 2
1908
- assert d == '..'
1909
- end
1910
-
1911
- def test_directory_read_past_length
1912
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1913
-
1914
- FileUtils.mkdir_p('/this/path/should/be/here')
1915
- test.each do |f|
1916
- FileUtils.touch("/this/path/should/be/here/#{f}")
1917
- end
1918
-
1919
- dir = Dir.new('/this/path/should/be/here')
1920
-
1921
- d = dir.read
1922
- refute_nil d
1923
- d = dir.read
1924
- refute_nil d
1925
- d = dir.read
1926
- refute_nil d
1927
- d = dir.read
1928
- refute_nil d
1929
- d = dir.read
1930
- refute_nil d
1931
- d = dir.read
1932
- refute_nil d
1933
- d = dir.read
1934
- refute_nil d
1935
- d = dir.read
1936
- assert_nil d
1937
- end
1938
-
1939
- def test_directory_rewind
1940
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1941
-
1942
- FileUtils.mkdir_p('/this/path/should/be/here')
1943
- test.each do |f|
1944
- FileUtils.touch("/this/path/should/be/here/#{f}")
1945
- end
1946
-
1947
- dir = Dir.new('/this/path/should/be/here')
1948
-
1949
- dir.read
1950
- dir.read
1951
- assert dir.pos == 2
1952
- dir.rewind
1953
- assert dir.pos == 0
1954
- end
1955
-
1956
- def test_directory_seek
1957
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1958
-
1959
- FileUtils.mkdir_p('/this/path/should/be/here')
1960
- test.each do |f|
1961
- FileUtils.touch("/this/path/should/be/here/#{f}")
1962
- end
1963
-
1964
- dir = Dir.new('/this/path/should/be/here')
1965
-
1966
- d = dir.seek 1
1967
- assert d == '..'
1968
- assert dir.pos == 1
1969
- end
1970
-
1971
- def test_directory_class_delete
1972
- FileUtils.mkdir_p('/this/path/should/be/here')
1973
- Dir.delete('/this/path/should/be/here')
1974
- assert File.exist?('/this/path/should/be/here') == false
1975
- end
1976
-
1977
- def test_directory_class_delete_does_not_act_on_non_empty_directory
1978
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1979
-
1980
- FileUtils.mkdir_p('/this/path/should/be/here')
1981
- test.each do |f|
1982
- FileUtils.touch("/this/path/should/be/here/#{f}")
1983
- end
1984
-
1985
- assert_raises(Errno::ENOTEMPTY) do
1986
- Dir.delete('/this/path/should/be/here')
1987
- end
1988
- end
1989
-
1990
- def test_directory_class_delete_does_not_work_if_dir_path_cannot_be_found
1991
- assert_raises(Errno::ENOENT) do
1992
- Dir.delete('/this/path/should/not/be/here')
1993
- end
1994
- end
1995
-
1996
- def test_directory_entries
1997
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
1998
-
1999
- FileUtils.mkdir_p('/this/path/should/be/here')
2000
-
2001
- test.each do |f|
2002
- FileUtils.touch("/this/path/should/be/here/#{f}")
2003
- end
2004
-
2005
- yielded = Dir.entries('/this/path/should/be/here')
2006
- assert yielded.size == test.size
2007
- test.each { |t| assert yielded.include?(t) }
2008
- end
2009
-
2010
- def test_directory_entries_works_with_trailing_slash
2011
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
2012
-
2013
- FileUtils.mkdir_p('/this/path/should/be/here')
2014
-
2015
- test.each do |f|
2016
- FileUtils.touch("/this/path/should/be/here/#{f}")
2017
- end
2018
-
2019
- yielded = Dir.entries('/this/path/should/be/here/')
2020
- assert yielded.size == test.size
2021
- test.each { |t| assert yielded.include?(t) }
2022
- end
2023
-
2024
- def test_directory_entries_does_not_work_if_dir_path_cannot_be_found
2025
- assert_raises(Errno::ENOENT) do
2026
- Dir.delete('/this/path/should/not/be/here')
2027
- end
2028
- end
2029
-
2030
- def test_directory_foreach
2031
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
2032
-
2033
- FileUtils.mkdir_p('/this/path/should/be/here')
2034
-
2035
- test.each do |f|
2036
- FileUtils.touch("/this/path/should/be/here/#{f}")
2037
- end
2038
-
2039
- yielded = []
2040
- Dir.foreach('/this/path/should/be/here') do |dir|
2041
- yielded << dir
2042
- end
2043
-
2044
- assert yielded.size == test.size
2045
- test.each { |t| assert yielded.include?(t) }
2046
- end
2047
-
2048
- def test_directory_foreach_relative_paths
2049
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
2050
-
2051
- FileUtils.mkdir_p('/this/path/should/be/here')
2052
-
2053
- test.each do |f|
2054
- FileUtils.touch("/this/path/should/be/here/#{f}")
2055
- end
2056
-
2057
- yielded = []
2058
- Dir.chdir '/this/path/should/be' do
2059
- Dir.foreach('here') do |dir|
2060
- yielded << dir
2061
- end
2062
- end
2063
-
2064
- assert yielded.size == test.size, 'wrong number of files yielded'
2065
- test.each { |t| assert yielded.include?(t), "#{t} was not included in #{yielded.inspect}" }
2066
- end
2067
-
2068
- def test_directory_mkdir
2069
- Dir.mkdir('/path')
2070
- assert File.exist?('/path')
2071
- end
2072
-
2073
- def test_directory_mkdir_nested
2074
- Dir.mkdir('/tmp/stream20120103-11847-xc8pb.lock')
2075
- assert File.exist?('/tmp/stream20120103-11847-xc8pb.lock')
2076
- end
2077
-
2078
- def test_can_create_subdirectories_with_dir_mkdir
2079
- Dir.mkdir 'foo'
2080
- Dir.mkdir 'foo/bar'
2081
- assert Dir.exist?('foo/bar')
2082
- end
2083
-
2084
- def test_can_create_absolute_subdirectories_with_dir_mkdir
2085
- Dir.mkdir '/foo'
2086
- Dir.mkdir '/foo/bar'
2087
- assert Dir.exist?('/foo/bar')
2088
- end
2089
-
2090
- def test_can_create_directories_starting_with_dot
2091
- Dir.mkdir './path'
2092
- assert File.exist? './path'
2093
- end
2094
-
2095
- def test_directory_mkdir_relative
2096
- FileUtils.mkdir_p('/new/root')
2097
- FakeFS::FileSystem.chdir('/new/root')
2098
- Dir.mkdir('path')
2099
- assert File.exist?('/new/root/path')
2100
- end
2101
-
2102
- def test_directory_mkdir_not_recursive
2103
- assert_raises(Errno::ENOENT) do
2104
- Dir.mkdir('/path/does/not/exist')
2105
- end
2106
- end
2107
-
2108
- def test_mkdir_raises_error_if_already_created
2109
- Dir.mkdir 'foo'
2110
-
2111
- assert_raises(Errno::EEXIST) do
2112
- Dir.mkdir 'foo'
2113
- end
2114
- end
2115
-
2116
- def test_directory_open
2117
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
2118
-
2119
- FileUtils.mkdir_p('/this/path/should/be/here')
2120
-
2121
- test.each do |f|
2122
- FileUtils.touch("/this/path/should/be/here/#{f}")
2123
- end
2124
-
2125
- dir = Dir.open('/this/path/should/be/here')
2126
- assert dir.path == '/this/path/should/be/here'
2127
- end
2128
-
2129
- def test_directory_open_block
2130
- test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5']
2131
-
2132
- FileUtils.mkdir_p('/this/path/should/be/here')
2133
-
2134
- test.each do |f|
2135
- FileUtils.touch("/this/path/should/be/here/#{f}")
2136
- end
2137
-
2138
- yielded = []
2139
- Dir.open('/this/path/should/be/here') do |dir|
2140
- yielded << dir
2141
- end
2142
-
2143
- assert yielded.size == test.size
2144
- test.each { |t| assert yielded.include?(t) }
2145
- end
2146
-
2147
- def test_directory_exists
2148
- assert Dir.exist?('/this/path/should/be/here') == false
2149
- assert Dir.exist?('/this/path/should/be/here') == false
2150
- FileUtils.mkdir_p('/this/path/should/be/here')
2151
- assert Dir.exist?('/this/path/should/be/here') == true
2152
- assert Dir.exist?('/this/path/should/be/here') == true
2153
- end
2154
-
2155
- def test_tmpdir
2156
- assert Dir.tmpdir == '/tmp'
2157
- end
2158
-
2159
- def test_rename_renames_a_file
2160
- FileUtils.touch('/foo')
2161
- File.rename('/foo', '/bar')
2162
- assert File.file?('/bar')
2163
- end
2164
-
2165
- def test_rename_returns
2166
- FileUtils.touch('/foo')
2167
- assert_equal 0, File.rename('/foo', '/bar')
2168
- end
2169
-
2170
- def test_rename_renames_two_files
2171
- FileUtils.touch('/foo')
2172
- FileUtils.touch('/bar')
2173
- File.rename('/foo', '/bar')
2174
- assert File.file?('/bar')
2175
- end
2176
-
2177
- def test_rename_renames_a_directories
2178
- Dir.mkdir('/foo')
2179
- File.rename('/foo', '/bar')
2180
- assert File.directory?('/bar')
2181
- end
2182
-
2183
- def test_rename_renames_two_directories
2184
- Dir.mkdir('/foo')
2185
- Dir.mkdir('/bar')
2186
- File.rename('/foo', '/bar')
2187
- assert File.directory?('/bar')
2188
- end
2189
-
2190
- def test_rename_file_to_directory_raises_error
2191
- FileUtils.touch('/foo')
2192
- Dir.mkdir('/bar')
2193
- assert_raises(Errno::EISDIR) do
2194
- File.rename('/foo', '/bar')
2195
- end
2196
- end
2197
-
2198
- def test_rename_directory_to_file_raises_error
2199
- Dir.mkdir('/foo')
2200
- FileUtils.touch('/bar')
2201
- assert_raises(Errno::ENOTDIR) do
2202
- File.rename('/foo', '/bar')
2203
- end
2204
- end
2205
-
2206
- def test_rename_with_missing_source_raises_error
2207
- assert_raises(Errno::ENOENT) do
2208
- File.rename('/no_such_file', '/bar')
2209
- end
2210
- end
2211
-
2212
- def test_rename_with_missing_dest_directory_raises_error
2213
- FileUtils.touch('/foo')
2214
- assert_raises(Errno::ENOENT) do
2215
- File.rename('/foo', '/bar/foo')
2216
- end
2217
- end
2218
-
2219
- def test_hard_link_creates_file
2220
- FileUtils.touch('/foo')
2221
-
2222
- File.link('/foo', '/bar')
2223
- assert File.exist?('/bar')
2224
- end
2225
-
2226
- def test_hard_link_with_missing_file_raises_error
2227
- assert_raises(Errno::ENOENT) do
2228
- File.link('/foo', '/bar')
2229
- end
2230
- end
2231
-
2232
- def test_hard_link_with_existing_destination_file
2233
- FileUtils.touch('/foo')
2234
- FileUtils.touch('/bar')
2235
-
2236
- assert_raises(Errno::EEXIST) do
2237
- File.link('/foo', '/bar')
2238
- end
2239
- end
2240
-
2241
- def test_hard_link_returns_0_when_successful
2242
- FileUtils.touch('/foo')
2243
-
2244
- assert_equal 0, File.link('/foo', '/bar')
2245
- end
2246
-
2247
- def test_hard_link_returns_duplicate_file
2248
- File.open('/foo', 'w') { |x| x << 'some content' }
2249
-
2250
- File.link('/foo', '/bar')
2251
- assert_equal 'some content', File.read('/bar')
2252
- end
2253
-
2254
- def test_hard_link_with_directory_raises_error
2255
- Dir.mkdir '/foo'
2256
-
2257
- assert_raises(Errno::EPERM) do
2258
- File.link('/foo', '/bar')
2259
- end
2260
- end
2261
-
2262
- def test_file_stat_returns_file_stat_object
2263
- FileUtils.touch('/foo')
2264
- assert_equal File::Stat, File.stat('/foo').class
2265
- end
2266
-
2267
- def test_can_delete_file_with_delete
2268
- FileUtils.touch('/foo')
2269
-
2270
- File.delete('/foo')
2271
-
2272
- refute File.exist?('/foo')
2273
- end
2274
-
2275
- def test_can_delete_multiple_files_with_delete
2276
- FileUtils.touch('/foo')
2277
- FileUtils.touch('/bar')
2278
-
2279
- File.delete('/foo', '/bar')
2280
-
2281
- refute File.exist?('/foo')
2282
- refute File.exist?('/bar')
2283
- end
2284
-
2285
- def test_delete_returns_zero_when_no_filename_given
2286
- assert_equal 0, File.delete
2287
- end
2288
-
2289
- def test_delete_returns_number_one_when_given_one_arg
2290
- FileUtils.touch('/foo')
2291
-
2292
- assert_equal 1, File.delete('/foo')
2293
- end
2294
-
2295
- def test_delete_returns_number_two_when_given_two_args
2296
- FileUtils.touch('/foo')
2297
- FileUtils.touch('/bar')
2298
-
2299
- assert_equal 2, File.delete('/foo', '/bar')
2300
- end
2301
-
2302
- def test_delete_raises_error_when_first_file_does_not_exist
2303
- assert_raises Errno::ENOENT do
2304
- File.delete('/foo')
2305
- end
2306
- end
2307
-
2308
- def test_unlink_removes_only_one_file_content
2309
- File.open('/foo', 'w') { |f| f << 'some_content' }
2310
- File.link('/foo', '/bar')
2311
-
2312
- File.unlink('/bar')
2313
- assert_equal 'some_content', File.read('/foo')
2314
- end
2315
-
2316
- def test_link_reports_correct_stat_info_after_unlinking
2317
- File.open('/foo', 'w') { |f| f << 'some_content' }
2318
- File.link('/foo', '/bar')
2319
-
2320
- File.unlink('/bar')
2321
- assert_equal 1, File.stat('/foo').nlink
2322
- end
2323
-
2324
- def test_delete_works_with_symlink
2325
- FileUtils.touch('/foo')
2326
- File.symlink('/foo', '/bar')
2327
-
2328
- File.unlink('/bar')
2329
-
2330
- assert File.exist?('/foo')
2331
- refute File.exist?('/bar')
2332
- end
2333
-
2334
- def test_delete_works_with_symlink_source
2335
- FileUtils.touch('/foo')
2336
- File.symlink('/foo', '/bar')
2337
-
2338
- File.unlink('/foo')
2339
-
2340
- refute File.exist?('/foo')
2341
- end
2342
-
2343
- def test_file_seek_returns_0
2344
- File.open('/foo', 'w') do |f|
2345
- f << "one\ntwo\nthree"
2346
- end
2347
-
2348
- file = File.open('/foo', 'r')
2349
-
2350
- assert_equal 0, file.seek(1)
2351
- end
2352
-
2353
- def test_file_seek_seeks_to_location
2354
- File.open('/foo', 'w') do |f|
2355
- f << '123'
2356
- end
2357
-
2358
- file = File.open('/foo', 'r')
2359
- file.seek(1)
2360
- assert_equal '23', file.read
2361
- end
2362
-
2363
- def test_file_seek_seeks_to_correct_location
2364
- File.open('/foo', 'w') do |f|
2365
- f << '123'
2366
- end
2367
-
2368
- file = File.open('/foo', 'r')
2369
- file.seek(2)
2370
- assert_equal '3', file.read
2371
- end
2372
-
2373
- def test_file_seek_can_take_negative_offset
2374
- File.open('/foo', 'w') do |f|
2375
- f << '123456789'
2376
- end
2377
-
2378
- file = File.open('/foo', 'r')
2379
-
2380
- file.seek(-1, IO::SEEK_END)
2381
- assert_equal '9', file.read
2382
-
2383
- file.seek(-2, IO::SEEK_END)
2384
- assert_equal '89', file.read
2385
-
2386
- file.seek(-3, IO::SEEK_END)
2387
- assert_equal '789', file.read
2388
- end
2389
-
2390
- def test_should_have_constants_inherited_from_descending_from_io
2391
- assert_equal IO::SEEK_CUR, File::SEEK_CUR
2392
- assert_equal IO::SEEK_END, File::SEEK_END
2393
- assert_equal IO::SEEK_SET, File::SEEK_SET
2394
- end
2395
-
2396
- def test_filetest_exists_return_correct_values
2397
- FileUtils.mkdir_p('/path/to/dir')
2398
- assert FileTest.exist?('/path/to/')
2399
-
2400
- FileUtils.rmdir('/path/to/dir')
2401
- refute FileTest.exist?('/path/to/dir')
2402
- end
2403
-
2404
- def test_filetest_directory_returns_correct_values
2405
- FileUtils.mkdir_p '/path/to/somedir'
2406
- assert FileTest.directory?('/path/to/somedir')
2407
-
2408
- FileUtils.rm_r '/path/to/somedir'
2409
- refute FileTest.directory?('/path/to/somedir')
2410
- end
2411
-
2412
- def test_filetest_file_returns_correct_values
2413
- FileUtils.mkdir_p('/path/to')
2414
-
2415
- path = '/path/to/file.txt'
2416
- File.open(path, 'w') { |f| f.write 'Yatta!' }
2417
- assert FileTest.file?(path)
2418
-
2419
- FileUtils.rm path
2420
- refute FileTest.file?(path)
2421
-
2422
- FileUtils.mkdir_p '/path/to/somedir'
2423
- refute FileTest.file?('/path/to/somedir')
2424
- end
2425
-
2426
- def test_filetest_readable_returns_correct_values
2427
- refute FileTest.readable?('not-here.txt'), 'missing files are not readable'
2428
-
2429
- FileUtils.touch 'here.txt'
2430
- assert FileTest.readable?('here.txt'), 'existing files are readable'
2431
-
2432
- FileUtils.mkdir 'dir'
2433
- assert FileTest.readable?('dir'), 'directories are readable'
2434
- end
2435
-
2436
- def test_filetest_writable_returns_correct_values
2437
- refute FileTest.writable?('not-here.txt'), 'missing files are not writable'
2438
-
2439
- FileUtils.touch 'here.txt'
2440
- assert FileTest.writable?('here.txt'), 'existing files are writable'
2441
-
2442
- FileUtils.mkdir 'dir'
2443
- assert FileTest.writable?('dir'), 'directories are writable'
2444
- end
2445
-
2446
- def test_dir_mktmpdir
2447
- # FileUtils.mkdir '/tmpdir'
2448
-
2449
- tmpdir = Dir.mktmpdir
2450
- assert File.directory?(tmpdir)
2451
- FileUtils.rm_r tmpdir
2452
-
2453
- Dir.mktmpdir do |t|
2454
- tmpdir = t
2455
- assert File.directory?(t)
2456
- end
2457
- refute File.directory?(tmpdir)
2458
- end
2459
-
2460
- def test_activating_returns_true
2461
- FakeFS.deactivate!
2462
- assert_equal true, FakeFS.activate!
2463
- end
2464
-
2465
- def test_deactivating_returns_true
2466
- assert_equal true, FakeFS.deactivate!
2467
- end
2468
-
2469
- def test_split
2470
- assert File.respond_to? :split
2471
- filename = '/this/is/what/we/expect.txt'
2472
- path, filename = File.split(filename)
2473
- assert_equal path, '/this/is/what/we'
2474
- assert_equal filename, 'expect.txt'
2475
- end
2476
-
2477
- #########################
2478
- def test_file_default_mode
2479
- FileUtils.touch 'foo'
2480
- assert_equal File.stat('foo').mode, (0100000 + 0666 - File.umask)
2481
- end
2482
-
2483
- def test_dir_default_mode
2484
- Dir.mkdir 'bar'
2485
- assert_equal File.stat('bar').mode, (0100000 + 0777 - File.umask)
2486
- end
2487
-
2488
- def test_file_default_uid_and_gid
2489
- FileUtils.touch 'foo'
2490
- assert_equal File.stat('foo').uid, Process.uid
2491
- assert_equal File.stat('foo').gid, Process.gid
2492
- end
2493
-
2494
- def test_file_chmod_of_file
2495
- FileUtils.touch 'foo'
2496
- File.chmod 0600, 'foo'
2497
- assert_equal File.stat('foo').mode, 0100600
2498
- File.new('foo').chmod 0644
2499
- assert_equal File.stat('foo').mode, 0100644
2500
- end
2501
-
2502
- def test_file_chmod_of_dir
2503
- Dir.mkdir 'bar'
2504
- File.chmod 0777, 'bar'
2505
- assert_equal File.stat('bar').mode, 0100777
2506
- File.new('bar').chmod 01700
2507
- assert_equal File.stat('bar').mode, 0101700
2508
- end
2509
-
2510
- def test_file_chown_of_file
2511
- FileUtils.touch 'foo'
2512
- File.chown 1337, 1338, 'foo'
2513
- assert_equal File.stat('foo').uid, 1337
2514
- assert_equal File.stat('foo').gid, 1338
2515
- end
2516
-
2517
- def test_file_chown_of_dir
2518
- Dir.mkdir 'bar'
2519
- File.chown 1337, 1338, 'bar'
2520
- assert_equal File.stat('bar').uid, 1337
2521
- assert_equal File.stat('bar').gid, 1338
2522
- end
2523
-
2524
- def test_file_chown_of_file_nil_user_group
2525
- FileUtils.touch 'foo'
2526
- File.chown 1337, 1338, 'foo'
2527
- File.chown nil, nil, 'foo'
2528
- assert_equal File.stat('foo').uid, 1337
2529
- assert_equal File.stat('foo').gid, 1338
2530
- end
2531
-
2532
- def test_file_chown_of_file_negative_user_group
2533
- FileUtils.touch 'foo'
2534
- File.chown 1337, 1338, 'foo'
2535
- File.chown(-1, -1, 'foo')
2536
- assert_equal File.stat('foo').uid, 1337
2537
- assert_equal File.stat('foo').gid, 1338
2538
- end
2539
-
2540
- def test_file_instance_chown_nil_user_group
2541
- FileUtils.touch('foo')
2542
- File.chown(1337, 1338, 'foo')
2543
- assert_equal File.stat('foo').uid, 1337
2544
- assert_equal File.stat('foo').gid, 1338
2545
- file = File.open('foo')
2546
- file.chown nil, nil
2547
- assert_equal File.stat('foo').uid, 1337
2548
- assert_equal File.stat('foo').gid, 1338
2549
- end
2550
-
2551
- def test_file_instance_chown_negative_user_group
2552
- FileUtils.touch('foo')
2553
- File.chown(1337, 1338, 'foo')
2554
- assert_equal File.stat('foo').uid, 1337
2555
- assert_equal File.stat('foo').gid, 1338
2556
- file = File.new('foo')
2557
- file.chown(-1, -1)
2558
- file.close
2559
- assert_equal File.stat('foo').uid, 1337
2560
- assert_equal File.stat('foo').gid, 1338
2561
- end
2562
-
2563
- def test_file_umask
2564
- assert_equal File.umask, RealFile.umask
2565
- File.umask(0740)
2566
-
2567
- assert_equal File.umask, RealFile.umask
2568
- assert_equal File.umask, 0740
2569
- end
2570
-
2571
- def test_file_stat_comparable
2572
- a_time = Time.new
2573
-
2574
- same1 = File.new('s1', 'w')
2575
- same2 = File.new('s2', 'w')
2576
- different1 = File.new('d1', 'w')
2577
- different2 = File.new('d2', 'w')
2578
-
2579
- FakeFS::FileSystem.find('s1').mtime = a_time
2580
- FakeFS::FileSystem.find('s2').mtime = a_time
2581
-
2582
- FakeFS::FileSystem.find('d1').mtime = a_time
2583
- FakeFS::FileSystem.find('d2').mtime = a_time + 1
2584
-
2585
- assert same1.mtime == same2.mtime
2586
- assert different1.mtime != different2.mtime
2587
-
2588
- assert same1.stat == same2.stat
2589
- assert((same1.stat <=> same2.stat) == 0)
2590
-
2591
- assert different1.stat != different2.stat
2592
- assert((different1.stat <=> different2.stat) == -1)
2593
- end
2594
-
2595
- def test_file_binread_works
2596
- File.open('testfile', 'w') do |f|
2597
- f << "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2598
- end
2599
-
2600
- assert_equal File.binread('testfile'), "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2601
- assert_equal File.binread('testfile', 20), "This is line one\nThi"
2602
- assert_equal File.binread('testfile', 20, 10), "ne one\nThis is line "
2603
- end
2604
-
2605
- def test_file_utils_compare_file
2606
- file1 = 'file1.txt'
2607
- file2 = 'file2.txt'
2608
- file3 = 'file3.txt'
2609
- content = "This is my \n file\content\n"
2610
- File.open(file1, 'w') do |f|
2611
- f.write content
2612
- end
2613
- File.open(file3, 'w') do |f|
2614
- f.write "#{content} with additional content"
2615
- end
2616
-
2617
- FileUtils.cp file1, file2
2618
-
2619
- assert_equal FileUtils.compare_file(file1, file2), true
2620
- assert_equal FileUtils.compare_file(file1, file3), false
2621
- assert_raises Errno::ENOENT do
2622
- FileUtils.compare_file(file1, 'file4.txt')
2623
- end
2624
- end
2625
-
2626
- def test_fnmatch
2627
- assert_equal File.fnmatch?('test', 'test'), true
2628
- assert_equal File.fnmatch('nope', 'blargh'), false
2629
- assert_equal File.fnmatch?('nope', 'blargh'), File.fnmatch('nope', 'blargh')
2630
- end
2631
-
2632
- if RUBY_VERSION >= '1.9.1'
2633
- def test_absolute_path_with_absolute_path
2634
- assert_equal '/foo/bar', File.absolute_path('/foo/bar')
2635
- end
2636
-
2637
- def test_absolute_path_with_absolute_path_with_dir_name
2638
- assert_equal '/foo/bar', File.absolute_path('/foo/bar', '/dir')
2639
- end
2640
-
2641
- def test_absolute_path_with_relative_path
2642
- assert_equal "#{Dir.getwd}foo/bar", File.absolute_path('foo/bar')
2643
- end
2644
-
2645
- def test_absolute_path_with_relative_path_with_dir_name
2646
- assert_equal '/dir/foo/bar', File.absolute_path('foo/bar', '/dir')
2647
- end
2648
- end
2649
-
2650
- if RUBY_VERSION >= '1.9.2'
2651
- def test_file_size
2652
- File.open('foo', 'w') do |f|
2653
- f << 'Yada Yada'
2654
- assert_equal 9, f.size
2655
- end
2656
- end
2657
-
2658
- def test_fdatasync
2659
- File.open('foo', 'w') do |f|
2660
- f << 'Yada Yada'
2661
- # nothing raised
2662
- f.fdatasync
2663
- end
2664
- end
2665
-
2666
- def test_autoclose
2667
- File.open('foo', 'w') do |f|
2668
- assert_equal true, f.autoclose?
2669
- f.autoclose = false
2670
- assert_equal false, f.autoclose?
2671
- end
2672
- end
2673
-
2674
- def test_to_path
2675
- File.new('foo', 'w') do |f|
2676
- assert_equal 'foo', f.to_path
2677
- end
2678
- end
2679
- end
2680
-
2681
- if RUBY_VERSION >= '1.9.3'
2682
- def test_advise
2683
- File.open('foo', 'w') do |f|
2684
- # nothing raised
2685
- f.advise(:normal, 0, 0)
2686
- end
2687
- end
2688
-
2689
- def test_file_read_respects_hashes
2690
- path = 'file.txt'
2691
- File.open(path, 'w') do |f|
2692
- f.write 'Yatta!'
2693
- end
2694
-
2695
- assert_equal 'ASCII-8BIT', File.read(path, mode: 'rb').encoding.to_s
2696
- end
2697
-
2698
- def test_file_read_respects_args_and_hashes
2699
- path = 'file.txt'
2700
- File.open(path, 'w') do |f|
2701
- f.write 'Yatta!'
2702
- end
2703
-
2704
- result = File.read(path, 2, 1, mode: 'rb')
2705
- assert_equal 'at', result
2706
- assert_equal 'ASCII-8BIT', result.encoding.to_s
2707
- end
2708
-
2709
- def test_file_write_can_write_a_file
2710
- File.write('testfile', '0123456789')
2711
- assert_equal File.read('testfile'), '0123456789'
2712
- end
2713
-
2714
- def test_file_write_returns_the_length_written
2715
- assert_equal File.write('testfile', '0123456789'), 10
2716
- end
2717
-
2718
- def test_file_write_truncates_file_if_offset_not_given
2719
- File.open('foo', 'w') do |f|
2720
- f << 'foo'
2721
- end
2722
-
2723
- File.write('foo', 'bar')
2724
- assert_equal File.read('foo'), 'bar'
2725
- end
2726
-
2727
- def test_file_write_writes_at_offset_and_does_not_truncate
2728
- File.open('foo', 'w') do |f|
2729
- f << 'foo'
2730
- end
2731
-
2732
- File.write('foo', 'bar', 3)
2733
- assert_equal File.read('foo'), 'foobar'
2734
- end
2735
-
2736
- def test_can_read_binary_data_in_binary_mode
2737
- File.open('foo', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
2738
- assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT'), File.open('foo', 'rb').read
2739
- end
2740
-
2741
- def test_can_read_binary_data_in_non_binary_mode
2742
- File.open('foo_non_bin', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
2743
- assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00".force_encoding('UTF-8'), File.open('foo_non_bin', 'r').read
2744
- end
2745
-
2746
- def test_can_read_binary_data_using_binread
2747
- File.open('foo', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
2748
- assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT'), File.binread('foo')
2749
- end
2750
- end
2751
-
2752
- if RUBY_VERSION >= '2.2.0'
2753
- def test_raises_error_on_birthtime_if_file_does_not_exist
2754
- assert_raises Errno::ENOENT do
2755
- File.birthtime('file.txt')
2756
- end
2757
- end
2758
-
2759
- def test_can_return_birthtime_on_existing_file
2760
- File.open('foo', 'w') { |f| f << 'some content' }
2761
- assert File.birthtime('foo').is_a?(Time)
2762
- end
2763
-
2764
- def test_file_birthtime_is_equal_to_file_stat_birthtime
2765
- File.open('foo', 'w') { |f| f << 'some content' }
2766
- assert_equal File.stat('foo').birthtime, File.birthtime('foo')
2767
- end
2768
- end
2769
- end