Mock{Dir, File, Symlink} => Fake{Dir, File, Symlink}

This commit is contained in:
Chris Wanstrath 2009-07-27 00:09:06 -07:00
parent 8dfbb9eece
commit ffeb6516f5
8 changed files with 27 additions and 27 deletions

View File

@ -1,9 +1,9 @@
require 'fileutils'
require 'pathname'
require 'fakefs/base'
require 'fakefs/mock/file'
require 'fakefs/mock/dir'
require 'fakefs/mock/symlink'
require 'fakefs/fake/file'
require 'fakefs/fake/dir'
require 'fakefs/fake/symlink'
require 'fakefs/file_system'
require 'fakefs/fileutils'
require 'fakefs/file'

View File

@ -1,5 +1,5 @@
module FakeFS
class MockDir < Hash
class FakeDir < Hash
attr_accessor :name, :parent
def initialize(name = nil, parent = nil)
@ -12,7 +12,7 @@ module FakeFS
end
def inspect
"(MockDir name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{size})"
"(FakeDir name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{size})"
end
def clone(parent = nil)

View File

@ -1,5 +1,5 @@
module FakeFS
class MockFile
class FakeFile
attr_accessor :name, :parent, :content
def initialize(name = nil, parent = nil)
@ -19,7 +19,7 @@ module FakeFS
end
def inspect
"(MockFile name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{content.size})"
"(FakeFile name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{content.size})"
end
def to_s

View File

@ -1,5 +1,5 @@
module FakeFS
class MockSymlink
class FakeSymlink
attr_accessor :name, :target
alias_method :to_s, :name

View File

@ -20,27 +20,27 @@ module FakeFS
def self.directory?(path)
if path.respond_to? :entry
path.entry.is_a? MockDir
path.entry.is_a? FakeDir
else
result = FileSystem.find(path)
result ? result.entry.is_a?(MockDir) : false
result ? result.entry.is_a?(FakeDir) : false
end
end
def self.symlink?(path)
if path.respond_to? :entry
path.is_a? MockSymlink
path.is_a? FakeSymlink
else
FileSystem.find(path).is_a? MockSymlink
FileSystem.find(path).is_a? FakeSymlink
end
end
def self.file?(path)
if path.respond_to? :entry
path.entry.is_a? MockFile
path.entry.is_a? FakeFile
else
result = FileSystem.find(path)
result ? result.entry.is_a?(MockFile) : false
result ? result.entry.is_a?(FakeFile) : false
end
end
@ -111,7 +111,7 @@ module FakeFS
raise IOError.new('closed stream') unless @open
if !File.exists?(@path)
@file = FileSystem.add(path, MockFile.new)
@file = FileSystem.add(path, FakeFile.new)
end
@file.content += content

View File

@ -7,7 +7,7 @@ module FakeFS
end
def fs
@fs ||= MockDir.new('.')
@fs ||= FakeDir.new('.')
end
def clear
@ -34,11 +34,11 @@ module FakeFS
end
end
def add(path, object=MockDir.new)
def add(path, object=FakeDir.new)
parts = path_parts(normalize_path(path))
d = parts[0...-1].inject(fs) do |dir, part|
dir[part] ||= MockDir.new(part, dir)
dir[part] ||= FakeDir.new(part, dir)
end
object.name = parts.last

View File

@ -3,7 +3,7 @@ module FakeFS
extend self
def mkdir_p(path)
FileSystem.add(path, MockDir.new)
FileSystem.add(path, FakeDir.new)
end
def rm(path)
@ -14,7 +14,7 @@ module FakeFS
def ln_s(target, path)
raise Errno::EEXIST, path if FileSystem.find(path)
FileSystem.add(path, MockSymlink.new(target))
FileSystem.add(path, FakeSymlink.new(target))
end
def cp(src, dest)
@ -93,7 +93,7 @@ module FakeFS
directory = File.dirname(f)
# FIXME this explicit check for '.' shouldn't need to happen
if File.exists?(directory) || directory == '.'
FileSystem.add(f, MockFile.new)
FileSystem.add(f, FakeFile.new)
else
raise Errno::ENOENT, f
end

View File

@ -23,7 +23,7 @@ class FakeFSTest < Test::Unit::TestCase
def test_can_create_directories
FileUtils.mkdir_p("/path/to/dir")
assert_kind_of MockDir, FileSystem.fs['path']['to']['dir']
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_knows_directories_exist
@ -57,7 +57,7 @@ class FakeFSTest < Test::Unit::TestCase
def test_can_create_symlinks
FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, "/path/to/link")
assert_kind_of MockSymlink, FileSystem.fs['path']['to']['link']
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
assert_raises(Errno::EEXIST) {
FileUtils.ln_s(target, '/path/to/link')