fakefs 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/CONTRIBUTORS +44 -6
- data/Gemfile +2 -6
- data/README.markdown +4 -2
- data/Rakefile +3 -32
- data/fakefs.gemspec +21 -79
- data/lib/fakefs/dir.rb +11 -5
- data/lib/fakefs/fake/symlink.rb +1 -1
- data/lib/fakefs/file.rb +32 -14
- data/lib/fakefs/file_system.rb +15 -9
- data/lib/fakefs/file_test.rb +10 -4
- data/lib/fakefs/fileutils.rb +37 -7
- data/lib/fakefs/version.rb +1 -1
- data/spec/fakefs/fakefs_bug_ruby_2.1.0-preview2_spec.rb +17 -0
- data/test/fakefs_test.rb +221 -17
- data/test/file/stat_test.rb +6 -1
- data/test/test_helper.rb +16 -0
- metadata +47 -25
- data/Gemfile.lock +0 -31
data/lib/fakefs/file_system.rb
CHANGED
@@ -3,7 +3,7 @@ module FakeFS
|
|
3
3
|
extend self
|
4
4
|
|
5
5
|
def dir_levels
|
6
|
-
@dir_levels ||= []
|
6
|
+
@dir_levels ||= ['/']
|
7
7
|
end
|
8
8
|
|
9
9
|
def fs
|
@@ -36,9 +36,11 @@ module FakeFS
|
|
36
36
|
parts = path_parts(normalize_path(path))
|
37
37
|
|
38
38
|
d = parts[0...-1].inject(fs) do |dir, part|
|
39
|
+
assert_dir dir[part] if dir[part]
|
39
40
|
dir[part] ||= FakeDir.new(part, dir)
|
40
41
|
end
|
41
42
|
|
43
|
+
assert_dir d
|
42
44
|
object.name = parts.last
|
43
45
|
object.parent = d
|
44
46
|
d[parts.last] ||= object
|
@@ -47,21 +49,21 @@ module FakeFS
|
|
47
49
|
# copies directories and files from the real filesystem
|
48
50
|
# into our fake one
|
49
51
|
def clone(path, target = nil)
|
50
|
-
path =
|
52
|
+
path = RealFile.expand_path(path)
|
51
53
|
pattern = File.join(path, '**', '*')
|
52
54
|
files = RealFile.file?(path) ? [path] : [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH)
|
53
55
|
|
54
56
|
files.each do |f|
|
55
57
|
target_path = target ? f.gsub(path, target) : f
|
56
|
-
if RealFile.
|
58
|
+
if RealFile.symlink?(f)
|
59
|
+
FileUtils.ln_s(RealFile.readlink(f), f)
|
60
|
+
elsif RealFile.file?(f)
|
57
61
|
FileUtils.mkdir_p(File.dirname(f))
|
58
62
|
File.open(target_path, File::WRITE_ONLY) do |g|
|
59
63
|
g.print RealFile.open(f){|h| h.read }
|
60
64
|
end
|
61
65
|
elsif RealFile.directory?(f)
|
62
66
|
FileUtils.mkdir_p(target_path)
|
63
|
-
elsif RealFile.symlink?(f)
|
64
|
-
FileUtils.ln_s()
|
65
67
|
end
|
66
68
|
end
|
67
69
|
end
|
@@ -91,15 +93,15 @@ module FakeFS
|
|
91
93
|
|
92
94
|
def normalize_path(path)
|
93
95
|
if Pathname.new(path).absolute?
|
94
|
-
|
96
|
+
RealFile.expand_path(path)
|
95
97
|
else
|
96
98
|
parts = dir_levels + [path]
|
97
|
-
|
99
|
+
RealFile.expand_path(parts.inject {|base, part| Pathname(base) + part }.to_s)
|
98
100
|
end
|
99
101
|
end
|
100
102
|
|
101
103
|
def current_dir
|
102
|
-
find(
|
104
|
+
find('.')
|
103
105
|
end
|
104
106
|
|
105
107
|
private
|
@@ -107,7 +109,7 @@ module FakeFS
|
|
107
109
|
def drop_root(path_parts)
|
108
110
|
# we need to remove parts from root dir at least for windows and jruby
|
109
111
|
return path_parts if path_parts.nil? || path_parts.empty?
|
110
|
-
root =
|
112
|
+
root = RealFile.expand_path('/').split(File::SEPARATOR).first
|
111
113
|
path_parts.shift if path_parts.first == root
|
112
114
|
path_parts
|
113
115
|
end
|
@@ -145,5 +147,9 @@ module FakeFS
|
|
145
147
|
children = dir.entries.select{|f| f.is_a? FakeDir}
|
146
148
|
([dir] + children + children.map{|c| directories_under(c)}).flatten.uniq
|
147
149
|
end
|
150
|
+
|
151
|
+
def assert_dir(dir)
|
152
|
+
raise Errno::EEXIST, dir.name unless dir.is_a?(FakeDir)
|
153
|
+
end
|
148
154
|
end
|
149
155
|
end
|
data/lib/fakefs/file_test.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
1
|
module FakeFS
|
2
|
-
|
3
|
-
|
2
|
+
module FileTest
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def exist?(file_name)
|
4
6
|
File.exist?(file_name)
|
5
7
|
end
|
6
8
|
|
7
|
-
def
|
9
|
+
def directory?(file_name)
|
8
10
|
File.directory?(file_name)
|
9
11
|
end
|
10
12
|
|
11
|
-
def
|
13
|
+
def file?(file_name)
|
12
14
|
File.file?(file_name)
|
13
15
|
end
|
16
|
+
|
17
|
+
def writable?(file_name)
|
18
|
+
File.writable?(file_name)
|
19
|
+
end
|
14
20
|
end
|
15
21
|
end
|
data/lib/fakefs/fileutils.rb
CHANGED
@@ -16,8 +16,8 @@ module FakeFS
|
|
16
16
|
list.each do |path|
|
17
17
|
parent = path.split('/')
|
18
18
|
parent.pop
|
19
|
-
raise Errno::ENOENT,
|
20
|
-
raise Errno::EEXIST,
|
19
|
+
raise Errno::ENOENT, path unless parent.join == "" || parent.join == "." || FileSystem.find(parent.join('/'))
|
20
|
+
raise Errno::EEXIST, path if FileSystem.find(path)
|
21
21
|
FileSystem.add(path, FakeDir.new)
|
22
22
|
end
|
23
23
|
end
|
@@ -27,7 +27,7 @@ module FakeFS
|
|
27
27
|
list.each do |l|
|
28
28
|
parent = l.split('/')
|
29
29
|
parent.pop
|
30
|
-
raise Errno::ENOENT,
|
30
|
+
raise Errno::ENOENT, l unless parent.join == "" || FileSystem.find(parent.join('/'))
|
31
31
|
raise Errno::ENOENT, l unless FileSystem.find(l)
|
32
32
|
raise Errno::ENOTEMPTY, l unless FileSystem.find(l).empty?
|
33
33
|
rm(l)
|
@@ -67,11 +67,17 @@ module FakeFS
|
|
67
67
|
|
68
68
|
alias_method :symlink, :ln_s
|
69
69
|
|
70
|
-
def cp(src, dest)
|
70
|
+
def cp(src, dest, options={})
|
71
71
|
if src.is_a?(Array) && !File.directory?(dest)
|
72
72
|
raise Errno::ENOTDIR, dest
|
73
73
|
end
|
74
74
|
|
75
|
+
# handle `verbose' flag
|
76
|
+
RealFileUtils.cp src, dest, options.merge(:noop => true)
|
77
|
+
|
78
|
+
# handle `noop' flag
|
79
|
+
return if options[:noop]
|
80
|
+
|
75
81
|
Array(src).each do |src|
|
76
82
|
dst_file = FileSystem.find(dest)
|
77
83
|
src_file = FileSystem.find(src)
|
@@ -91,6 +97,8 @@ module FakeFS
|
|
91
97
|
FileSystem.add(dest, src_file.entry.clone)
|
92
98
|
end
|
93
99
|
end
|
100
|
+
|
101
|
+
return nil
|
94
102
|
end
|
95
103
|
|
96
104
|
alias_method :copy, :cp
|
@@ -101,6 +109,12 @@ module FakeFS
|
|
101
109
|
end
|
102
110
|
|
103
111
|
def cp_r(src, dest, options={})
|
112
|
+
# handle `verbose' flag
|
113
|
+
RealFileUtils.cp_r src, dest, options.merge(:noop => true)
|
114
|
+
|
115
|
+
# handle `noop' flag
|
116
|
+
return if options[:noop]
|
117
|
+
|
104
118
|
Array(src).each do |src|
|
105
119
|
# This error sucks, but it conforms to the original Ruby
|
106
120
|
# method.
|
@@ -128,19 +142,35 @@ module FakeFS
|
|
128
142
|
FileSystem.add(dest, dir.entry.clone)
|
129
143
|
end
|
130
144
|
end
|
145
|
+
|
146
|
+
return nil
|
131
147
|
end
|
132
148
|
|
133
149
|
def mv(src, dest, options={})
|
150
|
+
# handle `verbose' flag
|
151
|
+
RealFileUtils.mv src, dest, options.merge(:noop => true)
|
152
|
+
|
153
|
+
# handle `noop' flag
|
154
|
+
return if options[:noop]
|
155
|
+
|
134
156
|
Array(src).each do |path|
|
135
157
|
if target = FileSystem.find(path)
|
136
158
|
dest_path = File.directory?(dest) ? File.join(dest, File.basename(path)) : dest
|
137
|
-
|
138
|
-
|
139
|
-
|
159
|
+
if File.directory?(dest_path)
|
160
|
+
raise Errno::EEXIST, dest_path unless options[:force]
|
161
|
+
elsif File.directory?(File.dirname(dest_path))
|
162
|
+
FileSystem.delete(dest_path)
|
163
|
+
FileSystem.add(dest_path, target.entry.clone)
|
164
|
+
FileSystem.delete(path)
|
165
|
+
else
|
166
|
+
raise Errno::ENOENT, dest_path unless options[:force]
|
167
|
+
end
|
140
168
|
else
|
141
169
|
raise Errno::ENOENT, path
|
142
170
|
end
|
143
171
|
end
|
172
|
+
|
173
|
+
return nil
|
144
174
|
end
|
145
175
|
|
146
176
|
alias_method :move, :mv
|
data/lib/fakefs/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'find'
|
2
|
+
require 'fakefs/spec_helpers'
|
3
|
+
|
4
|
+
RSpec.configure do |c|
|
5
|
+
c.mock_with(:rspec)
|
6
|
+
c.include(FakeFS::SpecHelpers, :fakefs => true)
|
7
|
+
end
|
8
|
+
|
9
|
+
if RUBY_VERSION >= '2.1'
|
10
|
+
describe 'Find.find', :fakefs => true do
|
11
|
+
it 'does not give an ArgumentError' do
|
12
|
+
FileUtils.mkdir_p('/tmp/foo')
|
13
|
+
found = Find.find('/tmp').to_a
|
14
|
+
expect(found).to eq(%w(/tmp /tmp/foo))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/test/fakefs_test.rb
CHANGED
@@ -68,6 +68,21 @@ class FakeFSTest < Test::Unit::TestCase
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
def test_raises_error_when_creating_a_new_dir_over_existing_file
|
72
|
+
File.open("file", "w") {|f| f << "This is a file, not a directory." }
|
73
|
+
|
74
|
+
assert_raise Errno::EEXIST do
|
75
|
+
FileUtils.mkdir_p("file/subdir")
|
76
|
+
end
|
77
|
+
|
78
|
+
FileUtils.mkdir("dir")
|
79
|
+
File.open("dir/subfile", "w") {|f| f << "This is a file inside a directory." }
|
80
|
+
|
81
|
+
assert_raise Errno::EEXIST do
|
82
|
+
FileUtils.mkdir_p("dir/subfile/subdir")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
71
86
|
def test_can_create_directories_with_mkpath
|
72
87
|
FileUtils.mkpath("/path/to/dir")
|
73
88
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
@@ -520,6 +535,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
520
535
|
assert_equal 9, File.size(path)
|
521
536
|
end
|
522
537
|
|
538
|
+
def test_can_get_correct_size_for_files_with_multibyte_characters
|
539
|
+
path = 'file.txt'
|
540
|
+
File.open(path, 'wb') do |f|
|
541
|
+
f << "Y\xC3\xA1da" # Yáda
|
542
|
+
end
|
543
|
+
assert_equal 5, File.size(path)
|
544
|
+
end
|
545
|
+
|
523
546
|
def test_can_check_if_file_has_size?
|
524
547
|
path = 'file.txt'
|
525
548
|
File.open(path, 'w') do |f|
|
@@ -564,20 +587,22 @@ class FakeFSTest < Test::Unit::TestCase
|
|
564
587
|
end
|
565
588
|
end
|
566
589
|
|
567
|
-
|
568
|
-
|
569
|
-
|
590
|
+
if RUBY_VERSION >= "1.9"
|
591
|
+
def test_can_set_mtime_on_new_file_touch_with_param
|
592
|
+
time = Time.new(2002, 10, 31, 2, 2, 2, "+02:00")
|
593
|
+
FileUtils.touch("foo.txt", :mtime => time)
|
570
594
|
|
571
|
-
|
572
|
-
|
595
|
+
assert_equal File.mtime("foo.txt"), time
|
596
|
+
end
|
573
597
|
|
574
|
-
|
575
|
-
|
598
|
+
def test_can_set_mtime_on_existing_file_touch_with_param
|
599
|
+
FileUtils.touch("foo.txt")
|
576
600
|
|
577
|
-
|
578
|
-
|
601
|
+
time = Time.new(2002, 10, 31, 2, 2, 2, "+02:00")
|
602
|
+
FileUtils.touch("foo.txt", :mtime => time)
|
579
603
|
|
580
|
-
|
604
|
+
assert_equal File.mtime("foo.txt"), time
|
605
|
+
end
|
581
606
|
end
|
582
607
|
|
583
608
|
def test_can_return_mtime_on_existing_file
|
@@ -981,11 +1006,32 @@ class FakeFSTest < Test::Unit::TestCase
|
|
981
1006
|
end
|
982
1007
|
end
|
983
1008
|
|
984
|
-
def
|
1009
|
+
def test_file_utils_cp_allows_verbose_option
|
1010
|
+
File.open('foo', 'w') {|f| f << 'TEST' }
|
1011
|
+
assert_equal "cp foo bar\n", capture_stderr { FileUtils.cp 'foo', 'bar', :verbose => true }
|
1012
|
+
end
|
1013
|
+
|
1014
|
+
def test_file_utils_cp_allows_noop_option
|
1015
|
+
File.open('foo', 'w') {|f| f << 'TEST' }
|
1016
|
+
FileUtils.cp 'foo', 'bar', :noop => true
|
1017
|
+
assert !File.exist?('bar'), 'does not actually copy'
|
1018
|
+
end
|
1019
|
+
|
1020
|
+
def test_file_utils_cp_raises_on_invalid_option
|
1021
|
+
assert_raises ArgumentError do
|
1022
|
+
FileUtils.cp 'foo', 'bar', :whatisthis => "I don't know"
|
1023
|
+
end
|
1024
|
+
end
|
1025
|
+
|
1026
|
+
def test_file_utils_cp_r_allows_verbose_option
|
985
1027
|
FileUtils.touch "/foo"
|
1028
|
+
assert_equal "cp -r /foo /bar\n", capture_stderr { FileUtils.cp_r '/foo', '/bar', :verbose => true }
|
1029
|
+
end
|
986
1030
|
|
987
|
-
|
988
|
-
|
1031
|
+
def test_file_utils_cp_r_allows_noop_option
|
1032
|
+
FileUtils.touch "/foo"
|
1033
|
+
FileUtils.cp_r '/foo', '/bar', :noop => true
|
1034
|
+
assert !File.exist?('/bar'), 'does not actually copy'
|
989
1035
|
end
|
990
1036
|
|
991
1037
|
def test_dir_glob_handles_root
|
@@ -1085,8 +1131,17 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1085
1131
|
assert_equal 1, fp.pos
|
1086
1132
|
end
|
1087
1133
|
|
1134
|
+
OMITTED_FILE_METHODS = [
|
1135
|
+
# omit methods from io/console
|
1136
|
+
:raw, :raw!, :cooked, :cooked!,
|
1137
|
+
:echo?, :echo=, :noecho,
|
1138
|
+
:winsize, :winsize=,
|
1139
|
+
:getch,
|
1140
|
+
:iflush, :ioflush, :oflush
|
1141
|
+
]
|
1142
|
+
|
1088
1143
|
def test_every_method_in_file_is_in_fake_fs_file
|
1089
|
-
RealFile.instance_methods.each do |method_name|
|
1144
|
+
(RealFile.instance_methods - OMITTED_FILE_METHODS).each do |method_name|
|
1090
1145
|
assert File.instance_methods.include?(method_name), "#{method_name} method is not available in File :("
|
1091
1146
|
end
|
1092
1147
|
end
|
@@ -1156,6 +1211,19 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1156
1211
|
assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
|
1157
1212
|
end
|
1158
1213
|
|
1214
|
+
def test_chdir_should_be_nestable_with_absolute_paths
|
1215
|
+
FileUtils.mkdir_p '/path/me'
|
1216
|
+
Dir.chdir '/path' do
|
1217
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
1218
|
+
Dir.chdir '/path/me' do
|
1219
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
1220
|
+
end
|
1221
|
+
end
|
1222
|
+
|
1223
|
+
assert_equal ['/path/foo','/path/me'], Dir.glob('/path/*').sort
|
1224
|
+
assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
|
1225
|
+
end
|
1226
|
+
|
1159
1227
|
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
|
1160
1228
|
assert_raise(Errno::ENOENT) do
|
1161
1229
|
Dir.chdir('/nope') do
|
@@ -1185,7 +1253,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1185
1253
|
rescue Errno::ENOENT
|
1186
1254
|
end
|
1187
1255
|
|
1188
|
-
assert_equal ['/path'], FileSystem.dir_levels
|
1256
|
+
assert_equal ['/', '/path'], FileSystem.dir_levels
|
1189
1257
|
end
|
1190
1258
|
|
1191
1259
|
assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
|
@@ -1221,6 +1289,20 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1221
1289
|
assert_equal '/path/subdir', Dir.getwd
|
1222
1290
|
end
|
1223
1291
|
|
1292
|
+
def test_current_dir_reflected_by_expand_path
|
1293
|
+
FileUtils.mkdir_p '/path'
|
1294
|
+
Dir.chdir '/path'
|
1295
|
+
|
1296
|
+
assert_equal '/path', File.expand_path('.')
|
1297
|
+
assert_equal '/path/foo', File.expand_path('foo')
|
1298
|
+
|
1299
|
+
FileUtils.mkdir_p 'subdir'
|
1300
|
+
Dir.chdir 'subdir'
|
1301
|
+
|
1302
|
+
assert_equal '/path/subdir', File.expand_path('.')
|
1303
|
+
assert_equal '/path/subdir/foo', File.expand_path('foo')
|
1304
|
+
end
|
1305
|
+
|
1224
1306
|
def test_file_open_defaults_to_read
|
1225
1307
|
File.open('foo','w') { |f| f.write 'bar' }
|
1226
1308
|
assert_equal 'bar', File.open('foo') { |f| f.read }
|
@@ -1277,6 +1359,46 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1277
1359
|
assert_equal('binky', File.open('destdir/baz') {|f| f.read })
|
1278
1360
|
end
|
1279
1361
|
|
1362
|
+
def test_mv_accepts_verbose_option
|
1363
|
+
FileUtils.touch 'foo'
|
1364
|
+
assert_equal "mv foo bar\n", capture_stderr { FileUtils.mv 'foo', 'bar', :verbose => true }
|
1365
|
+
end
|
1366
|
+
|
1367
|
+
def test_mv_accepts_noop_option
|
1368
|
+
FileUtils.touch 'foo'
|
1369
|
+
FileUtils.mv 'foo', 'bar', :noop => true
|
1370
|
+
assert File.exist?('foo'), 'does not remove src'
|
1371
|
+
assert !File.exist?('bar'), 'does not create target'
|
1372
|
+
end
|
1373
|
+
|
1374
|
+
def test_mv_raises_when_moving_file_onto_directory
|
1375
|
+
FileUtils.mkdir_p 'dir/stuff'
|
1376
|
+
FileUtils.touch 'stuff'
|
1377
|
+
assert_raises Errno::EEXIST do
|
1378
|
+
FileUtils.mv 'stuff', 'dir'
|
1379
|
+
end
|
1380
|
+
end
|
1381
|
+
|
1382
|
+
def test_mv_raises_when_moving_to_non_existent_directory
|
1383
|
+
FileUtils.touch 'stuff'
|
1384
|
+
assert_raises Errno::ENOENT do
|
1385
|
+
FileUtils.mv 'stuff', '/this/path/is/not/here'
|
1386
|
+
end
|
1387
|
+
end
|
1388
|
+
|
1389
|
+
def test_mv_ignores_failures_when_using_force
|
1390
|
+
FileUtils.mkdir_p 'dir/stuff'
|
1391
|
+
FileUtils.touch %w[stuff other]
|
1392
|
+
FileUtils.mv %w[stuff other], 'dir', :force => true
|
1393
|
+
assert File.exist?('stuff'), 'failed move remains where it was'
|
1394
|
+
assert File.exist?('dir/other'), 'successful one is moved'
|
1395
|
+
assert !File.exist?('other'), 'successful one is moved'
|
1396
|
+
|
1397
|
+
FileUtils.mv 'stuff', '/this/path/is/not/here', :force => true
|
1398
|
+
assert File.exist?('stuff'), 'failed move remains where it was'
|
1399
|
+
assert !File.exist?('/this/path/is/not/here'), 'nothing is created for a failed move'
|
1400
|
+
end
|
1401
|
+
|
1280
1402
|
def test_cp_actually_works
|
1281
1403
|
File.open('foo', 'w') {|f| f.write 'bar' }
|
1282
1404
|
FileUtils.cp('foo', 'baz')
|
@@ -1477,6 +1599,48 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1477
1599
|
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
|
1478
1600
|
end
|
1479
1601
|
|
1602
|
+
def test_clone_with_file_symlinks
|
1603
|
+
original = here('subdir/test-file')
|
1604
|
+
symlink = here('subdir/test-file.txt')
|
1605
|
+
|
1606
|
+
act_on_real_fs do
|
1607
|
+
RealDir.mkdir(RealFile.dirname(original))
|
1608
|
+
RealFile.open(original, 'w') {|f| f << 'stuff' }
|
1609
|
+
RealFileUtils.ln_s original, symlink
|
1610
|
+
assert RealFile.symlink?(symlink), 'real symlink is in place'
|
1611
|
+
end
|
1612
|
+
|
1613
|
+
assert !File.exists?(original), 'file does not already exist'
|
1614
|
+
|
1615
|
+
FileSystem.clone(File.dirname(original))
|
1616
|
+
assert File.symlink?(symlink), 'symlinks are cloned as symlinks'
|
1617
|
+
assert_equal 'stuff', File.read(symlink)
|
1618
|
+
ensure
|
1619
|
+
act_on_real_fs { RealFileUtils.rm_rf File.dirname(original) }
|
1620
|
+
end
|
1621
|
+
|
1622
|
+
def test_clone_with_dir_symlinks
|
1623
|
+
original = here('subdir/dir')
|
1624
|
+
symlink = here('subdir/dir.link')
|
1625
|
+
original_file = File.join(original, 'test-file')
|
1626
|
+
symlink_file = File.join(symlink, 'test-file')
|
1627
|
+
|
1628
|
+
act_on_real_fs do
|
1629
|
+
RealFileUtils.mkdir_p(original)
|
1630
|
+
RealFile.open(original_file, 'w') {|f| f << 'stuff' }
|
1631
|
+
RealFileUtils.ln_s original, symlink
|
1632
|
+
assert RealFile.symlink?(symlink), 'real symlink is in place'
|
1633
|
+
end
|
1634
|
+
|
1635
|
+
assert !File.exists?(original_file), 'file does not already exist'
|
1636
|
+
|
1637
|
+
FileSystem.clone(File.dirname(original))
|
1638
|
+
assert File.symlink?(symlink), 'symlinks are cloned as symlinks'
|
1639
|
+
assert_equal 'stuff', File.read(symlink_file)
|
1640
|
+
ensure
|
1641
|
+
act_on_real_fs { RealFileUtils.rm_rf File.dirname(original) }
|
1642
|
+
end
|
1643
|
+
|
1480
1644
|
def test_putting_a_dot_at_end_copies_the_contents
|
1481
1645
|
FileUtils.mkdir_p 'subdir'
|
1482
1646
|
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
@@ -1774,6 +1938,26 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1774
1938
|
test.each { |t| assert yielded.include?(t) }
|
1775
1939
|
end
|
1776
1940
|
|
1941
|
+
def test_directory_foreach_relative_paths
|
1942
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
1943
|
+
|
1944
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
1945
|
+
|
1946
|
+
test.each do |f|
|
1947
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
1948
|
+
end
|
1949
|
+
|
1950
|
+
yielded = []
|
1951
|
+
Dir.chdir '/this/path/should/be' do
|
1952
|
+
Dir.foreach('here') do |dir|
|
1953
|
+
yielded << dir
|
1954
|
+
end
|
1955
|
+
end
|
1956
|
+
|
1957
|
+
assert yielded.size == test.size, 'wrong number of files yielded'
|
1958
|
+
test.each { |t| assert yielded.include?(t), "#{t} was not included in #{yielded.inspect}" }
|
1959
|
+
end
|
1960
|
+
|
1777
1961
|
def test_directory_mkdir
|
1778
1962
|
Dir.mkdir('/path')
|
1779
1963
|
assert File.exists?('/path')
|
@@ -2129,6 +2313,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
2129
2313
|
assert !FileTest.file?('/path/to/somedir')
|
2130
2314
|
end
|
2131
2315
|
|
2316
|
+
def test_filetest_writable_returns_correct_values
|
2317
|
+
assert !FileTest.writable?('not-here.txt'), 'missing files are not writable'
|
2318
|
+
|
2319
|
+
FileUtils.touch 'here.txt'
|
2320
|
+
assert FileTest.writable?('here.txt'), 'existing files are writable'
|
2321
|
+
|
2322
|
+
FileUtils.mkdir 'dir'
|
2323
|
+
assert FileTest.writable?('dir'), 'directories are writable'
|
2324
|
+
end
|
2325
|
+
|
2132
2326
|
def test_pathname_exists_returns_correct_value
|
2133
2327
|
FileUtils.touch "foo"
|
2134
2328
|
assert Pathname.new("foo").exist?
|
@@ -2256,10 +2450,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
2256
2450
|
|
2257
2451
|
def test_file_umask
|
2258
2452
|
assert_equal File.umask, RealFile.umask
|
2259
|
-
File.umask(
|
2453
|
+
File.umask(0740)
|
2260
2454
|
|
2261
2455
|
assert_equal File.umask, RealFile.umask
|
2262
|
-
assert_equal File.umask,
|
2456
|
+
assert_equal File.umask, 0740
|
2263
2457
|
end
|
2264
2458
|
|
2265
2459
|
def test_file_stat_comparable
|
@@ -2388,5 +2582,15 @@ class FakeFSTest < Test::Unit::TestCase
|
|
2388
2582
|
File.write('foo', 'bar', 3)
|
2389
2583
|
assert_equal File.read('foo'), 'foobar'
|
2390
2584
|
end
|
2585
|
+
|
2586
|
+
def test_can_read_binary_data_in_binary_mode
|
2587
|
+
File.open('foo', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
|
2588
|
+
assert_equal "\x00\x00\x00\x03\x00\x03\x00\xA3\x00\x00\x00y\x00\x00\x00\x00\x00", File.open("foo", "rb").read
|
2589
|
+
end
|
2590
|
+
|
2591
|
+
def test_can_read_binary_data_in_non_binary_mode
|
2592
|
+
File.open('foo_non_bin', 'wb') { |f| f << "\u0000\u0000\u0000\u0003\u0000\u0003\u0000\xA3\u0000\u0000\u0000y\u0000\u0000\u0000\u0000\u0000" }
|
2593
|
+
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
|
2594
|
+
end
|
2391
2595
|
end
|
2392
2596
|
end
|