From 7444ef50b3ffbb8a534182b2a3af6a0b6073b32f Mon Sep 17 00:00:00 2001 From: jameswilding Date: Mon, 19 Oct 2009 19:38:59 +0800 Subject: [PATCH] Fix that FakeFS method wouldn't call ::FakeFS.deactivate! when block raised an exception --- lib/fakefs/base.rb | 6 +++--- test/safe_test.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/fakefs/base.rb b/lib/fakefs/base.rb index 5c54fb9..4d32e1a 100644 --- a/lib/fakefs/base.rb +++ b/lib/fakefs/base.rb @@ -31,8 +31,8 @@ end def FakeFS return ::FakeFS unless block_given? ::FakeFS.activate! - result = yield - ::FakeFS.deactivate! - result + yield +ensure +::FakeFS.deactivate! end diff --git a/test/safe_test.rb b/test/safe_test.rb index 5916d69..d87c00f 100644 --- a/test/safe_test.rb +++ b/test/safe_test.rb @@ -26,4 +26,17 @@ class FakeFSSafeTest < Test::Unit::TestCase assert_equal result, "Yatta!" end + + def test_FakeFS_method_deactivates_FakeFS_when_block_raises_exception + begin + FakeFS do + raise 'boom!' + end + rescue + end + + assert_equal RealFile, File, "File is #{File} (should be #{RealFile})" + assert_equal RealFileUtils, FileUtils, "FileUtils is #{FileUtils} (should be #{RealFileUtils})" + assert_equal RealDir, Dir, "Dir is #{Dir} (should be #{RealDir})" + end end