Require root path on environment load

This commit is contained in:
Mitchell Hashimoto 2010-09-07 10:52:58 -07:00
parent baccbd047d
commit 5f6e3acf40
11 changed files with 60 additions and 65 deletions

View File

@ -91,7 +91,7 @@ module Vagrant
# The options on the hash get priority, then the default # The options on the hash get priority, then the default
# values # values
value = opts[key] || @env["config"].nfs.send(key) value = opts.has_key?(key) ? opts[key] : @env["config"].nfs.send(key)
return value if value != :auto return value if value != :auto
# Get UID/GID from folder if we've made it this far # Get UID/GID from folder if we've made it this far

View File

@ -9,15 +9,9 @@ module Vagrant
@env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell) @env.ui = UI::Shell.new(@env, shell) if !@env.ui.is_a?(UI::Shell)
end end
def require_environment
raise Errors::NoEnvironmentError.new if !env.root_path
end
# This returns an array of {VM} objects depending on the arguments # This returns an array of {VM} objects depending on the arguments
# given to the command. # given to the command.
def target_vms def target_vms
require_environment
@target_vms ||= begin @target_vms ||= begin
if env.multivm? if env.multivm?
return env.vms.values if !self.name return env.vms.values if !self.name

View File

@ -209,6 +209,7 @@ module Vagrant
def load! def load!
if !loaded? if !loaded?
@loaded = true @loaded = true
raise Errors::NoEnvironmentError.new if !root_path
self.class.check_virtualbox! self.class.check_virtualbox!
load_config! load_config!
load_vm! load_vm!

13
test/support/objects.rb Normal file
View File

@ -0,0 +1,13 @@
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

@ -7,6 +7,7 @@ require 'contest'
require 'mocha' require 'mocha'
require 'support/path' require 'support/path'
require 'support/environment' 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
@ -25,6 +26,7 @@ I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)
class Test::Unit::TestCase class Test::Unit::TestCase
include VagrantTestHelpers::Path include VagrantTestHelpers::Path
include VagrantTestHelpers::Environment include VagrantTestHelpers::Environment
include VagrantTestHelpers::Objects
# Mocks an environment, setting it up with the given config. # Mocks an environment, setting it up with the given config.
def mock_environment def mock_environment
@ -102,9 +104,10 @@ class Test::Unit::TestCase
vm vm
end end
def mock_action_data def mock_action_data(v_env=nil)
v_env ||= vagrant_env
app = lambda { |env| } app = lambda { |env| }
env = Vagrant::Action::Environment.new(mock_environment) env = Vagrant::Action::Environment.new(v_env)
env["vagrant.test"] = true env["vagrant.test"] = true
[app, env] [app, env]
end end

View File

@ -3,8 +3,6 @@ require 'test_helper'
class CheckBoxVMActionTest < Test::Unit::TestCase class CheckBoxVMActionTest < Test::Unit::TestCase
setup do setup do
@klass = Vagrant::Action::VM::CheckBox @klass = Vagrant::Action::VM::CheckBox
@app, @env = mock_action_data
@instance = @klass.new(@app, @env)
end end
context "calling" do context "calling" do
@ -13,34 +11,48 @@ class CheckBoxVMActionTest < Test::Unit::TestCase
end end
should "raise error if box not specified" do should "raise error if box not specified" do
@env.env.config.vm.box = nil app, env = action_env(vagrant_env(vagrantfile(<<-vf)))
@app.expects(:call).never config.vm.box = nil
vf
instance = @klass.new(app, env)
app.expects(:call).never
assert_raises(Vagrant::Errors::BoxNotSpecified) { assert_raises(Vagrant::Errors::BoxNotSpecified) {
@instance.call(@env) instance.call(env)
} }
end end
should "error if box does not exist and URL not specified" do should "error if box does not exist and URL not specified" do
@env.env.config.vm.box_url = nil app, env = action_env(vagrant_env(vagrantfile(<<-vf)))
Vagrant::Box.expects(:find).with(@env.env, @env["config"].vm.box).returns(nil) config.vm.box = "yo"
config.vm.box_url = nil
vf
instance = @klass.new(app, env)
app.expects(:call).never
Vagrant::Box.expects(:find).with(env.env, env["config"].vm.box).returns(nil)
@app.expects(:call).never
assert_raises(Vagrant::Errors::BoxSpecifiedDoesntExist) { assert_raises(Vagrant::Errors::BoxSpecifiedDoesntExist) {
@instance.call(@env) instance.call(env)
} }
end end
should "attempt to download box and continue if URL specified" do should "attempt to download box and continue if URL specified" do
app, env = action_env(vagrant_env(vagrantfile(<<-vf)))
config.vm.box = "yo"
config.vm.box_url = "http://google.com"
vf
instance = @klass.new(app, env)
seq = sequence("seq") seq = sequence("seq")
@env.env.config.vm.box_url = "bar"
Vagrant::Box.expects(:find).returns(nil) Vagrant::Box.expects(:find).returns(nil)
Vagrant::Box.expects(:add).with(@env.env, @env["config"].vm.box, @env["config"].vm.box_url).in_sequence(seq) Vagrant::Box.expects(:add).with(env.env, env["config"].vm.box, env["config"].vm.box_url).in_sequence(seq)
@env.env.expects(:load_box!).in_sequence(seq) env.env.expects(:load_box!).in_sequence(seq)
@app.expects(:call).with(@env).once.in_sequence(seq) app.expects(:call).with(env).once.in_sequence(seq)
assert_nothing_raised { assert_nothing_raised {
@instance.call(@env) instance.call(env)
} }
end end
end end

View File

@ -3,7 +3,7 @@ require "test_helper"
class NFSVMActionTest < Test::Unit::TestCase class NFSVMActionTest < Test::Unit::TestCase
setup do setup do
@klass = Vagrant::Action::VM::NFS @klass = Vagrant::Action::VM::NFS
@app, @env = mock_action_data @app, @env = action_env
@vm = mock("vm") @vm = mock("vm")
@vm.stubs(:system).returns(mock("system")) @vm.stubs(:system).returns(mock("system"))
@ -109,11 +109,12 @@ class NFSVMActionTest < Test::Unit::TestCase
end end
should "return nil if the perm is not set" do should "return nil if the perm is not set" do
@env.env.config.nfs.map_uid = nil
assert_nil @instance.prepare_permission(:uid, {:gid => 7}) assert_nil @instance.prepare_permission(:uid, {:gid => 7})
end end
should "return nil if the perm explicitly says nil" do should "return nil if the perm explicitly says nil" do
assert_nil @instance.prepare_permission(:uid, {:uid => nil}) assert_nil @instance.prepare_permission(:uid, {:map_uid => nil})
end end
should "return the set value if it is set" do should "return the set value if it is set" do

View File

@ -24,22 +24,6 @@ class CommandHelpersTest < Test::Unit::TestCase
end end
end end
context "requiring environment" do
setup do
@env = mock_environment
end
should "raise an exception if no environment" do
@env.stubs(:root_path).returns(nil)
assert_raises(Vagrant::Errors::NoEnvironmentError) { command([], @env).require_environment }
end
should "not raise an exception if there is an environment" do
@env.stubs(:root_path).returns(7)
assert_nothing_raised { command([], @env).require_environment }
end
end
context "vms from args" do context "vms from args" do
setup do setup do
@env = mock_environment @env = mock_environment

View File

@ -433,12 +433,6 @@ class EnvironmentTest < Test::Unit::TestCase
end end
context "loading box" do context "loading box" do
should "do nothing if the root path is nil" do
env = @klass.new(:cwd => "/")
Vagrant::Box.expects(:find).never
env.load_box!
end
should "not load the box if its not set" do should "not load the box if its not set" do
env = vagrant_env env = vagrant_env
assert env.config.vm.box.nil? assert env.config.vm.box.nil?
@ -459,11 +453,7 @@ class EnvironmentTest < Test::Unit::TestCase
context "loading the UUID out from the persisted dotfile" do context "loading the UUID out from the persisted dotfile" do
setup do setup do
@local_data = {} @env = vagrant_env
@env = mock_environment
@env.stubs(:root_path).returns("foo")
@env.stubs(:local_data).returns(@local_data)
end end
should "blank the VMs" do should "blank the VMs" do
@ -474,13 +464,13 @@ class EnvironmentTest < Test::Unit::TestCase
end end
should "load all the VMs from the dotfile" do should "load all the VMs from the dotfile" do
@local_data[:active] = { :foo => "bar", :bar => "baz" } @env.local_data[:active] = { "foo" => "bar", "bar" => "baz" }
results = {} results = {}
@local_data[:active].each do |key, value| @env.local_data[:active].each do |key, value|
vm = mock("vm#{key}") vm = mock("vm#{key}")
Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm) Vagrant::VM.expects(:find).with(value, @env, key.to_sym).returns(vm)
results[key] = vm results[key.to_sym] = vm
end end
@env.load_vm! @env.load_vm!
@ -502,7 +492,7 @@ class EnvironmentTest < Test::Unit::TestCase
end end
should "uuid should be nil if local data contains nothing" do should "uuid should be nil if local data contains nothing" do
assert @local_data.empty? # sanity assert @env.local_data.empty? # sanity
@env.load_vm! @env.load_vm!
assert_nil @env.vm assert_nil @env.vm
end end

View File

@ -3,10 +3,8 @@ require "test_helper"
class BSDHostTest < Test::Unit::TestCase class BSDHostTest < Test::Unit::TestCase
setup do setup do
@klass = Vagrant::Hosts::BSD @klass = Vagrant::Hosts::BSD
@env = mock_environment @env = vagrant_env
@env.stubs(:vm).returns(Vagrant::VM.new(:env => @env)) @instance = @klass.new(@env.vms.values.first.env)
@instance = @klass.new(@env)
end end
context "supporting nfs check" do context "supporting nfs check" do
@ -39,7 +37,7 @@ class BSDHostTest < Test::Unit::TestCase
should "output the lines of the rendered template" do should "output the lines of the rendered template" do
output = %W[foo bar baz].join("\n") output = %W[foo bar baz].join("\n")
Vagrant::Util::TemplateRenderer.expects(:render).with("nfs/exports", Vagrant::Util::TemplateRenderer.expects(:render).with("nfs/exports",
:uuid => @env.vm.uuid, :uuid => @instance.env.vm.uuid,
:ip => @ip, :ip => @ip,
:folders => @folders).returns(output) :folders => @folders).returns(output)

View File

@ -3,10 +3,9 @@ require "test_helper"
class LinuxHostTest < Test::Unit::TestCase class LinuxHostTest < Test::Unit::TestCase
setup do setup do
@klass = Vagrant::Hosts::Linux @klass = Vagrant::Hosts::Linux
@env = mock_environment @env = vagrant_env
@env.stubs(:vm).returns(Vagrant::VM.new(:env => @env))
@instance = @klass.new(@env) @instance = @klass.new(@env.vms.values.first.env)
end end
context "supporting nfs check" do context "supporting nfs check" do
@ -39,7 +38,7 @@ class LinuxHostTest < Test::Unit::TestCase
should "output the lines of the rendered template" do should "output the lines of the rendered template" do
output = %W[foo bar baz].join("\n") output = %W[foo bar baz].join("\n")
Vagrant::Util::TemplateRenderer.expects(:render).with("nfs/exports_linux", Vagrant::Util::TemplateRenderer.expects(:render).with("nfs/exports_linux",
:uuid => @env.vm.uuid, :uuid => @instance.env.vm.uuid,
:ip => @ip, :ip => @ip,
:folders => @folders).returns(output) :folders => @folders).returns(output)