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