bmabey-fakefs 0.1.1 → 0.1.1.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.
data/README.markdown CHANGED
@@ -48,6 +48,12 @@ 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
+
51
57
  How is this different than MockFS?
52
58
  ----------------------------------
53
59
 
data/Rakefile CHANGED
@@ -4,17 +4,37 @@ end
4
4
 
5
5
  begin
6
6
  require 'jeweler'
7
- Jeweler::Tasks.new do |gem|
8
- gem.name = "fakefs"
9
- gem.summary = %Q{A fake filesystem. Use it in your tests.}
10
- gem.email = "chris@ozmm.org"
11
- gem.homepage = "http://ozmm.org/posts/fakefs.html"
12
- gem.authors = ["Chris Wanstrath", "Pat Nakajima"]
13
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
7
+
8
+ # We're not putting VERSION or VERSION.yml in the root,
9
+ # so we have to help Jeweler find our version.
10
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
11
+ require 'fakefs/version'
12
+
13
+ FakeFS::Version.instance_eval do
14
+ def refresh
15
+ end
14
16
  end
15
17
 
16
- rescue LoadError
17
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
- end
18
+ class Jeweler
19
+ def version_helper
20
+ FakeFS::Version
21
+ end
19
22
 
23
+ def version_exists?
24
+ true
25
+ end
26
+ end
20
27
 
28
+ Jeweler::Tasks.new do |gemspec|
29
+ gemspec.name = "fakefs"
30
+ gemspec.summary = "A fake filesystem. Use it in your tests."
31
+ gemspec.email = "chris@ozmm.org"
32
+ gemspec.homepage = "http://github.com/defunkt/fakefs"
33
+ gemspec.description = "A fake filesystem. Use it in your tests."
34
+ gemspec.authors = ["Chris Wanstrath"]
35
+ gemspec.has_rdoc = false
36
+ end
37
+ rescue LoadError
38
+ puts "Jeweler not available."
39
+ puts "Install it with: gem install technicalpickles-jeweler"
40
+ end
data/fakefs.gemspec CHANGED
@@ -1,12 +1,16 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{fakefs}
5
- s.version = "0.1.1"
8
+ s.version = "0.1.1.1"
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Chris Wanstrath", "Pat Nakajima"]
9
- s.date = %q{2009-08-10}
11
+ s.authors = ["Chris Wanstrath"]
12
+ s.date = %q{2009-09-23}
13
+ s.description = %q{A fake filesystem. Use it in your tests.}
10
14
  s.email = %q{chris@ozmm.org}
11
15
  s.extra_rdoc_files = [
12
16
  "LICENSE",
@@ -17,7 +21,6 @@ Gem::Specification.new do |s|
17
21
  "LICENSE",
18
22
  "README.markdown",
19
23
  "Rakefile",
20
- "VERSION",
21
24
  "fakefs.gemspec",
22
25
  "lib/fakefs.rb",
23
26
  "lib/fakefs/base.rb",
@@ -30,11 +33,12 @@ Gem::Specification.new do |s|
30
33
  "lib/fakefs/fileutils.rb",
31
34
  "lib/fakefs/safe.rb",
32
35
  "lib/fakefs/spec_helpers.rb",
36
+ "lib/fakefs/version.rb",
33
37
  "test/fakefs_test.rb",
34
38
  "test/safe_test.rb",
35
39
  "test/verify.rb"
36
40
  ]
37
- s.homepage = %q{http://ozmm.org/posts/fakefs.html}
41
+ s.homepage = %q{http://github.com/defunkt/fakefs}
38
42
  s.rdoc_options = ["--charset=UTF-8"]
39
43
  s.require_paths = ["lib"]
40
44
  s.rubygems_version = %q{1.3.3}
data/lib/fakefs/base.rb CHANGED
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  RealFile = File
4
2
  RealFileUtils = FileUtils
5
3
  RealDir = Dir
@@ -33,7 +31,8 @@ end
33
31
  def FakeFS
34
32
  return ::FakeFS unless block_given?
35
33
  ::FakeFS.activate!
36
- yield
34
+ result = yield
37
35
  ::FakeFS.deactivate!
36
+ result
38
37
  end
39
38
 
data/lib/fakefs/dir.rb CHANGED
@@ -1,7 +1,55 @@
1
1
  module FakeFS
2
2
  class Dir
3
- def self.glob(pattern)
4
- [FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort
3
+ include Enumerable
4
+
5
+ def initialize(string)
6
+ raise Errno::ENOENT, string unless FileSystem.find(string)
7
+ @path = string
8
+ @open = true
9
+ @pointer = 0
10
+ @contents = [ '.', '..', ] + FileSystem.find(@path).values
11
+ end
12
+
13
+ def close
14
+ @open = false
15
+ @pointer = nil
16
+ @contents = nil
17
+ nil
18
+ end
19
+
20
+ def each(&block)
21
+ while f = read
22
+ yield f
23
+ end
24
+ end
25
+
26
+ def path
27
+ @path
28
+ end
29
+
30
+ def pos
31
+ @pointer
32
+ end
33
+
34
+ def pos=(integer)
35
+ @pointer = integer
36
+ end
37
+
38
+ def read
39
+ raise IOError, "closed directory" if @pointer == nil
40
+ n = @contents[@pointer]
41
+ @pointer += 1
42
+ n.to_s.gsub(path + '/', '') if n
43
+ end
44
+
45
+ def rewind
46
+ @pointer = 0
47
+ end
48
+
49
+ def seek(integer)
50
+ raise IOError, "closed directory" if @pointer == nil
51
+ @pointer = integer
52
+ @contents[integer]
5
53
  end
6
54
 
7
55
  def self.[](pattern)
@@ -12,12 +60,55 @@ module FakeFS
12
60
  FileSystem.chdir(dir, &blk)
13
61
  end
14
62
 
63
+ def self.chroot(string)
64
+ # Not implemented yet
65
+ end
66
+
67
+ def self.delete(string)
68
+ raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty?
69
+ FileSystem.delete(string)
70
+ end
71
+
72
+ def self.entries(dirname)
73
+ raise SystemCallError, dirname unless FileSystem.find(dirname)
74
+ Dir.new(dirname).map { |file| file }
75
+ end
76
+
77
+ def self.foreach(dirname, &block)
78
+ Dir.open(dirname) { |file| yield file }
79
+ end
80
+
81
+ def self.glob(pattern)
82
+ [FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort
83
+ end
84
+
85
+ def self.mkdir(string, integer = 0)
86
+ parent = string.split('/')
87
+ parent.pop
88
+ raise Errno::ENOENT, "No such file or directory - #{string}" unless parent.join == "" || FileSystem.find(parent.join('/'))
89
+ FileUtils.mkdir_p(string)
90
+ end
91
+
92
+ def self.open(string, &block)
93
+ if block_given?
94
+ Dir.new(string).each { |file| yield(file) }
95
+ else
96
+ Dir.new(string)
97
+ end
98
+ end
99
+
100
+ def self.tmpdir
101
+ '/tmp'
102
+ end
103
+
15
104
  def self.pwd
16
105
  FileSystem.current_dir.to_s
17
106
  end
18
107
 
19
108
  class << self
20
109
  alias_method :getwd, :pwd
110
+ alias_method :rmdir, :delete
111
+ alias_method :unlink, :delete
21
112
  end
22
113
  end
23
114
  end
data/lib/fakefs/file.rb CHANGED
@@ -2,6 +2,10 @@ module FakeFS
2
2
  class File
3
3
  PATH_SEPARATOR = '/'
4
4
 
5
+ def self.extname(path)
6
+ RealFile.extname(path)
7
+ end
8
+
5
9
  def self.join(*parts)
6
10
  parts * PATH_SEPARATOR
7
11
  end
@@ -61,11 +65,11 @@ module FakeFS
61
65
  FileSystem.find(symlink.target).to_s
62
66
  end
63
67
 
64
- def self.open(path, mode='r')
68
+ def self.open(path, mode='r', perm = 0644)
65
69
  if block_given?
66
- yield new(path, mode)
70
+ yield new(path, mode, perm)
67
71
  else
68
- new(path, mode)
72
+ new(path, mode, perm)
69
73
  end
70
74
  end
71
75
 
@@ -83,7 +87,7 @@ module FakeFS
83
87
  end
84
88
 
85
89
  attr_reader :path
86
- def initialize(path, mode = nil)
90
+ def initialize(path, mode = nil, perm = nil)
87
91
  @path = path
88
92
  @mode = mode
89
93
  @file = FileSystem.find(path)
@@ -103,8 +107,10 @@ module FakeFS
103
107
  @file
104
108
  end
105
109
 
106
- def puts(content)
107
- write(content + "\n")
110
+ def puts(*content)
111
+ content.flatten.each do |obj|
112
+ write(obj.to_s + "\n")
113
+ end
108
114
  end
109
115
 
110
116
  def write(content)
@@ -22,9 +22,9 @@ module FakeFS
22
22
  def find(path)
23
23
  parts = path_parts(normalize_path(path))
24
24
  return fs if parts.empty? # '/'
25
-
25
+
26
26
  entries = find_recurser(fs, parts).flatten
27
-
27
+
28
28
  case entries.length
29
29
  when 0 then nil
30
30
  when 1 then entries.first
@@ -113,6 +113,6 @@ module FakeFS
113
113
  else
114
114
  matches.map{|entry| find_recurser(entry, parts) }
115
115
  end
116
- end
116
+ end
117
117
  end
118
118
  end
@@ -5,10 +5,24 @@ module FakeFS
5
5
  def mkdir_p(path)
6
6
  FileSystem.add(path, FakeDir.new)
7
7
  end
8
+ alias_method :mkpath, :mkdir_p
9
+
10
+ def rmdir(list, options = {})
11
+ list = [ list ] unless list.is_a?(Array)
12
+ list.each do |l|
13
+ parent = l.split('/')
14
+ parent.pop
15
+ raise Errno::ENOENT, "No such file or directory - #{l}" unless parent.join == "" || FileSystem.find(parent.join('/'))
16
+ raise Errno::ENOENT, l unless FileSystem.find(l)
17
+ raise Errno::ENOTEMPTY, l unless FileSystem.find(l).values.empty?
18
+ rm(l)
19
+ end
20
+ end
8
21
 
9
22
  def rm(path)
10
23
  FileSystem.delete(path)
11
24
  end
25
+
12
26
  alias_method :rm_rf, :rm
13
27
  alias_method :rm_r, :rm
14
28
 
@@ -1,3 +1,24 @@
1
+ # FakeFS::SpecHelpers provides a simple macro for RSpec example groups to turn FakeFS on and off.
2
+ # To use it need to require 'fakefs/safe' and 'fakefs/spec_helpers'. Then include FakeFS::SpecHelpers into any
3
+ # example groups that you wish to use FakeFS in. The "use_fakefs" macro is then available to install
4
+ # before and after hooks which will enable and disable FakeFS. For example:
5
+ #
6
+ # describe SomeClassThatDealsWithFiles
7
+ # include FakeFS::SpecHelpers
8
+ # use_fakefs
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/safe'
16
+ # require 'fakefs/spec_helpers'
17
+ # require 'fakefs/spec_helpers'
18
+ # Spec::Runner.configure do |config|
19
+ # config.extend FakeFS::SpecHelpers
20
+ # end
21
+ #
1
22
  module FakeFS
2
23
  module SpecHelpers
3
24
  def use_fakefs
@@ -0,0 +1,9 @@
1
+ module FakeFS
2
+ module Version
3
+ VERSION = "0.1.1.1"
4
+
5
+ def self.to_s
6
+ VERSION
7
+ end
8
+ end
9
+ end
data/test/fakefs_test.rb CHANGED
@@ -17,7 +17,6 @@ class FakeFSTest < Test::Unit::TestCase
17
17
  def xtest_can_be_initialized_with_an_existing_directory
18
18
  fs = FileSystem
19
19
  fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
20
- puts fs.files.inspect
21
20
  assert_equal 1, fs.files.size
22
21
  end
23
22
 
@@ -25,6 +24,18 @@ class FakeFSTest < Test::Unit::TestCase
25
24
  FileUtils.mkdir_p("/path/to/dir")
26
25
  assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
27
26
  end
27
+
28
+ def test_can_create_directories_with_mkpath
29
+ FileUtils.mkpath("/path/to/dir")
30
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
31
+ end
32
+
33
+ def test_can_delete_directories
34
+ FileUtils.mkdir_p("/path/to/dir")
35
+ FileUtils.rmdir("/path/to/dir")
36
+ assert File.exists?("/path/to/")
37
+ assert File.exists?("/path/to/dir") == false
38
+ end
28
39
 
29
40
  def test_knows_directories_exist
30
41
  FileUtils.mkdir_p(path = "/path/to/dir")
@@ -59,9 +70,9 @@ class FakeFSTest < Test::Unit::TestCase
59
70
  FileUtils.ln_s(target, "/path/to/link")
60
71
  assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
61
72
 
62
- assert_raises(Errno::EEXIST) {
73
+ assert_raises(Errno::EEXIST) do
63
74
  FileUtils.ln_s(target, '/path/to/link')
64
- }
75
+ end
65
76
  end
66
77
 
67
78
  def test_can_follow_symlinks
@@ -105,11 +116,11 @@ class FakeFSTest < Test::Unit::TestCase
105
116
  def test_can_read_with_File_readlines
106
117
  path = '/path/to/file.txt'
107
118
  File.open(path, 'w') do |f|
108
- f.puts "Yatta!"
109
- f.puts "woot"
119
+ f.puts "Yatta!", "Gatta!"
120
+ f.puts ["woot","toot"]
110
121
  end
111
122
 
112
- assert_equal ["Yatta!", "woot"], File.readlines(path)
123
+ assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
113
124
  end
114
125
 
115
126
  def test_File_close_disallows_further_access
@@ -163,9 +174,10 @@ class FakeFSTest < Test::Unit::TestCase
163
174
  def test_can_chown_files
164
175
  good = 'file.txt'
165
176
  bad = 'nofile.txt'
166
- File.open(good,'w'){|f| f.write "foo" }
177
+ File.open(good,'w') { |f| f.write "foo" }
167
178
 
168
- assert_equal [good], FileUtils.chown('noone', 'nogroup', good, :verbose => true)
179
+ out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
180
+ assert_equal [good], out
169
181
  assert_raises(Errno::ENOENT) do
170
182
  FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
171
183
  end
@@ -183,19 +195,19 @@ class FakeFSTest < Test::Unit::TestCase
183
195
 
184
196
  def test_can_chown_R_files
185
197
  FileUtils.mkdir_p '/path/'
186
- File.open('/path/foo', 'w'){|f| f.write 'foo' }
187
- File.open('/path/foobar', 'w'){|f| f.write 'foo' }
198
+ File.open('/path/foo', 'w') { |f| f.write 'foo' }
199
+ File.open('/path/foobar', 'w') { |f| f.write 'foo' }
188
200
  resp = FileUtils.chown_R('no', 'no', '/path')
189
201
  assert_equal ['/path'], resp
190
202
  end
191
203
 
192
204
  def test_dir_globs_paths
193
205
  FileUtils.mkdir_p '/path'
194
- File.open('/path/foo', 'w'){|f| f.write 'foo' }
195
- File.open('/path/foobar', 'w'){|f| f.write 'foo' }
206
+ File.open('/path/foo', 'w') { |f| f.write 'foo' }
207
+ File.open('/path/foobar', 'w') { |f| f.write 'foo' }
196
208
 
197
209
  FileUtils.mkdir_p '/path/bar'
198
- File.open('/path/bar/baz', 'w'){|f| f.write 'foo' }
210
+ File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
199
211
 
200
212
  FileUtils.cp_r '/path/bar', '/path/bar2'
201
213
 
@@ -227,8 +239,8 @@ class FakeFSTest < Test::Unit::TestCase
227
239
  assert_equal '.', FileSystem.fs.name
228
240
  assert_equal({}, FileSystem.fs['path'])
229
241
  Dir.chdir '/path' do
230
- File.open('foo', 'w'){|f| f.write 'foo'}
231
- File.open('foobar', 'w'){|f| f.write 'foo'}
242
+ File.open('foo', 'w') { |f| f.write 'foo'}
243
+ File.open('foobar', 'w') { |f| f.write 'foo'}
232
244
  end
233
245
 
234
246
  assert_equal '.', FileSystem.fs.name
@@ -236,7 +248,7 @@ class FakeFSTest < Test::Unit::TestCase
236
248
 
237
249
  c = nil
238
250
  Dir.chdir '/path' do
239
- c = File.open('foo', 'r'){|f| f.read }
251
+ c = File.open('foo', 'r') { |f| f.read }
240
252
  end
241
253
 
242
254
  assert_equal 'foo', c
@@ -246,8 +258,8 @@ class FakeFSTest < Test::Unit::TestCase
246
258
  FileUtils.mkdir_p '/path'
247
259
 
248
260
  Dir.chdir '/path' do
249
- File.open('foo', 'w'){|f| f.write 'foo'}
250
- File.open('/foobar', 'w'){|f| f.write 'foo'}
261
+ File.open('foo', 'w') { |f| f.write 'foo'}
262
+ File.open('/foobar', 'w') { |f| f.write 'foo'}
251
263
  end
252
264
  assert_equal ['foo'], FileSystem.fs['path'].keys.sort
253
265
  assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
@@ -264,9 +276,9 @@ class FakeFSTest < Test::Unit::TestCase
264
276
  def test_chdir_should_be_nestable
265
277
  FileUtils.mkdir_p '/path/me'
266
278
  Dir.chdir '/path' do
267
- File.open('foo', 'w'){|f| f.write 'foo'}
279
+ File.open('foo', 'w') { |f| f.write 'foo'}
268
280
  Dir.chdir 'me' do
269
- File.open('foobar', 'w'){|f| f.write 'foo'}
281
+ File.open('foobar', 'w') { |f| f.write 'foo'}
270
282
  end
271
283
  end
272
284
 
@@ -286,8 +298,8 @@ class FakeFSTest < Test::Unit::TestCase
286
298
  FileUtils.mkdir_p '/path'
287
299
 
288
300
  Dir.chdir '/path' do
289
- File.open('foo', 'w'){|f| f.write 'foo'}
290
- File.open('foobar', 'w'){|f| f.write 'foo'}
301
+ File.open('foo', 'w') { |f| f.write 'foo'}
302
+ File.open('foobar', 'w') { |f| f.write 'foo'}
291
303
  end
292
304
 
293
305
  begin
@@ -315,7 +327,7 @@ class FakeFSTest < Test::Unit::TestCase
315
327
  FileUtils.mkdir_p 'subdir'
316
328
  assert_equal ['subdir'], FileSystem.current_dir.keys
317
329
  Dir.chdir('subdir')
318
- File.open('foo', 'w'){|f| f.write 'foo'}
330
+ File.open('foo', 'w') { |f| f.write 'foo'}
319
331
  assert_equal ['foo'], FileSystem.current_dir.keys
320
332
 
321
333
  assert_raises(Errno::ENOENT) do
@@ -340,12 +352,12 @@ class FakeFSTest < Test::Unit::TestCase
340
352
  end
341
353
 
342
354
  def test_file_open_defaults_to_read
343
- File.open('foo','w'){|f| f.write 'bar' }
344
- assert_equal 'bar', File.open('foo'){|f| f.read }
355
+ File.open('foo','w') { |f| f.write 'bar' }
356
+ assert_equal 'bar', File.open('foo') { |f| f.read }
345
357
  end
346
358
 
347
359
  def test_flush_exists_on_file
348
- r = File.open('foo','w'){|f| f.write 'bar'; f.flush }
360
+ r = File.open('foo','w') { |f| f.write 'bar'; f.flush }
349
361
  assert_equal 'foo', r.path
350
362
  end
351
363
 
@@ -356,9 +368,9 @@ class FakeFSTest < Test::Unit::TestCase
356
368
  end
357
369
 
358
370
  def test_mv_actually_works
359
- File.open('foo', 'w') {|f| f.write 'bar' }
371
+ File.open('foo', 'w') { |f| f.write 'bar' }
360
372
  FileUtils.mv 'foo', 'baz'
361
- assert_equal 'bar', File.open('baz'){|f| f.read }
373
+ assert_equal 'bar', File.open('baz') { |f| f.read }
362
374
  end
363
375
 
364
376
  def test_cp_actually_works
@@ -398,10 +410,10 @@ class FakeFSTest < Test::Unit::TestCase
398
410
  end
399
411
 
400
412
  def test_cp_r_doesnt_tangle_files_together
401
- File.open('foo', 'w') {|f| f.write 'bar' }
413
+ File.open('foo', 'w') { |f| f.write 'bar' }
402
414
  FileUtils.cp_r('foo', 'baz')
403
- File.open('baz', 'w') {|f| f.write 'quux' }
404
- assert_equal 'bar', File.open('foo'){|f| f.read }
415
+ File.open('baz', 'w') { |f| f.write 'quux' }
416
+ assert_equal 'bar', File.open('foo') { |f| f.read }
405
417
  end
406
418
 
407
419
  def test_cp_r_should_raise_error_on_missing_file
@@ -414,29 +426,29 @@ class FakeFSTest < Test::Unit::TestCase
414
426
 
415
427
  def test_cp_r_handles_copying_directories
416
428
  FileUtils.mkdir_p 'subdir'
417
- Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
429
+ Dir.chdir('subdir'){ File.open('foo', 'w') { |f| f.write 'footext' } }
418
430
 
419
431
  FileUtils.mkdir_p 'baz'
420
432
 
421
433
  # To a previously uncreated directory
422
434
  FileUtils.cp_r('subdir', 'quux')
423
- assert_equal 'footext', File.open('quux/foo'){|f| f.read }
435
+ assert_equal 'footext', File.open('quux/foo') { |f| f.read }
424
436
 
425
437
  # To a directory that already exists
426
438
  FileUtils.cp_r('subdir', 'baz')
427
- assert_equal 'footext', File.open('baz/subdir/foo'){|f| f.read }
439
+ assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
428
440
 
429
441
  # To a subdirectory of a directory that does not exist
430
- assert_raises(Errno::ENOENT) {
442
+ assert_raises(Errno::ENOENT) do
431
443
  FileUtils.cp_r('subdir', 'nope/something')
432
- }
444
+ end
433
445
  end
434
446
 
435
447
  def test_cp_r_only_copies_into_directories
436
448
  FileUtils.mkdir_p 'subdir'
437
- Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
449
+ Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
438
450
 
439
- File.open('bar', 'w') {|f| f.write 'bartext' }
451
+ File.open('bar', 'w') { |f| f.write 'bartext' }
440
452
 
441
453
  assert_raises(Errno::EEXIST) do
442
454
  FileUtils.cp_r 'subdir', 'bar'
@@ -446,13 +458,13 @@ class FakeFSTest < Test::Unit::TestCase
446
458
  FileUtils.ln_s 'otherdir', 'symdir'
447
459
 
448
460
  FileUtils.cp_r 'subdir', 'symdir'
449
- assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
461
+ assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
450
462
  end
451
463
 
452
464
  def test_cp_r_sets_parent_correctly
453
465
  FileUtils.mkdir_p '/path/foo'
454
- File.open('/path/foo/bar', 'w'){|f| f.write 'foo' }
455
- File.open('/path/foo/baz', 'w'){|f| f.write 'foo' }
466
+ File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
467
+ File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
456
468
 
457
469
  FileUtils.cp_r '/path/foo', '/path/bar'
458
470
 
@@ -462,10 +474,10 @@ class FakeFSTest < Test::Unit::TestCase
462
474
  end
463
475
 
464
476
  def test_clone_clones_normal_files
465
- RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
477
+ RealFile.open(here('foo'), 'w') { |f| f.write 'bar' }
466
478
  assert !File.exists?(here('foo'))
467
479
  FileSystem.clone(here('foo'))
468
- assert_equal 'bar', File.open(here('foo')){|f| f.read }
480
+ assert_equal 'bar', File.open(here('foo')) { |f| f.read }
469
481
  ensure
470
482
  RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
471
483
  end
@@ -494,22 +506,22 @@ class FakeFSTest < Test::Unit::TestCase
494
506
 
495
507
  def test_putting_a_dot_at_end_copies_the_contents
496
508
  FileUtils.mkdir_p 'subdir'
497
- Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
509
+ Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
498
510
 
499
511
  FileUtils.mkdir_p 'newdir'
500
512
  FileUtils.cp_r 'subdir/.', 'newdir'
501
- assert_equal 'footext', File.open('newdir/foo'){|f| f.read }
513
+ assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
502
514
  end
503
515
 
504
516
  def test_file_can_read_from_symlinks
505
- File.open('first', 'w'){|f| f.write '1'}
517
+ File.open('first', 'w') { |f| f.write '1'}
506
518
  FileUtils.ln_s 'first', 'one'
507
- assert_equal '1', File.open('one'){|f| f.read }
519
+ assert_equal '1', File.open('one') { |f| f.read }
508
520
 
509
521
  FileUtils.mkdir_p 'subdir'
510
- File.open('subdir/nother','w'){|f| f.write 'works' }
522
+ File.open('subdir/nother','w') { |f| f.write 'works' }
511
523
  FileUtils.ln_s 'subdir', 'new'
512
- assert_equal 'works', File.open('new/nother'){|f| f.read }
524
+ assert_equal 'works', File.open('new/nother') { |f| f.read }
513
525
  end
514
526
 
515
527
  def test_files_can_be_touched
@@ -521,15 +533,291 @@ class FakeFSTest < Test::Unit::TestCase
521
533
  end
522
534
 
523
535
  def test_touch_does_not_work_if_the_dir_path_cannot_be_found
524
- assert_raises(Errno::ENOENT) {
536
+ assert_raises(Errno::ENOENT) do
525
537
  FileUtils.touch('this/path/should/not/be/here')
526
- }
538
+ end
527
539
  FileUtils.mkdir_p('subdir')
528
540
  list = ['subdir/foo', 'nosubdir/bar']
529
541
 
530
- assert_raises(Errno::ENOENT) {
542
+ assert_raises(Errno::ENOENT) do
531
543
  FileUtils.touch(list)
532
- }
544
+ end
545
+ end
546
+
547
+ def test_extname
548
+ assert File.extname("test.doc") == ".doc"
549
+ end
550
+
551
+ # Directory tests
552
+ def test_new_directory
553
+ FileUtils.mkdir_p('/this/path/should/be/here')
554
+
555
+ assert_nothing_raised do
556
+ Dir.new('/this/path/should/be/here')
557
+ end
558
+ end
559
+
560
+ def test_new_directory_does_not_work_if_dir_path_cannot_be_found
561
+ assert_raises(Errno::ENOENT) do
562
+ Dir.new('/this/path/should/not/be/here')
563
+ end
564
+ end
565
+
566
+ def test_directory_close
567
+ FileUtils.mkdir_p('/this/path/should/be/here')
568
+ dir = Dir.new('/this/path/should/be/here')
569
+ assert dir.close.nil?
570
+
571
+ assert_raises(IOError) do
572
+ dir.each { |dir| dir }
573
+ end
574
+ end
575
+
576
+ def test_directory_each
577
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
578
+
579
+ FileUtils.mkdir_p('/this/path/should/be/here')
580
+
581
+ test.each do |f|
582
+ FileUtils.touch("/this/path/should/be/here/#{f}")
583
+ end
584
+
585
+ dir = Dir.new('/this/path/should/be/here')
586
+
587
+ yielded = []
588
+ dir.each do |dir|
589
+ yielded << dir
590
+ end
591
+
592
+ assert yielded.size == test.size
593
+ test.each { |t| assert yielded.include?(t) }
594
+ end
595
+
596
+ def test_directory_path
597
+ FileUtils.mkdir_p('/this/path/should/be/here')
598
+ good_path = '/this/path/should/be/here'
599
+ assert_equal good_path, Dir.new('/this/path/should/be/here').path
600
+ end
601
+
602
+ def test_directory_pos
603
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
604
+ FileUtils.mkdir_p('/this/path/should/be/here')
605
+ test.each do |f|
606
+ FileUtils.touch("/this/path/should/be/here/#{f}")
607
+ end
608
+
609
+ dir = Dir.new('/this/path/should/be/here')
610
+
611
+ assert dir.pos == 0
612
+ dir.read
613
+ assert dir.pos == 1
614
+ dir.read
615
+ assert dir.pos == 2
616
+ dir.read
617
+ assert dir.pos == 3
618
+ dir.read
619
+ assert dir.pos == 4
620
+ dir.read
621
+ assert dir.pos == 5
622
+ end
623
+
624
+ def test_directory_pos_assign
625
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
626
+
627
+ FileUtils.mkdir_p('/this/path/should/be/here')
628
+ test.each do |f|
629
+ FileUtils.touch("/this/path/should/be/here/#{f}")
630
+ end
631
+
632
+ dir = Dir.new('/this/path/should/be/here')
633
+
634
+ assert dir.pos == 0
635
+ dir.pos = 2
636
+ assert dir.pos == 2
637
+ end
638
+
639
+ def test_directory_read
640
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
641
+
642
+ FileUtils.mkdir_p('/this/path/should/be/here')
643
+ test.each do |f|
644
+ FileUtils.touch("/this/path/should/be/here/#{f}")
645
+ end
646
+
647
+ dir = Dir.new('/this/path/should/be/here')
648
+
649
+ assert dir.pos == 0
650
+ d = dir.read
651
+ assert dir.pos == 1
652
+ assert d == '.'
653
+
654
+ d = dir.read
655
+ assert dir.pos == 2
656
+ assert d == '..'
657
+ end
658
+
659
+ def test_directory_read_past_length
660
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
661
+
662
+ FileUtils.mkdir_p('/this/path/should/be/here')
663
+ test.each do |f|
664
+ FileUtils.touch("/this/path/should/be/here/#{f}")
665
+ end
666
+
667
+ dir = Dir.new('/this/path/should/be/here')
668
+
669
+ d = dir.read
670
+ assert_not_nil d
671
+ d = dir.read
672
+ assert_not_nil d
673
+ d = dir.read
674
+ assert_not_nil d
675
+ d = dir.read
676
+ assert_not_nil d
677
+ d = dir.read
678
+ assert_not_nil d
679
+ d = dir.read
680
+ assert_not_nil d
681
+ d = dir.read
682
+ assert_not_nil d
683
+ d = dir.read
684
+ assert_nil d
685
+ end
686
+
687
+ def test_directory_rewind
688
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
689
+
690
+ FileUtils.mkdir_p('/this/path/should/be/here')
691
+ test.each do |f|
692
+ FileUtils.touch("/this/path/should/be/here/#{f}")
693
+ end
694
+
695
+ dir = Dir.new('/this/path/should/be/here')
696
+
697
+ d = dir.read
698
+ d = dir.read
699
+ assert dir.pos == 2
700
+ dir.rewind
701
+ assert dir.pos == 0
702
+ end
703
+
704
+ def test_directory_seek
705
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
706
+
707
+ FileUtils.mkdir_p('/this/path/should/be/here')
708
+ test.each do |f|
709
+ FileUtils.touch("/this/path/should/be/here/#{f}")
710
+ end
711
+
712
+ dir = Dir.new('/this/path/should/be/here')
713
+
714
+ d = dir.seek 1
715
+ assert d == '..'
716
+ assert dir.pos == 1
717
+ end
718
+
719
+ def test_directory_class_delete
720
+ FileUtils.mkdir_p('/this/path/should/be/here')
721
+ Dir.delete('/this/path/should/be/here')
722
+ assert File.exists?('/this/path/should/be/here') == false
723
+ end
724
+
725
+ def test_directory_class_delete_does_not_act_on_non_empty_directory
726
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
727
+
728
+ FileUtils.mkdir_p('/this/path/should/be/here')
729
+ test.each do |f|
730
+ FileUtils.touch("/this/path/should/be/here/#{f}")
731
+ end
732
+
733
+ assert_raises(SystemCallError) do
734
+ Dir.delete('/this/path/should/be/here')
735
+ end
736
+ end
737
+
738
+ def test_directory_entries
739
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
740
+
741
+ FileUtils.mkdir_p('/this/path/should/be/here')
742
+
743
+ test.each do |f|
744
+ FileUtils.touch("/this/path/should/be/here/#{f}")
745
+ end
746
+
747
+ yielded = Dir.entries('/this/path/should/be/here')
748
+ assert yielded.size == test.size
749
+ test.each { |t| assert yielded.include?(t) }
750
+ end
751
+
752
+ def test_directory_foreach
753
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
754
+
755
+ FileUtils.mkdir_p('/this/path/should/be/here')
756
+
757
+ test.each do |f|
758
+ FileUtils.touch("/this/path/should/be/here/#{f}")
759
+ end
760
+
761
+ yielded = []
762
+ Dir.foreach('/this/path/should/be/here') do |dir|
763
+ yielded << dir
764
+ end
765
+
766
+ assert yielded.size == test.size
767
+ test.each { |t| assert yielded.include?(t) }
768
+ end
769
+
770
+ def test_directory_mkdir
771
+ Dir.mkdir('/path')
772
+ assert File.exists?('/path')
773
+ end
774
+
775
+ def test_directory_mkdir_relative
776
+ FileUtils.mkdir_p('/new/root')
777
+ FileSystem.chdir('/new/root')
778
+ Dir.mkdir('path')
779
+ assert File.exists?('/new/root/path')
780
+ end
781
+
782
+ def test_directory_mkdir_not_recursive
783
+ assert_raises(Errno::ENOENT) do
784
+ Dir.mkdir('/path/does/not/exist')
785
+ end
786
+ end
787
+
788
+ def test_directory_open
789
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
790
+
791
+ FileUtils.mkdir_p('/this/path/should/be/here')
792
+
793
+ test.each do |f|
794
+ FileUtils.touch("/this/path/should/be/here/#{f}")
795
+ end
796
+
797
+ dir = Dir.open('/this/path/should/be/here')
798
+ assert dir.path == '/this/path/should/be/here'
799
+ end
800
+
801
+ def test_directory_open_block
802
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
803
+
804
+ FileUtils.mkdir_p('/this/path/should/be/here')
805
+
806
+ test.each do |f|
807
+ FileUtils.touch("/this/path/should/be/here/#{f}")
808
+ end
809
+
810
+ yielded = []
811
+ Dir.open('/this/path/should/be/here') do |dir|
812
+ yielded << dir
813
+ end
814
+
815
+ assert yielded.size == test.size
816
+ test.each { |t| assert yielded.include?(t) }
817
+ end
818
+
819
+ def test_tmpdir
820
+ assert Dir.tmpdir == "/tmp"
533
821
  end
534
822
 
535
823
  def here(fname)
data/test/safe_test.rb CHANGED
@@ -17,4 +17,13 @@ class FakeFSSafeTest < Test::Unit::TestCase
17
17
 
18
18
  assert ! File.exists?(path)
19
19
  end
20
+
21
+ def test_FakeFS_method_returns_value_of_yield
22
+ result = FakeFS do
23
+ File.open('myfile.txt', 'w') { |f| f.write "Yatta!" }
24
+ File.read('myfile.txt')
25
+ end
26
+
27
+ assert_equal result, "Yatta!"
28
+ end
20
29
  end
metadata CHANGED
@@ -1,20 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmabey-fakefs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
8
- - Pat Nakajima
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
11
 
13
- date: 2009-08-10 00:00:00 -07:00
12
+ date: 2009-09-23 00:00:00 -07:00
14
13
  default_executable:
15
14
  dependencies: []
16
15
 
17
- description:
16
+ description: A fake filesystem. Use it in your tests.
18
17
  email: chris@ozmm.org
19
18
  executables: []
20
19
 
@@ -28,7 +27,6 @@ files:
28
27
  - LICENSE
29
28
  - README.markdown
30
29
  - Rakefile
31
- - VERSION
32
30
  - fakefs.gemspec
33
31
  - lib/fakefs.rb
34
32
  - lib/fakefs/base.rb
@@ -41,11 +39,12 @@ files:
41
39
  - lib/fakefs/fileutils.rb
42
40
  - lib/fakefs/safe.rb
43
41
  - lib/fakefs/spec_helpers.rb
42
+ - lib/fakefs/version.rb
44
43
  - test/fakefs_test.rb
45
44
  - test/safe_test.rb
46
45
  - test/verify.rb
47
46
  has_rdoc: false
48
- homepage: http://ozmm.org/posts/fakefs.html
47
+ homepage: http://github.com/defunkt/fakefs
49
48
  licenses:
50
49
  post_install_message:
51
50
  rdoc_options:
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.1