fakefs/lib/fakefs/dir.rb

115 lines
2.3 KiB
Ruby
Raw Normal View History

2009-07-23 00:21:28 +00:00
module FakeFS
class Dir
2009-09-13 13:50:08 +00:00
include Enumerable
def initialize(string)
raise Errno::ENOENT, string unless FileSystem.find(string)
@path = string
@open = true
@pointer = 0
@contents = [ '.', '..', ] + FileSystem.find(@path).values
end
def close
@open = false
@pointer = nil
@contents = nil
nil
end
def each(&block)
while f = read
yield f
end
end
def path
@path
2009-07-23 00:21:28 +00:00
end
2009-09-13 13:50:08 +00:00
def pos
@pointer
end
def pos=(integer)
@pointer = integer
end
def read
raise IOError, "closed directory" if @pointer == nil
n = @contents[@pointer]
@pointer += 1
n.to_s.gsub(path + '/', '') if n
2009-09-13 13:50:08 +00:00
end
def rewind
@pointer = 0
end
def seek(integer)
raise IOError, "closed directory" if @pointer == nil
@pointer = integer
@contents[integer]
end
2009-09-22 02:38:35 +00:00
2009-07-23 00:21:28 +00:00
def self.[](pattern)
glob(pattern)
end
def self.chdir(dir, &blk)
FileSystem.chdir(dir, &blk)
end
2009-07-27 17:56:56 +00:00
2009-09-13 13:50:08 +00:00
def self.chroot(string)
# Not implemented yet
end
def self.delete(string)
raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty?
2009-09-22 02:38:35 +00:00
FileSystem.delete(string)
2009-09-13 13:50:08 +00:00
end
def self.entries(dirname)
raise SystemCallError, dirname unless FileSystem.find(dirname)
Dir.new(dirname).map { |file| file }
end
def self.foreach(dirname, &block)
Dir.open(dirname) { |file| yield file }
end
2009-09-22 02:38:35 +00:00
2009-09-13 13:50:08 +00:00
def self.glob(pattern)
[FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort
end
def self.mkdir(string, integer = 0)
parent = string.split('/')
parent.pop
raise Errno::ENOENT, "No such file or directory - #{string}" unless parent.join == "" || FileSystem.find(parent.join('/'))
2009-09-13 13:50:08 +00:00
FileUtils.mkdir_p(string)
end
def self.open(string, &block)
if block_given?
Dir.new(string).each { |file| yield(file) }
2009-09-13 13:50:08 +00:00
else
Dir.new(string)
end
end
def self.tmpdir
2009-09-13 13:50:08 +00:00
'/tmp'
end
2009-07-27 17:56:56 +00:00
def self.pwd
FileSystem.current_dir.to_s
end
class << self
alias_method :getwd, :pwd
2009-09-13 13:50:08 +00:00
alias_method :rmdir, :delete
2009-09-13 19:15:12 +00:00
alias_method :unlink, :delete
2009-07-27 17:56:56 +00:00
end
2009-07-23 00:21:28 +00:00
end
2009-07-27 17:56:56 +00:00
end