vagrant/test/unit/support/isolated_environment.rb

80 lines
1.9 KiB
Ruby
Raw Normal View History

2011-12-04 03:05:50 +00:00
require "fileutils"
require "pathname"
require "log4r"
2011-12-11 23:53:11 +00:00
require "support/isolated_environment"
2011-12-04 03:05:50 +00:00
module Unit
2011-12-11 23:53:11 +00:00
class IsolatedEnvironment < ::IsolatedEnvironment
2012-01-09 07:04:23 +00:00
def create_vagrant_env(options=nil)
options = {
:cwd => @workdir,
:home_path => @homedir
}.merge(options || {})
Vagrant::Environment.new(options)
2011-12-04 03:05:50 +00:00
end
# This creates a file in the isolated environment. By default this file
# will be created in the working directory of the isolated environment.
def file(name, contents)
@workdir.join(name).open("w+") do |f|
f.write(contents)
end
end
2011-12-04 03:15:53 +00:00
def vagrantfile(contents, root=nil)
root ||= @workdir
root.join("Vagrantfile").open("w+") do |f|
2011-12-04 03:05:50 +00:00
f.write(contents)
end
end
2011-12-04 03:15:53 +00:00
def box(name, vagrantfile_contents="")
# Create the box directory
2011-12-04 03:15:53 +00:00
box_dir = boxes_dir.join(name)
box_dir.mkpath
# Create the "box.ovf" file because that is how Vagrant heuristically
# determines a box is a V1 box.
box_dir.join("box.ovf").open("w") { |f| f.write("") }
# Populate the vagrantfile
2011-12-04 03:15:53 +00:00
vagrantfile(vagrantfile_contents, box_dir)
# Return the directory
2012-01-09 07:04:23 +00:00
box_dir
2011-12-04 03:15:53 +00:00
end
# Create an alias because "box" makes a V1 box, so "box1"
alias :box1 :box
# Creates a fake box to exist in this environment.
#
# @param [String] name Name of the box
# @param [Symbol] provider Provider the box was built for.
# @return [Pathname] Path to the box directory.
def box2(name, provider)
# Make the box directory
box_dir = boxes_dir.join(name, provider.to_s)
box_dir.mkpath
# Create a metadata.json file
box_metadata_file = box_dir.join("metadata.json")
box_metadata_file.open("w") do |f|
f.write("")
end
# Return the box directory
box_dir
end
2011-12-04 03:15:53 +00:00
def boxes_dir
dir = @homedir.join("boxes")
dir.mkpath
2011-12-04 03:15:53 +00:00
dir
end
2011-12-04 03:05:50 +00:00
end
end