Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 492d628994f99666cb4c4674a22c6ced04cd891d 1 parent 973a2ae
@rue rue authored smtlaissezfaire committed
Showing with 87 additions and 33 deletions.
  1. +15 −1 lib/fakefs/file.rb
  2. +71 −31 test/fakefs_test.rb
  3. +1 −1  test/safe_test.rb
View
16 lib/fakefs/file.rb
@@ -398,8 +398,22 @@ def mode_in_bitmask?(mask)
(@mode & mask) != 0 if @mode.is_a?(Integer)
end
+ # Create a missing file if the path's valid.
+ #
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)
end
end
View
102 test/fakefs_test.rb
@@ -175,8 +175,21 @@ def test_knows_symlinks_are_symlinks
assert File.symlink?(link)
end
- def test_can_create_files
- path = '/path/to/file.txt'
+ def test_can_create_files_in_current_dir
+ path = '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_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
@@ -186,7 +199,31 @@ def test_can_create_files
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
+ FileUtils.mkdir_p("/path/to")
+
path = '/path/to/file.txt'
File.open(path, File::RDWR | File::CREAT) do |f|
f.write "Yatta!"
@@ -305,7 +342,7 @@ def test_file_in_append_write_only_raises_error_when_reading
end
def test_can_read_files_once_written
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
@@ -314,7 +351,7 @@ def test_can_read_files_once_written
end
def test_can_write_to_files
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
@@ -328,11 +365,12 @@ def test_raises_error_when_opening_with_binary_mode_only
end
def test_can_open_file_in_binary_mode
- File.open("/foo", "wb") { |x| x << "a" }
- assert_equal "a", File.read("/foo")
+ File.open("foo", "wb") { |x| x << "a" }
+ assert_equal "a", File.read("foo")
end
def test_can_chunk_io_when_reading
+ FileUtils.mkdir_p "/path/to"
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
@@ -346,7 +384,7 @@ def test_can_chunk_io_when_reading
end
def test_can_get_size_of_files
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
@@ -354,20 +392,20 @@ def test_can_get_size_of_files
end
def test_can_check_if_file_has_size?
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert File.size?(path)
- assert_nil File.size?("/path/to/other.txt")
+ assert_nil File.size?("other.txt")
end
def test_can_check_size_of_empty_file
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f << ''
end
- assert_nil File.size?("/path/to/file.txt")
+ assert_nil File.size?("file.txt")
end
def test_raises_error_on_mtime_if_file_does_not_exist
@@ -377,16 +415,16 @@ def test_raises_error_on_mtime_if_file_does_not_exist
end
def test_can_return_mtime_on_existing_file
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f << ''
end
- assert File.mtime('/path/to/file.txt').is_a?(Time)
+ assert File.mtime('file.txt').is_a?(Time)
end
def test_raises_error_on_ctime_if_file_does_not_exist
assert_raise Errno::ENOENT do
- File.ctime('/path/to/file.txt')
+ File.ctime('file.txt')
end
end
@@ -446,16 +484,16 @@ def test_utime_raises_error_if_path_does_not_exist
def test_can_call_utime_on_an_existing_file
time = Time.now - 300 # Not now
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f << ''
end
File.utime(time, time, path)
- assert_equal time, File.mtime('/path/to/file.txt')
+ assert_equal time, File.mtime('file.txt')
end
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|
File.open(path, 'w') do |f|
f << ''
@@ -465,7 +503,7 @@ def test_utime_returns_number_of_paths
end
def test_can_read_with_File_readlines
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f.puts "Yatta!", "Gatta!"
f.puts ["woot","toot"]
@@ -475,7 +513,7 @@ def test_can_read_with_File_readlines
end
def test_File_close_disallows_further_access
- path = '/path/to/file.txt'
+ path = 'file.txt'
file = File.open(path, 'w')
file.write 'Yada'
file.close
@@ -485,7 +523,7 @@ def test_File_close_disallows_further_access
end
def test_File_close_disallows_further_writes
- path = '/path/to/file.txt'
+ path = 'file.txt'
file = File.open(path, 'w')
file.write 'Yada'
file.close
@@ -495,7 +533,7 @@ def test_File_close_disallows_further_writes
end
def test_can_read_from_file_objects
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
@@ -510,7 +548,7 @@ def test_file_read_errors_appropriately
end
def test_knows_files_are_files
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
@@ -519,17 +557,17 @@ def test_knows_files_are_files
end
def test_File_io_returns_self
- f = File.open("/foo", "w")
+ f = File.open("foo", "w")
assert_equal f, f.to_io
end
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)
end
def test_knows_symlink_files_are_files
- path = '/path/to/file.txt'
+ path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
@@ -615,6 +653,7 @@ def test_dir_glob_handles_root
end
def test_dir_glob_handles_recursive_globs
+ FileUtils.mkdir_p "/one/two/three"
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
@@ -623,6 +662,7 @@ def test_dir_glob_handles_recursive_globs
end
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/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
@@ -640,7 +680,7 @@ def test_dir_glob_with_block
end
def test_should_report_pos_as_0_when_opening
- File.open("/foo", "w") do |f|
+ File.open("foo", "w") do |f|
f << "foobar"
f.rewind
@@ -649,7 +689,7 @@ def test_should_report_pos_as_0_when_opening
end
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.rewind
@@ -660,22 +700,22 @@ def test_should_report_pos_as_1_when_seeking_one_char
end
def test_should_set_pos
- File.open("/foo", "w") do |f|
+ File.open("foo", "w") do |f|
f << "foo"
end
- fp = File.open("/foo", "r")
+ fp = File.open("foo", "r")
fp.pos = 1
assert_equal 1, fp.pos
end
def test_should_set_pos_with_tell_method
- File.open("/foo", "w") do |f|
+ File.open("foo", "w") do |f|
f << "foo"
end
- fp = File.open("/foo", "r")
+ fp = File.open("foo", "r")
fp.tell = 1
assert_equal 1, fp.pos
View
2  test/safe_test.rb
@@ -10,7 +10,7 @@ def teardown
end
def test_FakeFS_method_does_not_intrude_on_global_namespace
- path = '/path/to/file.txt'
+ path = 'file.txt'
FakeFS do
File.open(path, 'w') { |f| f.write "Yatta!" }
Please sign in to comment.
Something went wrong with that request. Please try again.