From 5db99c04d4b2ffff14b2006fbf589a2f88fa867f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Mar 2010 21:15:45 -0700 Subject: [PATCH] Removing references to Vagrant::Env --- lib/vagrant/actions/box/download.rb | 2 +- lib/vagrant/actions/vm/up.rb | 2 +- lib/vagrant/commands.rb | 4 +-- lib/vagrant/config.rb | 24 +++++++++++------ lib/vagrant/environment.rb | 2 +- templates/errors.yml | 10 +++---- test/test_helper.rb | 5 ++-- test/vagrant/actions/box/download_test.rb | 4 +-- test/vagrant/actions/box/unpackage_test.rb | 2 +- test/vagrant/commands_test.rb | 10 +++---- test/vagrant/config_test.rb | 31 +++++++++++++++------- test/vagrant/environment_test.rb | 12 ++++----- test/vagrant/vm_test.rb | 4 ++- 13 files changed, 65 insertions(+), 47 deletions(-) diff --git a/lib/vagrant/actions/box/download.rb b/lib/vagrant/actions/box/download.rb index 0e307a39a..89115727f 100644 --- a/lib/vagrant/actions/box/download.rb +++ b/lib/vagrant/actions/box/download.rb @@ -51,7 +51,7 @@ module Vagrant def with_tempfile logger.info "Creating tempfile for storing box file..." - Tempfile.open(BASENAME, Env.tmp_path) do |tempfile| + Tempfile.open(BASENAME, @runner.env.tmp_path) do |tempfile| yield tempfile end end diff --git a/lib/vagrant/actions/vm/up.rb b/lib/vagrant/actions/vm/up.rb index 5a128cd3d..6c9542085 100644 --- a/lib/vagrant/actions/vm/up.rb +++ b/lib/vagrant/actions/vm/up.rb @@ -5,7 +5,7 @@ module Vagrant def prepare # If the dotfile is not a file, raise error if File.exist?(@runner.env.dotfile_path) && !File.file?(@runner.env.dotfile_path) - raise ActionException.new(:dotfile_error) + raise ActionException.new(:dotfile_error, :env => @runner.env) end # Up is a "meta-action" so it really just queues up a bunch diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 692a57016..3e5d42cd7 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -12,7 +12,7 @@ module Vagrant # begin using vagrant. The configuration file contains some documentation # to get you started. def init(default_box=nil) - rootfile_path = File.join(Dir.pwd, Env::ROOTFILE_NAME) + rootfile_path = File.join(Dir.pwd, Environment::ROOTFILE_NAME) if File.exist?(rootfile_path) error_and_exit(:rootfile_already_exists) end @@ -20,7 +20,7 @@ module Vagrant # Copy over the rootfile template into this directory default_box ||= "base" File.open(rootfile_path, 'w+') do |f| - f.write(TemplateRenderer.render(Env::ROOTFILE_NAME, :default_box => default_box)) + f.write(TemplateRenderer.render(Environment::ROOTFILE_NAME, :default_box => default_box)) end end diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index f3a2f923f..b5028e1d8 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -9,17 +9,20 @@ module Vagrant @@config = nil class << self - def reset! + def reset!(env=nil) @@config = nil proc_stack.clear + + # Reset the configuration to the specified environment + config(env) end def configures(key, klass) config.class.configures(key, klass) end - def config - @@config ||= Config::Top.new + def config(env=nil) + @@config ||= Config::Top.new(env) end def run(&block) @@ -36,6 +39,8 @@ module Vagrant class Config class Base + attr_accessor :env + def [](key) send(key) end @@ -62,7 +67,7 @@ module Vagrant attr_accessor :private_key_path def private_key_path - File.expand_path(@private_key_path, Env.root_path) + File.expand_path(@private_key_path, env.root_path) end end @@ -108,11 +113,11 @@ module Vagrant end def shared_folder_uid - @shared_folder_uid || Vagrant.config.ssh.username + @shared_folder_uid || env.config.ssh.username end def shared_folder_gid - @shared_folder_gid || Vagrant.config.ssh.username + @shared_folder_gid || env.config.ssh.username end def customize(&block) @@ -155,12 +160,15 @@ module Vagrant configures :vm, VMConfig configures :vagrant, VagrantConfig - def initialize + def initialize(env=nil) self.class.configures_list.each do |key, klass| - instance_variable_set("@#{key}".to_sym, klass.new) + config = klass.new + config.env = env + instance_variable_set("@#{key}".to_sym, config) end @loaded = false + @env = env end def loaded? diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 3884e798e..c108eef77 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -125,7 +125,7 @@ module Vagrant load_paths << File.join(root_path, ROOTFILE_NAME) if root_path # Clear out the old data - Config.reset! + Config.reset!(self) # Load each of the config files in order load_paths.each do |path| diff --git a/templates/errors.yml b/templates/errors.yml index f01c48d0e..5f711dfbc 100644 --- a/templates/errors.yml +++ b/templates/errors.yml @@ -28,7 +28,7 @@ \nvagrant box list" :dotfile_error: "The dotfile which Vagrant uses to store the UUID of the project's \nvirtual machine already exists and is not a file! The dotfile is - \ncurrently configured to be `<%= Vagrant::Env.dotfile_path %>` + \ncurrently configured to be `<%= env.dotfile_path %>` \nTo change this value, please see `config.vagrant.dotfile_name` @@ -41,16 +41,16 @@ :environment_not_created: "The task you're trying to run requires that the vagrant environment \nalready be created, but unfortunately this vagrant still appears to \nhave no box! You can setup the environment by setting up your - \n<%= Vagrant::Env::ROOTFILE_NAME %> and running `vagrant up`" + \n<%= Vagrant::Environment::ROOTFILE_NAME %> and running `vagrant up`" :package_include_file_doesnt_exist: "File specified to include: '<%= filename %>' does not exist!" :package_requires_export: "Package must be used in conjunction with export." :provisioner_invalid_class: "Provisioners must be an instance of Vagrant::Provisioners::Base" :provisioner_unknown_type: "Unknown provisioner type: <%= provisioner %>" -:rootfile_already_exists: "It looks like this directory is already setup for vagrant! (A <%= Vagrant::Env::ROOTFILE_NAME %> +:rootfile_already_exists: "It looks like this directory is already setup for vagrant! (A <%= Vagrant::Environment::ROOTFILE_NAME %> \nalready exists.)" -:rootfile_not_found: "A `<%= Vagrant::Env::ROOTFILE_NAME %>` was not found! This file is required for vagrant to run +:rootfile_not_found: "A `<%= Vagrant::Environment::ROOTFILE_NAME %>` was not found! This file is required for vagrant to run \nsince it describes the expected environment that vagrant is supposed - \nto manage. Please create a `<%= Vagrant::Env::ROOTFILE_NAME %>` and place it in your project + \nto manage. Please create a `<%= Vagrant::Environment::ROOTFILE_NAME %>` and place it in your project \nroot." :ssh_bad_permissions: "The private key to connect to this box via SSH has invalid permissions \nset on it. The permissions of the private key should be set to 0600, otherwise SSH will diff --git a/test/test_helper.rb b/test/test_helper.rb index e759edb82..ba780bff7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -20,7 +20,9 @@ require 'mocha' class Test::Unit::TestCase # Mocks an environment, setting it up with the given config. def mock_environment - Vagrant::Config.reset! + environment = Vagrant::Environment.new + + Vagrant::Config.reset!(environment) Vagrant::Config.run do |config| config.vagrant.dotfile_name = ".vagrant" @@ -66,7 +68,6 @@ class Test::Unit::TestCase config = Vagrant::Config.execute! - environment = Vagrant::Environment.new environment.instance_variable_set(:@config, config) environment end diff --git a/test/vagrant/actions/box/download_test.rb b/test/vagrant/actions/box/download_test.rb index 63ed9ffd3..a416563bb 100644 --- a/test/vagrant/actions/box/download_test.rb +++ b/test/vagrant/actions/box/download_test.rb @@ -8,7 +8,7 @@ class DownloadBoxActionTest < Test::Unit::TestCase @runner.stubs(:temp_path=) mock_config - Vagrant::Env.stubs(:tmp_path).returns("foo") + @runner.env.stubs(:tmp_path).returns("foo") end context "preparing" do @@ -81,7 +81,7 @@ class DownloadBoxActionTest < Test::Unit::TestCase context "tempfile" do should "create a tempfile in the vagrant tmp directory" do - Tempfile.expects(:open).with(Vagrant::Actions::Box::Download::BASENAME, Vagrant::Env.tmp_path).once + Tempfile.expects(:open).with(Vagrant::Actions::Box::Download::BASENAME, @runner.env.tmp_path).once @action.with_tempfile end diff --git a/test/vagrant/actions/box/unpackage_test.rb b/test/vagrant/actions/box/unpackage_test.rb index 80fca672e..ff5141bec 100644 --- a/test/vagrant/actions/box/unpackage_test.rb +++ b/test/vagrant/actions/box/unpackage_test.rb @@ -7,7 +7,7 @@ class UnpackageBoxActionTest < Test::Unit::TestCase @runner.stubs(:temp_path).returns("bar") mock_config - Vagrant::Env.stubs(:boxes_path).returns("bar") + @runner.env.stubs(:boxes_path).returns("bar") end context "executing" do diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index c706459c1..0f028f1a8 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -2,12 +2,8 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class CommandsTest < Test::Unit::TestCase setup do - Vagrant::Env.stubs(:load!) - @persisted_vm = mock("persisted_vm") @persisted_vm.stubs(:execute!) - Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm) - Vagrant::Env.stubs(:require_persisted_vm) @env = mock_environment @env.stubs(:vm).returns(@persisted_vm) @@ -20,7 +16,7 @@ class CommandsTest < Test::Unit::TestCase @file = mock("file") @file.stubs(:write) File.stubs(:open).yields(@file) - @rootfile_path = File.join(Dir.pwd, Vagrant::Env::ROOTFILE_NAME) + @rootfile_path = File.join(Dir.pwd, Vagrant::Environment::ROOTFILE_NAME) Vagrant::Util::TemplateRenderer.stubs(:render) end @@ -42,12 +38,12 @@ class CommandsTest < Test::Unit::TestCase should "use the given base box if given" do box = "zooo" - Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Env::ROOTFILE_NAME, :default_box => box) + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => box) Vagrant::Commands.init(box) end should "use the default `base` if no box is given" do - Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Env::ROOTFILE_NAME, :default_box => "base") + Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Environment::ROOTFILE_NAME, :default_box => "base") Vagrant::Commands.init end end diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index c355cac6c..1fadb94d9 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -2,10 +2,14 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class ConfigTest < Test::Unit::TestCase context "the ssh config" do + setup do + @env = mock_environment + @env.stubs(:root_path).returns("foo") + end + should "expand any path when requesting the value" do - Vagrant::Env.stubs(:root_path).returns('foo') - File.stubs(:expand_path).with(Vagrant.config.ssh[:private_key_path], 'foo').returns('success') - assert Vagrant.config.ssh.private_key_path, 'success' + result = File.expand_path(@env.config.ssh[:private_key_path], @env.root_path) + assert_equal result, @env.config.ssh.private_key_path end end @@ -40,6 +44,12 @@ class ConfigTest < Test::Unit::TestCase Vagrant::Config.reset! assert Vagrant::Config.proc_stack.empty? end + + should "reload the config object based on the given environment" do + env = mock("env") + Vagrant::Config.expects(:config).with(env).once + Vagrant::Config.reset!(env) + end end context "accessing configuration" do @@ -144,21 +154,25 @@ class ConfigTest < Test::Unit::TestCase end should "initialize each configurer and set it to its key" do + env = mock('env') + 5.times do |i| key = "key#{i}" klass = mock("klass#{i}") instance = mock("instance#{i}") + instance.expects(:env=).with(env) klass.expects(:new).returns(instance) @configures_list << [key, klass] end - Vagrant::Config::Top.new + Vagrant::Config::Top.new(env) end should "allow reading via methods" do key = "my_foo_bar_key" klass = mock("klass") instance = mock("instance") + instance.stubs(:env=) klass.expects(:new).returns(instance) Vagrant::Config::Top.configures(key, klass) @@ -202,12 +216,9 @@ class ConfigTest < Test::Unit::TestCase context "VM configuration" do setup do - @config = Vagrant::Config::VMConfig.new - @username = "bob" - - mock_config do |config| - config.ssh.username = @username - end + @env = mock_environment + @config = @env.config.vm + @env.config.ssh.username = @username end should "include the stacked proc runner module" do diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index a88c410ae..723426dcb 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -14,7 +14,7 @@ class EnvironmentTest < Test::Unit::TestCase should "not error and exit if everything is good" do VirtualBox::Command.expects(:version).returns("3.1.4") VirtualBox::Global.expects(:vboxconfig?).returns(true) - Vagrant::Env.expects(:error_and_exit).never + Vagrant::Environment.expects(:error_and_exit).never Vagrant::Environment.check_virtualbox! end @@ -194,10 +194,10 @@ class EnvironmentTest < Test::Unit::TestCase should "should set the path for the rootfile" do path = "/foo" - File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true) + File.expects(:exist?).with("#{path}/#{Vagrant::Environment::ROOTFILE_NAME}").returns(true) - assert Vagrant::Env.load_root_path!(Pathname.new(path)) - assert_equal path, Vagrant::Env.root_path + assert @env.load_root_path!(Pathname.new(path)) + assert_equal path, @env.root_path end end @@ -214,7 +214,7 @@ class EnvironmentTest < Test::Unit::TestCase end should "reset the configuration object" do - Vagrant::Config.expects(:reset!).once + Vagrant::Config.expects(:reset!).with(@env).once @env.load_config! end @@ -293,7 +293,7 @@ class EnvironmentTest < Test::Unit::TestCase should "create each directory if it doesn't exist" do create_seq = sequence("create_seq") File.stubs(:directory?).returns(false) - Vagrant::Env::HOME_SUBDIRS.each do |subdir| + Vagrant::Environment::HOME_SUBDIRS.each do |subdir| FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq) end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index e167e69d7..9d468eb05 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -6,7 +6,9 @@ class VMTest < Test::Unit::TestCase mock_config @persisted_vm = mock("persisted_vm") - Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm) + + @env = mock_environment + @env.stubs(:vm).returns(@persisted_vm) Net::SSH.stubs(:start) end