From 194cd2145de1c965cf3b7a991171638bd49450ce Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 26 Jun 2012 15:04:37 -0700 Subject: [PATCH] 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. --- test/unit/support/shared/base_context.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unit/support/shared/base_context.rb b/test/unit/support/shared/base_context.rb index e6452a619..025d74e4c 100644 --- a/test/unit/support/shared/base_context.rb +++ b/test/unit/support/shared/base_context.rb @@ -3,6 +3,12 @@ require "tempfile" require "unit/support/isolated_environment" 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 # muck around with your real system during unit tests. # @@ -25,6 +31,10 @@ shared_context "unit" do f.flush 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) end