2011-12-04 01:12:48 +00:00
|
|
|
require "tempfile"
|
2014-01-15 18:48:19 +00:00
|
|
|
require "tmpdir"
|
2011-12-04 01:12:48 +00:00
|
|
|
|
2015-11-20 23:09:17 +00:00
|
|
|
require "vagrant/util/platform"
|
|
|
|
|
2011-12-11 23:53:11 +00:00
|
|
|
require "unit/support/isolated_environment"
|
2011-12-04 03:05:50 +00:00
|
|
|
|
2011-12-04 01:12:48 +00:00
|
|
|
shared_context "unit" do
|
2012-06-26 22:04:37 +00:00
|
|
|
before(:each) do
|
2012-08-13 01:35:19 +00:00
|
|
|
# State to store the list of registered plugins that we have to
|
|
|
|
# unregister later.
|
|
|
|
@_plugins = []
|
|
|
|
|
2012-06-26 22:04:37 +00:00
|
|
|
# Create a thing to store our temporary files so that they aren't
|
|
|
|
# unlinked right away.
|
|
|
|
@_temp_files = []
|
2015-07-10 11:37:33 +00:00
|
|
|
|
|
|
|
# Roughly simulate the embedded Bundler availability
|
|
|
|
$vagrant_bundler_runtime = Object.new
|
2012-06-26 22:04:37 +00:00
|
|
|
end
|
|
|
|
|
2012-08-13 01:35:19 +00:00
|
|
|
after(:each) do
|
|
|
|
# Unregister each of the plugins we have may have temporarily
|
|
|
|
# registered for the duration of this test.
|
|
|
|
@_plugins.each do |plugin|
|
2012-11-07 05:32:26 +00:00
|
|
|
Vagrant.plugin("1").manager.unregister(plugin)
|
2012-11-07 05:19:27 +00:00
|
|
|
Vagrant.plugin("2").manager.unregister(plugin)
|
2012-08-13 01:35:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-04 03:05:50 +00:00
|
|
|
# This creates an isolated environment so that Vagrant doesn't
|
|
|
|
# muck around with your real system during unit tests.
|
|
|
|
#
|
|
|
|
# The returned isolated environment has a variety of helper
|
|
|
|
# methods on it to easily create files, Vagrantfiles, boxes,
|
|
|
|
# etc.
|
|
|
|
def isolated_environment
|
|
|
|
env = Unit::IsolatedEnvironment.new
|
|
|
|
yield env if block_given?
|
|
|
|
env
|
|
|
|
end
|
|
|
|
|
2012-08-13 01:35:19 +00:00
|
|
|
# This registers a Vagrant plugin for the duration of a single test.
|
|
|
|
# This will yield a new plugin class that you can then call the
|
|
|
|
# public plugin methods on.
|
|
|
|
#
|
|
|
|
# @yield [plugin] Yields the plugin class for you to call the public
|
|
|
|
# API that you need to.
|
2012-11-07 05:32:26 +00:00
|
|
|
def register_plugin(version=nil)
|
|
|
|
version ||= Vagrant::Config::CURRENT_VERSION
|
|
|
|
plugin = Class.new(Vagrant.plugin(version))
|
2012-08-13 01:35:19 +00:00
|
|
|
plugin.name("Test Plugin #{plugin.inspect}")
|
|
|
|
yield plugin if block_given?
|
|
|
|
@_plugins << plugin
|
|
|
|
plugin
|
|
|
|
end
|
|
|
|
|
2011-12-04 01:12:48 +00:00
|
|
|
# This helper creates a temporary file and returns a Pathname
|
|
|
|
# object pointed to it.
|
2012-06-27 23:55:12 +00:00
|
|
|
#
|
|
|
|
# @return [Pathname]
|
2011-12-04 01:12:48 +00:00
|
|
|
def temporary_file(contents=nil)
|
|
|
|
f = Tempfile.new("vagrant-unit")
|
|
|
|
|
|
|
|
if contents
|
|
|
|
f.write(contents)
|
|
|
|
f.flush
|
|
|
|
end
|
|
|
|
|
2012-06-26 22:04:37 +00:00
|
|
|
# Store the tempfile in an instance variable so that it is not
|
|
|
|
# garbage collected, so that the tempfile is not unlinked.
|
|
|
|
@_temp_files << f
|
|
|
|
|
2011-12-18 04:22:46 +00:00
|
|
|
return Pathname.new(f.path)
|
2011-12-04 01:12:48 +00:00
|
|
|
end
|
2012-06-24 06:56:39 +00:00
|
|
|
|
2012-06-27 23:55:12 +00:00
|
|
|
# This creates a temporary directory and returns a {Pathname}
|
|
|
|
# pointing to it.
|
|
|
|
#
|
|
|
|
# @return [Pathname]
|
|
|
|
def temporary_dir
|
|
|
|
# Create a temporary directory and append it to the instance
|
|
|
|
# variabe so that it isn't garbage collected and deleted
|
2014-01-15 18:48:19 +00:00
|
|
|
d = Dir.mktmpdir("vagrant")
|
2012-06-27 23:55:12 +00:00
|
|
|
@_temp_files << d
|
|
|
|
|
|
|
|
# Return the pathname
|
2015-11-20 23:09:17 +00:00
|
|
|
result = Pathname.new(Vagrant::Util::Platform.fs_real_path(d))
|
|
|
|
yield result if block_given?
|
|
|
|
return result
|
2012-06-27 23:55:12 +00:00
|
|
|
end
|
|
|
|
|
2014-12-15 04:28:14 +00:00
|
|
|
# Stub the given environment in ENV, without actually touching ENV. Keys and
|
|
|
|
# values are converted to strings because that's how the real ENV works.
|
|
|
|
def stub_env(hash)
|
|
|
|
allow(ENV).to receive(:[]).and_call_original
|
|
|
|
|
|
|
|
hash.each do |key, value|
|
|
|
|
allow(ENV).to receive(:[])
|
|
|
|
.with(key.to_s)
|
|
|
|
.and_return(value.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-24 06:56:39 +00:00
|
|
|
# This helper provides temporary environmental variable changes.
|
|
|
|
def with_temp_env(environment)
|
|
|
|
# Build up the new environment, preserving the old values so we
|
|
|
|
# can replace them back in later.
|
|
|
|
old_env = {}
|
|
|
|
environment.each do |key, value|
|
|
|
|
old_env[key] = ENV[key]
|
|
|
|
ENV[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Call the block, returning its return value
|
|
|
|
return yield
|
|
|
|
ensure
|
|
|
|
# Reset the environment no matter what
|
|
|
|
old_env.each do |key, value|
|
|
|
|
ENV[key] = value
|
|
|
|
end
|
|
|
|
end
|
2015-11-19 02:38:03 +00:00
|
|
|
|
|
|
|
# This helper provides a randomly available port(s) for each argument to the
|
|
|
|
# block.
|
|
|
|
def with_random_port(&block)
|
|
|
|
ports = []
|
|
|
|
|
|
|
|
block.arity.times do
|
|
|
|
server = TCPServer.new('127.0.0.1', 0)
|
|
|
|
ports << server.addr[1]
|
|
|
|
server.close
|
|
|
|
end
|
|
|
|
|
|
|
|
block.call(*ports)
|
|
|
|
end
|
2011-12-04 01:12:48 +00:00
|
|
|
end
|