From bae6c57230c682ccc94ddc214e1cc89744614f44 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 15 Sep 2010 09:19:38 -0600 Subject: [PATCH] Expose test helpers [closes GH-162] --- CHANGELOG.md | 2 + lib/vagrant.rb | 1 + lib/vagrant/test_helpers.rb | 91 +++++++++++++++++++++++++++++++++++++ test/support/environment.rb | 46 ------------------- test/support/objects.rb | 13 ------ test/support/path.rb | 36 --------------- test/test_helper.rb | 7 +-- 7 files changed, 95 insertions(+), 101 deletions(-) create mode 100644 lib/vagrant/test_helpers.rb delete mode 100644 test/support/environment.rb delete mode 100644 test/support/objects.rb delete mode 100644 test/support/path.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cd80624b..2d3b4abc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 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` file somewhere in their load path. Please read the documentation on vagrantup.com before attempting to create a plugin (which is very easy) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index e1a9ece92..b2a1c37af 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -15,6 +15,7 @@ module Vagrant autoload :DataStore, 'vagrant/data_store' autoload :Errors, 'vagrant/errors' autoload :Plugin, 'vagrant/plugin' + autoload :TestHelpers, 'vagrant/test_helpers' autoload :Util, 'vagrant/util' module Command diff --git a/lib/vagrant/test_helpers.rb b/lib/vagrant/test_helpers.rb new file mode 100644 index 000000000..f89981fb8 --- /dev/null +++ b/lib/vagrant/test_helpers.rb @@ -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 diff --git a/test/support/environment.rb b/test/support/environment.rb deleted file mode 100644 index 65eca7e8b..000000000 --- a/test/support/environment.rb +++ /dev/null @@ -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 diff --git a/test/support/objects.rb b/test/support/objects.rb deleted file mode 100644 index 1f44f177d..000000000 --- a/test/support/objects.rb +++ /dev/null @@ -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 diff --git a/test/support/path.rb b/test/support/path.rb deleted file mode 100644 index a84170456..000000000 --- a/test/support/path.rb +++ /dev/null @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index e42ff6692..1774ff89d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,9 +5,6 @@ require 'vagrant' require 'mario' require 'contest' require 'mocha' -require 'support/path' -require 'support/environment' -require 'support/objects' # 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 @@ -24,9 +21,7 @@ Mario::Platform.logger(nil) I18n.load_path << File.expand_path("../locales/en.yml", __FILE__) class Test::Unit::TestCase - include VagrantTestHelpers::Path - include VagrantTestHelpers::Environment - include VagrantTestHelpers::Objects + include Vagrant::TestHelpers # Sets up the mocks for a VM def mock_vm(env=nil)