Expose test helpers [closes GH-162]

This commit is contained in:
Mitchell Hashimoto 2010-09-15 09:19:38 -06:00
parent 90aaf5cb5e
commit bae6c57230
7 changed files with 95 additions and 101 deletions

View File

@ -1,5 +1,7 @@
## 0.6.0 (unreleased) ## 0.6.0 (unreleased)
- Exposed Vagrant test helpers in `Vagrant::TestHelpers` for plugins to easily
test themselves against Vagrant environments.
- **Plugins** have landed. Plugins are simply gems which have a `vagrant_init.rb` - **Plugins** have landed. Plugins are simply gems which have a `vagrant_init.rb`
file somewhere in their load path. Please read the documentation on file somewhere in their load path. Please read the documentation on
vagrantup.com before attempting to create a plugin (which is very easy) vagrantup.com before attempting to create a plugin (which is very easy)

View File

@ -15,6 +15,7 @@ module Vagrant
autoload :DataStore, 'vagrant/data_store' autoload :DataStore, 'vagrant/data_store'
autoload :Errors, 'vagrant/errors' autoload :Errors, 'vagrant/errors'
autoload :Plugin, 'vagrant/plugin' autoload :Plugin, 'vagrant/plugin'
autoload :TestHelpers, 'vagrant/test_helpers'
autoload :Util, 'vagrant/util' autoload :Util, 'vagrant/util'
module Command module Command

View File

@ -0,0 +1,91 @@
module Vagrant
module TestHelpers
#------------------------------------------------------------
# Environment creation helpers
#------------------------------------------------------------
# Creates a "vagrant_app" directory in the test tmp folder
# which can be used for creating test Vagrant environments.
# Returns the root directory of the app.
def vagrant_app(*path)
root = tmp_path.join("vagrant_app")
FileUtils.rm_rf(root)
FileUtils.mkdir_p(root)
root.join(*path)
end
# Creates a Vagrantfile with the given contents in the given
# app directory.
def vagrantfile(*args)
path = args.shift.join("Vagrantfile") if Pathname === args.first
path ||= vagrant_app("Vagrantfile")
str = args.shift || ""
File.open(path.to_s, "w") do |f|
f.puts "Vagrant::Config.run do |config|"
f.puts "config.vagrant.log_output = nil"
f.puts "config.vagrant.home = '#{home_path}'"
f.puts str
f.puts "end"
end
path.parent
end
# Creates and _loads_ a Vagrant environment at the given path
def vagrant_env(*args)
path = args.shift if Pathname === args.first
path ||= vagrantfile
Vagrant::Environment.new(:cwd => path).load!
end
# Creates the folder to contain a vagrant box
def vagrant_box(name)
result = boxes_path.join(name)
FileUtils.mkdir_p(result)
result
end
# Returns a blank app (callable) and action environment with the
# given vagrant environment.
def action_env(v_env = nil)
v_env ||= vagrant_env
app = lambda { |env| }
env = Vagrant::Action::Environment.new(v_env)
env["vagrant.test"] = true
[app, env]
end
#------------------------------------------------------------
# Path helpers
#------------------------------------------------------------
# Path to the tmp directory for the tests
def tmp_path
result = Vagrant.source_root.join("test", "tmp")
FileUtils.mkdir_p(result)
result
end
# Path to the "home" directory for the tests
def home_path
result = tmp_path.join("home")
FileUtils.mkdir_p(result)
result
end
# Path to the boxes directory in the home directory
def boxes_path
result = home_path.join("boxes")
FileUtils.mkdir_p(result)
result
end
# Cleans all the test temp paths
def clean_paths
FileUtils.rm_rf(tmp_path)
# Call these methods only to rebuild the directories
tmp_path
home_path
boxes_path
end
end
end

View File

@ -1,46 +0,0 @@
require 'fileutils'
module VagrantTestHelpers
module Environment
# Creates a "vagrant_app" directory in the test tmp folder
# which can be used for creating test Vagrant environments.
# Returns the root directory of the app.
def vagrant_app(*path)
root = tmp_path.join("vagrant_app")
FileUtils.rm_rf(root)
FileUtils.mkdir_p(root)
root.join(*path)
end
# Creates a Vagrantfile with the given contents in the given
# app directory.
def vagrantfile(*args)
path = args.shift.join("Vagrantfile") if Pathname === args.first
path ||= vagrant_app("Vagrantfile")
str = args.shift || ""
File.open(path.to_s, "w") do |f|
f.puts "Vagrant::Config.run do |config|"
f.puts "config.vagrant.log_output = nil"
f.puts "config.vagrant.home = '#{home_path}'"
f.puts str
f.puts "end"
end
path.parent
end
# Creates and _loads_ a Vagrant environment at the given path
def vagrant_env(*args)
path = args.shift if Pathname === args.first
path ||= vagrantfile
Vagrant::Environment.new(:cwd => path).load!
end
# Creates the folder to contain a vagrant box
def vagrant_box(name)
result = boxes_path.join(name)
FileUtils.mkdir_p(result)
result
end
end
end

View File

@ -1,13 +0,0 @@
module VagrantTestHelpers
module Objects
# Returns a blank app (callable) and action environment with the
# given vagrant environment.
def action_env(v_env = nil)
v_env ||= vagrant_env
app = lambda { |env| }
env = Vagrant::Action::Environment.new(v_env)
env["vagrant.test"] = true
[app, env]
end
end
end

View File

@ -1,36 +0,0 @@
require 'fileutils'
module VagrantTestHelpers
module Path
# Path to the tmp directory for the tests
def tmp_path
result = Vagrant.source_root.join("test", "tmp")
FileUtils.mkdir_p(result)
result
end
# Path to the "home" directory for the tests
def home_path
result = tmp_path.join("home")
FileUtils.mkdir_p(result)
result
end
# Path to the boxes directory in the home directory
def boxes_path
result = home_path.join("boxes")
FileUtils.mkdir_p(result)
result
end
# Cleans all the test temp paths
def clean_paths
FileUtils.rm_rf(tmp_path)
# Call these methods only to rebuild the directories
tmp_path
home_path
boxes_path
end
end
end

View File

@ -5,9 +5,6 @@ require 'vagrant'
require 'mario' require 'mario'
require 'contest' require 'contest'
require 'mocha' require 'mocha'
require 'support/path'
require 'support/environment'
require 'support/objects'
# Try to load ruby debug since its useful if it is available. # Try to load ruby debug since its useful if it is available.
# But not a big deal if its not available (probably on a non-MRI # But not a big deal if its not available (probably on a non-MRI
@ -24,9 +21,7 @@ Mario::Platform.logger(nil)
I18n.load_path << File.expand_path("../locales/en.yml", __FILE__) I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)
class Test::Unit::TestCase class Test::Unit::TestCase
include VagrantTestHelpers::Path include Vagrant::TestHelpers
include VagrantTestHelpers::Environment
include VagrantTestHelpers::Objects
# Sets up the mocks for a VM # Sets up the mocks for a VM
def mock_vm(env=nil) def mock_vm(env=nil)