diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 421801f..95de805 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -1,9 +1,57 @@ module FakeFS class Dir - def self.glob(pattern) - [FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort + 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 + end + + 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 if n + end + + def rewind + @pointer = 0 + end + + def seek(integer) + raise IOError, "closed directory" if @pointer == nil + @pointer = integer + @contents[integer] + end + def self.[](pattern) glob(pattern) end @@ -12,12 +60,52 @@ module FakeFS FileSystem.chdir(dir, &blk) end + def self.chroot(string) + # Not implemented yet + end + + def self.delete(string) + FileSystem.delete(string) + 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 + + def self.glob(pattern) + [FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort + end + + def self.mkdir(string, integer = 0) + FileUtils.mkdir_p(string) + end + + def self.open(string, &block) + if block_given? + d = Dir.new(string).each { |file| yield(file) } + d.close + else + Dir.new(string) + end + end + + def tmpdir + '/tmp' + end + def self.pwd FileSystem.current_dir.to_s end class << self alias_method :getwd, :pwd + alias_method :rmdir, :delete + alias_method :unline, :delete end end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 26747b7..82b64f5 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -532,6 +532,169 @@ class FakeFSTest < Test::Unit::TestCase } end + # Directory tests + def test_new_directory + FileUtils.mkdir_p('/this/path/should/be/here') + + assert_nothing_raised { + Dir.new('/this/path/should/be/here') + } + end + + def test_new_directory_does_not_work_if_dir_path_cannot_be_found + assert_raises(Errno::ENOENT) { + Dir.new('/this/path/should/not/be/here') + } + end + + def test_close + FileUtils.mkdir_p('/this/path/should/be/here') + dir = Dir.new('/this/path/should/be/here') + assert dir.close.nil? + + assert_raises(IOError) { + dir.each { |dir| dir } + } + end + + def test_each + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + + FileUtils.mkdir_p('/this/path/should/be/here') + + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + yielded = [] + dir.each do |dir| + yielded << dir + end + + assert yielded.size == test.size + test.each { |t| assert yielded.include?(t) } + end + + def test_path + FileUtils.mkdir_p('/this/path/should/be/here') + assert Dir.new('/this/path/should/be/here').path == '/this/path/should/be/here' + end + + def test_pos + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + FileUtils.mkdir_p('/this/path/should/be/here') + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + assert dir.pos == 0 + dir.read + assert dir.pos == 1 + dir.read + assert dir.pos == 2 + dir.read + assert dir.pos == 3 + dir.read + assert dir.pos == 4 + dir.read + assert dir.pos == 5 + end + + def test_pos_assign + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + FileUtils.mkdir_p('/this/path/should/be/here') + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + assert dir.pos == 0 + dir.pos = 2 + assert dir.pos == 2 + end + + def test_read + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + FileUtils.mkdir_p('/this/path/should/be/here') + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + assert dir.pos == 0 + d = dir.read + assert dir.pos == 1 + assert d == '.' + + d = dir.read + assert dir.pos == 2 + assert d == '..' + end + + def test_read_past_length + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + FileUtils.mkdir_p('/this/path/should/be/here') + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + d = dir.read + assert_not_nil d + d = dir.read + assert_not_nil d + d = dir.read + assert_not_nil d + d = dir.read + assert_not_nil d + d = dir.read + assert_not_nil d + d = dir.read + assert_not_nil d + d = dir.read + assert_not_nil d + d = dir.read + assert_nil d + end + + def test_rewind + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + FileUtils.mkdir_p('/this/path/should/be/here') + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + d = dir.read + d = dir.read + assert dir.pos == 2 + dir.rewind + assert dir.pos == 0 + end + + def test_seek + test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ] + FileUtils.mkdir_p('/this/path/should/be/here') + test.each do |f| + FileUtils.touch(f) + end + + dir = Dir.new('/this/path/should/be/here') + + d = dir.seek 1 + puts d + assert d == '..' + assert dir.pos == 1 + end + def here(fname) RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname) end