1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
commit af733ea5606391c594fb5e228fc1e94515c1e7e4
Author: Mateusz Juraszek <meceo00@gmail.com>
Date: Tue Jan 3 11:00:56 2012 +0100
add external encoding as default to File @content string,
handle hash as second parameter to File::new,
extend FakeFS::FileSystem::clone method to accept 'target' as a optional parameter,
fix Dir::mkdir for nested structure
diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb
index 628626f..ed571ff 100644
--- a/lib/fakefs/dir.rb
+++ b/lib/fakefs/dir.rb
@@ -105,7 +105,7 @@ module FakeFS
parent = string.split('/')
parent.pop
- joined_parent_path = parent.join
+ joined_parent_path = parent.join("/")
_check_for_valid_file(joined_parent_path) unless joined_parent_path == ""
raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string)
diff --git a/lib/fakefs/fake/file.rb b/lib/fakefs/fake/file.rb
index 10bdf7f..22f32d9 100644
--- a/lib/fakefs/fake/file.rb
+++ b/lib/fakefs/fake/file.rb
@@ -5,7 +5,8 @@ module FakeFS
class Inode
def initialize(file_owner)
- @content = ""
+ #1.9.3 when possible set default external encoding
+ @content = "".respond_to?(:encode) ? "".encode(Encoding.default_external) : ""
@links = [file_owner]
end
diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb
index 5684ede..5f36376 100644
--- a/lib/fakefs/file.rb
+++ b/lib/fakefs/file.rb
@@ -275,7 +275,7 @@ module FakeFS
def initialize(path, mode = READ_ONLY, perm = nil)
@path = path
- @mode = mode
+ @mode = mode.is_a?(Hash) ? (mode[:mode] || READ_ONLY) : mode
@file = FileSystem.find(path)
@autoclose = true
@@ -283,7 +283,7 @@ module FakeFS
file_creation_mode? ? create_missing_file : check_file_existence!
- super(@file.content, mode)
+ super(@file.content, @mode)
end
def exists?
diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb
index a5c8087..da58ad9 100644
--- a/lib/fakefs/file_system.rb
+++ b/lib/fakefs/file_system.rb
@@ -46,19 +46,20 @@ module FakeFS
# copies directories and files from the real filesystem
# into our fake one
- def clone(path)
+ def clone(path, target = nil)
path = File.expand_path(path)
pattern = File.join(path, '**', '*')
files = RealFile.file?(path) ? [path] : [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH)
files.each do |f|
+ target_path = target ? f.gsub(path, target) : f
if RealFile.file?(f)
FileUtils.mkdir_p(File.dirname(f))
- File.open(f, File::WRITE_ONLY) do |g|
+ File.open(target_path, File::WRITE_ONLY) do |g|
g.print RealFile.open(f){|h| h.read }
end
elsif RealFile.directory?(f)
- FileUtils.mkdir_p(f)
+ FileUtils.mkdir_p(target_path)
elsif RealFile.symlink?(f)
FileUtils.ln_s()
end
diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb
index 0f34950..7e2b8dc 100644
--- a/test/fakefs_test.rb
+++ b/test/fakefs_test.rb
@@ -647,6 +647,21 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal "Yatta!", File.new(path).read
end
+ if RUBY_VERSION >= "1.9"
+ def test_file_object_has_default_external_encoding
+ Encoding.default_external = "UTF-8"
+ path = 'file.txt'
+ File.open(path, 'w'){|f| f.write 'Yatta!' }
+ assert_equal "UTF-8", File.new(path).read.encoding.name
+ end
+ end
+
+ def test_file_object_initialization_with_mode_in_hash_parameter
+ assert_nothing_raised do
+ File.open("file.txt", {:mode => "w"}){ |f| f.write 'Yatta!' }
+ end
+ end
+
def test_file_read_errors_appropriately
assert_raise Errno::ENOENT do
File.read('anything')
@@ -1132,24 +1147,18 @@ class FakeFSTest < Test::Unit::TestCase
end
def test_clone_clones_directories
- FakeFS.deactivate!
- RealFileUtils.mkdir_p(here('subdir'))
- FakeFS.activate!
+ act_on_real_fs { RealFileUtils.mkdir_p(here('subdir')) }
FileSystem.clone(here('subdir'))
assert File.exists?(here('subdir')), 'subdir was cloned'
assert File.directory?(here('subdir')), 'subdir is a directory'
ensure
- FakeFS.deactivate!
- RealFileUtils.rm_rf(here('subdir'))
- FakeFS.activate!
+ act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
end
def test_clone_clones_dot_files_even_hard_to_find_ones
- FakeFS.deactivate!
- RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
- FakeFS.activate!
+ act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }
assert !File.exists?(here('subdir'))
@@ -1157,9 +1166,20 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
ensure
- FakeFS.deactivate!
- RealFileUtils.rm_rf(here('subdir'))
- FakeFS.activate!
+ act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
+ end
+
+ def test_clone_with_target_specified
+ act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }
+
+ assert !File.exists?(here('subdir'))
+
+ FileSystem.clone(here('subdir'), here('subdir2'))
+ assert !File.exists?(here('subdir'))
+ assert_equal ['.bar'], FileSystem.find(here('subdir2')).keys
+ assert_equal ['foo'], FileSystem.find(here('subdir2/.bar/baz/.quux')).keys
+ ensure
+ act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
end
def test_putting_a_dot_at_end_copies_the_contents
@@ -1464,6 +1484,12 @@ class FakeFSTest < Test::Unit::TestCase
assert File.exists?('/path')
end
+ def test_directory_mkdir_nested
+ Dir.mkdir("/tmp")
+ Dir.mkdir("/tmp/stream20120103-11847-xc8pb.lock")
+ assert File.exists?("/tmp/stream20120103-11847-xc8pb.lock")
+ end
+
def test_directory_mkdir_relative
FileUtils.mkdir_p('/new/root')
FileSystem.chdir('/new/root')
diff --git a/test/test_helper.rb b/test/test_helper.rb
index c41dd67..b6596fa 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -5,4 +5,11 @@ require 'test/unit'
begin
require 'redgreen'
rescue LoadError
-end
\ No newline at end of file
+end
+
+def act_on_real_fs
+ raise ArgumentError unless block_given?
+ FakeFS.deactivate!
+ yield
+ FakeFS.activate!
+end
|