Fix some nondeterminism in tests with GCing tempfiles.

Before, the tempfile "f" could be GC'd before the path was used,
resulting in failed tests because when it is GC'd the tempfile is
removed. We now store the tempfile in an instance variable so that it
isn't even available for GC until after the test is finished running.
This commit is contained in:
Mitchell Hashimoto 2012-06-26 15:04:37 -07:00
parent 2e00a007ce
commit 194cd2145d
1 changed files with 10 additions and 0 deletions

View File

@ -3,6 +3,12 @@ require "tempfile"
require "unit/support/isolated_environment" require "unit/support/isolated_environment"
shared_context "unit" do shared_context "unit" do
before(:each) do
# Create a thing to store our temporary files so that they aren't
# unlinked right away.
@_temp_files = []
end
# This creates an isolated environment so that Vagrant doesn't # This creates an isolated environment so that Vagrant doesn't
# muck around with your real system during unit tests. # muck around with your real system during unit tests.
# #
@ -25,6 +31,10 @@ shared_context "unit" do
f.flush f.flush
end end
# Store the tempfile in an instance variable so that it is not
# garbage collected, so that the tempfile is not unlinked.
@_temp_files << f
return Pathname.new(f.path) return Pathname.new(f.path)
end end