fakefs 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +5 -0
- data/.gitignore +2 -0
- data/CONTRIBUTORS +12 -3
- data/README.markdown +29 -7
- data/Rakefile +22 -8
- data/fakefs.gemspec +86 -0
- data/lib/fakefs/base.rb +16 -9
- data/lib/fakefs/dir.rb +1 -0
- data/lib/fakefs/fake/dir.rb +4 -1
- data/lib/fakefs/fake/file.rb +4 -2
- data/lib/fakefs/fake/symlink.rb +1 -1
- data/lib/fakefs/file.rb +125 -31
- data/lib/fakefs/file_system.rb +5 -2
- data/lib/fakefs/file_test.rb +7 -0
- data/lib/fakefs/fileutils.rb +6 -6
- data/lib/fakefs/safe.rb +1 -1
- data/lib/fakefs/version.rb +1 -1
- data/spec/fakefs/spec_helpers_spec.rb +1 -1
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +1 -1
- data/test/fake/file/lstat_test.rb +59 -0
- data/test/fake/file/stat_test.rb +39 -0
- data/test/fake/file/sysseek_test.rb +44 -0
- data/test/fake/file/syswrite_test.rb +62 -0
- data/test/fake/file_test.rb +4 -6
- data/test/fake/symlink_test.rb +1 -3
- data/test/fakefs_test.rb +259 -22
- data/test/file/stat_test.rb +6 -3
- data/test/safe_test.rb +9 -7
- data/test/test_helper.rb +8 -0
- data/test/verify.rb +22 -18
- metadata +34 -6
data/lib/fakefs/file_system.rb
CHANGED
@@ -109,11 +109,14 @@ module FakeFS
|
|
109
109
|
matches = case pattern
|
110
110
|
when '**'
|
111
111
|
case parts
|
112
|
-
when ['*']
|
112
|
+
when ['*']
|
113
113
|
parts = [] # end recursion
|
114
114
|
directories_under(dir).map do |d|
|
115
|
-
d.values.select{|f| f.is_a?
|
115
|
+
d.values.select{|f| f.is_a?(FakeFile) || f.is_a?(FakeDir) }
|
116
116
|
end.flatten.uniq
|
117
|
+
when []
|
118
|
+
parts = [] # end recursion
|
119
|
+
dir.values.flatten.uniq
|
117
120
|
else
|
118
121
|
directories_under(dir)
|
119
122
|
end
|
data/lib/fakefs/fileutils.rb
CHANGED
@@ -2,7 +2,7 @@ module FakeFS
|
|
2
2
|
module FileUtils
|
3
3
|
extend self
|
4
4
|
|
5
|
-
def mkdir_p(path)
|
5
|
+
def mkdir_p(path, options = {})
|
6
6
|
FileSystem.add(path, FakeDir.new)
|
7
7
|
end
|
8
8
|
alias_method :mkpath, :mkdir_p
|
@@ -19,8 +19,10 @@ module FakeFS
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
def rm(
|
23
|
-
|
22
|
+
def rm(list, options = {})
|
23
|
+
Array(list).each do |path|
|
24
|
+
FileSystem.delete(path)
|
25
|
+
end
|
24
26
|
end
|
25
27
|
|
26
28
|
alias_method :rm_rf, :rm
|
@@ -36,8 +38,6 @@ module FakeFS
|
|
36
38
|
ln_s(target, path, { :force => true })
|
37
39
|
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
41
|
def cp(src, dest)
|
42
42
|
dst_file = FileSystem.find(dest)
|
43
43
|
src_file = FileSystem.find(src)
|
@@ -86,7 +86,7 @@ module FakeFS
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
-
def mv(src, dest)
|
89
|
+
def mv(src, dest, options={})
|
90
90
|
if target = FileSystem.find(src)
|
91
91
|
FileSystem.add(dest, target.entry.clone)
|
92
92
|
FileSystem.delete(src)
|
data/lib/fakefs/safe.rb
CHANGED
data/lib/fakefs/version.rb
CHANGED
data/spec/spec.opts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--color
|
1
|
+
--color
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FileStat < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FakeFS.activate!
|
6
|
+
FakeFS::FileSystem.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
FakeFS.deactivate!
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_calling_lstat_should_create_a_new_file_stat_object
|
14
|
+
File.open("foo", "w") do |f|
|
15
|
+
f << "bar"
|
16
|
+
end
|
17
|
+
|
18
|
+
File.open("foo") do |f|
|
19
|
+
assert_equal File::Stat, f.lstat.class
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_lstat_should_use_correct_file
|
24
|
+
File.open("bar", "w") do |f|
|
25
|
+
f << "1"
|
26
|
+
end
|
27
|
+
|
28
|
+
File.open("bar") do |f|
|
29
|
+
assert_equal 1, f.lstat.size
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_lstat_should_report_on_symlink_itself
|
34
|
+
File.open("foo", "w") { |f| f << "some content" }
|
35
|
+
File.symlink "foo", "my_symlink"
|
36
|
+
|
37
|
+
assert_not_equal File.lstat("my_symlink").size, File.lstat("foo").size
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_report_on_symlink_itself_with_size_instance_method
|
41
|
+
File.open("foo", "w") { |f| f << "some content" }
|
42
|
+
File.symlink "foo", "my_symlink"
|
43
|
+
|
44
|
+
file = File.open("foo")
|
45
|
+
symlink = File.open("my_symlink")
|
46
|
+
|
47
|
+
assert_not_equal file.lstat.size, symlink.lstat.size
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_symlink_size_is_size_of_path_pointed_to
|
51
|
+
File.open("a", "w") { |x| x << "foobarbazfoobarbaz" }
|
52
|
+
File.symlink "a", "one_char_symlink"
|
53
|
+
assert_equal 1, File.lstat("one_char_symlink").size
|
54
|
+
|
55
|
+
File.open("ab", "w") { |x| x << "foobarbazfoobarbaz" }
|
56
|
+
File.symlink "ab", "two_char_symlink"
|
57
|
+
assert_equal 2, File.lstat("two_char_symlink").size
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FileStat < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FakeFS.activate!
|
6
|
+
FakeFS::FileSystem.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
FakeFS.deactivate!
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_calling_stat_should_create_a_new_file_stat_object
|
14
|
+
File.open("foo", "w") do |f|
|
15
|
+
f << "bar"
|
16
|
+
end
|
17
|
+
|
18
|
+
File.open("foo") do |f|
|
19
|
+
assert_equal File::Stat, f.stat.class
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_stat_should_use_correct_file
|
24
|
+
File.open("bar", "w") do |f|
|
25
|
+
f << "1"
|
26
|
+
end
|
27
|
+
|
28
|
+
File.open("bar") do |f|
|
29
|
+
assert_equal 1, f.stat.size
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_stat_should_report_on_symlink_pointer
|
34
|
+
File.open("foo", "w") { |f| f << "some content" }
|
35
|
+
File.symlink "foo", "my_symlink"
|
36
|
+
|
37
|
+
assert_equal File.stat("my_symlink").size, File.stat("foo").size
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FileSysSeek < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FakeFS.activate!
|
6
|
+
FakeFS::FileSystem.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
FakeFS.deactivate!
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_seek_to_position
|
14
|
+
file = File.open("foo", "w") do |f|
|
15
|
+
f << "0123456789"
|
16
|
+
end
|
17
|
+
|
18
|
+
File.open("foo", "r") do |f|
|
19
|
+
f.sysseek(3)
|
20
|
+
assert_equal 3, f.pos
|
21
|
+
|
22
|
+
f.sysseek(0)
|
23
|
+
assert_equal 0, f.pos
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_seek_returns_offset_into_file
|
28
|
+
File.open("foo", "w") do |f|
|
29
|
+
# 66 chars long
|
30
|
+
str = "0123456789" +
|
31
|
+
"0123456789" +
|
32
|
+
"0123456789" +
|
33
|
+
"0123456789" +
|
34
|
+
"0123456789" +
|
35
|
+
"0123456789" +
|
36
|
+
"012345"
|
37
|
+
|
38
|
+
f << str
|
39
|
+
end
|
40
|
+
|
41
|
+
f = File.open("foo")
|
42
|
+
assert_equal 53, f.sysseek(-13, IO::SEEK_END)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FileSysWriteTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
FakeFS.activate!
|
6
|
+
FakeFS::FileSystem.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
FakeFS.deactivate!
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_returns_one_byte_when_written
|
14
|
+
f = File.open "foo", "w"
|
15
|
+
result = f.syswrite "a"
|
16
|
+
assert_equal 1, result
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_returns_two_bytes_when_two_written
|
20
|
+
f = File.open "foo", "w"
|
21
|
+
result = f.syswrite "ab"
|
22
|
+
assert_equal 2, result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_syswrite_writes_file
|
26
|
+
f = File.open "foo", "w"
|
27
|
+
f.syswrite "abcdef"
|
28
|
+
f.close
|
29
|
+
|
30
|
+
assert_equal "abcdef", File.read("foo")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_writes_to_the_actual_position_when_called_after_buffered_io_read
|
34
|
+
File.open("foo", "w") do |file|
|
35
|
+
file.syswrite("012345678901234567890123456789")
|
36
|
+
end
|
37
|
+
|
38
|
+
file = File.open("foo", "r+")
|
39
|
+
file.read(5)
|
40
|
+
file.syswrite("abcde")
|
41
|
+
|
42
|
+
File.open("foo") do |file|
|
43
|
+
assert_equal "01234abcde", file.sysread(10)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_writes_all_of_the_strings_bytes_but_does_not_buffer_them
|
48
|
+
File.open("foo", "w") do |file|
|
49
|
+
file.syswrite("012345678901234567890123456789")
|
50
|
+
end
|
51
|
+
|
52
|
+
file = File.open("foo", "r+")
|
53
|
+
written = file.syswrite("abcde")
|
54
|
+
|
55
|
+
File.open("foo") do |file|
|
56
|
+
assert_equal "abcde56789", file.sysread(10)
|
57
|
+
file.seek(0)
|
58
|
+
file.fsync
|
59
|
+
assert_equal "abcde56789", file.sysread(10)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/test/fake/file_test.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
|
-
|
2
|
-
require 'fakefs/safe'
|
3
|
-
require 'test/unit'
|
1
|
+
require "test_helper"
|
4
2
|
|
5
3
|
class FakeFileTest < Test::Unit::TestCase
|
6
4
|
include FakeFS
|
7
5
|
|
8
6
|
def setup
|
9
7
|
FileSystem.clear
|
10
|
-
|
8
|
+
|
11
9
|
@file = FakeFile.new
|
12
10
|
end
|
13
11
|
|
@@ -43,9 +41,9 @@ class FakeFileTest < Test::Unit::TestCase
|
|
43
41
|
|
44
42
|
def test_links_are_mutual
|
45
43
|
other_file = FakeFile.new
|
46
|
-
|
44
|
+
|
47
45
|
@file.link(other_file)
|
48
|
-
|
46
|
+
|
49
47
|
assert_equal [@file, other_file], other_file.links
|
50
48
|
end
|
51
49
|
|
data/test/fake/symlink_test.rb
CHANGED
data/test/fakefs_test.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
|
-
|
2
|
-
require 'fakefs'
|
3
|
-
require 'test/unit'
|
1
|
+
require "test_helper"
|
4
2
|
|
5
3
|
class FakeFSTest < Test::Unit::TestCase
|
6
4
|
include FakeFS
|
7
5
|
|
8
6
|
def setup
|
7
|
+
FakeFS.activate!
|
9
8
|
FileSystem.clear
|
10
9
|
end
|
11
10
|
|
11
|
+
def teardown
|
12
|
+
FakeFS.deactivate!
|
13
|
+
end
|
14
|
+
|
12
15
|
def test_can_be_initialized_empty
|
13
16
|
fs = FileSystem
|
14
17
|
assert_equal 0, fs.files.size
|
@@ -24,12 +27,22 @@ class FakeFSTest < Test::Unit::TestCase
|
|
24
27
|
FileUtils.mkdir_p("/path/to/dir")
|
25
28
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
26
29
|
end
|
27
|
-
|
30
|
+
|
31
|
+
def test_can_create_directories_with_options
|
32
|
+
FileUtils.mkdir_p("/path/to/dir", :mode => 0755)
|
33
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
34
|
+
end
|
35
|
+
|
28
36
|
def test_can_create_directories_with_mkpath
|
29
37
|
FileUtils.mkpath("/path/to/dir")
|
30
38
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
31
39
|
end
|
32
40
|
|
41
|
+
def test_can_create_directories_with_mkpath_and_options
|
42
|
+
FileUtils.mkpath("/path/to/dir", :mode => 0755)
|
43
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
44
|
+
end
|
45
|
+
|
33
46
|
def test_can_delete_directories
|
34
47
|
FileUtils.mkdir_p("/path/to/dir")
|
35
48
|
FileUtils.rmdir("/path/to/dir")
|
@@ -37,6 +50,13 @@ class FakeFSTest < Test::Unit::TestCase
|
|
37
50
|
assert File.exists?("/path/to/dir") == false
|
38
51
|
end
|
39
52
|
|
53
|
+
def test_can_delete_multiple_files
|
54
|
+
FileUtils.touch(["foo", "bar"])
|
55
|
+
FileUtils.rm(["foo", "bar"])
|
56
|
+
assert File.exists?("foo") == false
|
57
|
+
assert File.exists?("bar") == false
|
58
|
+
end
|
59
|
+
|
40
60
|
def test_knows_directories_exist
|
41
61
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
42
62
|
assert File.exists?(path)
|
@@ -74,14 +94,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
74
94
|
FileUtils.ln_s(target, '/path/to/link')
|
75
95
|
end
|
76
96
|
end
|
77
|
-
|
97
|
+
|
78
98
|
def test_can_force_creation_of_symlinks
|
79
99
|
FileUtils.mkdir_p(target = "/path/to/first/target")
|
80
100
|
FileUtils.ln_s(target, "/path/to/link")
|
81
101
|
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
82
102
|
FileUtils.ln_s(target, '/path/to/link', :force => true)
|
83
103
|
end
|
84
|
-
|
104
|
+
|
85
105
|
def test_create_symlink_using_ln_sf
|
86
106
|
FileUtils.mkdir_p(target = "/path/to/first/target")
|
87
107
|
FileUtils.ln_s(target, "/path/to/link")
|
@@ -288,7 +308,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
288
308
|
assert_nil File.size?("/path/to/other.txt")
|
289
309
|
end
|
290
310
|
|
291
|
-
def
|
311
|
+
def test_can_check_size_of_empty_file
|
292
312
|
path = '/path/to/file.txt'
|
293
313
|
File.open(path, 'w') do |f|
|
294
314
|
f << ''
|
@@ -310,6 +330,60 @@ class FakeFSTest < Test::Unit::TestCase
|
|
310
330
|
assert File.mtime('/path/to/file.txt').is_a?(Time)
|
311
331
|
end
|
312
332
|
|
333
|
+
def test_raises_error_on_ctime_if_file_does_not_exist
|
334
|
+
assert_raise Errno::ENOENT do
|
335
|
+
File.ctime('/path/to/file.txt')
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
def test_can_return_ctime_on_existing_file
|
340
|
+
File.open("foo", "w") { |f| f << "some content" }
|
341
|
+
assert File.ctime('foo').is_a?(Time)
|
342
|
+
end
|
343
|
+
|
344
|
+
def test_ctime_and_mtime_are_equal_for_new_files
|
345
|
+
File.open("foo", "w") { |f| f << "some content" }
|
346
|
+
ctime = File.ctime("foo")
|
347
|
+
mtime = File.mtime("foo")
|
348
|
+
assert ctime.is_a?(Time)
|
349
|
+
assert mtime.is_a?(Time)
|
350
|
+
assert_equal ctime, mtime
|
351
|
+
|
352
|
+
File.open("foo", "r") do |f|
|
353
|
+
assert_equal ctime, f.ctime
|
354
|
+
assert_equal mtime, f.mtime
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_ctime_and_mtime_are_equal_for_new_directories
|
359
|
+
FileUtils.mkdir_p("foo")
|
360
|
+
ctime = File.ctime("foo")
|
361
|
+
mtime = File.mtime("foo")
|
362
|
+
assert ctime.is_a?(Time)
|
363
|
+
assert mtime.is_a?(Time)
|
364
|
+
assert_equal ctime, mtime
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_file_ctime_is_equal_to_file_stat_ctime
|
368
|
+
File.open("foo", "w") { |f| f << "some content" }
|
369
|
+
assert_equal File.stat("foo").ctime, File.ctime("foo")
|
370
|
+
end
|
371
|
+
|
372
|
+
def test_directory_ctime_is_equal_to_directory_stat_ctime
|
373
|
+
FileUtils.mkdir_p("foo")
|
374
|
+
assert_equal File.stat("foo").ctime, File.ctime("foo")
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_file_mtime_is_equal_to_file_stat_mtime
|
378
|
+
File.open("foo", "w") { |f| f << "some content" }
|
379
|
+
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_directory_mtime_is_equal_to_directory_stat_mtime
|
383
|
+
FileUtils.mkdir_p("foo")
|
384
|
+
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
385
|
+
end
|
386
|
+
|
313
387
|
def test_can_read_with_File_readlines
|
314
388
|
path = '/path/to/file.txt'
|
315
389
|
File.open(path, 'w') do |f|
|
@@ -330,6 +404,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
330
404
|
end
|
331
405
|
end
|
332
406
|
|
407
|
+
def test_File_close_disallows_further_writes
|
408
|
+
path = '/path/to/file.txt'
|
409
|
+
file = File.open(path, 'w')
|
410
|
+
file.write 'Yada'
|
411
|
+
file.close
|
412
|
+
assert_raise IOError do
|
413
|
+
file << "foo"
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
333
417
|
def test_can_read_from_file_objects
|
334
418
|
path = '/path/to/file.txt'
|
335
419
|
File.open(path, 'w') do |f|
|
@@ -354,6 +438,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
354
438
|
assert File.file?(path)
|
355
439
|
end
|
356
440
|
|
441
|
+
def test_File_io_returns_self
|
442
|
+
f = File.open("/foo", "w")
|
443
|
+
assert_equal f, f.to_io
|
444
|
+
end
|
445
|
+
|
446
|
+
def test_File_to_i_is_alias_for_filno
|
447
|
+
f = File.open("/foo", "w")
|
448
|
+
assert_equal f.method(:to_i), f.method(:fileno)
|
449
|
+
end
|
450
|
+
|
357
451
|
def test_knows_symlink_files_are_files
|
358
452
|
path = '/path/to/file.txt'
|
359
453
|
File.open(path, 'w') do |f|
|
@@ -418,6 +512,13 @@ class FakeFSTest < Test::Unit::TestCase
|
|
418
512
|
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
|
419
513
|
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
|
420
514
|
|
515
|
+
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
516
|
+
assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/**/*']
|
517
|
+
|
518
|
+
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
519
|
+
|
520
|
+
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
|
521
|
+
|
421
522
|
FileUtils.cp_r '/path', '/otherpath'
|
422
523
|
|
423
524
|
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
|
@@ -438,11 +539,74 @@ class FakeFSTest < Test::Unit::TestCase
|
|
438
539
|
assert_equal ['/one/two/three'], Dir['/one/**/three']
|
439
540
|
end
|
440
541
|
|
441
|
-
def
|
542
|
+
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
|
442
543
|
File.open('/one/two/three/four.rb', 'w')
|
443
544
|
File.open('/one/five.rb', 'w')
|
444
|
-
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*']
|
445
|
-
assert_equal ['/one/five.rb', '/one/two
|
545
|
+
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
|
546
|
+
assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**']
|
547
|
+
end
|
548
|
+
|
549
|
+
def test_should_report_pos_as_0_when_opening
|
550
|
+
File.open("/foo", "w") do |f|
|
551
|
+
f << "foobar"
|
552
|
+
f.rewind
|
553
|
+
|
554
|
+
assert_equal 0, f.pos
|
555
|
+
end
|
556
|
+
end
|
557
|
+
|
558
|
+
def test_should_report_pos_as_1_when_seeking_one_char
|
559
|
+
File.open("/foo", "w") do |f|
|
560
|
+
f << "foobar"
|
561
|
+
|
562
|
+
f.rewind
|
563
|
+
f.seek(1)
|
564
|
+
|
565
|
+
assert_equal 1, f.pos
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
def test_should_set_pos
|
570
|
+
File.open("/foo", "w") do |f|
|
571
|
+
f << "foo"
|
572
|
+
end
|
573
|
+
|
574
|
+
fp = File.open("/foo", "r")
|
575
|
+
fp.pos = 1
|
576
|
+
|
577
|
+
assert_equal 1, fp.pos
|
578
|
+
end
|
579
|
+
|
580
|
+
def test_should_set_pos_with_tell_method
|
581
|
+
File.open("/foo", "w") do |f|
|
582
|
+
f << "foo"
|
583
|
+
end
|
584
|
+
|
585
|
+
fp = File.open("/foo", "r")
|
586
|
+
fp.tell = 1
|
587
|
+
|
588
|
+
assert_equal 1, fp.pos
|
589
|
+
end
|
590
|
+
|
591
|
+
# Every method in File is in FakeFS::File
|
592
|
+
RealFile.instance_methods.each do |method_name|
|
593
|
+
define_method "test_should_have_method_#{method_name}_from_real_file_class" do
|
594
|
+
assert File.instance_methods.include?(method_name)
|
595
|
+
end
|
596
|
+
end
|
597
|
+
|
598
|
+
# No methods which are in StringIO that aren't in File are included into
|
599
|
+
# FakeFS::File (because of inheritence)
|
600
|
+
uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
|
601
|
+
uniq_string_io_methods.each do |method_name|
|
602
|
+
define_method "test_file_should_not_respond_to_#{method_name}" do
|
603
|
+
assert !File.instance_methods.include?(method_name)
|
604
|
+
end
|
605
|
+
end
|
606
|
+
|
607
|
+
def test_does_not_remove_methods_from_stringio
|
608
|
+
stringio = StringIO.new("foo")
|
609
|
+
assert stringio.respond_to?(:size)
|
446
610
|
end
|
447
611
|
|
448
612
|
def test_chdir_changes_directories_like_a_boss
|
@@ -585,6 +749,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|
585
749
|
assert_equal 'bar', File.open('baz') { |f| f.read }
|
586
750
|
end
|
587
751
|
|
752
|
+
def test_mv_works_with_options
|
753
|
+
File.open('foo', 'w') {|f| f.write 'bar'}
|
754
|
+
FileUtils.mv 'foo', 'baz', :force => true
|
755
|
+
assert_equal('bar', File.open('baz') { |f| f.read })
|
756
|
+
end
|
757
|
+
|
588
758
|
def test_cp_actually_works
|
589
759
|
File.open('foo', 'w') {|f| f.write 'bar' }
|
590
760
|
FileUtils.cp('foo', 'baz')
|
@@ -695,25 +865,34 @@ class FakeFSTest < Test::Unit::TestCase
|
|
695
865
|
end
|
696
866
|
|
697
867
|
def test_clone_clones_directories
|
868
|
+
FakeFS.deactivate!
|
698
869
|
RealFileUtils.mkdir_p(here('subdir'))
|
870
|
+
FakeFS.activate!
|
699
871
|
|
700
872
|
FileSystem.clone(here('subdir'))
|
701
873
|
|
702
874
|
assert File.exists?(here('subdir')), 'subdir was cloned'
|
703
875
|
assert File.directory?(here('subdir')), 'subdir is a directory'
|
704
876
|
ensure
|
705
|
-
|
877
|
+
FakeFS.deactivate!
|
878
|
+
RealFileUtils.rm_rf(here('subdir'))
|
879
|
+
FakeFS.activate!
|
706
880
|
end
|
707
881
|
|
708
882
|
def test_clone_clones_dot_files_even_hard_to_find_ones
|
883
|
+
FakeFS.deactivate!
|
709
884
|
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
|
885
|
+
FakeFS.activate!
|
886
|
+
|
710
887
|
assert !File.exists?(here('subdir'))
|
711
888
|
|
712
889
|
FileSystem.clone(here('subdir'))
|
713
890
|
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
|
714
891
|
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
|
715
892
|
ensure
|
716
|
-
|
893
|
+
FakeFS.deactivate!
|
894
|
+
RealFileUtils.rm_rf(here('subdir'))
|
895
|
+
FakeFS.activate!
|
717
896
|
end
|
718
897
|
|
719
898
|
def test_putting_a_dot_at_end_copies_the_contents
|
@@ -1019,6 +1198,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1019
1198
|
end
|
1020
1199
|
end
|
1021
1200
|
|
1201
|
+
def test_mkdir_raises_error_if_already_created
|
1202
|
+
Dir.mkdir "foo"
|
1203
|
+
|
1204
|
+
assert_raises(Errno::EEXIST) do
|
1205
|
+
Dir.mkdir "foo"
|
1206
|
+
end
|
1207
|
+
end
|
1208
|
+
|
1022
1209
|
def test_directory_open
|
1023
1210
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1024
1211
|
|
@@ -1053,38 +1240,38 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1053
1240
|
def test_tmpdir
|
1054
1241
|
assert Dir.tmpdir == "/tmp"
|
1055
1242
|
end
|
1056
|
-
|
1243
|
+
|
1057
1244
|
def test_hard_link_creates_file
|
1058
1245
|
FileUtils.touch("/foo")
|
1059
|
-
|
1246
|
+
|
1060
1247
|
File.link("/foo", "/bar")
|
1061
1248
|
assert File.exists?("/bar")
|
1062
1249
|
end
|
1063
|
-
|
1250
|
+
|
1064
1251
|
def test_hard_link_with_missing_file_raises_error
|
1065
1252
|
assert_raises(Errno::ENOENT) do
|
1066
1253
|
File.link("/foo", "/bar")
|
1067
1254
|
end
|
1068
1255
|
end
|
1069
|
-
|
1256
|
+
|
1070
1257
|
def test_hard_link_with_existing_destination_file
|
1071
1258
|
FileUtils.touch("/foo")
|
1072
1259
|
FileUtils.touch("/bar")
|
1073
|
-
|
1260
|
+
|
1074
1261
|
assert_raises(Errno::EEXIST) do
|
1075
1262
|
File.link("/foo", "/bar")
|
1076
1263
|
end
|
1077
1264
|
end
|
1078
|
-
|
1265
|
+
|
1079
1266
|
def test_hard_link_returns_0_when_successful
|
1080
1267
|
FileUtils.touch("/foo")
|
1081
|
-
|
1268
|
+
|
1082
1269
|
assert_equal 0, File.link("/foo", "/bar")
|
1083
1270
|
end
|
1084
|
-
|
1271
|
+
|
1085
1272
|
def test_hard_link_returns_duplicate_file
|
1086
1273
|
File.open("/foo", "w") { |x| x << "some content" }
|
1087
|
-
|
1274
|
+
|
1088
1275
|
File.link("/foo", "/bar")
|
1089
1276
|
assert_equal "some content", File.read("/bar")
|
1090
1277
|
end
|
@@ -1245,7 +1432,57 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1245
1432
|
assert_equal IO::SEEK_SET, File::SEEK_SET
|
1246
1433
|
end
|
1247
1434
|
|
1435
|
+
def test_filetest_exists_return_correct_values
|
1436
|
+
FileUtils.mkdir_p("/path/to/dir")
|
1437
|
+
assert FileTest.exist?("/path/to/")
|
1438
|
+
|
1439
|
+
FileUtils.rmdir("/path/to/dir")
|
1440
|
+
assert !FileTest.exist?("/path/to/dir")
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
def test_pathname_exists_returns_correct_value
|
1444
|
+
FileUtils.touch "foo"
|
1445
|
+
assert Pathname.new("foo").exist?
|
1446
|
+
|
1447
|
+
assert !Pathname.new("bar").exist?
|
1448
|
+
end
|
1449
|
+
|
1450
|
+
def test_activating_returns_true
|
1451
|
+
FakeFS.deactivate!
|
1452
|
+
assert_equal true, FakeFS.activate!
|
1453
|
+
end
|
1454
|
+
|
1455
|
+
def test_deactivating_returns_true
|
1456
|
+
assert_equal true, FakeFS.deactivate!
|
1457
|
+
end
|
1458
|
+
|
1248
1459
|
def here(fname)
|
1249
|
-
RealFile.expand_path(RealFile.dirname(__FILE__)
|
1460
|
+
RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
|
1461
|
+
end
|
1462
|
+
|
1463
|
+
if RUBY_VERSION >= "1.9.2"
|
1464
|
+
def test_file_size
|
1465
|
+
File.open("foo", 'w') do |f|
|
1466
|
+
f << 'Yada Yada'
|
1467
|
+
assert_equal 9, f.size
|
1468
|
+
end
|
1469
|
+
end
|
1470
|
+
|
1471
|
+
def test_fdatasync
|
1472
|
+
File.open("foo", 'w') do |f|
|
1473
|
+
f << 'Yada Yada'
|
1474
|
+
assert_nothing_raised do
|
1475
|
+
f.fdatasync
|
1476
|
+
end
|
1477
|
+
end
|
1478
|
+
end
|
1479
|
+
|
1480
|
+
def test_autoclose
|
1481
|
+
File.open("foo", 'w') do |f|
|
1482
|
+
assert_equal true, f.autoclose?
|
1483
|
+
f.autoclose = false
|
1484
|
+
assert_equal false, f.autoclose?
|
1485
|
+
end
|
1486
|
+
end
|
1250
1487
|
end
|
1251
1488
|
end
|