fakefs 0.2.0 → 0.2.1

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.
@@ -0,0 +1 @@
1
+ *.sw?
@@ -0,0 +1,12 @@
1
+ * Chris Wanstrath
2
+ * Jeff Hodges
3
+ * Scott Taylor
4
+ * Pat Nakajima
5
+ * Myles Eftos
6
+ * Matt Freels
7
+ * Nick Quaranto
8
+ * Tymon Tobolski
9
+ * msassak
10
+ * Rob Sanheim
11
+ * Jon Yurek
12
+ * David Reese
@@ -48,6 +48,13 @@ Don't Fake the FS Immediately
48
48
  end
49
49
 
50
50
 
51
+ RSpec
52
+ -----------------------------
53
+ The above approach works with RSpec as well. In addition to this you may use the
54
+ 'use_fakefs' macro to turn FakeFS on and off in a given example group. See
55
+ lib/spec_helpers for more details on it's usage.
56
+
57
+
51
58
  How is this different than MockFS?
52
59
  ----------------------------------
53
60
 
@@ -56,6 +63,14 @@ test-time dependency: your actual library does not need to use or know about
56
63
  FakeFS.
57
64
 
58
65
 
66
+ Caveats
67
+ -------
68
+
69
+ FakeFS internally uses the `Pathname` and `FileUtils` constants. If you use
70
+ these in your app, be certain you're properly requiring them and not counting
71
+ on FakeFS' own require.
72
+
73
+
59
74
  Speed?
60
75
  ------
61
76
  <http://gist.github.com/156091>
@@ -73,26 +88,11 @@ Installation
73
88
  $ rip install git://github.com/defunkt/fakefs.git
74
89
 
75
90
 
76
- Contributors
77
- ------------
78
-
79
- * Chris Wanstrath
80
- * David Reese
81
- * Jeff Hodges
82
- * Jon Yurek
83
- * Matt Freels
84
- * Myles Eftos
85
- * Pat Nakajima
86
- * Rob Sanheim
87
- * Scott Taylor
88
- * Tymon Tobolski
89
- * msassak
90
-
91
-
92
91
  Meta
93
92
  ----
94
93
 
95
94
  * Code: `git clone git://github.com/defunkt/fakefs.git`
95
+ * Home: <http://github.com/defunkt/fakefs>
96
96
  * Docs: <http://defunkt.github.com/fakefs>
97
97
  * Bugs: <http://github.com/defunkt/fakefs/issues>
98
98
  * List: <http://groups.google.com/group/fakefs>
data/Rakefile CHANGED
@@ -31,3 +31,17 @@ begin
31
31
  rescue LoadError
32
32
  puts "sdoc support not enabled. Please gem install sdoc-helpers."
33
33
  end
34
+
35
+ desc "Build a gem"
36
+ task :gem => [ :gemspec, :build ]
37
+
38
+ desc "Push a new version to Gemcutter"
39
+ task :publish => [ :gemspec, :build ] do
40
+ abort("Tests failed!") unless system("rake test")
41
+ system "git tag v#{FakeFS::Version}"
42
+ system "git push origin v#{FakeFS::Version}"
43
+ system "git push origin master"
44
+ system "gem push pkg/fakefs-#{FakeFS::Version}.gem"
45
+ system "git clean -fd"
46
+ exec "rake pages"
47
+ end
@@ -31,8 +31,8 @@ end
31
31
  def FakeFS
32
32
  return ::FakeFS unless block_given?
33
33
  ::FakeFS.activate!
34
- result = yield
35
- ::FakeFS.deactivate!
36
- result
34
+ yield
35
+ ensure
36
+ ::FakeFS.deactivate!
37
37
  end
38
38
 
@@ -71,7 +71,7 @@ module FakeFS
71
71
 
72
72
  def self.entries(dirname)
73
73
  raise SystemCallError, dirname unless FileSystem.find(dirname)
74
- Dir.new(dirname).map { |file| file }
74
+ Dir.new(dirname).map { |file| File.basename(file) }
75
75
  end
76
76
 
77
77
  def self.foreach(dirname, &block)
@@ -1,6 +1,6 @@
1
1
  module FakeFS
2
2
  class FakeFile
3
- attr_accessor :name, :parent
3
+ attr_accessor :name, :parent, :content, :mtime
4
4
 
5
5
  class Inode
6
6
  def initialize(file_owner)
@@ -19,12 +19,19 @@ module FakeFS
19
19
  def unlink(file)
20
20
  links.delete(file)
21
21
  end
22
+
23
+ def clone
24
+ clone = super
25
+ clone.content = content.dup
26
+ clone
27
+ end
22
28
  end
23
29
 
24
30
  def initialize(name = nil, parent = nil)
25
31
  @name = name
26
32
  @parent = parent
27
33
  @inode = Inode.new(self)
34
+ @mtime = Time.now
28
35
  end
29
36
 
30
37
  attr_accessor :inode
@@ -13,19 +13,18 @@ module FakeFS
13
13
 
14
14
  FILE_CREATION_MODES = MODES - [READ_ONLY, READ_WRITE]
15
15
 
16
- READ_ONLY_MODES = [
17
- READ_ONLY
18
- ]
19
-
20
- WRITE_ONLY_MODES = [
21
- WRITE_ONLY,
22
- APPEND_WRITE_ONLY
23
- ]
24
-
25
- TRUNCATION_MODES = [
26
- WRITE_ONLY,
27
- READ_WRITE_TRUNCATE
28
- ]
16
+ MODE_BITMASK = RealFile::RDONLY |
17
+ RealFile::WRONLY |
18
+ RealFile::RDWR |
19
+ RealFile::APPEND |
20
+ RealFile::CREAT |
21
+ RealFile::EXCL |
22
+ RealFile::NONBLOCK |
23
+ RealFile::TRUNC |
24
+ RealFile::NOCTTY |
25
+ RealFile::SYNC
26
+
27
+ FILE_CREATION_BITMASK = RealFile::CREAT
29
28
 
30
29
  def self.extname(path)
31
30
  RealFile.extname(path)
@@ -41,12 +40,32 @@ module FakeFS
41
40
 
42
41
  class << self
43
42
  alias_method :exists?, :exist?
43
+
44
+ # Assuming that everyone can read and write files
45
+ alias_method :readable?, :exist?
46
+ alias_method :writable?, :exist?
47
+ end
48
+
49
+ def self.mtime(path)
50
+ if exists?(path)
51
+ FileSystem.find(path).mtime
52
+ else
53
+ raise Errno::ENOENT
54
+ end
44
55
  end
45
56
 
46
57
  def self.size(path)
47
58
  read(path).length
48
59
  end
49
60
 
61
+ def self.size?(path)
62
+ if exists?(path) && !size(path).zero?
63
+ true
64
+ else
65
+ nil
66
+ end
67
+ end
68
+
50
69
  def self.const_missing(name)
51
70
  RealFile.const_get(name)
52
71
  end
@@ -114,7 +133,7 @@ module FakeFS
114
133
  def self.readlines(path)
115
134
  read(path).split("\n")
116
135
  end
117
-
136
+
118
137
  def self.link(source, dest)
119
138
  if directory?(source)
120
139
  raise Errno::EPERM, "Operation not permitted - #{source} or #{dest}"
@@ -123,11 +142,11 @@ module FakeFS
123
142
  if !exists?(source)
124
143
  raise Errno::ENOENT, "No such file or directory - #{source} or #{dest}"
125
144
  end
126
-
145
+
127
146
  if exists?(dest)
128
147
  raise Errno::EEXIST, "File exists - #{source} or #{dest}"
129
148
  end
130
-
149
+
131
150
  source = FileSystem.find(source)
132
151
  dest = FileSystem.add(dest, source.entry.clone)
133
152
  source.link(dest)
@@ -191,75 +210,67 @@ module FakeFS
191
210
  @file = FileSystem.find(path)
192
211
  @open = true
193
212
 
194
- check_valid_mode
213
+ check_modes!
214
+
195
215
  file_creation_mode? ? create_missing_file : check_file_existence!
196
- truncate_file if truncation_mode?
216
+
217
+ @stream = StringIO.new(@file.content, mode)
197
218
  end
198
219
 
199
220
  def close
200
221
  @open = false
201
222
  end
202
223
 
203
- def read
204
- raise IOError, 'closed stream' unless @open
205
- raise IOError, 'not opened for reading' if write_only?
224
+ def read(chunk = nil)
225
+ @stream.read(chunk)
226
+ end
206
227
 
207
- @file.content
228
+ def rewind
229
+ @stream.rewind
208
230
  end
209
231
 
210
232
  def exists?
211
- @file
233
+ true
212
234
  end
213
235
 
214
236
  def puts(*content)
215
- content.flatten.each do |obj|
216
- write(obj.to_s + "\n")
217
- end
237
+ @stream.puts(*content)
218
238
  end
219
239
 
220
240
  def write(content)
221
- raise IOError, 'closed stream' unless @open
222
- raise IOError, 'not open for writing' if read_only?
223
-
224
- @file.content += content
241
+ @stream.write(content)
225
242
  end
226
243
  alias_method :print, :write
227
244
  alias_method :<<, :write
228
245
 
229
246
  def flush; self; end
230
247
 
248
+ def seek(amount, whence = SEEK_SET)
249
+ @stream.seek(amount, whence)
250
+ end
251
+
231
252
  private
232
253
 
254
+ def check_modes!
255
+ StringIO.new("", @mode)
256
+ end
257
+
233
258
  def check_file_existence!
234
259
  unless @file
235
260
  raise Errno::ENOENT, "No such file or directory - #{@file}"
236
261
  end
237
262
  end
238
263
 
239
- def check_valid_mode
240
- if !mode_in?(MODES)
241
- raise ArgumentError, "illegal access mode #{@mode}"
242
- end
243
- end
244
-
245
- def read_only?
246
- mode_in? READ_ONLY_MODES
247
- end
248
-
249
264
  def file_creation_mode?
250
- mode_in? FILE_CREATION_MODES
251
- end
252
-
253
- def write_only?
254
- mode_in? WRITE_ONLY_MODES
265
+ mode_in?(FILE_CREATION_MODES) || mode_in_bitmask?(FILE_CREATION_BITMASK)
255
266
  end
256
267
 
257
- def truncation_mode?
258
- mode_in? TRUNCATION_MODES
268
+ def mode_in?(list)
269
+ list.any? { |element| @mode.include?(element) } if @mode.respond_to?(:include?)
259
270
  end
260
271
 
261
- def mode_in?(list)
262
- list.include?(@mode)
272
+ def mode_in_bitmask?(mask)
273
+ (@mode & mask) != 0 if @mode.is_a?(Integer)
263
274
  end
264
275
 
265
276
  def create_missing_file
@@ -267,9 +278,5 @@ module FakeFS
267
278
  @file = FileSystem.add(path, FakeFile.new)
268
279
  end
269
280
  end
270
-
271
- def truncate_file
272
- @file.content = ""
273
- end
274
281
  end
275
282
  end
@@ -106,7 +106,20 @@ module FakeFS
106
106
  return [] unless dir.respond_to? :[]
107
107
 
108
108
  pattern , *parts = parts
109
- matches = dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values
109
+ matches = case pattern
110
+ when '**'
111
+ case parts
112
+ when ['*'], []
113
+ parts = [] # end recursion
114
+ directories_under(dir).map do |d|
115
+ d.values.select{|f| f.is_a? FakeFile }
116
+ end.flatten.uniq
117
+ else
118
+ directories_under(dir)
119
+ end
120
+ else
121
+ dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values
122
+ end
110
123
 
111
124
  if parts.empty? # we're done recursing
112
125
  matches
@@ -114,5 +127,10 @@ module FakeFS
114
127
  matches.map{|entry| find_recurser(entry, parts) }
115
128
  end
116
129
  end
130
+
131
+ def directories_under(dir)
132
+ children = dir.values.select{|f| f.is_a? FakeDir}
133
+ ([dir] + children + children.map{|c| directories_under(c)}).flatten.uniq
134
+ end
117
135
  end
118
136
  end
@@ -25,11 +25,18 @@ module FakeFS
25
25
 
26
26
  alias_method :rm_rf, :rm
27
27
  alias_method :rm_r, :rm
28
+ alias_method :rm_f, :rm
28
29
 
29
- def ln_s(target, path)
30
- raise Errno::EEXIST, path if FileSystem.find(path)
30
+ def ln_s(target, path, options = {})
31
+ options = { :force => false }.merge(options)
32
+ (FileSystem.find(path) and !options[:force]) ? raise(Errno::EEXIST, path) : FileSystem.delete(path)
31
33
  FileSystem.add(path, FakeSymlink.new(target))
32
34
  end
35
+ def ln_sf(target, path)
36
+ ln_s(target, path, { :force => true })
37
+ end
38
+
39
+
33
40
 
34
41
  def cp(src, dest)
35
42
  dst_file = FileSystem.find(dest)
@@ -0,0 +1,46 @@
1
+ # FakeFS::SpecHelpers provides a simple macro for RSpec example groups to turn FakeFS on and off.
2
+ # To use it simply require 'fakefs/spec_helpers', then include FakeFS::SpecHelpers into any
3
+ # example groups that you wish to use FakeFS in. For example:
4
+ #
5
+ # require 'fakefs/spec_helpers'
6
+ #
7
+ # describe "Some specs that deal with files" do
8
+ # include FakeFS::SpecHelpers
9
+ # ...
10
+ # end
11
+ #
12
+ # Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's
13
+ # configuration block in your spec helper:
14
+ #
15
+ # require 'fakefs/spec_helpers'
16
+ #
17
+ # Spec::Runner.configure do |config|
18
+ # config.include FakeFS::SpecHelpers
19
+ # end
20
+ #
21
+ # If you do the above then use_fakefs will be available in all of your example groups.
22
+ #
23
+ require 'fakefs/safe'
24
+
25
+ module FakeFS
26
+ module SpecHelpers
27
+ def self.extended(example_group)
28
+ example_group.use_fakefs(example_group)
29
+ end
30
+
31
+ def self.included(example_group)
32
+ example_group.extend self
33
+ end
34
+
35
+ def use_fakefs(describe_block)
36
+ describe_block.before :each do
37
+ FakeFS.activate!
38
+ end
39
+
40
+ describe_block.after :each do
41
+ FakeFS.deactivate!
42
+ FakeFS::FileSystem.clear
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,6 +1,6 @@
1
1
  module FakeFS
2
2
  module Version
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
 
5
5
  def self.to_s
6
6
  VERSION
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ module FakeFS
4
+ describe SpecHelpers do
5
+ before do
6
+ @rspec_example_group = Class.new do
7
+ def self.before(sym = :each)
8
+ yield if block_given?
9
+ end
10
+
11
+ def self.after(sym = :each)
12
+ yield if block_given?
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "when extending" do
18
+ context "before each" do
19
+ it "should call it" do
20
+ @rspec_example_group.should_receive(:before).with(:each)
21
+ @rspec_example_group.extend FakeFS::SpecHelpers
22
+ end
23
+
24
+ it "should call FakeFS.activate!" do
25
+ FakeFS.should_receive(:activate!)
26
+ @rspec_example_group.extend FakeFS::SpecHelpers
27
+ end
28
+ end
29
+
30
+ context "after each" do
31
+ it "should call it" do
32
+ @rspec_example_group.should_receive(:after).with(:each)
33
+ @rspec_example_group.extend FakeFS::SpecHelpers
34
+ end
35
+
36
+ it "should deactivate fakefs" do
37
+ FakeFS.should_receive(:deactivate!)
38
+ @rspec_example_group.extend FakeFS::SpecHelpers
39
+ end
40
+
41
+ it "should clear the fakefs filesystem for the next run" do
42
+ FakeFS::FileSystem.should_receive(:clear)
43
+ @rspec_example_group.extend FakeFS::SpecHelpers
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "when including" do
49
+ it "should call before :each" do
50
+ @rspec_example_group.should_receive(:before)
51
+ @rspec_example_group.class_eval do
52
+ include FakeFS::SpecHelpers
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'fakefs/spec_helpers'
@@ -4,8 +4,9 @@ require 'test/unit'
4
4
 
5
5
  class FakeSymlinkTest < Test::Unit::TestCase
6
6
  include FakeFS
7
-
7
+
8
8
  def test_symlink_has_method_missing_as_private
9
- assert FakeSymlink.private_instance_methods.include?("method_missing")
9
+ methods = FakeSymlink.private_instance_methods.map { |m| m.to_s }
10
+ assert methods.include?("method_missing")
10
11
  end
11
- end
12
+ end
@@ -74,6 +74,20 @@ class FakeFSTest < Test::Unit::TestCase
74
74
  FileUtils.ln_s(target, '/path/to/link')
75
75
  end
76
76
  end
77
+
78
+ def test_can_force_creation_of_symlinks
79
+ FileUtils.mkdir_p(target = "/path/to/first/target")
80
+ FileUtils.ln_s(target, "/path/to/link")
81
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
82
+ FileUtils.ln_s(target, '/path/to/link', :force => true)
83
+ end
84
+
85
+ def test_create_symlink_using_ln_sf
86
+ FileUtils.mkdir_p(target = "/path/to/first/target")
87
+ FileUtils.ln_s(target, "/path/to/link")
88
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
89
+ FileUtils.ln_sf(target, '/path/to/link')
90
+ end
77
91
 
78
92
  def test_can_follow_symlinks
79
93
  FileUtils.mkdir_p(target = "/path/to/target")
@@ -94,6 +108,19 @@ class FakeFSTest < Test::Unit::TestCase
94
108
  end
95
109
 
96
110
  assert File.exists?(path)
111
+ assert File.readable?(path)
112
+ assert File.writable?(path)
113
+ end
114
+
115
+ def test_can_create_files_with_bitmasks
116
+ path = '/path/to/file.txt'
117
+ File.open(path, File::RDWR | File::CREAT) do |f|
118
+ f.write "Yatta!"
119
+ end
120
+
121
+ assert File.exists?(path)
122
+ assert File.readable?(path)
123
+ assert File.writable?(path)
97
124
  end
98
125
 
99
126
  def test_file_opens_in_read_only_mode
@@ -106,6 +133,16 @@ class FakeFSTest < Test::Unit::TestCase
106
133
  end
107
134
  end
108
135
 
136
+ def test_file_opens_in_read_only_mode_with_bitmasks
137
+ File.open("foo", "w") { |f| f << "foo" }
138
+
139
+ f = File.open("foo", File::RDONLY)
140
+
141
+ assert_raises(IOError) do
142
+ f << "bar"
143
+ end
144
+ end
145
+
109
146
  def test_file_opens_in_invalid_mode
110
147
  FileUtils.touch("foo")
111
148
 
@@ -131,6 +168,17 @@ class FakeFSTest < Test::Unit::TestCase
131
168
  assert File.exists?("foo")
132
169
  end
133
170
 
171
+ def test_creates_files_in_write_only_mode_with_bitmasks
172
+ File.open("foo", File::WRONLY | File::CREAT)
173
+ assert File.exists?("foo")
174
+ end
175
+
176
+ def test_raises_in_write_only_mode_without_create_bitmask
177
+ assert_raises(Errno::ENOENT) do
178
+ File.open("foo", File::WRONLY)
179
+ end
180
+ end
181
+
134
182
  def test_creates_files_in_read_write_truncate_mode
135
183
  File.open("foo", "w+")
136
184
  assert File.exists?("foo")
@@ -199,6 +247,30 @@ class FakeFSTest < Test::Unit::TestCase
199
247
  assert_equal 'Yada Yada', File.read(path)
200
248
  end
201
249
 
250
+ def test_raises_error_when_opening_with_binary_mode_only
251
+ assert_raise ArgumentError do
252
+ File.open("/foo", "b")
253
+ end
254
+ end
255
+
256
+ def test_can_open_file_in_binary_mode
257
+ File.open("/foo", "wb") { |x| x << "a" }
258
+ assert_equal "a", File.read("/foo")
259
+ end
260
+
261
+ def test_can_chunk_io_when_reading
262
+ path = '/path/to/file.txt'
263
+ File.open(path, 'w') do |f|
264
+ f << 'Yada Yada'
265
+ end
266
+ file = File.new(path, 'r')
267
+ assert_equal 'Yada', file.read(4)
268
+ assert_equal ' Yada', file.read(5)
269
+ assert_equal '', file.read
270
+ file.rewind
271
+ assert_equal 'Yada Yada', file.read
272
+ end
273
+
202
274
  def test_can_get_size_of_files
203
275
  path = '/path/to/file.txt'
204
276
  File.open(path, 'w') do |f|
@@ -207,6 +279,37 @@ class FakeFSTest < Test::Unit::TestCase
207
279
  assert_equal 9, File.size(path)
208
280
  end
209
281
 
282
+ def test_can_check_if_file_has_size?
283
+ path = '/path/to/file.txt'
284
+ File.open(path, 'w') do |f|
285
+ f << 'Yada Yada'
286
+ end
287
+ assert File.size?(path)
288
+ assert_nil File.size?("/path/to/other.txt")
289
+ end
290
+
291
+ def test_can_check_size?_of_empty_file
292
+ path = '/path/to/file.txt'
293
+ File.open(path, 'w') do |f|
294
+ f << ''
295
+ end
296
+ assert_nil File.size?("/path/to/file.txt")
297
+ end
298
+
299
+ def test_raises_error_on_mtime_if_file_does_not_exist
300
+ assert_raise Errno::ENOENT do
301
+ File.mtime('/path/to/file.txt')
302
+ end
303
+ end
304
+
305
+ def test_can_return_mtime_on_existing_file
306
+ path = '/path/to/file.txt'
307
+ File.open(path, 'w') do |f|
308
+ f << ''
309
+ end
310
+ assert File.mtime('/path/to/file.txt').is_a?(Time)
311
+ end
312
+
210
313
  def test_can_read_with_File_readlines
211
314
  path = '/path/to/file.txt'
212
315
  File.open(path, 'w') do |f|
@@ -327,6 +430,21 @@ class FakeFSTest < Test::Unit::TestCase
327
430
  #assert_equal ['/'], Dir['/']
328
431
  end
329
432
 
433
+ def test_dir_glob_handles_recursive_globs
434
+ File.open('/one/two/three/four.rb', 'w')
435
+ File.open('/one/five.rb', 'w')
436
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
437
+ assert_equal ['/one/two'], Dir['/one/**/two']
438
+ assert_equal ['/one/two/three'], Dir['/one/**/three']
439
+ end
440
+
441
+ def test_dir_recursive_glob_ending_in_wildcards_only_returns_files
442
+ File.open('/one/two/three/four.rb', 'w')
443
+ File.open('/one/five.rb', 'w')
444
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*']
445
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**']
446
+ end
447
+
330
448
  def test_chdir_changes_directories_like_a_boss
331
449
  # I know memes!
332
450
  FileUtils.mkdir_p '/path'
@@ -851,6 +969,20 @@ class FakeFSTest < Test::Unit::TestCase
851
969
  test.each { |t| assert yielded.include?(t) }
852
970
  end
853
971
 
972
+ def test_directory_entries_works_with_trailing_slash
973
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
974
+
975
+ FileUtils.mkdir_p('/this/path/should/be/here')
976
+
977
+ test.each do |f|
978
+ FileUtils.touch("/this/path/should/be/here/#{f}")
979
+ end
980
+
981
+ yielded = Dir.entries('/this/path/should/be/here/')
982
+ assert yielded.size == test.size
983
+ test.each { |t| assert yielded.include?(t) }
984
+ end
985
+
854
986
  def test_directory_foreach
855
987
  test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
856
988
 
@@ -1060,6 +1192,59 @@ class FakeFSTest < Test::Unit::TestCase
1060
1192
  assert !File.exists?("/foo")
1061
1193
  end
1062
1194
 
1195
+ def test_file_seek_returns_0
1196
+ File.open("/foo", "w") do |f|
1197
+ f << "one\ntwo\nthree"
1198
+ end
1199
+
1200
+ file = File.open("/foo", "r")
1201
+
1202
+ assert_equal 0, file.seek(1)
1203
+ end
1204
+
1205
+ def test_file_seek_seeks_to_location
1206
+ File.open("/foo", "w") do |f|
1207
+ f << "123"
1208
+ end
1209
+
1210
+ file = File.open("/foo", "r")
1211
+ file.seek(1)
1212
+ assert_equal "23", file.read
1213
+ end
1214
+
1215
+ def test_file_seek_seeks_to_correct_location
1216
+ File.open("/foo", "w") do |f|
1217
+ f << "123"
1218
+ end
1219
+
1220
+ file = File.open("/foo", "r")
1221
+ file.seek(2)
1222
+ assert_equal "3", file.read
1223
+ end
1224
+
1225
+ def test_file_seek_can_take_negative_offset
1226
+ File.open("/foo", "w") do |f|
1227
+ f << "123456789"
1228
+ end
1229
+
1230
+ file = File.open("/foo", "r")
1231
+
1232
+ file.seek(-1, IO::SEEK_END)
1233
+ assert_equal "9", file.read
1234
+
1235
+ file.seek(-2, IO::SEEK_END)
1236
+ assert_equal "89", file.read
1237
+
1238
+ file.seek(-3, IO::SEEK_END)
1239
+ assert_equal "789", file.read
1240
+ end
1241
+
1242
+ def test_should_have_constants_inherited_from_descending_from_io
1243
+ assert_equal IO::SEEK_CUR, File::SEEK_CUR
1244
+ assert_equal IO::SEEK_END, File::SEEK_END
1245
+ assert_equal IO::SEEK_SET, File::SEEK_SET
1246
+ end
1247
+
1063
1248
  def here(fname)
1064
1249
  RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname)
1065
1250
  end
@@ -26,4 +26,17 @@ class FakeFSSafeTest < Test::Unit::TestCase
26
26
 
27
27
  assert_equal result, "Yatta!"
28
28
  end
29
+
30
+ def test_FakeFS_method_deactivates_FakeFS_when_block_raises_exception
31
+ begin
32
+ FakeFS do
33
+ raise 'boom!'
34
+ end
35
+ rescue
36
+ end
37
+
38
+ assert_equal RealFile, File, "File is #{File} (should be #{RealFile})"
39
+ assert_equal RealFileUtils, FileUtils, "FileUtils is #{FileUtils} (should be #{RealFileUtils})"
40
+ assert_equal RealDir, Dir, "Dir is #{Dir} (should be #{RealDir})"
41
+ end
29
42
  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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-06 00:00:00 -07:00
12
+ date: 2009-10-30 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,8 @@ extra_rdoc_files:
23
23
  - LICENSE
24
24
  - README.markdown
25
25
  files:
26
+ - .gitignore
27
+ - CONTRIBUTORS
26
28
  - LICENSE
27
29
  - README.markdown
28
30
  - Rakefile
@@ -36,7 +38,11 @@ files:
36
38
  - lib/fakefs/file_system.rb
37
39
  - lib/fakefs/fileutils.rb
38
40
  - lib/fakefs/safe.rb
41
+ - lib/fakefs/spec_helpers.rb
39
42
  - lib/fakefs/version.rb
43
+ - spec/fakefs/spec_helpers_spec.rb
44
+ - spec/spec.opts
45
+ - spec/spec_helper.rb
40
46
  - test/fake/file_test.rb
41
47
  - test/fake/symlink_test.rb
42
48
  - test/fakefs_test.rb
@@ -72,6 +78,8 @@ signing_key:
72
78
  specification_version: 3
73
79
  summary: A fake filesystem. Use it in your tests.
74
80
  test_files:
81
+ - spec/fakefs/spec_helpers_spec.rb
82
+ - spec/spec_helper.rb
75
83
  - test/fake/file_test.rb
76
84
  - test/fake/symlink_test.rb
77
85
  - test/fakefs_test.rb