Dir.chdir can now be called without a block, and another error state
in chdir is taken care of. Specifically, where chdir into a non-existent directory could put the filesystem into a broken state.
This commit is contained in:
parent
83c06cd8d4
commit
97e2133282
@ -224,14 +224,15 @@ module FakeFS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def chdir(dir, &blk)
|
def chdir(dir, &blk)
|
||||||
raise "you must pass in a block" unless blk
|
|
||||||
|
|
||||||
new_dir = find(dir)
|
new_dir = find(dir)
|
||||||
raise Errno::ENOENT unless new_dir
|
dir_levels.push dir if blk
|
||||||
dir_levels.push dir
|
|
||||||
blk.call
|
raise Errno::ENOENT, dir unless new_dir
|
||||||
|
|
||||||
|
dir_levels.push dir if !blk
|
||||||
|
blk.call if blk
|
||||||
ensure
|
ensure
|
||||||
dir_levels.pop
|
dir_levels.pop if blk
|
||||||
end
|
end
|
||||||
|
|
||||||
def path_parts(path)
|
def path_parts(path)
|
||||||
@ -246,6 +247,10 @@ module FakeFS
|
|||||||
File.expand_path(File.join(*parts))
|
File.expand_path(File.join(*parts))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def current_dir
|
||||||
|
find(normalize_path('.'))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class MockFile
|
class MockFile
|
||||||
|
@ -165,10 +165,6 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
assert_equal 'foo', c
|
assert_equal 'foo', c
|
||||||
|
|
||||||
assert_raise(RuntimeError, 'no block chdir is not yet supported') do
|
|
||||||
Dir.chdir('/path')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_chdir_shouldnt_keep_us_from_absolute_paths
|
def test_chdir_shouldnt_keep_us_from_absolute_paths
|
||||||
@ -226,9 +222,34 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
rescue Exception # hardcore
|
rescue Exception # hardcore
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Dir.chdir('/path') do
|
||||||
|
begin
|
||||||
|
Dir.chdir('nope'){ }
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal ['/path'], FileSystem.dir_levels
|
||||||
|
end
|
||||||
|
|
||||||
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_chdir_with_no_block_is_awesome
|
||||||
|
FileUtils.mkdir_p '/path'
|
||||||
|
Dir.chdir('/path')
|
||||||
|
FileUtils.mkdir_p 'subdir'
|
||||||
|
assert_equal ['subdir'], FileSystem.current_dir.keys
|
||||||
|
Dir.chdir('subdir')
|
||||||
|
File.open('foo', 'w'){|f| f.write 'foo'}
|
||||||
|
assert_equal ['foo'], FileSystem.current_dir.keys
|
||||||
|
|
||||||
|
assert_raises(Errno::ENOENT) do
|
||||||
|
Dir.chdir('subsubdir')
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal ['foo'], FileSystem.current_dir.keys
|
||||||
|
end
|
||||||
|
|
||||||
def test_file_open_defaults_to_read
|
def test_file_open_defaults_to_read
|
||||||
File.open('foo','w'){|f| f.write 'bar' }
|
File.open('foo','w'){|f| f.write 'bar' }
|
||||||
assert_equal 'bar', File.open('foo'){|f| f.read }
|
assert_equal 'bar', File.open('foo'){|f| f.read }
|
||||||
|
Loading…
Reference in New Issue
Block a user