2009-05-29 17:23:39 +00:00
|
|
|
require 'fileutils'
|
2009-05-31 04:12:40 +00:00
|
|
|
require 'pathname'
|
2009-05-29 17:23:39 +00:00
|
|
|
|
|
|
|
RealFile = File
|
|
|
|
RealFileUtils = FileUtils
|
|
|
|
RealDir = Dir
|
2009-06-02 06:54:04 +00:00
|
|
|
RealFileUtils::Dir = RealDir
|
|
|
|
RealFileUtils::File = RealFile
|
2009-05-29 17:23:39 +00:00
|
|
|
|
2009-05-29 16:48:28 +00:00
|
|
|
module FakeFS
|
2009-05-29 08:12:10 +00:00
|
|
|
module FileUtils
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def mkdir_p(path)
|
|
|
|
FileSystem.add(path, MockDir.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm(path)
|
|
|
|
FileSystem.delete(path)
|
|
|
|
end
|
|
|
|
alias_method :rm_rf, :rm
|
|
|
|
|
|
|
|
def ln_s(target, path)
|
2009-06-01 10:52:08 +00:00
|
|
|
raise Errno::EEXIST, path if FileSystem.find(path)
|
2009-05-29 08:12:10 +00:00
|
|
|
FileSystem.add(path, MockSymlink.new(target))
|
|
|
|
end
|
|
|
|
|
2009-06-04 00:53:07 +00:00
|
|
|
def cp(src, dest)
|
|
|
|
dst_file = FileSystem.find(dest)
|
|
|
|
src_file = FileSystem.find(src)
|
|
|
|
|
|
|
|
if !src_file
|
|
|
|
raise Errno::ENOENT, src
|
|
|
|
end
|
|
|
|
|
|
|
|
if File.directory? src_file
|
|
|
|
raise Errno::EISDIR, src
|
|
|
|
end
|
|
|
|
|
2009-06-11 22:11:44 +00:00
|
|
|
if dst_file and File.directory?(dst_file)
|
2009-07-19 21:16:21 +00:00
|
|
|
FileSystem.add(File.join(dest, src), src_file.entry.clone(dest))
|
2009-06-11 22:11:44 +00:00
|
|
|
else
|
|
|
|
FileSystem.delete(dest)
|
2009-07-19 21:16:21 +00:00
|
|
|
FileSystem.add(dest, src_file.entry.clone(dest))
|
2009-06-11 22:11:44 +00:00
|
|
|
end
|
2009-06-04 00:53:07 +00:00
|
|
|
end
|
|
|
|
|
2009-05-29 08:12:10 +00:00
|
|
|
def cp_r(src, dest)
|
2009-06-01 10:52:08 +00:00
|
|
|
# This error sucks, but it conforms to the original Ruby
|
|
|
|
# method.
|
|
|
|
raise "unknown file type: #{src}" unless dir = FileSystem.find(src)
|
2009-06-04 00:53:07 +00:00
|
|
|
|
2009-06-01 10:52:08 +00:00
|
|
|
new_dir = FileSystem.find(dest)
|
|
|
|
|
|
|
|
if new_dir && !File.directory?(dest)
|
|
|
|
raise Errno::EEXIST, dest
|
|
|
|
end
|
2009-06-04 00:53:07 +00:00
|
|
|
|
2009-06-01 10:52:08 +00:00
|
|
|
if !new_dir && !FileSystem.find(dest+'/../')
|
|
|
|
raise Errno::ENOENT, dest
|
|
|
|
end
|
|
|
|
|
2009-06-01 11:13:53 +00:00
|
|
|
# This last bit is a total abuse and should be thought hard
|
|
|
|
# about and cleaned up.
|
2009-06-01 10:52:08 +00:00
|
|
|
if new_dir
|
2009-06-01 11:13:53 +00:00
|
|
|
if src[-2..-1] == '/.'
|
|
|
|
dir.values.each{|f| new_dir[f.name] = f }
|
|
|
|
else
|
2009-07-19 21:16:21 +00:00
|
|
|
new_dir[dir.name] = dir.entry.clone(new_dir)
|
2009-06-01 11:13:53 +00:00
|
|
|
end
|
2009-05-31 06:40:45 +00:00
|
|
|
else
|
2009-07-19 21:16:21 +00:00
|
|
|
FileSystem.add(dest, dir.entry.clone(dest))
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mv(src, dest)
|
|
|
|
if target = FileSystem.find(src)
|
2009-07-19 21:16:21 +00:00
|
|
|
FileSystem.add(dest, target.entry.clone(dest))
|
2009-05-29 08:12:10 +00:00
|
|
|
FileSystem.delete(src)
|
2009-05-31 05:04:17 +00:00
|
|
|
else
|
|
|
|
raise Errno::ENOENT, src
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
end
|
2009-05-30 17:56:06 +00:00
|
|
|
|
|
|
|
def chown(user, group, list, options={})
|
|
|
|
list = Array(list)
|
|
|
|
list.each do |f|
|
|
|
|
unless File.exists?(f)
|
|
|
|
raise Errno::ENOENT, f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
|
|
|
def chown_R(user, group, list, options={})
|
|
|
|
chown(user, group, list, options={})
|
|
|
|
end
|
2009-07-14 07:08:58 +00:00
|
|
|
|
2009-06-14 05:29:00 +00:00
|
|
|
def touch(list, options={})
|
2009-06-14 06:56:14 +00:00
|
|
|
Array(list).each do |f|
|
2009-06-14 06:52:22 +00:00
|
|
|
directory = File.dirname(f)
|
|
|
|
# FIXME this explicit check for '.' shouldn't need to happen
|
|
|
|
if File.exists?(directory) || directory == '.'
|
2009-06-14 05:29:00 +00:00
|
|
|
FileSystem.add(f, MockFile.new)
|
2009-06-14 06:52:22 +00:00
|
|
|
else
|
|
|
|
raise Errno::ENOENT, f
|
2009-06-14 05:29:00 +00:00
|
|
|
end
|
|
|
|
end
|
2009-06-13 07:22:37 +00:00
|
|
|
end
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class File
|
|
|
|
PATH_SEPARATOR = '/'
|
|
|
|
|
|
|
|
def self.join(*parts)
|
|
|
|
parts * PATH_SEPARATOR
|
|
|
|
end
|
|
|
|
|
2009-07-14 07:08:58 +00:00
|
|
|
def self.exist?(path)
|
2009-05-29 18:00:44 +00:00
|
|
|
FileSystem.find(path) || false
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
|
2009-07-14 07:08:58 +00:00
|
|
|
class << self
|
|
|
|
alias_method :exists?, :exist?
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def self.directory?(path)
|
2009-06-04 00:53:07 +00:00
|
|
|
if path.respond_to? :entry
|
|
|
|
path.entry.is_a? MockDir
|
|
|
|
else
|
2009-06-12 22:22:48 +00:00
|
|
|
result = FileSystem.find(path)
|
|
|
|
result ? result.entry.is_a?(MockDir) : false
|
2009-06-04 00:53:07 +00:00
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.symlink?(path)
|
2009-06-04 00:53:07 +00:00
|
|
|
if path.respond_to? :entry
|
|
|
|
path.is_a? MockSymlink
|
|
|
|
else
|
|
|
|
FileSystem.find(path).is_a? MockSymlink
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.file?(path)
|
2009-06-04 00:53:07 +00:00
|
|
|
if path.respond_to? :entry
|
|
|
|
path.entry.is_a? MockFile
|
|
|
|
else
|
2009-06-12 21:54:32 +00:00
|
|
|
result = FileSystem.find(path)
|
2009-06-12 22:16:16 +00:00
|
|
|
result ? result.entry.is_a?(MockFile) : false
|
2009-06-04 00:53:07 +00:00
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.expand_path(*args)
|
|
|
|
RealFile.expand_path(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.basename(*args)
|
|
|
|
RealFile.basename(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.dirname(path)
|
|
|
|
RealFile.dirname(path)
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.readlink(path)
|
|
|
|
symlink = FileSystem.find(path)
|
|
|
|
FileSystem.find(symlink.target).to_s
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
|
2009-05-30 18:31:19 +00:00
|
|
|
def self.open(path, mode='r')
|
2009-05-29 17:23:39 +00:00
|
|
|
if block_given?
|
|
|
|
yield new(path, mode)
|
|
|
|
else
|
|
|
|
new(path, mode)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-29 18:42:39 +00:00
|
|
|
def self.read(path)
|
2009-06-04 00:47:10 +00:00
|
|
|
file = new(path)
|
|
|
|
if file.exists?
|
|
|
|
file.read
|
|
|
|
else
|
|
|
|
raise Errno::ENOENT
|
|
|
|
end
|
2009-05-29 18:42:39 +00:00
|
|
|
end
|
|
|
|
|
2009-05-29 18:46:49 +00:00
|
|
|
def self.readlines(path)
|
|
|
|
read(path).split("\n")
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
attr_reader :path
|
2009-05-29 18:42:39 +00:00
|
|
|
def initialize(path, mode = nil)
|
2009-05-29 17:23:39 +00:00
|
|
|
@path = path
|
|
|
|
@mode = mode
|
2009-05-29 18:42:39 +00:00
|
|
|
@file = FileSystem.find(path)
|
2009-07-16 20:19:03 +00:00
|
|
|
@open = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@open = false
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
2009-07-16 20:19:03 +00:00
|
|
|
raise IOError.new('closed stream') unless @open
|
2009-05-29 18:42:39 +00:00
|
|
|
@file.content
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
2009-06-04 00:47:10 +00:00
|
|
|
def exists?
|
|
|
|
@file
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def puts(content)
|
|
|
|
write(content + "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(content)
|
2009-07-16 20:19:03 +00:00
|
|
|
raise IOError.new('closed stream') unless @open
|
|
|
|
|
2009-05-29 18:42:39 +00:00
|
|
|
if !File.exists?(@path)
|
|
|
|
@file = FileSystem.add(path, MockFile.new)
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
|
2009-05-29 18:42:39 +00:00
|
|
|
@file.content += content
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
alias_method :print, :write
|
2009-07-16 20:19:03 +00:00
|
|
|
alias_method :<<, :write
|
2009-05-31 04:22:57 +00:00
|
|
|
|
|
|
|
def flush; self; end
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class Dir
|
|
|
|
def self.glob(pattern)
|
2009-05-30 17:56:06 +00:00
|
|
|
if pattern[-1,1] == '*'
|
|
|
|
blk = proc { |entry| entry.to_s }
|
|
|
|
else
|
|
|
|
blk = proc { |entry| entry[1].parent.to_s }
|
|
|
|
end
|
|
|
|
(FileSystem.find(pattern) || []).map(&blk).uniq.sort
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](pattern)
|
|
|
|
glob(pattern)
|
|
|
|
end
|
2009-05-30 18:30:46 +00:00
|
|
|
|
|
|
|
def self.chdir(dir, &blk)
|
|
|
|
FileSystem.chdir(dir, &blk)
|
|
|
|
end
|
2009-05-29 08:12:10 +00:00
|
|
|
end
|
|
|
|
|
2009-05-29 16:48:28 +00:00
|
|
|
module FileSystem
|
|
|
|
extend self
|
|
|
|
|
2009-05-31 03:21:44 +00:00
|
|
|
def dir_levels
|
2009-05-31 03:23:48 +00:00
|
|
|
@dir_levels ||= []
|
2009-05-31 03:21:44 +00:00
|
|
|
end
|
|
|
|
|
2009-05-29 16:48:28 +00:00
|
|
|
def fs
|
|
|
|
@fs ||= MockDir.new('.')
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
2009-06-01 10:25:12 +00:00
|
|
|
@dir_levels = nil
|
2009-05-29 16:48:28 +00:00
|
|
|
@fs = nil
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def files
|
|
|
|
fs.values
|
|
|
|
end
|
|
|
|
|
2009-05-29 16:48:28 +00:00
|
|
|
def find(path)
|
2009-05-31 03:21:44 +00:00
|
|
|
parts = path_parts(normalize_path(path))
|
2009-05-29 16:48:28 +00:00
|
|
|
|
|
|
|
target = parts[0...-1].inject(fs) do |dir, part|
|
|
|
|
dir[part] || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
case parts.last
|
|
|
|
when '*'
|
|
|
|
target.values
|
|
|
|
else
|
|
|
|
target[parts.last]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def add(path, object=MockDir.new)
|
2009-05-31 03:21:44 +00:00
|
|
|
parts = path_parts(normalize_path(path))
|
2009-05-29 16:48:28 +00:00
|
|
|
|
|
|
|
d = parts[0...-1].inject(fs) do |dir, part|
|
|
|
|
dir[part] ||= MockDir.new(part, dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
object.name = parts.last
|
|
|
|
object.parent = d
|
2009-05-29 18:00:44 +00:00
|
|
|
d[parts.last] ||= object
|
2009-05-29 16:48:28 +00:00
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
# copies directories and files from the real filesystem
|
|
|
|
# into our fake one
|
|
|
|
def clone(path)
|
|
|
|
path = File.expand_path(path)
|
|
|
|
pattern = File.join(path, '**', '*')
|
2009-06-02 07:16:19 +00:00
|
|
|
files = RealFile.file?(path) ? [path] : [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH)
|
2009-05-29 17:23:39 +00:00
|
|
|
|
|
|
|
files.each do |f|
|
|
|
|
if RealFile.file?(f)
|
|
|
|
FileUtils.mkdir_p(File.dirname(f))
|
2009-05-31 09:24:33 +00:00
|
|
|
File.open(f, 'w') do |g|
|
|
|
|
g.print RealFile.open(f){|h| h.read }
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
elsif RealFile.directory?(f)
|
|
|
|
FileUtils.mkdir_p(f)
|
|
|
|
elsif RealFile.symlink?(f)
|
|
|
|
FileUtils.ln_s()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-29 16:48:28 +00:00
|
|
|
def delete(path)
|
|
|
|
if dir = FileSystem.find(path)
|
|
|
|
dir.parent.delete(dir.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-30 18:30:46 +00:00
|
|
|
def chdir(dir, &blk)
|
2009-05-31 03:21:44 +00:00
|
|
|
new_dir = find(dir)
|
2009-05-31 03:40:21 +00:00
|
|
|
dir_levels.push dir if blk
|
|
|
|
|
|
|
|
raise Errno::ENOENT, dir unless new_dir
|
|
|
|
|
|
|
|
dir_levels.push dir if !blk
|
|
|
|
blk.call if blk
|
2009-05-30 18:30:46 +00:00
|
|
|
ensure
|
2009-05-31 03:40:21 +00:00
|
|
|
dir_levels.pop if blk
|
2009-05-30 18:30:46 +00:00
|
|
|
end
|
|
|
|
|
2009-05-29 16:48:28 +00:00
|
|
|
def path_parts(path)
|
2009-05-29 17:23:39 +00:00
|
|
|
path.split(File::PATH_SEPARATOR).reject { |part| part.empty? }
|
|
|
|
end
|
2009-05-31 03:21:44 +00:00
|
|
|
|
|
|
|
def normalize_path(path)
|
2009-05-31 04:12:40 +00:00
|
|
|
if Pathname.new(path).absolute?
|
2009-05-31 03:21:44 +00:00
|
|
|
File.expand_path(path)
|
|
|
|
else
|
|
|
|
parts = dir_levels + [path]
|
|
|
|
File.expand_path(File.join(*parts))
|
|
|
|
end
|
|
|
|
end
|
2009-05-31 03:40:21 +00:00
|
|
|
|
|
|
|
def current_dir
|
|
|
|
find(normalize_path('.'))
|
|
|
|
end
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class MockFile
|
2009-05-29 18:42:39 +00:00
|
|
|
attr_accessor :name, :parent, :content
|
2009-05-29 17:23:39 +00:00
|
|
|
def initialize(name = nil, parent = nil)
|
|
|
|
@name = name
|
|
|
|
@parent = parent
|
2009-05-29 18:42:39 +00:00
|
|
|
@content = ''
|
2009-05-29 17:23:39 +00:00
|
|
|
end
|
|
|
|
|
2009-07-19 21:16:21 +00:00
|
|
|
def clone(parent)
|
|
|
|
clone = super()
|
|
|
|
clone.parent = parent
|
|
|
|
clone
|
|
|
|
end
|
|
|
|
|
2009-05-29 17:23:39 +00:00
|
|
|
def entry
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
File.join(parent.to_s, name)
|
2009-05-29 16:48:28 +00:00
|
|
|
end
|
|
|
|
end
|
2009-05-29 08:12:10 +00:00
|
|
|
|
|
|
|
class MockDir < Hash
|
|
|
|
attr_accessor :name, :parent
|
|
|
|
|
|
|
|
def initialize(name = nil, parent = nil)
|
|
|
|
@name = name
|
|
|
|
@parent = parent
|
|
|
|
end
|
|
|
|
|
|
|
|
def entry
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-07-19 21:16:21 +00:00
|
|
|
def clone(parent)
|
2009-07-19 21:23:02 +00:00
|
|
|
clone = Marshal.load(Marshal.dump(self))
|
2009-07-19 21:16:21 +00:00
|
|
|
clone.each do |key, value|
|
|
|
|
value.parent = parent
|
|
|
|
end
|
|
|
|
clone
|
|
|
|
end
|
|
|
|
|
2009-05-29 08:12:10 +00:00
|
|
|
def to_s
|
|
|
|
if parent && parent.to_s != '.'
|
2009-05-29 17:23:39 +00:00
|
|
|
File.join(parent.to_s, name)
|
|
|
|
elsif parent && parent.to_s == '.'
|
|
|
|
"#{File::PATH_SEPARATOR}#{name}"
|
2009-05-29 08:12:10 +00:00
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MockSymlink
|
|
|
|
attr_accessor :name, :target
|
|
|
|
alias_method :to_s, :name
|
|
|
|
|
|
|
|
def initialize(target)
|
|
|
|
@target = target
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"symlink(#{target.split('/').last})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def entry
|
|
|
|
FileSystem.find(target)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(*args, &block)
|
|
|
|
entry.send(*args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Object.class_eval do
|
|
|
|
remove_const(:Dir)
|
|
|
|
remove_const(:File)
|
|
|
|
remove_const(:FileUtils)
|
|
|
|
end
|
|
|
|
|
|
|
|
File = FakeFS::File
|
|
|
|
FileUtils = FakeFS::FileUtils
|
|
|
|
Dir = FakeFS::Dir
|