Module: Vagrant::TestHelpers
- Defined in:
- lib/vagrant/test_helpers.rb
Overview
Test helpers provided by Vagrant to allow for plugin developers
to write automated tests for their code. This module simply provides
methods which can be included into any test framework (test/unit
,
RSpec, Shoulda, etc.)
Instance Method Summary collapse
-
#action_env(v_env = nil) ⇒ Object
Returns a blank app (callable) and action environment with the given vagrant environment.
-
#boxes_path ⇒ Pathname
Path to the boxes directory in the home directory.
-
#clean_paths ⇒ Object
Cleans all the test temp paths, which includes the boxes path, home path, etc.
-
#home_path ⇒ Pathname
Path to the "home" directory for the tests.
-
#tmp_path ⇒ Pathname
------------------------------------------------------------ Path helpers ------------------------------------------------------------ Path to the tmp directory for the tests.
-
#vagrant_app(*path) ⇒ Object
------------------------------------------------------------ Environment creation helpers ------------------------------------------------------------ Creates a "vagrant_app" directory in the test tmp folder which can be used for creating test Vagrant environments.
-
#vagrant_box(name) ⇒ Pathname
Creates the folder to contain a vagrant box.
-
#vagrant_env(*args) ⇒ Object
Creates and loads a Vagrant environment at the given path.
-
#vagrant_mock_downloader(klass) ⇒ Array
Returns an instantiated downloader with a mocked tempfile which can be passed into it.
-
#vagrantfile(*args) ⇒ Object
Creates a Vagrantfile with the given contents in the given app directory.
Instance Method Details
#action_env(v_env = nil) ⇒ Object
Returns a blank app (callable) and action environment with the given vagrant environment. This allows for testing of middlewares.
79 80 81 82 83 84 85 |
# File 'lib/vagrant/test_helpers.rb', line 79 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 |
#boxes_path ⇒ Pathname
Path to the boxes directory in the home directory
111 112 113 114 115 |
# File 'lib/vagrant/test_helpers.rb', line 111 def boxes_path result = home_path.join("boxes") FileUtils.mkdir_p(result) result end |
#clean_paths ⇒ Object
Cleans all the test temp paths, which includes the boxes path, home path, etc. This allows for cleaning between tests.
119 120 121 122 123 124 125 126 |
# File 'lib/vagrant/test_helpers.rb', line 119 def clean_paths FileUtils.rm_rf(tmp_path) # Call these methods only to rebuild the directories tmp_path home_path boxes_path end |
#home_path ⇒ Pathname
Path to the "home" directory for the tests
102 103 104 105 106 |
# File 'lib/vagrant/test_helpers.rb', line 102 def home_path result = tmp_path.join("home") FileUtils.mkdir_p(result) result end |
#tmp_path ⇒ Pathname
Path helpers
Path to the tmp directory for the tests.
93 94 95 96 97 |
# File 'lib/vagrant/test_helpers.rb', line 93 def tmp_path result = Vagrant.source_root.join("test", "tmp") FileUtils.mkdir_p(result) result end |
#vagrant_app(*path) ⇒ Object
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. This typically doesn't need to be called directly unless you're setting up a custom application. See the examples for common use cases.
15 16 17 18 19 20 |
# File 'lib/vagrant/test_helpers.rb', line 15 def vagrant_app(*path) root = tmp_path.join("vagrant_app") FileUtils.rm_rf(root) FileUtils.mkdir_p(root) root.join(*path) end |
#vagrant_box(name) ⇒ Pathname
Creates the folder to contain a vagrant box. This allows for "fake" boxes to be made with the specified name.
58 59 60 61 62 |
# File 'lib/vagrant/test_helpers.rb', line 58 def vagrant_box(name) result = boxes_path.join(name) FileUtils.mkdir_p(result) result end |
#vagrant_env(*args) ⇒ Object
Creates and loads a Vagrant environment at the given path. If no path is given, then a default #vagrantfile is used.
47 48 49 50 51 |
# File 'lib/vagrant/test_helpers.rb', line 47 def vagrant_env(*args) path = args.shift if Pathname === args.first path ||= vagrantfile Vagrant::Environment.new(:cwd => path).load! end |
#vagrant_mock_downloader(klass) ⇒ Array
Returns an instantiated downloader with a mocked tempfile which can be passed into it.
69 70 71 72 73 74 75 |
# File 'lib/vagrant/test_helpers.rb', line 69 def vagrant_mock_downloader(klass) tempfile = mock("tempfile") tempfile.stubs(:write) _, env = action_env [klass.new(env), tempfile] end |
#vagrantfile(*args) ⇒ Object
Creates a Vagrantfile with the given contents in the given app directory. If no app directory is specified, then a default Vagrant app is used.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vagrant/test_helpers.rb', line 25 def vagrantfile(*args) path = args.shift.join("Vagrantfile") if Pathname === args.first path ||= vagrant_app("Vagrantfile") # Create this box so that it exists vagrant_box("base") str = args.shift || "" File.open(path.to_s, "w") do |f| f.puts "ENV['VAGRANT_HOME'] = '#{home_path}'" f.puts "Vagrant::Config.run do |config|" f.puts "config.vm.base_mac = 'foo' if !config.vm.base_mac" f.puts "config.vm.box = 'base'" f.puts str f.puts "end" end path.parent end |