fakefs 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -128,10 +128,6 @@ Installation
128
128
 
129
129
  $ gem install fakefs
130
130
 
131
- ### [Rip](http://hellorip.com)
132
-
133
- $ rip install git://github.com/defunkt/fakefs.git
134
-
135
131
 
136
132
  Contributing
137
133
  ------------
@@ -151,7 +147,6 @@ Meta
151
147
  * Home: <http://github.com/defunkt/fakefs>
152
148
  * Docs: <http://defunkt.github.com/fakefs>
153
149
  * Bugs: <http://github.com/defunkt/fakefs/issues>
154
- * List: <http://groups.google.com/group/fakefs>
155
150
  * Test: <http://travisci.org/#!/defunkt/fakefs>
156
151
  * Gems: <http://rubygems.org/gems/fakefs>
157
152
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "fakefs"
8
- s.version = "0.4.2"
8
+ s.version = "0.4.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"]
12
- s.date = "2012-11-24"
12
+ s.date = "2013-09-07"
13
13
  s.description = "A fake filesystem. Use it in your tests."
14
14
  s.email = "chris@ozmm.org"
15
15
  s.extra_rdoc_files = [
@@ -90,7 +90,7 @@ module FakeFS
90
90
  Dir.open(dirname) { |file| yield file }
91
91
  end
92
92
 
93
- def self.glob(pattern, &block)
93
+ def self.glob(pattern, flags = 0, &block)
94
94
  matches_for_pattern = lambda do |matcher|
95
95
  [FileSystem.find(matcher) || []].flatten.map{|e|
96
96
  Dir.pwd.match(%r[\A/?\z]) || !e.to_s.match(%r[\A#{Dir.pwd}/?]) ? e.to_s : e.to_s.match(%r[\A#{Dir.pwd}/?]).post_match}.sort
@@ -180,6 +180,7 @@ module FakeFS
180
180
  alias_method :getwd, :pwd
181
181
  alias_method :rmdir, :delete
182
182
  alias_method :unlink, :delete
183
+ alias_method :exist?, :exists?
183
184
  end
184
185
  end
185
186
  end
@@ -94,12 +94,16 @@ module FakeFS
94
94
 
95
95
  def self.size?(path)
96
96
  if exists?(path) && !size(path).zero?
97
- true
97
+ size(path)
98
98
  else
99
99
  nil
100
100
  end
101
101
  end
102
102
 
103
+ def self.zero?(path)
104
+ exists?(path) && size(path) == 0
105
+ end
106
+
103
107
  def self.const_missing(name)
104
108
  RealFile.const_get(name)
105
109
  end
@@ -149,18 +153,24 @@ module FakeFS
149
153
 
150
154
  def self.read(path, *args)
151
155
  file = new(path)
156
+
157
+ raise Errno::ENOENT if !file.exists?
158
+ raise Errno::EISDIR, "Is a directory - #{path}" if directory?(path)
159
+
160
+ FileSystem.find(path).atime = Time.now
161
+ file.read
162
+ end
163
+
164
+ def self.readlines(path)
165
+ file = new(path)
152
166
  if file.exists?
153
167
  FileSystem.find(path).atime = Time.now
154
- file.read
168
+ file.readlines
155
169
  else
156
170
  raise Errno::ENOENT
157
171
  end
158
172
  end
159
173
 
160
- def self.readlines(path)
161
- read(path).split("\n")
162
- end
163
-
164
174
  def self.rename(source, dest)
165
175
  if directory?(source) && file?(dest)
166
176
  raise Errno::ENOTDIR, "Not a directory - #{source} or #{dest}"
@@ -236,6 +246,14 @@ module FakeFS
236
246
  FileSystem.find(filename).mode = 0100000 + mode_int
237
247
  end
238
248
 
249
+ # Not exactly right, returns true if the file is chmod +x for owner. In the
250
+ # context of when you would use fakefs, this is usually what you want.
251
+ def self.executable?(filename)
252
+ file = FileSystem.find(filename)
253
+ return false unless file
254
+ (file.mode - 0100000) & 0100 != 0
255
+ end
256
+
239
257
  def self.chown(owner_int, group_int, filename)
240
258
  file = FileSystem.find(filename)
241
259
  if owner_int && owner_int != -1
@@ -248,8 +266,18 @@ module FakeFS
248
266
  end
249
267
  end
250
268
 
251
- def self.umask
252
- RealFile.umask
269
+ def self.umask(*args)
270
+ RealFile.umask(*args)
271
+ end
272
+
273
+ def self.binread(file, length = nil, offset = 0)
274
+ contents = File.read(file)
275
+
276
+ if length
277
+ contents.slice(offset, length)
278
+ else
279
+ contents
280
+ end
253
281
  end
254
282
 
255
283
  class Stat
@@ -279,6 +307,12 @@ module FakeFS
279
307
  File.directory?(@file)
280
308
  end
281
309
 
310
+ def ftype
311
+ return 'link' if symlink?
312
+ return 'directory' if directory?
313
+ return 'file'
314
+ end
315
+
282
316
  # assumes, like above, that all files are readable and writable
283
317
  def readable?
284
318
  true
@@ -418,6 +452,10 @@ module FakeFS
418
452
  end
419
453
 
420
454
  if RUBY_VERSION >= "1.9"
455
+ def self.realpath(*args)
456
+ RealFile.realpath(*args)
457
+ end
458
+
421
459
  def binmode?
422
460
  raise NotImplementedError
423
461
  end
@@ -460,6 +498,21 @@ module FakeFS
460
498
  if RUBY_VERSION >= "1.9.3"
461
499
  def advise(advice, offset=0, len=0)
462
500
  end
501
+
502
+ def self.write(filename, contents, offset = nil)
503
+ if offset
504
+ open(filename, 'a') do |f|
505
+ f.seek(offset)
506
+ f.write(contents)
507
+ end
508
+ else
509
+ open(filename, 'w') do |f|
510
+ f << contents
511
+ end
512
+ end
513
+
514
+ contents.length
515
+ end
463
516
  end
464
517
 
465
518
  private
@@ -2,18 +2,24 @@ module FakeFS
2
2
  module FileUtils
3
3
  extend self
4
4
 
5
- def mkdir_p(path, options = {})
6
- FileSystem.add(path, FakeDir.new)
5
+ def mkdir_p(list, options = {})
6
+ list = [ list ] unless list.is_a?(Array)
7
+ list.each do |path|
8
+ FileSystem.add(path, FakeDir.new)
9
+ end
7
10
  end
8
11
  alias_method :mkpath, :mkdir_p
9
12
  alias_method :makedirs, :mkdir_p
10
13
 
11
- def mkdir(path)
12
- parent = path.split('/')
13
- parent.pop
14
- raise Errno::ENOENT, "No such file or directory - #{path}" unless parent.join == "" || parent.join == "." || FileSystem.find(parent.join('/'))
15
- raise Errno::EEXIST, "File exists - #{path}" if FileSystem.find(path)
16
- FileSystem.add(path, FakeDir.new)
14
+ def mkdir(list, ignored_options={})
15
+ list = [ list ] unless list.is_a?(Array)
16
+ list.each do |path|
17
+ parent = path.split('/')
18
+ parent.pop
19
+ raise Errno::ENOENT, "No such file or directory - #{path}" unless parent.join == "" || parent.join == "." || FileSystem.find(parent.join('/'))
20
+ raise Errno::EEXIST, "File exists - #{path}" if FileSystem.find(path)
21
+ FileSystem.add(path, FakeDir.new)
22
+ end
17
23
  end
18
24
 
19
25
  def rmdir(list, options = {})
@@ -37,6 +43,10 @@ module FakeFS
37
43
  alias_method :rm_rf, :rm
38
44
  alias_method :rm_r, :rm
39
45
  alias_method :rm_f, :rm
46
+ alias_method :remove, :rm
47
+ alias_method :rmtree, :rm_rf
48
+ alias_method :safe_unlink, :rm_f
49
+ alias_method :remove_entry_secure, :rm_rf
40
50
 
41
51
  def ln_s(target, path, options = {})
42
52
  options = { :force => false }.merge(options)
@@ -55,6 +65,8 @@ module FakeFS
55
65
  ln_s(target, path, { :force => true })
56
66
  end
57
67
 
68
+ alias_method :symlink, :ln_s
69
+
58
70
  def cp(src, dest)
59
71
  if src.is_a?(Array) && !File.directory?(dest)
60
72
  raise Errno::ENOTDIR, dest
@@ -73,7 +85,7 @@ module FakeFS
73
85
  end
74
86
 
75
87
  if dst_file && File.directory?(dst_file)
76
- FileSystem.add(File.join(dest, src), src_file.entry.clone(dst_file))
88
+ FileSystem.add(File.join(dest, File.basename(src)), src_file.entry.clone(dst_file))
77
89
  else
78
90
  FileSystem.delete(dest)
79
91
  FileSystem.add(dest, src_file.entry.clone)
@@ -81,7 +93,14 @@ module FakeFS
81
93
  end
82
94
  end
83
95
 
84
- def cp_r(src, dest)
96
+ alias_method :copy, :cp
97
+
98
+ def copy_file(src, dest, preserve = false, dereference = true)
99
+ # Not a perfect match, but similar to what regular FileUtils does.
100
+ cp(src, dest)
101
+ end
102
+
103
+ def cp_r(src, dest, options={})
85
104
  Array(src).each do |src|
86
105
  # This error sucks, but it conforms to the original Ruby
87
106
  # method.
@@ -124,6 +143,8 @@ module FakeFS
124
143
  end
125
144
  end
126
145
 
146
+ alias_method :move, :mv
147
+
127
148
  def chown(user, group, list, options={})
128
149
  list = Array(list)
129
150
  list.each do |f|
@@ -184,18 +205,30 @@ module FakeFS
184
205
  Array(list).each do |f|
185
206
  if fs = FileSystem.find(f)
186
207
  now = Time.now
187
- fs.mtime = now
208
+ fs.mtime = options[:mtime] || now
188
209
  fs.atime = now
189
210
  else
190
- f = File.open(f, 'w')
191
- f.close
211
+ file = File.open(f, 'w')
212
+ file.close
213
+
214
+ if mtime = options[:mtime]
215
+ fs = FileSystem.find(f)
216
+ fs.mtime = mtime
217
+ end
192
218
  end
193
219
  end
194
220
  end
195
221
 
196
- def cd(dir)
197
- FileSystem.chdir(dir)
222
+ def cd(dir, &block)
223
+ FileSystem.chdir(dir, &block)
198
224
  end
199
225
  alias_method :chdir, :cd
226
+
227
+ def compare_file(file1, file2)
228
+ # we do a strict comparison of both files content
229
+ File.readlines(file1) == File.readlines(file2)
230
+ end
231
+ alias_method :cmp, :compare_file
232
+ alias_method :identical?, :compare_file
200
233
  end
201
234
  end
@@ -9,6 +9,10 @@
9
9
  # ...
10
10
  # end
11
11
  #
12
+ # By default, including FakeFS::SpecHelpers will run for each example inside a describe block.
13
+ # If you want to turn on FakeFS one time only for all your examples, you will need to
14
+ # include FakeFS::SpecHelpers::All.
15
+ #
12
16
  # Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's
13
17
  # configuration block in your spec helper:
14
18
  #
@@ -23,23 +27,37 @@
23
27
  require 'fakefs/safe'
24
28
 
25
29
  module FakeFS
30
+ def use_fakefs(describe_block, opts)
31
+ describe_block.before opts[:with] do
32
+ FakeFS.activate!
33
+ end
34
+
35
+ describe_block.after opts[:with] do
36
+ FakeFS.deactivate!
37
+ FakeFS::FileSystem.clear if opts[:with] == :each
38
+ end
39
+ end
40
+
26
41
  module SpecHelpers
42
+ include ::FakeFS
43
+
27
44
  def self.extended(example_group)
28
- example_group.use_fakefs(example_group)
45
+ example_group.use_fakefs(example_group, :with => :each)
29
46
  end
30
47
 
31
48
  def self.included(example_group)
32
49
  example_group.extend self
33
50
  end
34
51
 
35
- def use_fakefs(describe_block)
36
- describe_block.before :each do
37
- FakeFS.activate!
52
+ module All
53
+ include ::FakeFS
54
+
55
+ def self.extended(example_group)
56
+ example_group.use_fakefs(example_group, :with => :all)
38
57
  end
39
58
 
40
- describe_block.after :each do
41
- FakeFS.deactivate!
42
- FakeFS::FileSystem.clear
59
+ def self.included(example_group)
60
+ example_group.extend self
43
61
  end
44
62
  end
45
63
  end
@@ -1,6 +1,6 @@
1
1
  module FakeFS
2
2
  module Version
3
- VERSION = "0.4.2"
3
+ VERSION = "0.4.3"
4
4
 
5
5
  def self.to_s
6
6
  VERSION
@@ -53,5 +53,49 @@ module FakeFS
53
53
  end
54
54
  end
55
55
  end
56
+
57
+ describe SpecHelpers::All do
58
+ describe "when extending" do
59
+ context "before :all" do
60
+ it "should call it" do
61
+ @rspec_example_group.should_receive(:before).with(:all)
62
+ @rspec_example_group.extend FakeFS::SpecHelpers::All
63
+ end
64
+
65
+ it "should call FakeFS.activate!" do
66
+ FakeFS.should_receive(:activate!)
67
+ @rspec_example_group.extend FakeFS::SpecHelpers::All
68
+ end
69
+ end
70
+
71
+ context "after :all" do
72
+ it "should call it" do
73
+ @rspec_example_group.should_receive(:after).with(:all)
74
+ @rspec_example_group.extend FakeFS::SpecHelpers::All
75
+ end
76
+
77
+ it "should call FakeFS.deactivate!" do
78
+ FakeFS.should_receive(:deactivate!)
79
+ @rspec_example_group.extend FakeFS::SpecHelpers::All
80
+ end
81
+
82
+ it "should not call FakeFS::FileSystem.clear" do
83
+ FakeFS::FileSystem.should_not_receive(:clear)
84
+ @rspec_example_group.extend FakeFS::SpecHelpers::All
85
+ end
86
+ end
87
+ end
88
+
89
+ describe "when including" do
90
+ context "before :all" do
91
+ it "should call it" do
92
+ @rspec_example_group.should_receive(:before)
93
+ @rspec_example_group.class_eval do
94
+ include FakeFS::SpecHelpers::All
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
56
100
  end
57
101
  end
@@ -28,6 +28,22 @@ class FakeFSTest < Test::Unit::TestCase
28
28
  assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
29
29
  end
30
30
 
31
+ def test_can_cd_to_directory_with_block
32
+ FileUtils.mkdir_p("/path/to/dir")
33
+ new_path = nil
34
+ FileUtils.cd("/path/to") do
35
+ new_path = Dir.getwd
36
+ end
37
+
38
+ assert_equal new_path, "/path/to"
39
+ end
40
+
41
+ def test_can_create_a_list_of_directories_with_file_utils_mkdir_p
42
+ FileUtils.mkdir_p(["/path/to/dir1", "/path/to/dir2"])
43
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir1']
44
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir2']
45
+ end
46
+
31
47
  def test_can_create_directories_with_options
32
48
  FileUtils.mkdir_p("/path/to/dir", :mode => 0755)
33
49
  assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
@@ -39,6 +55,13 @@ class FakeFSTest < Test::Unit::TestCase
39
55
  assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir']
40
56
  end
41
57
 
58
+ def test_can_create_a_list_of_directories_with_file_utils_mkdir
59
+ FileUtils.mkdir_p("/path/to/dir")
60
+ FileUtils.mkdir(["/path/to/dir/subdir1", "/path/to/dir/subdir2"])
61
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir1']
62
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir2']
63
+ end
64
+
42
65
  def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path
43
66
  assert_raises Errno::ENOENT do
44
67
  FileUtils.mkdir("/this/path/does/not/exists/newdir")
@@ -91,6 +114,23 @@ class FakeFSTest < Test::Unit::TestCase
91
114
  assert File.exists?("bar") == false
92
115
  end
93
116
 
117
+ def test_aliases_exist
118
+ assert File.respond_to?(:unlink)
119
+ assert FileUtils.respond_to?(:rm_f)
120
+ assert FileUtils.respond_to?(:rm_r)
121
+ assert FileUtils.respond_to?(:rm)
122
+ assert FileUtils.respond_to?(:rm_rf)
123
+ assert FileUtils.respond_to?(:symlink)
124
+ assert FileUtils.respond_to?(:move)
125
+ assert FileUtils.respond_to?(:copy)
126
+ assert FileUtils.respond_to?(:remove)
127
+ assert FileUtils.respond_to?(:rmtree)
128
+ assert FileUtils.respond_to?(:safe_unlink)
129
+ assert FileUtils.respond_to?(:remove_entry_secure)
130
+ assert FileUtils.respond_to?(:cmp)
131
+ assert FileUtils.respond_to?(:identical?)
132
+ end
133
+
94
134
  def test_knows_directories_exist
95
135
  FileUtils.mkdir_p(path = "/path/to/dir")
96
136
  assert File.exists?(path)
@@ -130,6 +170,11 @@ class FakeFSTest < Test::Unit::TestCase
130
170
  assert File.exists?(path)
131
171
  end
132
172
 
173
+ def test_file_utils_mkdir_takes_options
174
+ FileUtils.mkdir("/foo", :some => :option)
175
+ assert File.exists?("/foo")
176
+ end
177
+
133
178
  def test_symlink_with_missing_refferent_does_not_exist
134
179
  File.symlink('/foo', '/bar')
135
180
  assert !File.exists?('/bar')
@@ -480,7 +525,7 @@ class FakeFSTest < Test::Unit::TestCase
480
525
  File.open(path, 'w') do |f|
481
526
  f << 'Yada Yada'
482
527
  end
483
- assert File.size?(path)
528
+ assert_equal 9, File.size?(path)
484
529
  assert_nil File.size?("other.txt")
485
530
  end
486
531
 
@@ -492,12 +537,49 @@ class FakeFSTest < Test::Unit::TestCase
492
537
  assert_nil File.size?("file.txt")
493
538
  end
494
539
 
540
+ def test_zero_on_empty_file
541
+ path = 'file.txt'
542
+ File.open(path, 'w') do |f|
543
+ f << ''
544
+ end
545
+ assert_equal true, File.zero?(path)
546
+ end
547
+
548
+ def test_zero_on_non_empty_file
549
+ path = 'file.txt'
550
+ File.open(path, 'w') do |f|
551
+ f << 'Not empty'
552
+ end
553
+ assert_equal false, File.zero?(path)
554
+ end
555
+
556
+ def test_zero_on_non_existent_file
557
+ path = 'file_does_not_exist.txt'
558
+ assert_equal false, File.zero?(path)
559
+ end
560
+
495
561
  def test_raises_error_on_mtime_if_file_does_not_exist
496
562
  assert_raise Errno::ENOENT do
497
563
  File.mtime('/path/to/file.txt')
498
564
  end
499
565
  end
500
566
 
567
+ def test_can_set_mtime_on_new_file_touch_with_param
568
+ time = Time.new(2002, 10, 31, 2, 2, 2, "+02:00")
569
+ FileUtils.touch("foo.txt", :mtime => time)
570
+
571
+ assert_equal File.mtime("foo.txt"), time
572
+ end
573
+
574
+ def test_can_set_mtime_on_existing_file_touch_with_param
575
+ FileUtils.touch("foo.txt")
576
+
577
+ time = Time.new(2002, 10, 31, 2, 2, 2, "+02:00")
578
+ FileUtils.touch("foo.txt", :mtime => time)
579
+
580
+ assert_equal File.mtime("foo.txt"), time
581
+ end
582
+
501
583
  def test_can_return_mtime_on_existing_file
502
584
  path = 'file.txt'
503
585
  File.open(path, 'w') do |f|
@@ -638,7 +720,25 @@ class FakeFSTest < Test::Unit::TestCase
638
720
  f.puts ["woot","toot"]
639
721
  end
640
722
 
641
- assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
723
+ assert_equal ["Yatta!\n", "Gatta!\n", "woot\n", "toot\n"], File.readlines(path)
724
+ end
725
+
726
+ def test_can_read_with_File_readlines_and_only_empty_lines
727
+ path = 'file.txt'
728
+ File.open(path, 'w') do |f|
729
+ f.write "\n"
730
+ end
731
+
732
+ assert_equal ["\n"], File.readlines(path)
733
+ end
734
+
735
+ def test_can_read_with_File_readlines_and_new_lines
736
+ path = 'file.txt'
737
+ File.open(path, 'w') do |f|
738
+ f.write "this\nis\na\ntest\n"
739
+ end
740
+
741
+ assert_equal ["this\n", "is\n", "a\n", "test\n"], File.readlines(path)
642
742
  end
643
743
 
644
744
  def test_File_close_disallows_further_access
@@ -691,6 +791,14 @@ class FakeFSTest < Test::Unit::TestCase
691
791
  end
692
792
  end
693
793
 
794
+ def test_file_read_errors_on_directory
795
+ FileUtils.mkdir_p("a_directory")
796
+
797
+ assert_raise Errno::EISDIR do
798
+ File.read("a_directory")
799
+ end
800
+ end
801
+
694
802
  def test_knows_files_are_files
695
803
  path = 'file.txt'
696
804
  File.open(path, 'w') do |f|
@@ -724,6 +832,10 @@ class FakeFSTest < Test::Unit::TestCase
724
832
  assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
725
833
  end
726
834
 
835
+ def test_executable_returns_false_for_non_existent_files
836
+ assert !File.executable?('/does/not/exist')
837
+ end
838
+
727
839
  def test_can_chown_files
728
840
  good = 'file.txt'
729
841
  bad = 'nofile.txt'
@@ -785,6 +897,7 @@ class FakeFSTest < Test::Unit::TestCase
785
897
 
786
898
  assert_equal [good], FileUtils.chmod(0600, good, :verbose => true)
787
899
  assert_equal File.stat(good).mode, 0100600
900
+ assert_equal File.executable?(good), false
788
901
  assert_raises(Errno::ENOENT) do
789
902
  FileUtils.chmod(0600, bad)
790
903
  end
@@ -800,6 +913,14 @@ class FakeFSTest < Test::Unit::TestCase
800
913
  assert_raises(Errno::ENOENT) do
801
914
  FileUtils.chmod(0644, bad)
802
915
  end
916
+
917
+ assert_equal [good], FileUtils.chmod(0744, [good])
918
+ assert_equal File.executable?(good), true
919
+
920
+ # This behaviour is unimplemented, the spec below is only to show that it
921
+ # is a deliberate YAGNI omission.
922
+ assert_equal [good], FileUtils.chmod(0477, [good])
923
+ assert_equal File.executable?(good), false
803
924
  end
804
925
 
805
926
  def test_can_chmod_R_files
@@ -860,6 +981,13 @@ class FakeFSTest < Test::Unit::TestCase
860
981
  end
861
982
  end
862
983
 
984
+ def test_file_utils_cp_r_takes_ignored_options
985
+ FileUtils.touch "/foo"
986
+
987
+ FileUtils.cp_r '/foo', '/bar', :verbose => true
988
+ assert_equal Dir.glob("/*").sort, ["/bar", "/foo"]
989
+ end
990
+
863
991
  def test_dir_glob_handles_root
864
992
  FileUtils.mkdir_p '/path'
865
993
 
@@ -867,6 +995,11 @@ class FakeFSTest < Test::Unit::TestCase
867
995
  assert_equal ['/'], Dir['/']
868
996
  end
869
997
 
998
+ def test_dir_glob_takes_optional_flags
999
+ FileUtils.touch "/foo"
1000
+ assert_equal Dir.glob("/*", 0), ["/foo"]
1001
+ end
1002
+
870
1003
  def test_dir_glob_handles_recursive_globs
871
1004
  FileUtils.mkdir_p "/one/two/three"
872
1005
  File.open('/one/two/three/four.rb', 'w')
@@ -894,6 +1027,16 @@ class FakeFSTest < Test::Unit::TestCase
894
1027
  assert_equal 2, yielded.size
895
1028
  end
896
1029
 
1030
+ def test_copy_with_subdirectory
1031
+ FileUtils.mkdir_p "/one/two/three/"
1032
+ FileUtils.mkdir_p "/onebis/two/three/"
1033
+ FileUtils.touch "/one/two/three/foo"
1034
+ Dir.glob("/one/two/three/*") do |hook|
1035
+ FileUtils.cp(hook, "/onebis/two/three/")
1036
+ end
1037
+ assert_equal ['/onebis/two/three/foo'], Dir['/onebis/two/three/*']
1038
+ end
1039
+
897
1040
  if RUBY_VERSION >= "1.9"
898
1041
  def test_dir_home
899
1042
  assert_equal RealDir.home, Dir.home
@@ -1189,6 +1332,12 @@ class FakeFSTest < Test::Unit::TestCase
1189
1332
  end
1190
1333
  end
1191
1334
 
1335
+ def test_copy_file_works
1336
+ File.open('foo', 'w') {|f| f.write 'bar' }
1337
+ FileUtils.copy_file('foo', 'baz', :ignore_param_1, :ignore_param_2)
1338
+ assert_equal 'bar', File.read('baz')
1339
+ end
1340
+
1192
1341
  def test_cp_r_doesnt_tangle_files_together
1193
1342
  File.open('foo', 'w') { |f| f.write 'bar' }
1194
1343
  FileUtils.cp_r('foo', 'baz')
@@ -1705,6 +1854,14 @@ class FakeFSTest < Test::Unit::TestCase
1705
1854
  test.each { |t| assert yielded.include?(t) }
1706
1855
  end
1707
1856
 
1857
+ def test_directory_exists
1858
+ assert Dir.exists?('/this/path/should/be/here') == false
1859
+ assert Dir.exist?('/this/path/should/be/here') == false
1860
+ FileUtils.mkdir_p('/this/path/should/be/here')
1861
+ assert Dir.exists?('/this/path/should/be/here') == true
1862
+ assert Dir.exist?('/this/path/should/be/here') == true
1863
+ end
1864
+
1708
1865
  def test_tmpdir
1709
1866
  assert Dir.tmpdir == "/tmp"
1710
1867
  end
@@ -1854,10 +2011,6 @@ class FakeFSTest < Test::Unit::TestCase
1854
2011
  end
1855
2012
  end
1856
2013
 
1857
- def test_unlink_is_alias_for_delete
1858
- assert_equal File.method(:unlink), File.method(:delete)
1859
- end
1860
-
1861
2014
  def test_unlink_removes_only_one_file_content
1862
2015
  File.open("/foo", "w") { |f| f << "some_content" }
1863
2016
  File.link("/foo", "/bar")
@@ -2103,6 +2256,10 @@ class FakeFSTest < Test::Unit::TestCase
2103
2256
 
2104
2257
  def test_file_umask
2105
2258
  assert_equal File.umask, RealFile.umask
2259
+ File.umask(1234)
2260
+
2261
+ assert_equal File.umask, RealFile.umask
2262
+ assert_equal File.umask, 1234
2106
2263
  end
2107
2264
 
2108
2265
  def test_file_stat_comparable
@@ -2129,10 +2286,41 @@ class FakeFSTest < Test::Unit::TestCase
2129
2286
  assert (different1.stat <=> different2.stat) == -1
2130
2287
  end
2131
2288
 
2289
+ def test_file_binread_works
2290
+ File.open("testfile", 'w') do |f|
2291
+ f << "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2292
+ end
2293
+
2294
+ assert_equal File.binread("testfile"), "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2295
+ assert_equal File.binread("testfile", 20), "This is line one\nThi"
2296
+ assert_equal File.binread("testfile", 20, 10), "ne one\nThis is line "
2297
+ end
2298
+
2132
2299
  def here(fname)
2133
2300
  RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
2134
2301
  end
2135
2302
 
2303
+ def test_file_utils_compare_file
2304
+ file1 = 'file1.txt'
2305
+ file2 = 'file2.txt'
2306
+ file3 = 'file3.txt'
2307
+ content = "This is my \n file\content\n"
2308
+ File.open(file1, 'w') do |f|
2309
+ f.write content
2310
+ end
2311
+ File.open(file3, 'w') do |f|
2312
+ f.write "#{content} with additional content"
2313
+ end
2314
+
2315
+ FileUtils.cp file1, file2
2316
+
2317
+ assert_equal FileUtils.compare_file(file1, file2), true
2318
+ assert_equal FileUtils.compare_file(file1, file3), false
2319
+ assert_raises Errno::ENOENT do
2320
+ FileUtils.compare_file(file1, "file4.txt")
2321
+ end
2322
+ end
2323
+
2136
2324
  if RUBY_VERSION >= "1.9.2"
2137
2325
  def test_file_size
2138
2326
  File.open("foo", 'w') do |f|
@@ -2173,5 +2361,32 @@ class FakeFSTest < Test::Unit::TestCase
2173
2361
  end
2174
2362
  end
2175
2363
  end
2364
+
2365
+ def test_file_write_can_write_a_file
2366
+ File.write("testfile", "0123456789")
2367
+ assert_equal File.read("testfile"), "0123456789"
2368
+ end
2369
+
2370
+ def test_file_write_returns_the_length_written
2371
+ assert_equal File.write("testfile", "0123456789"), 10
2372
+ end
2373
+
2374
+ def test_file_write_truncates_file_if_offset_not_given
2375
+ File.open("foo", 'w') do |f|
2376
+ f << "foo"
2377
+ end
2378
+
2379
+ File.write('foo', 'bar')
2380
+ assert_equal File.read('foo'), 'bar'
2381
+ end
2382
+
2383
+ def test_file_write_writes_at_offset_and_does_not_truncate
2384
+ File.open("foo", 'w') do |f|
2385
+ f << "foo"
2386
+ end
2387
+
2388
+ File.write('foo', 'bar', 3)
2389
+ assert_equal File.read('foo'), 'foobar'
2390
+ end
2176
2391
  end
2177
2392
  end
@@ -34,24 +34,28 @@ class FileStatTest < Test::Unit::TestCase
34
34
  ln_s("/foo", "/bar")
35
35
 
36
36
  assert File::Stat.new("/bar").symlink?
37
+ assert File::Stat.new("/bar").ftype == "link"
37
38
  end
38
39
 
39
40
  def test_symlink_should_be_false_when_not_a_symlink
40
41
  FileUtils.touch("/foo")
41
42
 
42
43
  assert !File::Stat.new("/foo").symlink?
44
+ assert File::Stat.new("/foo").ftype == "file"
43
45
  end
44
46
 
45
47
  def test_should_return_false_for_directory_when_not_a_directory
46
48
  FileUtils.touch("/foo")
47
49
 
48
50
  assert !File::Stat.new("/foo").directory?
51
+ assert File::Stat.new("/foo").ftype == "file"
49
52
  end
50
53
 
51
54
  def test_should_return_true_for_directory_when_a_directory
52
55
  mkdir "/foo"
53
56
 
54
57
  assert File::Stat.new("/foo").directory?
58
+ assert File::Stat.new("/foo").ftype == "directory"
55
59
  end
56
60
 
57
61
  def test_writable_is_true
@@ -106,4 +110,12 @@ class FileStatTest < Test::Unit::TestCase
106
110
  File.open('/foo', 'w') { |f| f << 'test' }
107
111
  assert File.mtime("/foo") > mtime
108
112
  end
113
+
114
+ def test_responds_to_realpath_only_on_1_9
115
+ if RUBY_VERSION > '1.9'
116
+ assert File.respond_to?(:realpath)
117
+ else
118
+ assert !File.respond_to?(:realpath)
119
+ end
120
+ end
109
121
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakefs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2012-11-24 00:00:00.000000000 Z
15
+ date: 2013-09-07 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: rspec
@@ -123,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  segments:
125
125
  - 0
126
- hash: 4092255673165272959
126
+ hash: -790320842587735539
127
127
  required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements: