mnoble-fakefs 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 +3 -0
- data/CONTRIBUTORS +27 -0
- data/LICENSE +20 -0
- data/README.markdown +123 -0
- data/Rakefile +66 -0
- data/fakefs.gemspec +86 -0
- data/lib/fakefs.rb +3 -0
- data/lib/fakefs/base.rb +45 -0
- data/lib/fakefs/dir.rb +116 -0
- data/lib/fakefs/fake/dir.rb +48 -0
- data/lib/fakefs/fake/file.rb +81 -0
- data/lib/fakefs/fake/symlink.rb +32 -0
- data/lib/fakefs/file.rb +403 -0
- data/lib/fakefs/file_system.rb +139 -0
- data/lib/fakefs/file_test.rb +7 -0
- data/lib/fakefs/fileutils.rb +140 -0
- data/lib/fakefs/safe.rb +11 -0
- data/lib/fakefs/spec_helpers.rb +46 -0
- data/lib/fakefs/version.rb +9 -0
- data/spec/fakefs/spec_helpers_spec.rb +57 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +3 -0
- 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 +97 -0
- data/test/fake/symlink_test.rb +10 -0
- data/test/fakefs_test.rb +1611 -0
- data/test/file/stat_test.rb +73 -0
- data/test/safe_test.rb +44 -0
- data/test/test_helper.rb +8 -0
- data/test/verify.rb +31 -0
- metadata +105 -0
@@ -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
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FakeFileTest < Test::Unit::TestCase
|
4
|
+
include FakeFS
|
5
|
+
|
6
|
+
def setup
|
7
|
+
FileSystem.clear
|
8
|
+
|
9
|
+
@file = FakeFile.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_fake_file_has_empty_content_by_default
|
13
|
+
assert_equal "", @file.content
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_fake_file_can_read_and_write_to_content
|
17
|
+
@file.content = "foobar"
|
18
|
+
assert_equal "foobar", @file.content
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_fake_file_has_1_link_by_default
|
22
|
+
assert_equal [@file], @file.links
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_fake_file_can_create_link
|
26
|
+
other_file = FakeFile.new
|
27
|
+
|
28
|
+
@file.link(other_file)
|
29
|
+
|
30
|
+
assert_equal [@file, other_file], @file.links
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_fake_file_wont_add_link_to_same_file_twice
|
34
|
+
other_file = FakeFile.new
|
35
|
+
|
36
|
+
@file.link other_file
|
37
|
+
@file.link other_file
|
38
|
+
|
39
|
+
assert_equal [@file, other_file], @file.links
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_links_are_mutual
|
43
|
+
other_file = FakeFile.new
|
44
|
+
|
45
|
+
@file.link(other_file)
|
46
|
+
|
47
|
+
assert_equal [@file, other_file], other_file.links
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_can_link_multiple_files
|
51
|
+
file_two = FakeFile.new
|
52
|
+
file_three = FakeFile.new
|
53
|
+
|
54
|
+
@file.link file_two
|
55
|
+
@file.link file_three
|
56
|
+
|
57
|
+
assert_equal [@file, file_two, file_three], @file.links
|
58
|
+
assert_equal [@file, file_two, file_three], file_two.links
|
59
|
+
assert_equal [@file, file_two, file_three], file_three.links
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_links_share_same_content
|
63
|
+
other_file = FakeFile.new
|
64
|
+
|
65
|
+
@file.link other_file
|
66
|
+
|
67
|
+
@file.content = "foobar"
|
68
|
+
|
69
|
+
assert_equal "foobar", other_file.content
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_clone_creates_new_inode
|
73
|
+
clone = @file.clone
|
74
|
+
assert !clone.inode.equal?(@file.inode)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_cloning_does_not_use_same_content_object
|
78
|
+
clone = @file.clone
|
79
|
+
|
80
|
+
clone.content = "foo"
|
81
|
+
@file.content = "bar"
|
82
|
+
|
83
|
+
assert_equal "foo", clone.content
|
84
|
+
assert_equal "bar", @file.content
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_raises_an_error_with_the_correct_path
|
88
|
+
path = "/some/non/existing/file"
|
89
|
+
begin
|
90
|
+
FakeFS::File.new path
|
91
|
+
msg = nil
|
92
|
+
rescue Errno::ENOENT => e
|
93
|
+
msg = e.message
|
94
|
+
end
|
95
|
+
assert_equal "No such file or directory - #{path}", msg
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FakeSymlinkTest < Test::Unit::TestCase
|
4
|
+
include FakeFS
|
5
|
+
|
6
|
+
def test_symlink_has_method_missing_as_private
|
7
|
+
methods = FakeSymlink.private_instance_methods.map { |m| m.to_s }
|
8
|
+
assert methods.include?("method_missing")
|
9
|
+
end
|
10
|
+
end
|
data/test/fakefs_test.rb
ADDED
@@ -0,0 +1,1611 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class FakeFSTest < Test::Unit::TestCase
|
4
|
+
include FakeFS
|
5
|
+
|
6
|
+
def setup
|
7
|
+
FakeFS.activate!
|
8
|
+
FileSystem.clear
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
FakeFS.deactivate!
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_can_be_initialized_empty
|
16
|
+
fs = FileSystem
|
17
|
+
assert_equal 0, fs.files.size
|
18
|
+
end
|
19
|
+
|
20
|
+
def xtest_can_be_initialized_with_an_existing_directory
|
21
|
+
fs = FileSystem
|
22
|
+
fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
|
23
|
+
assert_equal 1, fs.files.size
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_can_create_directories_with_file_utils_mkdir_p
|
27
|
+
FileUtils.mkdir_p("/path/to/dir")
|
28
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
29
|
+
end
|
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
|
+
|
36
|
+
def test_can_create_directories_with_file_utils_mkdir
|
37
|
+
FileUtils.mkdir_p("/path/to/dir")
|
38
|
+
FileUtils.mkdir("/path/to/dir/subdir")
|
39
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir']
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path
|
43
|
+
assert_raises Errno::ENOENT do
|
44
|
+
FileUtils.mkdir("/this/path/does/not/exists/newdir")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_can_create_directories_with_mkpath
|
49
|
+
FileUtils.mkpath("/path/to/dir")
|
50
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_can_create_directories_with_mkpath_and_options
|
54
|
+
FileUtils.mkpath("/path/to/dir", :mode => 0755)
|
55
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_can_delete_directories
|
59
|
+
FileUtils.mkdir_p("/path/to/dir")
|
60
|
+
FileUtils.rmdir("/path/to/dir")
|
61
|
+
assert File.exists?("/path/to/")
|
62
|
+
assert File.exists?("/path/to/dir") == false
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_can_delete_multiple_files
|
66
|
+
FileUtils.touch(["foo", "bar"])
|
67
|
+
FileUtils.rm(["foo", "bar"])
|
68
|
+
assert File.exists?("foo") == false
|
69
|
+
assert File.exists?("bar") == false
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_knows_directories_exist
|
73
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
74
|
+
assert File.exists?(path)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_knows_directories_are_directories
|
78
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
79
|
+
assert File.directory?(path)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_knows_symlink_directories_are_directories
|
83
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
84
|
+
FileUtils.ln_s path, sympath = '/sympath'
|
85
|
+
assert File.directory?(sympath)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_knows_non_existent_directories_arent_directories
|
89
|
+
path = 'does/not/exist/'
|
90
|
+
assert_equal RealFile.directory?(path), File.directory?(path)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_doesnt_overwrite_existing_directories
|
94
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
95
|
+
assert File.exists?(path)
|
96
|
+
FileUtils.mkdir_p("/path/to")
|
97
|
+
assert File.exists?(path)
|
98
|
+
assert_raises Errno::EEXIST do
|
99
|
+
FileUtils.mkdir("/path/to")
|
100
|
+
end
|
101
|
+
assert File.exists?(path)
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_can_create_symlinks
|
105
|
+
FileUtils.mkdir_p(target = "/path/to/target")
|
106
|
+
FileUtils.ln_s(target, "/path/to/link")
|
107
|
+
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
108
|
+
|
109
|
+
assert_raises(Errno::EEXIST) do
|
110
|
+
FileUtils.ln_s(target, '/path/to/link')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_can_force_creation_of_symlinks
|
115
|
+
FileUtils.mkdir_p(target = "/path/to/first/target")
|
116
|
+
FileUtils.ln_s(target, "/path/to/link")
|
117
|
+
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
118
|
+
FileUtils.ln_s(target, '/path/to/link', :force => true)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_create_symlink_using_ln_sf
|
122
|
+
FileUtils.mkdir_p(target = "/path/to/first/target")
|
123
|
+
FileUtils.ln_s(target, "/path/to/link")
|
124
|
+
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
125
|
+
FileUtils.ln_sf(target, '/path/to/link')
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_can_follow_symlinks
|
129
|
+
FileUtils.mkdir_p(target = "/path/to/target")
|
130
|
+
FileUtils.ln_s(target, link = "/path/to/symlink")
|
131
|
+
assert_equal target, File.readlink(link)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_knows_symlinks_are_symlinks
|
135
|
+
FileUtils.mkdir_p(target = "/path/to/target")
|
136
|
+
FileUtils.ln_s(target, link = "/path/to/symlink")
|
137
|
+
assert File.symlink?(link)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_can_create_files
|
141
|
+
path = '/path/to/file.txt'
|
142
|
+
File.open(path, 'w') do |f|
|
143
|
+
f.write "Yatta!"
|
144
|
+
end
|
145
|
+
|
146
|
+
assert File.exists?(path)
|
147
|
+
assert File.readable?(path)
|
148
|
+
assert File.writable?(path)
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_can_create_files_with_bitmasks
|
152
|
+
path = '/path/to/file.txt'
|
153
|
+
File.open(path, File::RDWR | File::CREAT) do |f|
|
154
|
+
f.write "Yatta!"
|
155
|
+
end
|
156
|
+
|
157
|
+
assert File.exists?(path)
|
158
|
+
assert File.readable?(path)
|
159
|
+
assert File.writable?(path)
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_file_opens_in_read_only_mode
|
163
|
+
File.open("foo", "w") { |f| f << "foo" }
|
164
|
+
|
165
|
+
f = File.open("foo")
|
166
|
+
|
167
|
+
assert_raises(IOError) do
|
168
|
+
f << "bar"
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_file_opens_in_read_only_mode_with_bitmasks
|
173
|
+
File.open("foo", "w") { |f| f << "foo" }
|
174
|
+
|
175
|
+
f = File.open("foo", File::RDONLY)
|
176
|
+
|
177
|
+
assert_raises(IOError) do
|
178
|
+
f << "bar"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_file_opens_in_invalid_mode
|
183
|
+
FileUtils.touch("foo")
|
184
|
+
|
185
|
+
assert_raises(ArgumentError) do
|
186
|
+
File.open("foo", "an_illegal_mode")
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_raises_error_when_cannot_find_file_in_read_mode
|
191
|
+
assert_raises(Errno::ENOENT) do
|
192
|
+
File.open("does_not_exist", "r")
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_raises_error_when_cannot_find_file_in_read_write_mode
|
197
|
+
assert_raises(Errno::ENOENT) do
|
198
|
+
File.open("does_not_exist", "r+")
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_creates_files_in_write_only_mode
|
203
|
+
File.open("foo", "w")
|
204
|
+
assert File.exists?("foo")
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_creates_files_in_write_only_mode_with_bitmasks
|
208
|
+
File.open("foo", File::WRONLY | File::CREAT)
|
209
|
+
assert File.exists?("foo")
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_raises_in_write_only_mode_without_create_bitmask
|
213
|
+
assert_raises(Errno::ENOENT) do
|
214
|
+
File.open("foo", File::WRONLY)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_creates_files_in_read_write_truncate_mode
|
219
|
+
File.open("foo", "w+")
|
220
|
+
assert File.exists?("foo")
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_creates_files_in_append_write_only
|
224
|
+
File.open("foo", "a")
|
225
|
+
assert File.exists?("foo")
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_creates_files_in_append_read_write
|
229
|
+
File.open("foo", "a+")
|
230
|
+
assert File.exists?("foo")
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_file_in_write_only_raises_error_when_reading
|
234
|
+
FileUtils.touch("foo")
|
235
|
+
|
236
|
+
f = File.open("foo", "w")
|
237
|
+
|
238
|
+
assert_raises(IOError) do
|
239
|
+
f.read
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_file_in_write_mode_truncates_existing_file
|
244
|
+
File.open("foo", "w") { |f| f << "contents" }
|
245
|
+
|
246
|
+
f = File.open("foo", "w")
|
247
|
+
|
248
|
+
assert_equal "", File.read("foo")
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_file_in_read_write_truncation_mode_truncates_file
|
252
|
+
File.open("foo", "w") { |f| f << "foo" }
|
253
|
+
|
254
|
+
f = File.open("foo", "w+")
|
255
|
+
|
256
|
+
assert_equal "", File.read("foo")
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_file_in_append_write_only_raises_error_when_reading
|
260
|
+
FileUtils.touch("foo")
|
261
|
+
|
262
|
+
f = File.open("foo", "a")
|
263
|
+
|
264
|
+
assert_raises(IOError) do
|
265
|
+
f.read
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_can_read_files_once_written
|
270
|
+
path = '/path/to/file.txt'
|
271
|
+
File.open(path, 'w') do |f|
|
272
|
+
f.write "Yatta!"
|
273
|
+
end
|
274
|
+
|
275
|
+
assert_equal "Yatta!", File.read(path)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_can_write_to_files
|
279
|
+
path = '/path/to/file.txt'
|
280
|
+
File.open(path, 'w') do |f|
|
281
|
+
f << 'Yada Yada'
|
282
|
+
end
|
283
|
+
assert_equal 'Yada Yada', File.read(path)
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_raises_error_when_opening_with_binary_mode_only
|
287
|
+
assert_raise ArgumentError do
|
288
|
+
File.open("/foo", "b")
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_can_open_file_in_binary_mode
|
293
|
+
File.open("/foo", "wb") { |x| x << "a" }
|
294
|
+
assert_equal "a", File.read("/foo")
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_can_chunk_io_when_reading
|
298
|
+
path = '/path/to/file.txt'
|
299
|
+
File.open(path, 'w') do |f|
|
300
|
+
f << 'Yada Yada'
|
301
|
+
end
|
302
|
+
file = File.new(path, 'r')
|
303
|
+
assert_equal 'Yada', file.read(4)
|
304
|
+
assert_equal ' Yada', file.read(5)
|
305
|
+
assert_equal '', file.read
|
306
|
+
file.rewind
|
307
|
+
assert_equal 'Yada Yada', file.read
|
308
|
+
end
|
309
|
+
|
310
|
+
def test_can_get_size_of_files
|
311
|
+
path = '/path/to/file.txt'
|
312
|
+
File.open(path, 'w') do |f|
|
313
|
+
f << 'Yada Yada'
|
314
|
+
end
|
315
|
+
assert_equal 9, File.size(path)
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_can_check_if_file_has_size?
|
319
|
+
path = '/path/to/file.txt'
|
320
|
+
File.open(path, 'w') do |f|
|
321
|
+
f << 'Yada Yada'
|
322
|
+
end
|
323
|
+
assert File.size?(path)
|
324
|
+
assert_nil File.size?("/path/to/other.txt")
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_can_check_size_of_empty_file
|
328
|
+
path = '/path/to/file.txt'
|
329
|
+
File.open(path, 'w') do |f|
|
330
|
+
f << ''
|
331
|
+
end
|
332
|
+
assert_nil File.size?("/path/to/file.txt")
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_raises_error_on_mtime_if_file_does_not_exist
|
336
|
+
assert_raise Errno::ENOENT do
|
337
|
+
File.mtime('/path/to/file.txt')
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_can_return_mtime_on_existing_file
|
342
|
+
path = '/path/to/file.txt'
|
343
|
+
File.open(path, 'w') do |f|
|
344
|
+
f << ''
|
345
|
+
end
|
346
|
+
assert File.mtime('/path/to/file.txt').is_a?(Time)
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_raises_error_on_ctime_if_file_does_not_exist
|
350
|
+
assert_raise Errno::ENOENT do
|
351
|
+
File.ctime('/path/to/file.txt')
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_can_return_ctime_on_existing_file
|
356
|
+
File.open("foo", "w") { |f| f << "some content" }
|
357
|
+
assert File.ctime('foo').is_a?(Time)
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_ctime_and_mtime_are_equal_for_new_files
|
361
|
+
File.open("foo", "w") { |f| f << "some content" }
|
362
|
+
ctime = File.ctime("foo")
|
363
|
+
mtime = File.mtime("foo")
|
364
|
+
assert ctime.is_a?(Time)
|
365
|
+
assert mtime.is_a?(Time)
|
366
|
+
assert_equal ctime, mtime
|
367
|
+
|
368
|
+
File.open("foo", "r") do |f|
|
369
|
+
assert_equal ctime, f.ctime
|
370
|
+
assert_equal mtime, f.mtime
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_ctime_and_mtime_are_equal_for_new_directories
|
375
|
+
FileUtils.mkdir_p("foo")
|
376
|
+
ctime = File.ctime("foo")
|
377
|
+
mtime = File.mtime("foo")
|
378
|
+
assert ctime.is_a?(Time)
|
379
|
+
assert mtime.is_a?(Time)
|
380
|
+
assert_equal ctime, mtime
|
381
|
+
end
|
382
|
+
|
383
|
+
def test_file_ctime_is_equal_to_file_stat_ctime
|
384
|
+
File.open("foo", "w") { |f| f << "some content" }
|
385
|
+
assert_equal File.stat("foo").ctime, File.ctime("foo")
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_directory_ctime_is_equal_to_directory_stat_ctime
|
389
|
+
FileUtils.mkdir_p("foo")
|
390
|
+
assert_equal File.stat("foo").ctime, File.ctime("foo")
|
391
|
+
end
|
392
|
+
|
393
|
+
def test_file_mtime_is_equal_to_file_stat_mtime
|
394
|
+
File.open("foo", "w") { |f| f << "some content" }
|
395
|
+
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_directory_mtime_is_equal_to_directory_stat_mtime
|
399
|
+
FileUtils.mkdir_p("foo")
|
400
|
+
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_utime_raises_error_if_path_does_not_exist
|
404
|
+
assert_raise Errno::ENOENT do
|
405
|
+
File.utime(Time.now, Time.now, '/path/to/file.txt')
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_can_call_utime_on_an_existing_file
|
410
|
+
time = Time.now - 300 # Not now
|
411
|
+
path = '/path/to/file.txt'
|
412
|
+
File.open(path, 'w') do |f|
|
413
|
+
f << ''
|
414
|
+
end
|
415
|
+
File.utime(time, time, path)
|
416
|
+
assert_equal time, File.mtime('/path/to/file.txt')
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_utime_returns_number_of_paths
|
420
|
+
path1, path2 = '/path/to/file.txt', '/path/to/another_file.txt'
|
421
|
+
[path1, path2].each do |path|
|
422
|
+
File.open(path, 'w') do |f|
|
423
|
+
f << ''
|
424
|
+
end
|
425
|
+
end
|
426
|
+
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
|
427
|
+
end
|
428
|
+
|
429
|
+
def test_can_read_with_File_readlines
|
430
|
+
path = '/path/to/file.txt'
|
431
|
+
File.open(path, 'w') do |f|
|
432
|
+
f.puts "Yatta!", "Gatta!"
|
433
|
+
f.puts ["woot","toot"]
|
434
|
+
end
|
435
|
+
|
436
|
+
assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_File_close_disallows_further_access
|
440
|
+
path = '/path/to/file.txt'
|
441
|
+
file = File.open(path, 'w')
|
442
|
+
file.write 'Yada'
|
443
|
+
file.close
|
444
|
+
assert_raise IOError do
|
445
|
+
file.read
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
def test_File_close_disallows_further_writes
|
450
|
+
path = '/path/to/file.txt'
|
451
|
+
file = File.open(path, 'w')
|
452
|
+
file.write 'Yada'
|
453
|
+
file.close
|
454
|
+
assert_raise IOError do
|
455
|
+
file << "foo"
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
def test_can_read_from_file_objects
|
460
|
+
path = '/path/to/file.txt'
|
461
|
+
File.open(path, 'w') do |f|
|
462
|
+
f.write "Yatta!"
|
463
|
+
end
|
464
|
+
|
465
|
+
assert_equal "Yatta!", File.new(path).read
|
466
|
+
end
|
467
|
+
|
468
|
+
def test_file_read_errors_appropriately
|
469
|
+
assert_raise Errno::ENOENT do
|
470
|
+
File.read('anything')
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
def test_knows_files_are_files
|
475
|
+
path = '/path/to/file.txt'
|
476
|
+
File.open(path, 'w') do |f|
|
477
|
+
f.write "Yatta!"
|
478
|
+
end
|
479
|
+
|
480
|
+
assert File.file?(path)
|
481
|
+
end
|
482
|
+
|
483
|
+
def test_File_io_returns_self
|
484
|
+
f = File.open("/foo", "w")
|
485
|
+
assert_equal f, f.to_io
|
486
|
+
end
|
487
|
+
|
488
|
+
def test_File_to_i_is_alias_for_filno
|
489
|
+
f = File.open("/foo", "w")
|
490
|
+
assert_equal f.method(:to_i), f.method(:fileno)
|
491
|
+
end
|
492
|
+
|
493
|
+
def test_knows_symlink_files_are_files
|
494
|
+
path = '/path/to/file.txt'
|
495
|
+
File.open(path, 'w') do |f|
|
496
|
+
f.write "Yatta!"
|
497
|
+
end
|
498
|
+
FileUtils.ln_s path, sympath='/sympath'
|
499
|
+
|
500
|
+
assert File.file?(sympath)
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_knows_non_existent_files_arent_files
|
504
|
+
assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
|
505
|
+
end
|
506
|
+
|
507
|
+
def test_can_chown_files
|
508
|
+
good = 'file.txt'
|
509
|
+
bad = 'nofile.txt'
|
510
|
+
File.open(good,'w') { |f| f.write "foo" }
|
511
|
+
|
512
|
+
out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
|
513
|
+
assert_equal [good], out
|
514
|
+
assert_raises(Errno::ENOENT) do
|
515
|
+
FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
|
516
|
+
end
|
517
|
+
|
518
|
+
assert_equal [good], FileUtils.chown('noone', 'nogroup', good)
|
519
|
+
assert_raises(Errno::ENOENT) do
|
520
|
+
FileUtils.chown('noone', 'nogroup', bad)
|
521
|
+
end
|
522
|
+
|
523
|
+
assert_equal [good], FileUtils.chown('noone', 'nogroup', [good])
|
524
|
+
assert_raises(Errno::ENOENT) do
|
525
|
+
FileUtils.chown('noone', 'nogroup', [good, bad])
|
526
|
+
end
|
527
|
+
end
|
528
|
+
|
529
|
+
def test_can_chown_R_files
|
530
|
+
FileUtils.mkdir_p '/path/'
|
531
|
+
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
532
|
+
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
533
|
+
resp = FileUtils.chown_R('no', 'no', '/path')
|
534
|
+
assert_equal ['/path'], resp
|
535
|
+
end
|
536
|
+
|
537
|
+
def test_dir_globs_paths
|
538
|
+
FileUtils.mkdir_p '/path'
|
539
|
+
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
540
|
+
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
541
|
+
|
542
|
+
FileUtils.mkdir_p '/path/bar'
|
543
|
+
File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
|
544
|
+
|
545
|
+
FileUtils.cp_r '/path/bar', '/path/bar2'
|
546
|
+
|
547
|
+
assert_equal ['/path'], Dir['/path']
|
548
|
+
assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
|
549
|
+
|
550
|
+
assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
|
551
|
+
assert_equal ['/path/foo'], Dir['/path/foo']
|
552
|
+
|
553
|
+
assert_equal ['/path'], Dir['/path*']
|
554
|
+
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
|
555
|
+
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
|
556
|
+
|
557
|
+
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
558
|
+
assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/**/*']
|
559
|
+
|
560
|
+
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
561
|
+
|
562
|
+
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
|
563
|
+
|
564
|
+
FileUtils.cp_r '/path', '/otherpath'
|
565
|
+
|
566
|
+
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
|
567
|
+
end
|
568
|
+
|
569
|
+
def test_dir_glob_handles_root
|
570
|
+
FileUtils.mkdir_p '/path'
|
571
|
+
|
572
|
+
# this fails. the root dir should be named '/' but it is '.'
|
573
|
+
#assert_equal ['/'], Dir['/']
|
574
|
+
end
|
575
|
+
|
576
|
+
def test_dir_glob_handles_recursive_globs
|
577
|
+
File.open('/one/two/three/four.rb', 'w')
|
578
|
+
File.open('/one/five.rb', 'w')
|
579
|
+
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
|
580
|
+
assert_equal ['/one/two'], Dir['/one/**/two']
|
581
|
+
assert_equal ['/one/two/three'], Dir['/one/**/three']
|
582
|
+
end
|
583
|
+
|
584
|
+
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
|
585
|
+
File.open('/one/two/three/four.rb', 'w')
|
586
|
+
File.open('/one/five.rb', 'w')
|
587
|
+
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
|
588
|
+
assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**']
|
589
|
+
end
|
590
|
+
|
591
|
+
def test_dir_glob_with_block
|
592
|
+
FileUtils.touch('foo')
|
593
|
+
FileUtils.touch('bar')
|
594
|
+
|
595
|
+
yielded = []
|
596
|
+
Dir.glob('*') { |file| yielded << file }
|
597
|
+
|
598
|
+
assert_equal 2, yielded.size
|
599
|
+
end
|
600
|
+
|
601
|
+
def test_should_report_pos_as_0_when_opening
|
602
|
+
File.open("/foo", "w") do |f|
|
603
|
+
f << "foobar"
|
604
|
+
f.rewind
|
605
|
+
|
606
|
+
assert_equal 0, f.pos
|
607
|
+
end
|
608
|
+
end
|
609
|
+
|
610
|
+
def test_should_report_pos_as_1_when_seeking_one_char
|
611
|
+
File.open("/foo", "w") do |f|
|
612
|
+
f << "foobar"
|
613
|
+
|
614
|
+
f.rewind
|
615
|
+
f.seek(1)
|
616
|
+
|
617
|
+
assert_equal 1, f.pos
|
618
|
+
end
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_should_set_pos
|
622
|
+
File.open("/foo", "w") do |f|
|
623
|
+
f << "foo"
|
624
|
+
end
|
625
|
+
|
626
|
+
fp = File.open("/foo", "r")
|
627
|
+
fp.pos = 1
|
628
|
+
|
629
|
+
assert_equal 1, fp.pos
|
630
|
+
end
|
631
|
+
|
632
|
+
def test_should_set_pos_with_tell_method
|
633
|
+
File.open("/foo", "w") do |f|
|
634
|
+
f << "foo"
|
635
|
+
end
|
636
|
+
|
637
|
+
fp = File.open("/foo", "r")
|
638
|
+
fp.tell = 1
|
639
|
+
|
640
|
+
assert_equal 1, fp.pos
|
641
|
+
end
|
642
|
+
|
643
|
+
# Every method in File is in FakeFS::File
|
644
|
+
RealFile.instance_methods.each do |method_name|
|
645
|
+
define_method "test_should_have_method_#{method_name}_from_real_file_class" do
|
646
|
+
assert File.instance_methods.include?(method_name)
|
647
|
+
end
|
648
|
+
end
|
649
|
+
|
650
|
+
# No methods which are in StringIO that aren't in File are included into
|
651
|
+
# FakeFS::File (because of inheritence)
|
652
|
+
uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
|
653
|
+
uniq_string_io_methods.each do |method_name|
|
654
|
+
define_method "test_file_should_not_respond_to_#{method_name}" do
|
655
|
+
assert !File.instance_methods.include?(method_name)
|
656
|
+
end
|
657
|
+
end
|
658
|
+
|
659
|
+
def test_does_not_remove_methods_from_stringio
|
660
|
+
stringio = StringIO.new("foo")
|
661
|
+
assert stringio.respond_to?(:size)
|
662
|
+
end
|
663
|
+
|
664
|
+
def test_chdir_changes_directories_like_a_boss
|
665
|
+
# I know memes!
|
666
|
+
FileUtils.mkdir_p '/path'
|
667
|
+
assert_equal '.', FileSystem.fs.name
|
668
|
+
assert_equal({}, FileSystem.fs['path'])
|
669
|
+
Dir.chdir '/path' do
|
670
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
671
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
672
|
+
end
|
673
|
+
|
674
|
+
assert_equal '.', FileSystem.fs.name
|
675
|
+
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
676
|
+
|
677
|
+
c = nil
|
678
|
+
Dir.chdir '/path' do
|
679
|
+
c = File.open('foo', 'r') { |f| f.read }
|
680
|
+
end
|
681
|
+
|
682
|
+
assert_equal 'foo', c
|
683
|
+
end
|
684
|
+
|
685
|
+
def test_chdir_shouldnt_keep_us_from_absolute_paths
|
686
|
+
FileUtils.mkdir_p '/path'
|
687
|
+
|
688
|
+
Dir.chdir '/path' do
|
689
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
690
|
+
File.open('/foobar', 'w') { |f| f.write 'foo'}
|
691
|
+
end
|
692
|
+
assert_equal ['foo'], FileSystem.fs['path'].keys.sort
|
693
|
+
assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
|
694
|
+
|
695
|
+
Dir.chdir '/path' do
|
696
|
+
FileUtils.rm('foo')
|
697
|
+
FileUtils.rm('/foobar')
|
698
|
+
end
|
699
|
+
|
700
|
+
assert_equal [], FileSystem.fs['path'].keys.sort
|
701
|
+
assert_equal ['path'], FileSystem.fs.keys.sort
|
702
|
+
end
|
703
|
+
|
704
|
+
def test_chdir_should_be_nestable
|
705
|
+
FileUtils.mkdir_p '/path/me'
|
706
|
+
Dir.chdir '/path' do
|
707
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
708
|
+
Dir.chdir 'me' do
|
709
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
710
|
+
end
|
711
|
+
end
|
712
|
+
|
713
|
+
assert_equal ['foo','me'], FileSystem.fs['path'].keys.sort
|
714
|
+
assert_equal ['foobar'], FileSystem.fs['path']['me'].keys.sort
|
715
|
+
end
|
716
|
+
|
717
|
+
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
|
718
|
+
assert_raise(Errno::ENOENT) do
|
719
|
+
Dir.chdir('/nope') do
|
720
|
+
1
|
721
|
+
end
|
722
|
+
end
|
723
|
+
end
|
724
|
+
|
725
|
+
def test_chdir_shouldnt_lose_state_because_of_errors
|
726
|
+
FileUtils.mkdir_p '/path'
|
727
|
+
|
728
|
+
Dir.chdir '/path' do
|
729
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
730
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
731
|
+
end
|
732
|
+
|
733
|
+
begin
|
734
|
+
Dir.chdir('/path') do
|
735
|
+
raise Exception
|
736
|
+
end
|
737
|
+
rescue Exception # hardcore
|
738
|
+
end
|
739
|
+
|
740
|
+
Dir.chdir('/path') do
|
741
|
+
begin
|
742
|
+
Dir.chdir('nope'){ }
|
743
|
+
rescue Errno::ENOENT
|
744
|
+
end
|
745
|
+
|
746
|
+
assert_equal ['/path'], FileSystem.dir_levels
|
747
|
+
end
|
748
|
+
|
749
|
+
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
750
|
+
end
|
751
|
+
|
752
|
+
def test_chdir_with_no_block_is_awesome
|
753
|
+
FileUtils.mkdir_p '/path'
|
754
|
+
Dir.chdir('/path')
|
755
|
+
FileUtils.mkdir_p 'subdir'
|
756
|
+
assert_equal ['subdir'], FileSystem.current_dir.keys
|
757
|
+
Dir.chdir('subdir')
|
758
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
759
|
+
assert_equal ['foo'], FileSystem.current_dir.keys
|
760
|
+
|
761
|
+
assert_raises(Errno::ENOENT) do
|
762
|
+
Dir.chdir('subsubdir')
|
763
|
+
end
|
764
|
+
|
765
|
+
assert_equal ['foo'], FileSystem.current_dir.keys
|
766
|
+
end
|
767
|
+
|
768
|
+
def test_current_dir_reflected_by_pwd
|
769
|
+
FileUtils.mkdir_p '/path'
|
770
|
+
Dir.chdir('/path')
|
771
|
+
|
772
|
+
assert_equal '/path', Dir.pwd
|
773
|
+
assert_equal '/path', Dir.getwd
|
774
|
+
|
775
|
+
FileUtils.mkdir_p 'subdir'
|
776
|
+
Dir.chdir('subdir')
|
777
|
+
|
778
|
+
assert_equal '/path/subdir', Dir.pwd
|
779
|
+
assert_equal '/path/subdir', Dir.getwd
|
780
|
+
end
|
781
|
+
|
782
|
+
def test_file_open_defaults_to_read
|
783
|
+
File.open('foo','w') { |f| f.write 'bar' }
|
784
|
+
assert_equal 'bar', File.open('foo') { |f| f.read }
|
785
|
+
end
|
786
|
+
|
787
|
+
def test_flush_exists_on_file
|
788
|
+
r = File.open('foo','w') { |f| f.write 'bar'; f.flush }
|
789
|
+
assert_equal 'foo', r.path
|
790
|
+
end
|
791
|
+
|
792
|
+
def test_mv_should_raise_error_on_missing_file
|
793
|
+
assert_raise(Errno::ENOENT) do
|
794
|
+
FileUtils.mv 'blafgag', 'foo'
|
795
|
+
end
|
796
|
+
end
|
797
|
+
|
798
|
+
def test_mv_actually_works
|
799
|
+
File.open('foo', 'w') { |f| f.write 'bar' }
|
800
|
+
FileUtils.mv 'foo', 'baz'
|
801
|
+
assert_equal 'bar', File.open('baz') { |f| f.read }
|
802
|
+
end
|
803
|
+
|
804
|
+
def test_mv_works_with_options
|
805
|
+
File.open('foo', 'w') {|f| f.write 'bar'}
|
806
|
+
FileUtils.mv 'foo', 'baz', :force => true
|
807
|
+
assert_equal('bar', File.open('baz') { |f| f.read })
|
808
|
+
end
|
809
|
+
|
810
|
+
def test_mv_to_directory
|
811
|
+
File.open('foo', 'w') {|f| f.write 'bar'}
|
812
|
+
FileUtils.mkdir_p 'destdir'
|
813
|
+
FileUtils.mv 'foo', 'destdir'
|
814
|
+
assert_equal('bar', File.open('destdir/foo') {|f| f.read })
|
815
|
+
assert File.directory?('destdir')
|
816
|
+
end
|
817
|
+
|
818
|
+
def test_mv_array
|
819
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
820
|
+
File.open('baz', 'w') {|f| f.write 'binky' }
|
821
|
+
FileUtils.mkdir_p 'destdir'
|
822
|
+
FileUtils.mv %w(foo baz), 'destdir'
|
823
|
+
assert_equal('bar', File.open('destdir/foo') {|f| f.read })
|
824
|
+
assert_equal('binky', File.open('destdir/baz') {|f| f.read })
|
825
|
+
end
|
826
|
+
|
827
|
+
def test_cp_actually_works
|
828
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
829
|
+
FileUtils.cp('foo', 'baz')
|
830
|
+
assert_equal 'bar', File.read('baz')
|
831
|
+
end
|
832
|
+
|
833
|
+
def test_cp_file_into_dir
|
834
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
835
|
+
FileUtils.mkdir_p 'baz'
|
836
|
+
|
837
|
+
FileUtils.cp('foo', 'baz')
|
838
|
+
assert_equal 'bar', File.read('baz/foo')
|
839
|
+
end
|
840
|
+
|
841
|
+
def test_cp_overwrites_dest_file
|
842
|
+
File.open('foo', 'w') {|f| f.write 'FOO' }
|
843
|
+
File.open('bar', 'w') {|f| f.write 'BAR' }
|
844
|
+
|
845
|
+
FileUtils.cp('foo', 'bar')
|
846
|
+
assert_equal 'FOO', File.read('bar')
|
847
|
+
end
|
848
|
+
|
849
|
+
def test_cp_fails_on_no_source
|
850
|
+
assert_raise Errno::ENOENT do
|
851
|
+
FileUtils.cp('foo', 'baz')
|
852
|
+
end
|
853
|
+
end
|
854
|
+
|
855
|
+
def test_cp_fails_on_directory_copy
|
856
|
+
FileUtils.mkdir_p 'baz'
|
857
|
+
|
858
|
+
assert_raise Errno::EISDIR do
|
859
|
+
FileUtils.cp('baz', 'bar')
|
860
|
+
end
|
861
|
+
end
|
862
|
+
|
863
|
+
def test_cp_r_doesnt_tangle_files_together
|
864
|
+
File.open('foo', 'w') { |f| f.write 'bar' }
|
865
|
+
FileUtils.cp_r('foo', 'baz')
|
866
|
+
File.open('baz', 'w') { |f| f.write 'quux' }
|
867
|
+
assert_equal 'bar', File.open('foo') { |f| f.read }
|
868
|
+
end
|
869
|
+
|
870
|
+
def test_cp_r_should_raise_error_on_missing_file
|
871
|
+
# Yes, this error sucks, but it conforms to the original Ruby
|
872
|
+
# method.
|
873
|
+
assert_raise(RuntimeError) do
|
874
|
+
FileUtils.cp_r 'blafgag', 'foo'
|
875
|
+
end
|
876
|
+
end
|
877
|
+
|
878
|
+
def test_cp_r_handles_copying_directories
|
879
|
+
FileUtils.mkdir_p 'subdir'
|
880
|
+
Dir.chdir('subdir'){ File.open('foo', 'w') { |f| f.write 'footext' } }
|
881
|
+
|
882
|
+
FileUtils.mkdir_p 'baz'
|
883
|
+
|
884
|
+
# To a previously uncreated directory
|
885
|
+
FileUtils.cp_r('subdir', 'quux')
|
886
|
+
assert_equal 'footext', File.open('quux/foo') { |f| f.read }
|
887
|
+
|
888
|
+
# To a directory that already exists
|
889
|
+
FileUtils.cp_r('subdir', 'baz')
|
890
|
+
assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
|
891
|
+
|
892
|
+
# To a subdirectory of a directory that does not exist
|
893
|
+
assert_raises(Errno::ENOENT) do
|
894
|
+
FileUtils.cp_r('subdir', 'nope/something')
|
895
|
+
end
|
896
|
+
end
|
897
|
+
|
898
|
+
def test_cp_r_only_copies_into_directories
|
899
|
+
FileUtils.mkdir_p 'subdir'
|
900
|
+
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
901
|
+
|
902
|
+
File.open('bar', 'w') { |f| f.write 'bartext' }
|
903
|
+
|
904
|
+
assert_raises(Errno::EEXIST) do
|
905
|
+
FileUtils.cp_r 'subdir', 'bar'
|
906
|
+
end
|
907
|
+
|
908
|
+
FileUtils.mkdir_p 'otherdir'
|
909
|
+
FileUtils.ln_s 'otherdir', 'symdir'
|
910
|
+
|
911
|
+
FileUtils.cp_r 'subdir', 'symdir'
|
912
|
+
assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
|
913
|
+
end
|
914
|
+
|
915
|
+
def test_cp_r_sets_parent_correctly
|
916
|
+
FileUtils.mkdir_p '/path/foo'
|
917
|
+
File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
|
918
|
+
File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
|
919
|
+
|
920
|
+
FileUtils.cp_r '/path/foo', '/path/bar'
|
921
|
+
|
922
|
+
assert File.exists?('/path/bar/baz')
|
923
|
+
FileUtils.rm_rf '/path/bar/baz'
|
924
|
+
assert_equal %w( /path/bar/bar ), Dir['/path/bar/*']
|
925
|
+
end
|
926
|
+
|
927
|
+
def test_clone_clones_normal_files
|
928
|
+
RealFile.open(here('foo'), 'w') { |f| f.write 'bar' }
|
929
|
+
assert !File.exists?(here('foo'))
|
930
|
+
FileSystem.clone(here('foo'))
|
931
|
+
assert_equal 'bar', File.open(here('foo')) { |f| f.read }
|
932
|
+
ensure
|
933
|
+
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
|
934
|
+
end
|
935
|
+
|
936
|
+
def test_clone_clones_directories
|
937
|
+
FakeFS.deactivate!
|
938
|
+
RealFileUtils.mkdir_p(here('subdir'))
|
939
|
+
FakeFS.activate!
|
940
|
+
|
941
|
+
FileSystem.clone(here('subdir'))
|
942
|
+
|
943
|
+
assert File.exists?(here('subdir')), 'subdir was cloned'
|
944
|
+
assert File.directory?(here('subdir')), 'subdir is a directory'
|
945
|
+
ensure
|
946
|
+
FakeFS.deactivate!
|
947
|
+
RealFileUtils.rm_rf(here('subdir'))
|
948
|
+
FakeFS.activate!
|
949
|
+
end
|
950
|
+
|
951
|
+
def test_clone_clones_dot_files_even_hard_to_find_ones
|
952
|
+
FakeFS.deactivate!
|
953
|
+
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
|
954
|
+
FakeFS.activate!
|
955
|
+
|
956
|
+
assert !File.exists?(here('subdir'))
|
957
|
+
|
958
|
+
FileSystem.clone(here('subdir'))
|
959
|
+
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
|
960
|
+
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
|
961
|
+
ensure
|
962
|
+
FakeFS.deactivate!
|
963
|
+
RealFileUtils.rm_rf(here('subdir'))
|
964
|
+
FakeFS.activate!
|
965
|
+
end
|
966
|
+
|
967
|
+
def test_putting_a_dot_at_end_copies_the_contents
|
968
|
+
FileUtils.mkdir_p 'subdir'
|
969
|
+
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
970
|
+
|
971
|
+
FileUtils.mkdir_p 'newdir'
|
972
|
+
FileUtils.cp_r 'subdir/.', 'newdir'
|
973
|
+
assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
|
974
|
+
end
|
975
|
+
|
976
|
+
def test_file_can_read_from_symlinks
|
977
|
+
File.open('first', 'w') { |f| f.write '1'}
|
978
|
+
FileUtils.ln_s 'first', 'one'
|
979
|
+
assert_equal '1', File.open('one') { |f| f.read }
|
980
|
+
|
981
|
+
FileUtils.mkdir_p 'subdir'
|
982
|
+
File.open('subdir/nother','w') { |f| f.write 'works' }
|
983
|
+
FileUtils.ln_s 'subdir', 'new'
|
984
|
+
assert_equal 'works', File.open('new/nother') { |f| f.read }
|
985
|
+
end
|
986
|
+
|
987
|
+
def test_can_symlink_through_file
|
988
|
+
FileUtils.touch("/foo")
|
989
|
+
|
990
|
+
File.symlink("/foo", "/bar")
|
991
|
+
|
992
|
+
assert File.symlink?("/bar")
|
993
|
+
end
|
994
|
+
|
995
|
+
def test_files_can_be_touched
|
996
|
+
FileUtils.touch('touched_file')
|
997
|
+
assert File.exists?('touched_file')
|
998
|
+
list = ['newfile', 'another']
|
999
|
+
FileUtils.touch(list)
|
1000
|
+
list.each { |fp| assert(File.exists?(fp)) }
|
1001
|
+
end
|
1002
|
+
|
1003
|
+
def test_touch_does_not_work_if_the_dir_path_cannot_be_found
|
1004
|
+
assert_raises(Errno::ENOENT) do
|
1005
|
+
FileUtils.touch('this/path/should/not/be/here')
|
1006
|
+
end
|
1007
|
+
FileUtils.mkdir_p('subdir')
|
1008
|
+
list = ['subdir/foo', 'nosubdir/bar']
|
1009
|
+
|
1010
|
+
assert_raises(Errno::ENOENT) do
|
1011
|
+
FileUtils.touch(list)
|
1012
|
+
end
|
1013
|
+
end
|
1014
|
+
|
1015
|
+
def test_extname
|
1016
|
+
assert File.extname("test.doc") == ".doc"
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
# Directory tests
|
1020
|
+
def test_new_directory
|
1021
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1022
|
+
|
1023
|
+
assert_nothing_raised do
|
1024
|
+
Dir.new('/this/path/should/be/here')
|
1025
|
+
end
|
1026
|
+
end
|
1027
|
+
|
1028
|
+
def test_new_directory_does_not_work_if_dir_path_cannot_be_found
|
1029
|
+
assert_raises(Errno::ENOENT) do
|
1030
|
+
Dir.new('/this/path/should/not/be/here')
|
1031
|
+
end
|
1032
|
+
end
|
1033
|
+
|
1034
|
+
def test_directory_close
|
1035
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1036
|
+
dir = Dir.new('/this/path/should/be/here')
|
1037
|
+
assert dir.close.nil?
|
1038
|
+
|
1039
|
+
assert_raises(IOError) do
|
1040
|
+
dir.each { |dir| dir }
|
1041
|
+
end
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
def test_directory_each
|
1045
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1046
|
+
|
1047
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1048
|
+
|
1049
|
+
test.each do |f|
|
1050
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1051
|
+
end
|
1052
|
+
|
1053
|
+
dir = Dir.new('/this/path/should/be/here')
|
1054
|
+
|
1055
|
+
yielded = []
|
1056
|
+
dir.each do |dir|
|
1057
|
+
yielded << dir
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
assert yielded.size == test.size
|
1061
|
+
test.each { |t| assert yielded.include?(t) }
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
def test_directory_path
|
1065
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1066
|
+
good_path = '/this/path/should/be/here'
|
1067
|
+
assert_equal good_path, Dir.new('/this/path/should/be/here').path
|
1068
|
+
end
|
1069
|
+
|
1070
|
+
def test_directory_pos
|
1071
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1072
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1073
|
+
test.each do |f|
|
1074
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1075
|
+
end
|
1076
|
+
|
1077
|
+
dir = Dir.new('/this/path/should/be/here')
|
1078
|
+
|
1079
|
+
assert dir.pos == 0
|
1080
|
+
dir.read
|
1081
|
+
assert dir.pos == 1
|
1082
|
+
dir.read
|
1083
|
+
assert dir.pos == 2
|
1084
|
+
dir.read
|
1085
|
+
assert dir.pos == 3
|
1086
|
+
dir.read
|
1087
|
+
assert dir.pos == 4
|
1088
|
+
dir.read
|
1089
|
+
assert dir.pos == 5
|
1090
|
+
end
|
1091
|
+
|
1092
|
+
def test_directory_pos_assign
|
1093
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1094
|
+
|
1095
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1096
|
+
test.each do |f|
|
1097
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1098
|
+
end
|
1099
|
+
|
1100
|
+
dir = Dir.new('/this/path/should/be/here')
|
1101
|
+
|
1102
|
+
assert dir.pos == 0
|
1103
|
+
dir.pos = 2
|
1104
|
+
assert dir.pos == 2
|
1105
|
+
end
|
1106
|
+
|
1107
|
+
def test_directory_read
|
1108
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1109
|
+
|
1110
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1111
|
+
test.each do |f|
|
1112
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1113
|
+
end
|
1114
|
+
|
1115
|
+
dir = Dir.new('/this/path/should/be/here')
|
1116
|
+
|
1117
|
+
assert dir.pos == 0
|
1118
|
+
d = dir.read
|
1119
|
+
assert dir.pos == 1
|
1120
|
+
assert d == '.'
|
1121
|
+
|
1122
|
+
d = dir.read
|
1123
|
+
assert dir.pos == 2
|
1124
|
+
assert d == '..'
|
1125
|
+
end
|
1126
|
+
|
1127
|
+
def test_directory_read_past_length
|
1128
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1129
|
+
|
1130
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1131
|
+
test.each do |f|
|
1132
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
dir = Dir.new('/this/path/should/be/here')
|
1136
|
+
|
1137
|
+
d = dir.read
|
1138
|
+
assert_not_nil d
|
1139
|
+
d = dir.read
|
1140
|
+
assert_not_nil d
|
1141
|
+
d = dir.read
|
1142
|
+
assert_not_nil d
|
1143
|
+
d = dir.read
|
1144
|
+
assert_not_nil d
|
1145
|
+
d = dir.read
|
1146
|
+
assert_not_nil d
|
1147
|
+
d = dir.read
|
1148
|
+
assert_not_nil d
|
1149
|
+
d = dir.read
|
1150
|
+
assert_not_nil d
|
1151
|
+
d = dir.read
|
1152
|
+
assert_nil d
|
1153
|
+
end
|
1154
|
+
|
1155
|
+
def test_directory_rewind
|
1156
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1157
|
+
|
1158
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1159
|
+
test.each do |f|
|
1160
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1161
|
+
end
|
1162
|
+
|
1163
|
+
dir = Dir.new('/this/path/should/be/here')
|
1164
|
+
|
1165
|
+
d = dir.read
|
1166
|
+
d = dir.read
|
1167
|
+
assert dir.pos == 2
|
1168
|
+
dir.rewind
|
1169
|
+
assert dir.pos == 0
|
1170
|
+
end
|
1171
|
+
|
1172
|
+
def test_directory_seek
|
1173
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1174
|
+
|
1175
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1176
|
+
test.each do |f|
|
1177
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1178
|
+
end
|
1179
|
+
|
1180
|
+
dir = Dir.new('/this/path/should/be/here')
|
1181
|
+
|
1182
|
+
d = dir.seek 1
|
1183
|
+
assert d == '..'
|
1184
|
+
assert dir.pos == 1
|
1185
|
+
end
|
1186
|
+
|
1187
|
+
def test_directory_class_delete
|
1188
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1189
|
+
Dir.delete('/this/path/should/be/here')
|
1190
|
+
assert File.exists?('/this/path/should/be/here') == false
|
1191
|
+
end
|
1192
|
+
|
1193
|
+
def test_directory_class_delete_does_not_act_on_non_empty_directory
|
1194
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1195
|
+
|
1196
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1197
|
+
test.each do |f|
|
1198
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
assert_raises(SystemCallError) do
|
1202
|
+
Dir.delete('/this/path/should/be/here')
|
1203
|
+
end
|
1204
|
+
end
|
1205
|
+
|
1206
|
+
def test_directory_entries
|
1207
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1208
|
+
|
1209
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1210
|
+
|
1211
|
+
test.each do |f|
|
1212
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1213
|
+
end
|
1214
|
+
|
1215
|
+
yielded = Dir.entries('/this/path/should/be/here')
|
1216
|
+
assert yielded.size == test.size
|
1217
|
+
test.each { |t| assert yielded.include?(t) }
|
1218
|
+
end
|
1219
|
+
|
1220
|
+
def test_directory_entries_works_with_trailing_slash
|
1221
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1222
|
+
|
1223
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1224
|
+
|
1225
|
+
test.each do |f|
|
1226
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
yielded = Dir.entries('/this/path/should/be/here/')
|
1230
|
+
assert yielded.size == test.size
|
1231
|
+
test.each { |t| assert yielded.include?(t) }
|
1232
|
+
end
|
1233
|
+
|
1234
|
+
def test_directory_foreach
|
1235
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1236
|
+
|
1237
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1238
|
+
|
1239
|
+
test.each do |f|
|
1240
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1241
|
+
end
|
1242
|
+
|
1243
|
+
yielded = []
|
1244
|
+
Dir.foreach('/this/path/should/be/here') do |dir|
|
1245
|
+
yielded << dir
|
1246
|
+
end
|
1247
|
+
|
1248
|
+
assert yielded.size == test.size
|
1249
|
+
test.each { |t| assert yielded.include?(t) }
|
1250
|
+
end
|
1251
|
+
|
1252
|
+
def test_directory_mkdir
|
1253
|
+
Dir.mkdir('/path')
|
1254
|
+
assert File.exists?('/path')
|
1255
|
+
end
|
1256
|
+
|
1257
|
+
def test_directory_mkdir_relative
|
1258
|
+
FileUtils.mkdir_p('/new/root')
|
1259
|
+
FileSystem.chdir('/new/root')
|
1260
|
+
Dir.mkdir('path')
|
1261
|
+
assert File.exists?('/new/root/path')
|
1262
|
+
end
|
1263
|
+
|
1264
|
+
def test_directory_mkdir_not_recursive
|
1265
|
+
assert_raises(Errno::ENOENT) do
|
1266
|
+
Dir.mkdir('/path/does/not/exist')
|
1267
|
+
end
|
1268
|
+
end
|
1269
|
+
|
1270
|
+
def test_mkdir_raises_error_if_already_created
|
1271
|
+
Dir.mkdir "foo"
|
1272
|
+
|
1273
|
+
assert_raises(Errno::EEXIST) do
|
1274
|
+
Dir.mkdir "foo"
|
1275
|
+
end
|
1276
|
+
end
|
1277
|
+
|
1278
|
+
def test_directory_open
|
1279
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1280
|
+
|
1281
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1282
|
+
|
1283
|
+
test.each do |f|
|
1284
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1285
|
+
end
|
1286
|
+
|
1287
|
+
dir = Dir.open('/this/path/should/be/here')
|
1288
|
+
assert dir.path == '/this/path/should/be/here'
|
1289
|
+
end
|
1290
|
+
|
1291
|
+
def test_directory_open_block
|
1292
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1293
|
+
|
1294
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1295
|
+
|
1296
|
+
test.each do |f|
|
1297
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1298
|
+
end
|
1299
|
+
|
1300
|
+
yielded = []
|
1301
|
+
Dir.open('/this/path/should/be/here') do |dir|
|
1302
|
+
yielded << dir
|
1303
|
+
end
|
1304
|
+
|
1305
|
+
assert yielded.size == test.size
|
1306
|
+
test.each { |t| assert yielded.include?(t) }
|
1307
|
+
end
|
1308
|
+
|
1309
|
+
def test_tmpdir
|
1310
|
+
assert Dir.tmpdir == "/tmp"
|
1311
|
+
end
|
1312
|
+
|
1313
|
+
def test_rename_renames_a_file
|
1314
|
+
FileUtils.touch("/foo")
|
1315
|
+
File.rename("/foo", "/bar")
|
1316
|
+
assert File.file?("/bar")
|
1317
|
+
end
|
1318
|
+
|
1319
|
+
def test_rename_returns
|
1320
|
+
FileUtils.touch("/foo")
|
1321
|
+
assert_equal 0, File.rename("/foo", "/bar")
|
1322
|
+
end
|
1323
|
+
|
1324
|
+
def test_rename_renames_two_files
|
1325
|
+
FileUtils.touch("/foo")
|
1326
|
+
FileUtils.touch("/bar")
|
1327
|
+
File.rename("/foo", "/bar")
|
1328
|
+
assert File.file?("/bar")
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
def test_rename_renames_a_directories
|
1332
|
+
Dir.mkdir("/foo")
|
1333
|
+
File.rename("/foo", "/bar")
|
1334
|
+
assert File.directory?("/bar")
|
1335
|
+
end
|
1336
|
+
|
1337
|
+
def test_rename_renames_two_directories
|
1338
|
+
Dir.mkdir("/foo")
|
1339
|
+
Dir.mkdir("/bar")
|
1340
|
+
File.rename("/foo", "/bar")
|
1341
|
+
assert File.directory?("/bar")
|
1342
|
+
end
|
1343
|
+
|
1344
|
+
def test_rename_file_to_directory_raises_error
|
1345
|
+
FileUtils.touch("/foo")
|
1346
|
+
Dir.mkdir("/bar")
|
1347
|
+
assert_raises(Errno::EISDIR) do
|
1348
|
+
File.rename("/foo", "/bar")
|
1349
|
+
end
|
1350
|
+
end
|
1351
|
+
|
1352
|
+
def test_rename_directory_to_file_raises_error
|
1353
|
+
Dir.mkdir("/foo")
|
1354
|
+
FileUtils.touch("/bar")
|
1355
|
+
assert_raises(Errno::ENOTDIR) do
|
1356
|
+
File.rename("/foo", "/bar")
|
1357
|
+
end
|
1358
|
+
end
|
1359
|
+
|
1360
|
+
|
1361
|
+
def test_rename_with_missing_source_raises_error
|
1362
|
+
assert_raises(Errno::ENOENT) do
|
1363
|
+
File.rename("/no_such_file", "/bar")
|
1364
|
+
end
|
1365
|
+
end
|
1366
|
+
|
1367
|
+
def test_hard_link_creates_file
|
1368
|
+
FileUtils.touch("/foo")
|
1369
|
+
|
1370
|
+
File.link("/foo", "/bar")
|
1371
|
+
assert File.exists?("/bar")
|
1372
|
+
end
|
1373
|
+
|
1374
|
+
def test_hard_link_with_missing_file_raises_error
|
1375
|
+
assert_raises(Errno::ENOENT) do
|
1376
|
+
File.link("/foo", "/bar")
|
1377
|
+
end
|
1378
|
+
end
|
1379
|
+
|
1380
|
+
def test_hard_link_with_existing_destination_file
|
1381
|
+
FileUtils.touch("/foo")
|
1382
|
+
FileUtils.touch("/bar")
|
1383
|
+
|
1384
|
+
assert_raises(Errno::EEXIST) do
|
1385
|
+
File.link("/foo", "/bar")
|
1386
|
+
end
|
1387
|
+
end
|
1388
|
+
|
1389
|
+
def test_hard_link_returns_0_when_successful
|
1390
|
+
FileUtils.touch("/foo")
|
1391
|
+
|
1392
|
+
assert_equal 0, File.link("/foo", "/bar")
|
1393
|
+
end
|
1394
|
+
|
1395
|
+
def test_hard_link_returns_duplicate_file
|
1396
|
+
File.open("/foo", "w") { |x| x << "some content" }
|
1397
|
+
|
1398
|
+
File.link("/foo", "/bar")
|
1399
|
+
assert_equal "some content", File.read("/bar")
|
1400
|
+
end
|
1401
|
+
|
1402
|
+
def test_hard_link_with_directory_raises_error
|
1403
|
+
Dir.mkdir "/foo"
|
1404
|
+
|
1405
|
+
assert_raises(Errno::EPERM) do
|
1406
|
+
File.link("/foo", "/bar")
|
1407
|
+
end
|
1408
|
+
end
|
1409
|
+
|
1410
|
+
def test_file_stat_returns_file_stat_object
|
1411
|
+
FileUtils.touch("/foo")
|
1412
|
+
assert_equal File::Stat, File.stat("/foo").class
|
1413
|
+
end
|
1414
|
+
|
1415
|
+
def test_can_delete_file_with_delete
|
1416
|
+
FileUtils.touch("/foo")
|
1417
|
+
|
1418
|
+
File.delete("/foo")
|
1419
|
+
|
1420
|
+
assert !File.exists?("/foo")
|
1421
|
+
end
|
1422
|
+
|
1423
|
+
def test_can_delete_multiple_files_with_delete
|
1424
|
+
FileUtils.touch("/foo")
|
1425
|
+
FileUtils.touch("/bar")
|
1426
|
+
|
1427
|
+
File.delete("/foo", "/bar")
|
1428
|
+
|
1429
|
+
assert !File.exists?("/foo")
|
1430
|
+
assert !File.exists?("/bar")
|
1431
|
+
end
|
1432
|
+
|
1433
|
+
def test_delete_raises_argument_error_with_no_filename_given
|
1434
|
+
assert_raises ArgumentError do
|
1435
|
+
File.delete
|
1436
|
+
end
|
1437
|
+
end
|
1438
|
+
|
1439
|
+
def test_delete_returns_number_one_when_given_one_arg
|
1440
|
+
FileUtils.touch("/foo")
|
1441
|
+
|
1442
|
+
assert_equal 1, File.delete("/foo")
|
1443
|
+
end
|
1444
|
+
|
1445
|
+
def test_delete_returns_number_two_when_given_two_args
|
1446
|
+
FileUtils.touch("/foo")
|
1447
|
+
FileUtils.touch("/bar")
|
1448
|
+
|
1449
|
+
assert_equal 2, File.delete("/foo", "/bar")
|
1450
|
+
end
|
1451
|
+
|
1452
|
+
def test_delete_raises_error_when_first_file_does_not_exist
|
1453
|
+
assert_raises Errno::ENOENT do
|
1454
|
+
File.delete("/foo")
|
1455
|
+
end
|
1456
|
+
end
|
1457
|
+
|
1458
|
+
def test_delete_does_not_raise_error_when_second_file_does_not_exist
|
1459
|
+
FileUtils.touch("/foo")
|
1460
|
+
|
1461
|
+
assert_nothing_raised do
|
1462
|
+
File.delete("/foo", "/bar")
|
1463
|
+
end
|
1464
|
+
end
|
1465
|
+
|
1466
|
+
def test_unlink_is_alias_for_delete
|
1467
|
+
assert_equal File.method(:unlink), File.method(:delete)
|
1468
|
+
end
|
1469
|
+
|
1470
|
+
def test_unlink_removes_only_one_file_content
|
1471
|
+
File.open("/foo", "w") { |f| f << "some_content" }
|
1472
|
+
File.link("/foo", "/bar")
|
1473
|
+
|
1474
|
+
File.unlink("/bar")
|
1475
|
+
File.read("/foo") == "some_content"
|
1476
|
+
end
|
1477
|
+
|
1478
|
+
def test_link_reports_correct_stat_info_after_unlinking
|
1479
|
+
File.open("/foo", "w") { |f| f << "some_content" }
|
1480
|
+
File.link("/foo", "/bar")
|
1481
|
+
|
1482
|
+
File.unlink("/bar")
|
1483
|
+
assert_equal 1, File.stat("/foo").nlink
|
1484
|
+
end
|
1485
|
+
|
1486
|
+
def test_delete_works_with_symlink
|
1487
|
+
FileUtils.touch("/foo")
|
1488
|
+
File.symlink("/foo", "/bar")
|
1489
|
+
|
1490
|
+
File.unlink("/bar")
|
1491
|
+
|
1492
|
+
assert File.exists?("/foo")
|
1493
|
+
assert !File.exists?("/bar")
|
1494
|
+
end
|
1495
|
+
|
1496
|
+
def test_delete_works_with_symlink_source
|
1497
|
+
FileUtils.touch("/foo")
|
1498
|
+
File.symlink("/foo", "/bar")
|
1499
|
+
|
1500
|
+
File.unlink("/foo")
|
1501
|
+
|
1502
|
+
assert !File.exists?("/foo")
|
1503
|
+
end
|
1504
|
+
|
1505
|
+
def test_file_seek_returns_0
|
1506
|
+
File.open("/foo", "w") do |f|
|
1507
|
+
f << "one\ntwo\nthree"
|
1508
|
+
end
|
1509
|
+
|
1510
|
+
file = File.open("/foo", "r")
|
1511
|
+
|
1512
|
+
assert_equal 0, file.seek(1)
|
1513
|
+
end
|
1514
|
+
|
1515
|
+
def test_file_seek_seeks_to_location
|
1516
|
+
File.open("/foo", "w") do |f|
|
1517
|
+
f << "123"
|
1518
|
+
end
|
1519
|
+
|
1520
|
+
file = File.open("/foo", "r")
|
1521
|
+
file.seek(1)
|
1522
|
+
assert_equal "23", file.read
|
1523
|
+
end
|
1524
|
+
|
1525
|
+
def test_file_seek_seeks_to_correct_location
|
1526
|
+
File.open("/foo", "w") do |f|
|
1527
|
+
f << "123"
|
1528
|
+
end
|
1529
|
+
|
1530
|
+
file = File.open("/foo", "r")
|
1531
|
+
file.seek(2)
|
1532
|
+
assert_equal "3", file.read
|
1533
|
+
end
|
1534
|
+
|
1535
|
+
def test_file_seek_can_take_negative_offset
|
1536
|
+
File.open("/foo", "w") do |f|
|
1537
|
+
f << "123456789"
|
1538
|
+
end
|
1539
|
+
|
1540
|
+
file = File.open("/foo", "r")
|
1541
|
+
|
1542
|
+
file.seek(-1, IO::SEEK_END)
|
1543
|
+
assert_equal "9", file.read
|
1544
|
+
|
1545
|
+
file.seek(-2, IO::SEEK_END)
|
1546
|
+
assert_equal "89", file.read
|
1547
|
+
|
1548
|
+
file.seek(-3, IO::SEEK_END)
|
1549
|
+
assert_equal "789", file.read
|
1550
|
+
end
|
1551
|
+
|
1552
|
+
def test_should_have_constants_inherited_from_descending_from_io
|
1553
|
+
assert_equal IO::SEEK_CUR, File::SEEK_CUR
|
1554
|
+
assert_equal IO::SEEK_END, File::SEEK_END
|
1555
|
+
assert_equal IO::SEEK_SET, File::SEEK_SET
|
1556
|
+
end
|
1557
|
+
|
1558
|
+
def test_filetest_exists_return_correct_values
|
1559
|
+
FileUtils.mkdir_p("/path/to/dir")
|
1560
|
+
assert FileTest.exist?("/path/to/")
|
1561
|
+
|
1562
|
+
FileUtils.rmdir("/path/to/dir")
|
1563
|
+
assert !FileTest.exist?("/path/to/dir")
|
1564
|
+
end
|
1565
|
+
|
1566
|
+
def test_pathname_exists_returns_correct_value
|
1567
|
+
FileUtils.touch "foo"
|
1568
|
+
assert Pathname.new("foo").exist?
|
1569
|
+
|
1570
|
+
assert !Pathname.new("bar").exist?
|
1571
|
+
end
|
1572
|
+
|
1573
|
+
def test_activating_returns_true
|
1574
|
+
FakeFS.deactivate!
|
1575
|
+
assert_equal true, FakeFS.activate!
|
1576
|
+
end
|
1577
|
+
|
1578
|
+
def test_deactivating_returns_true
|
1579
|
+
assert_equal true, FakeFS.deactivate!
|
1580
|
+
end
|
1581
|
+
|
1582
|
+
def here(fname)
|
1583
|
+
RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
|
1584
|
+
end
|
1585
|
+
|
1586
|
+
if RUBY_VERSION >= "1.9.2"
|
1587
|
+
def test_file_size
|
1588
|
+
File.open("foo", 'w') do |f|
|
1589
|
+
f << 'Yada Yada'
|
1590
|
+
assert_equal 9, f.size
|
1591
|
+
end
|
1592
|
+
end
|
1593
|
+
|
1594
|
+
def test_fdatasync
|
1595
|
+
File.open("foo", 'w') do |f|
|
1596
|
+
f << 'Yada Yada'
|
1597
|
+
assert_nothing_raised do
|
1598
|
+
f.fdatasync
|
1599
|
+
end
|
1600
|
+
end
|
1601
|
+
end
|
1602
|
+
|
1603
|
+
def test_autoclose
|
1604
|
+
File.open("foo", 'w') do |f|
|
1605
|
+
assert_equal true, f.autoclose?
|
1606
|
+
f.autoclose = false
|
1607
|
+
assert_equal false, f.autoclose?
|
1608
|
+
end
|
1609
|
+
end
|
1610
|
+
end
|
1611
|
+
end
|