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 'fileutils'
require 'pathname' require 'pathname'
require 'fakefs/base' require 'fakefs/base'
require 'fakefs/mock/file' require 'fakefs/fake/file'
require 'fakefs/mock/dir' require 'fakefs/fake/dir'
require 'fakefs/mock/symlink' require 'fakefs/fake/symlink'
require 'fakefs/file_system' require 'fakefs/file_system'
require 'fakefs/fileutils' require 'fakefs/fileutils'
require 'fakefs/file' require 'fakefs/file'

View File

@ -1,5 +1,5 @@
module FakeFS module FakeFS
class MockDir < Hash class FakeDir < Hash
attr_accessor :name, :parent attr_accessor :name, :parent
def initialize(name = nil, parent = nil) def initialize(name = nil, parent = nil)
@ -12,7 +12,7 @@ module FakeFS
end end
def inspect 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 end
def clone(parent = nil) def clone(parent = nil)
@ -34,4 +34,4 @@ module FakeFS
end end
end end
end end
end end

View File

@ -1,5 +1,5 @@
module FakeFS module FakeFS
class MockFile class FakeFile
attr_accessor :name, :parent, :content attr_accessor :name, :parent, :content
def initialize(name = nil, parent = nil) def initialize(name = nil, parent = nil)
@ -19,11 +19,11 @@ module FakeFS
end end
def inspect 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 end
def to_s def to_s
File.join(parent.to_s, name) File.join(parent.to_s, name)
end end
end end
end end

View File

@ -1,5 +1,5 @@
module FakeFS module FakeFS
class MockSymlink class FakeSymlink
attr_accessor :name, :target attr_accessor :name, :target
alias_method :to_s, :name alias_method :to_s, :name
@ -19,4 +19,4 @@ module FakeFS
entry.send(*args, &block) entry.send(*args, &block)
end end
end end
end end

View File

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

View File

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

View File

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

View File

@ -23,7 +23,7 @@ class FakeFSTest < Test::Unit::TestCase
def test_can_create_directories def test_can_create_directories
FileUtils.mkdir_p("/path/to/dir") 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 end
def test_knows_directories_exist def test_knows_directories_exist
@ -57,7 +57,7 @@ class FakeFSTest < Test::Unit::TestCase
def test_can_create_symlinks def test_can_create_symlinks
FileUtils.mkdir_p(target = "/path/to/target") FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, "/path/to/link") 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) { assert_raises(Errno::EEXIST) {
FileUtils.ln_s(target, '/path/to/link') FileUtils.ln_s(target, '/path/to/link')