From c1973ebcb3c054d9afa2ba14a5a0e28dba858bfa Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Fri, 29 May 2009 09:48:28 -0700 Subject: [PATCH] Revert "start adding basic tests for fakefs" This reverts commit f451efdf2d426d06e78caab17c8de1ecc647d109. --- lib/fakefs.rb | 117 +++++++++++++++++++++--------------------------- test/fake_fs.rb | 15 ------- 2 files changed, 50 insertions(+), 82 deletions(-) delete mode 100644 test/fake_fs.rb diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 5d8d535..e1b2d34 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -1,6 +1,4 @@ -require 'fileutils' - -class FakeFS +module FakeFS module FileUtils extend self @@ -43,17 +41,13 @@ class FakeFS end def self.expand_path(path) - RealFile.expand_path(path) + ::File.expand_path(path) end def self.readlink(path) symlink = FileSystem.find(path) FileSystem.find(symlink.target).to_s end - - def self.dirname(path) - RealFile.dirname(path) - end end class Dir @@ -66,6 +60,54 @@ class FakeFS end end + module FileSystem + extend self + + def fs + @fs ||= MockDir.new('.') + end + + def clear + @fs = nil + end + + def find(path) + parts = path_parts(path) + + target = parts[0...-1].inject(fs) do |dir, part| + dir[part] || {} + end + + case parts.last + when '*' + target.values + else + target[parts.last] + end + end + + def add(path, object) + parts = path_parts(path) + + d = parts[0...-1].inject(fs) do |dir, part| + dir[part] ||= MockDir.new(part, dir) + end + + object.name = parts.last + object.parent = d + d[parts.last] = object + end + + def delete(path) + if dir = FileSystem.find(path) + dir.parent.delete(dir.name) + end + end + + def path_parts(path) + path.split(File::PATH_SEPARATOR) + end + end class MockDir < Hash attr_accessor :name, :parent @@ -75,10 +117,6 @@ class FakeFS @parent = parent end - def files - values - end - def entry self end @@ -112,63 +150,8 @@ class FakeFS entry.send(*args, &block) end end - - def fs - @fs ||= MockDir.new('.') - end - - def clear - @fs = nil - end - - def find(path) - parts = path_parts(path) - - target = parts[0...-1].inject(fs) do |dir, part| - dir[part] || {} - end - - case parts.last - when '*' - target.values - else - target[parts.last] - end - end - - def add(path, object) - parts = path_parts(path) - - d = parts[0...-1].inject(fs) do |dir, part| - dir[part] ||= MockDir.new(part, dir) - end - - object.name = parts.last - object.parent = d - d[parts.last] = object - end - - def delete(path) - if dir = FileSystem.find(path) - dir.parent.delete(dir.name) - end - end - - def path_parts(path) - path.split(File::PATH_SEPARATOR) - end - - def method_missing(*args, &block) - fs.send(*args, &block) - end - - FileSystem = FakeFS.new('.') end -RealFile = File -RealFileUtils = FileUtils -RealDir = Dir - Object.class_eval do remove_const(:Dir) remove_const(:File) diff --git a/test/fake_fs.rb b/test/fake_fs.rb deleted file mode 100644 index d3fe3ec..0000000 --- a/test/fake_fs.rb +++ /dev/null @@ -1,15 +0,0 @@ -$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') -require 'fakefs' -require 'test/unit' - -class FakeFSTest < Test::Unit::TestCase - def test_can_be_initialized_empty - fs = FakeFS.new('.') - assert_equal 0, fs.files.size - end - - def test_can_be_initialized_with_an_existing_directory - fs = FakeFS.new(File.expand_path(File.dirname(__FILE__))) - assert_equal 1, fs.files.size - end -end