core: don't use Tempdir for tests, use Dir.mktmpdir

Tempdir didn't work on Windows, just spun forever.
This commit is contained in:
Mitchell Hashimoto 2014-01-15 10:48:19 -08:00
parent 0a0d82469f
commit 37e9553bf5
6 changed files with 18 additions and 61 deletions

View File

@ -1,10 +1,9 @@
require "fileutils"
require "pathname"
require "tmpdir"
require "log4r"
require "support/tempdir"
# This class manages an isolated environment for Vagrant to
# run in. It creates a temporary directory to act as the
# working directory as well as sets a custom home directory.
@ -27,12 +26,12 @@ class IsolatedEnvironment
@logger = Log4r::Logger.new("test::isolated_environment")
# Create a temporary directory for our work
@tempdir = Tempdir.new("vagrant")
@logger.info("Initialize isolated environment: #{@tempdir.path}")
@tempdir = Dir.mktmpdir("vagrant")
@logger.info("Initialize isolated environment: #{@tempdir}")
# Setup the home and working directories
@homedir = Pathname.new(File.join(@tempdir.path, "home"))
@workdir = Pathname.new(File.join(@tempdir.path, "work"))
@homedir = Pathname.new(File.join(@tempdir, "home"))
@workdir = Pathname.new(File.join(@tempdir, "work"))
@homedir.mkdir
@workdir.mkdir
@ -40,7 +39,7 @@ class IsolatedEnvironment
# This closes the environment by cleaning it up.
def close
@logger.info("Removing isolated environment: #{@tempdir.path}")
FileUtils.rm_rf(@tempdir.path)
@logger.info("Removing isolated environment: #{@tempdir}")
FileUtils.rm_rf(@tempdir)
end
end

View File

@ -1,43 +0,0 @@
require 'fileutils'
require 'tempfile'
# This class provides an easy way of creating a temporary
# directory and having it removed when the application exits.
class Tempdir
attr_reader :path
def initialize(basename="vagrant")
@path = nil
# Loop and attempt to create a temporary directory until
# it succeeds.
while @path.nil?
file = Tempfile.new(basename)
@path = file.path
file.unlink
begin
Dir.mkdir(@path)
rescue
@path = nil
end
end
# Setup a finalizer to delete the directory. This is the same way
# that Tempfile and friends do this...
@cleanup_proc = lambda do
FileUtils.rm_rf(@path) if File.directory?(@path)
end
ObjectSpace.define_finalizer(self, @cleanup_proc)
end
# This deletes the temporary directory.
def unlink
# Delete the directory
@cleanup_proc.call
# Undefine the finalizer since we're all cleaned up
ObjectSpace.undefine_finalizer(self)
end
end

View File

@ -1,3 +1,4 @@
require "tmpdir"
require "rubygems"
require "rspec/autorun"
@ -9,7 +10,6 @@ require "vagrant"
$:.unshift File.expand_path("../../", __FILE__)
# Load in helpers
require "support/tempdir"
require "unit/support/dummy_communicator"
require "unit/support/dummy_provider"
require "unit/support/shared/base_context"
@ -29,7 +29,7 @@ end
# Configure VAGRANT_CWD so that the tests never find an actual
# Vagrantfile anywhere, or at least this minimizes those chances.
ENV["VAGRANT_CWD"] = Tempdir.new.path
ENV["VAGRANT_CWD"] = Dir.mktmpdir("vagrant")
# Set the dummy provider to the default for tests
ENV["VAGRANT_DEFAULT_PROVIDER"] = "dummy"

View File

@ -1,6 +1,7 @@
require "fileutils"
require "pathname"
require "tempfile"
require "tmpdir"
require "json"
require "log4r"
@ -9,7 +10,6 @@ require "vagrant/util/platform"
require "vagrant/util/subprocess"
require "support/isolated_environment"
require "support/tempdir"
module Unit
class IsolatedEnvironment < ::IsolatedEnvironment
@ -94,8 +94,8 @@ module Unit
# @return [Pathname] Path to the newly created box.
def box1_file
# Create a temporary directory to store our data we will tar up
td_source = Tempdir.new
td_dest = Tempdir.new
td_source = Dir.mktmpdir
td_dest = Dir.mktmpdir
# Store the temporary directory so it is not deleted until
# this instance is garbage collected.
@ -139,8 +139,8 @@ module Unit
}.merge(options[:metadata] || {})
# Create a temporary directory to store our data we will tar up
td_source = Tempdir.new
td_dest = Tempdir.new
td_source = Dir.mktmpdir
td_dest = Dir.mktmpdir
# Store the temporary directory so it is not deleted until
# this instance is garbage collected.

View File

@ -1,6 +1,6 @@
require "tempfile"
require "tmpdir"
require "support/tempdir"
require "unit/support/isolated_environment"
shared_context "unit" do
@ -76,7 +76,7 @@ shared_context "unit" do
def temporary_dir
# Create a temporary directory and append it to the instance
# variabe so that it isn't garbage collected and deleted
d = Tempdir.new("vagrant-unit")
d = Dir.mktmpdir("vagrant")
@_temp_files << d
# Return the pathname

View File

@ -1,4 +1,5 @@
require "pathname"
require "tmpdir"
require File.expand_path("../../base", __FILE__)
@ -17,7 +18,7 @@ describe Vagrant::Machine do
let(:provider_options) { {} }
let(:box) { Object.new }
let(:config) { env.config_global }
let(:data_dir) { Pathname.new(Tempdir.new.path) }
let(:data_dir) { Pathname.new(Dir.mktmpdir("vagrant")) }
let(:env) do
# We need to create a Vagrantfile so that this test environment
# has a proper root path