Skip to content

Commit

Permalink
Fix some nondeterminism in tests with GCing tempfiles.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mitchellh committed Jun 26, 2012
1 parent 2e00a00 commit 194cd21
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/unit/support/shared/base_context.rb
Expand Up @@ -3,6 +3,12 @@
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.
#
Expand All @@ -25,6 +31,10 @@ def temporary_file(contents=nil)
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

Expand Down

0 comments on commit 194cd21

Please sign in to comment.