Revert "start adding basic tests for fakefs"

This reverts commit f451efdf2d.
This commit is contained in:
Chris Wanstrath 2009-05-29 09:48:28 -07:00
parent f451efdf2d
commit c1973ebcb3
2 changed files with 50 additions and 82 deletions

View File

@ -1,6 +1,4 @@
require 'fileutils' module FakeFS
class FakeFS
module FileUtils module FileUtils
extend self extend self
@ -43,17 +41,13 @@ class FakeFS
end end
def self.expand_path(path) def self.expand_path(path)
RealFile.expand_path(path) ::File.expand_path(path)
end end
def self.readlink(path) def self.readlink(path)
symlink = FileSystem.find(path) symlink = FileSystem.find(path)
FileSystem.find(symlink.target).to_s FileSystem.find(symlink.target).to_s
end end
def self.dirname(path)
RealFile.dirname(path)
end
end end
class Dir class Dir
@ -66,6 +60,54 @@ class FakeFS
end end
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 class MockDir < Hash
attr_accessor :name, :parent attr_accessor :name, :parent
@ -75,10 +117,6 @@ class FakeFS
@parent = parent @parent = parent
end end
def files
values
end
def entry def entry
self self
end end
@ -112,63 +150,8 @@ class FakeFS
entry.send(*args, &block) entry.send(*args, &block)
end end
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 end
RealFile = File
RealFileUtils = FileUtils
RealDir = Dir
Object.class_eval do Object.class_eval do
remove_const(:Dir) remove_const(:Dir)
remove_const(:File) remove_const(:File)

View File

@ -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