From e051533cd42f49ceb0041fbc855d5b64e5286c77 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Wed, 3 Jun 2009 17:47:10 -0700 Subject: [PATCH] ensure File.read raises the correct error --- lib/fakefs.rb | 11 ++++++++++- test/fakefs_test.rb | 11 ++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 4fd169a..1a0942f 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -126,7 +126,12 @@ module FakeFS end def self.read(path) - new(path).read + file = new(path) + if file.exists? + file.read + else + raise Errno::ENOENT + end end def self.readlines(path) @@ -144,6 +149,10 @@ module FakeFS @file.content end + def exists? + @file + end + def puts(content) write(content + "\n") end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index c59a152..744f1dd 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -41,7 +41,7 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.ln_s path, sympath = '/sympath' assert File.directory?(sympath) end - + def test_doesnt_overwrite_existing_directories FileUtils.mkdir_p(path = "/path/to/dir") assert File.exists?(path) @@ -108,6 +108,12 @@ class FakeFSTest < Test::Unit::TestCase assert_equal "Yatta!", File.new(path).read end + def test_file_read_errors_appropriately + assert_raise Errno::ENOENT do + File.read('anything') + end + end + def test_knows_files_are_files path = '/path/to/file.txt' File.open(path, 'w') do |f| @@ -126,7 +132,7 @@ class FakeFSTest < Test::Unit::TestCase assert File.file?(sympath) end - + def test_can_chown_files good = 'file.txt' bad = 'nofile.txt' @@ -393,7 +399,6 @@ class FakeFSTest < Test::Unit::TestCase File.open('subdir/nother','w'){|f| f.write 'works' } FileUtils.ln_s 'subdir', 'new' assert_equal 'works', File.open('new/nother'){|f| f.read } - end def here(fname)