File.open fails given nonexistent path or path to a dir. Closes #82.

* Removed unnecessary paths from existing tests (they don't
  need to exercise paths).
* Added tests for nonexistent, existing and trying to open
  a directory.
This commit is contained in:
Eero Saynatkari 2011-05-08 22:31:44 +03:00 committed by Scott Taylor
parent 973a2ae1f3
commit 492d628994
3 changed files with 87 additions and 33 deletions

View File

@ -398,8 +398,22 @@ module FakeFS
(@mode & mask) != 0 if @mode.is_a?(Integer) (@mode & mask) != 0 if @mode.is_a?(Integer)
end end
# Create a missing file if the path's valid.
#
def create_missing_file def create_missing_file
if !File.exists?(@path) raise Errno::EISDIR, "Is a directory - #{path}" if File.directory?(@path)
if !File.exists?(@path) # Unnecessary check, probably.
dirname = RealFile.dirname @path
unless dirname == "."
dir = FileSystem.find dirname
unless dir.kind_of? FakeDir
raise Errno::ENOENT, "No such file or directory - #{path}"
end
end
@file = FileSystem.add(path, FakeFile.new) @file = FileSystem.add(path, FakeFile.new)
end end
end end

View File

@ -175,8 +175,8 @@ class FakeFSTest < Test::Unit::TestCase
assert File.symlink?(link) assert File.symlink?(link)
end end
def test_can_create_files def test_can_create_files_in_current_dir
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -186,7 +186,44 @@ class FakeFSTest < Test::Unit::TestCase
assert File.writable?(path) assert File.writable?(path)
end end
def test_can_create_files_in_existing_dir
FileUtils.mkdir_p "/path/to"
path = "/path/to/file.txt"
File.open(path, 'w') do |f|
f.write "Yatta!"
end
assert File.exists?(path)
assert File.readable?(path)
assert File.writable?(path)
end
def test_raises_ENOENT_trying_to_create_files_in_nonexistent_dir
path = "/path/to/file.txt"
assert_raises(Errno::ENOENT) {
File.open(path, 'w') do |f|
f.write "Yatta!"
end
}
end
def test_raises_EISDIR_if_trying_to_open_existing_directory_name
path = "/path/to"
FileUtils.mkdir_p path
assert_raises(Errno::EISDIR) {
File.open(path, 'w') do |f|
f.write "Yatta!"
end
}
end
def test_can_create_files_with_bitmasks def test_can_create_files_with_bitmasks
FileUtils.mkdir_p("/path/to")
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, File::RDWR | File::CREAT) do |f| File.open(path, File::RDWR | File::CREAT) do |f|
f.write "Yatta!" f.write "Yatta!"
@ -305,7 +342,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_read_files_once_written def test_can_read_files_once_written
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -314,7 +351,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_write_to_files def test_can_write_to_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
end end
@ -328,11 +365,12 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_open_file_in_binary_mode def test_can_open_file_in_binary_mode
File.open("/foo", "wb") { |x| x << "a" } File.open("foo", "wb") { |x| x << "a" }
assert_equal "a", File.read("/foo") assert_equal "a", File.read("foo")
end end
def test_can_chunk_io_when_reading def test_can_chunk_io_when_reading
FileUtils.mkdir_p "/path/to"
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
@ -346,7 +384,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_get_size_of_files def test_can_get_size_of_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
end end
@ -354,20 +392,20 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_check_if_file_has_size? def test_can_check_if_file_has_size?
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
end end
assert File.size?(path) assert File.size?(path)
assert_nil File.size?("/path/to/other.txt") assert_nil File.size?("other.txt")
end end
def test_can_check_size_of_empty_file def test_can_check_size_of_empty_file
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
end end
assert_nil File.size?("/path/to/file.txt") assert_nil File.size?("file.txt")
end end
def test_raises_error_on_mtime_if_file_does_not_exist def test_raises_error_on_mtime_if_file_does_not_exist
@ -377,16 +415,16 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_return_mtime_on_existing_file def test_can_return_mtime_on_existing_file
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
end end
assert File.mtime('/path/to/file.txt').is_a?(Time) assert File.mtime('file.txt').is_a?(Time)
end end
def test_raises_error_on_ctime_if_file_does_not_exist def test_raises_error_on_ctime_if_file_does_not_exist
assert_raise Errno::ENOENT do assert_raise Errno::ENOENT do
File.ctime('/path/to/file.txt') File.ctime('file.txt')
end end
end end
@ -446,16 +484,16 @@ class FakeFSTest < Test::Unit::TestCase
def test_can_call_utime_on_an_existing_file def test_can_call_utime_on_an_existing_file
time = Time.now - 300 # Not now time = Time.now - 300 # Not now
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
end end
File.utime(time, time, path) File.utime(time, time, path)
assert_equal time, File.mtime('/path/to/file.txt') assert_equal time, File.mtime('file.txt')
end end
def test_utime_returns_number_of_paths def test_utime_returns_number_of_paths
path1, path2 = '/path/to/file.txt', '/path/to/another_file.txt' path1, path2 = 'file.txt', 'another_file.txt'
[path1, path2].each do |path| [path1, path2].each do |path|
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
@ -465,7 +503,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_read_with_File_readlines def test_can_read_with_File_readlines
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.puts "Yatta!", "Gatta!" f.puts "Yatta!", "Gatta!"
f.puts ["woot","toot"] f.puts ["woot","toot"]
@ -475,7 +513,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_File_close_disallows_further_access def test_File_close_disallows_further_access
path = '/path/to/file.txt' path = 'file.txt'
file = File.open(path, 'w') file = File.open(path, 'w')
file.write 'Yada' file.write 'Yada'
file.close file.close
@ -485,7 +523,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_File_close_disallows_further_writes def test_File_close_disallows_further_writes
path = '/path/to/file.txt' path = 'file.txt'
file = File.open(path, 'w') file = File.open(path, 'w')
file.write 'Yada' file.write 'Yada'
file.close file.close
@ -495,7 +533,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_read_from_file_objects def test_can_read_from_file_objects
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -510,7 +548,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_knows_files_are_files def test_knows_files_are_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -519,17 +557,17 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_File_io_returns_self def test_File_io_returns_self
f = File.open("/foo", "w") f = File.open("foo", "w")
assert_equal f, f.to_io assert_equal f, f.to_io
end end
def test_File_to_i_is_alias_for_filno def test_File_to_i_is_alias_for_filno
f = File.open("/foo", "w") f = File.open("foo", "w")
assert_equal f.method(:to_i), f.method(:fileno) assert_equal f.method(:to_i), f.method(:fileno)
end end
def test_knows_symlink_files_are_files def test_knows_symlink_files_are_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -615,6 +653,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_dir_glob_handles_recursive_globs def test_dir_glob_handles_recursive_globs
FileUtils.mkdir_p "/one/two/three"
File.open('/one/two/three/four.rb', 'w') File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w') File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb'] assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
@ -623,6 +662,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
FileUtils.mkdir_p "/one/two/three"
File.open('/one/two/three/four.rb', 'w') File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w') File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*'] assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
@ -640,7 +680,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_should_report_pos_as_0_when_opening def test_should_report_pos_as_0_when_opening
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foobar" f << "foobar"
f.rewind f.rewind
@ -649,7 +689,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_should_report_pos_as_1_when_seeking_one_char def test_should_report_pos_as_1_when_seeking_one_char
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foobar" f << "foobar"
f.rewind f.rewind
@ -660,22 +700,22 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_should_set_pos def test_should_set_pos
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foo" f << "foo"
end end
fp = File.open("/foo", "r") fp = File.open("foo", "r")
fp.pos = 1 fp.pos = 1
assert_equal 1, fp.pos assert_equal 1, fp.pos
end end
def test_should_set_pos_with_tell_method def test_should_set_pos_with_tell_method
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foo" f << "foo"
end end
fp = File.open("/foo", "r") fp = File.open("foo", "r")
fp.tell = 1 fp.tell = 1
assert_equal 1, fp.pos assert_equal 1, fp.pos

View File

@ -10,7 +10,7 @@ class FakeFSSafeTest < Test::Unit::TestCase
end end
def test_FakeFS_method_does_not_intrude_on_global_namespace def test_FakeFS_method_does_not_intrude_on_global_namespace
path = '/path/to/file.txt' path = 'file.txt'
FakeFS do FakeFS do
File.open(path, 'w') { |f| f.write "Yatta!" } File.open(path, 'w') { |f| f.write "Yatta!" }