fakefs/lib/fakefs/base.rb
msassak 0da41d92a7 Return result of yield from FakeFS block
Signed-off-by: Chris Wanstrath <chris@ozmm.org>
2009-09-23 18:46:58 +08:00

39 lines
773 B
Ruby

RealFile = File
RealFileUtils = FileUtils
RealDir = Dir
RealFileUtils::Dir = RealDir
RealFileUtils::File = RealFile
module FakeFS
def self.activate!
Object.class_eval do
remove_const(:Dir)
remove_const(:File)
remove_const(:FileUtils)
const_set(:Dir, FakeFS::Dir)
const_set(:File, FakeFS::File)
const_set(:FileUtils, FakeFS::FileUtils)
end
end
def self.deactivate!
Object.class_eval do
remove_const(:Dir)
remove_const(:File)
remove_const(:FileUtils)
const_set(:Dir, RealDir)
const_set(:File, RealFile)
const_set(:FileUtils, RealFileUtils)
end
end
end
def FakeFS
return ::FakeFS unless block_given?
::FakeFS.activate!
result = yield
::FakeFS.deactivate!
result
end