Removing references to Vagrant::Env

This commit is contained in:
Mitchell Hashimoto 2010-03-19 21:15:45 -07:00
parent bafd15661d
commit 5db99c04d4
13 changed files with 65 additions and 47 deletions

View File

@ -51,7 +51,7 @@ module Vagrant
def with_tempfile def with_tempfile
logger.info "Creating tempfile for storing box file..." 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 yield tempfile
end end
end end

View File

@ -5,7 +5,7 @@ module Vagrant
def prepare def prepare
# If the dotfile is not a file, raise error # If the dotfile is not a file, raise error
if File.exist?(@runner.env.dotfile_path) && !File.file?(@runner.env.dotfile_path) 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 end
# Up is a "meta-action" so it really just queues up a bunch # Up is a "meta-action" so it really just queues up a bunch

View File

@ -12,7 +12,7 @@ module Vagrant
# begin using vagrant. The configuration file contains some documentation # begin using vagrant. The configuration file contains some documentation
# to get you started. # to get you started.
def init(default_box=nil) 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) if File.exist?(rootfile_path)
error_and_exit(:rootfile_already_exists) error_and_exit(:rootfile_already_exists)
end end
@ -20,7 +20,7 @@ module Vagrant
# Copy over the rootfile template into this directory # Copy over the rootfile template into this directory
default_box ||= "base" default_box ||= "base"
File.open(rootfile_path, 'w+') do |f| 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
end end

View File

@ -9,17 +9,20 @@ module Vagrant
@@config = nil @@config = nil
class << self class << self
def reset! def reset!(env=nil)
@@config = nil @@config = nil
proc_stack.clear proc_stack.clear
# Reset the configuration to the specified environment
config(env)
end end
def configures(key, klass) def configures(key, klass)
config.class.configures(key, klass) config.class.configures(key, klass)
end end
def config def config(env=nil)
@@config ||= Config::Top.new @@config ||= Config::Top.new(env)
end end
def run(&block) def run(&block)
@ -36,6 +39,8 @@ module Vagrant
class Config class Config
class Base class Base
attr_accessor :env
def [](key) def [](key)
send(key) send(key)
end end
@ -62,7 +67,7 @@ module Vagrant
attr_accessor :private_key_path attr_accessor :private_key_path
def 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
end end
@ -108,11 +113,11 @@ module Vagrant
end end
def shared_folder_uid def shared_folder_uid
@shared_folder_uid || Vagrant.config.ssh.username @shared_folder_uid || env.config.ssh.username
end end
def shared_folder_gid def shared_folder_gid
@shared_folder_gid || Vagrant.config.ssh.username @shared_folder_gid || env.config.ssh.username
end end
def customize(&block) def customize(&block)
@ -155,12 +160,15 @@ module Vagrant
configures :vm, VMConfig configures :vm, VMConfig
configures :vagrant, VagrantConfig configures :vagrant, VagrantConfig
def initialize def initialize(env=nil)
self.class.configures_list.each do |key, klass| 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 end
@loaded = false @loaded = false
@env = env
end end
def loaded? def loaded?

View File

@ -125,7 +125,7 @@ module Vagrant
load_paths << File.join(root_path, ROOTFILE_NAME) if root_path load_paths << File.join(root_path, ROOTFILE_NAME) if root_path
# Clear out the old data # Clear out the old data
Config.reset! Config.reset!(self)
# Load each of the config files in order # Load each of the config files in order
load_paths.each do |path| load_paths.each do |path|

View File

@ -28,7 +28,7 @@
\nvagrant box list" \nvagrant box list"
:dotfile_error: "The dotfile which Vagrant uses to store the UUID of the project's :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 \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` \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 :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 \nalready be created, but unfortunately this vagrant still appears to
\nhave no box! You can setup the environment by setting up your \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_include_file_doesnt_exist: "File specified to include: '<%= filename %>' does not exist!"
:package_requires_export: "Package must be used in conjunction with export." :package_requires_export: "Package must be used in conjunction with export."
:provisioner_invalid_class: "Provisioners must be an instance of Vagrant::Provisioners::Base" :provisioner_invalid_class: "Provisioners must be an instance of Vagrant::Provisioners::Base"
:provisioner_unknown_type: "Unknown provisioner type: <%= provisioner %>" :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.)" \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 \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." \nroot."
:ssh_bad_permissions: "The private key to connect to this box via SSH has invalid permissions :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 \nset on it. The permissions of the private key should be set to 0600, otherwise SSH will

View File

@ -20,7 +20,9 @@ require 'mocha'
class Test::Unit::TestCase class Test::Unit::TestCase
# 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
Vagrant::Config.reset! environment = Vagrant::Environment.new
Vagrant::Config.reset!(environment)
Vagrant::Config.run do |config| Vagrant::Config.run do |config|
config.vagrant.dotfile_name = ".vagrant" config.vagrant.dotfile_name = ".vagrant"
@ -66,7 +68,6 @@ class Test::Unit::TestCase
config = Vagrant::Config.execute! config = Vagrant::Config.execute!
environment = Vagrant::Environment.new
environment.instance_variable_set(:@config, config) environment.instance_variable_set(:@config, config)
environment environment
end end

View File

@ -8,7 +8,7 @@ class DownloadBoxActionTest < Test::Unit::TestCase
@runner.stubs(:temp_path=) @runner.stubs(:temp_path=)
mock_config mock_config
Vagrant::Env.stubs(:tmp_path).returns("foo") @runner.env.stubs(:tmp_path).returns("foo")
end end
context "preparing" do context "preparing" do
@ -81,7 +81,7 @@ class DownloadBoxActionTest < Test::Unit::TestCase
context "tempfile" do context "tempfile" do
should "create a tempfile in the vagrant tmp directory" 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 @action.with_tempfile
end end

View File

@ -7,7 +7,7 @@ class UnpackageBoxActionTest < Test::Unit::TestCase
@runner.stubs(:temp_path).returns("bar") @runner.stubs(:temp_path).returns("bar")
mock_config mock_config
Vagrant::Env.stubs(:boxes_path).returns("bar") @runner.env.stubs(:boxes_path).returns("bar")
end end
context "executing" do context "executing" do

View File

@ -2,12 +2,8 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class CommandsTest < Test::Unit::TestCase class CommandsTest < Test::Unit::TestCase
setup do setup do
Vagrant::Env.stubs(:load!)
@persisted_vm = mock("persisted_vm") @persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!) @persisted_vm.stubs(:execute!)
Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm)
Vagrant::Env.stubs(:require_persisted_vm)
@env = mock_environment @env = mock_environment
@env.stubs(:vm).returns(@persisted_vm) @env.stubs(:vm).returns(@persisted_vm)
@ -20,7 +16,7 @@ class CommandsTest < Test::Unit::TestCase
@file = mock("file") @file = mock("file")
@file.stubs(:write) @file.stubs(:write)
File.stubs(:open).yields(@file) 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) Vagrant::Util::TemplateRenderer.stubs(:render)
end end
@ -42,12 +38,12 @@ class CommandsTest < Test::Unit::TestCase
should "use the given base box if given" do should "use the given base box if given" do
box = "zooo" 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) Vagrant::Commands.init(box)
end end
should "use the default `base` if no box is given" do 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 Vagrant::Commands.init
end end
end end

View File

@ -2,10 +2,14 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class ConfigTest < Test::Unit::TestCase class ConfigTest < Test::Unit::TestCase
context "the ssh config" do 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 should "expand any path when requesting the value" do
Vagrant::Env.stubs(:root_path).returns('foo') result = File.expand_path(@env.config.ssh[:private_key_path], @env.root_path)
File.stubs(:expand_path).with(Vagrant.config.ssh[:private_key_path], 'foo').returns('success') assert_equal result, @env.config.ssh.private_key_path
assert Vagrant.config.ssh.private_key_path, 'success'
end end
end end
@ -40,6 +44,12 @@ class ConfigTest < Test::Unit::TestCase
Vagrant::Config.reset! Vagrant::Config.reset!
assert Vagrant::Config.proc_stack.empty? assert Vagrant::Config.proc_stack.empty?
end 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 end
context "accessing configuration" do context "accessing configuration" do
@ -144,21 +154,25 @@ class ConfigTest < Test::Unit::TestCase
end end
should "initialize each configurer and set it to its key" do should "initialize each configurer and set it to its key" do
env = mock('env')
5.times do |i| 5.times do |i|
key = "key#{i}" key = "key#{i}"
klass = mock("klass#{i}") klass = mock("klass#{i}")
instance = mock("instance#{i}") instance = mock("instance#{i}")
instance.expects(:env=).with(env)
klass.expects(:new).returns(instance) klass.expects(:new).returns(instance)
@configures_list << [key, klass] @configures_list << [key, klass]
end end
Vagrant::Config::Top.new Vagrant::Config::Top.new(env)
end end
should "allow reading via methods" do should "allow reading via methods" do
key = "my_foo_bar_key" key = "my_foo_bar_key"
klass = mock("klass") klass = mock("klass")
instance = mock("instance") instance = mock("instance")
instance.stubs(:env=)
klass.expects(:new).returns(instance) klass.expects(:new).returns(instance)
Vagrant::Config::Top.configures(key, klass) Vagrant::Config::Top.configures(key, klass)
@ -202,12 +216,9 @@ class ConfigTest < Test::Unit::TestCase
context "VM configuration" do context "VM configuration" do
setup do setup do
@config = Vagrant::Config::VMConfig.new @env = mock_environment
@username = "bob" @config = @env.config.vm
@env.config.ssh.username = @username
mock_config do |config|
config.ssh.username = @username
end
end end
should "include the stacked proc runner module" do should "include the stacked proc runner module" do

View File

@ -14,7 +14,7 @@ class EnvironmentTest < Test::Unit::TestCase
should "not error and exit if everything is good" do should "not error and exit if everything is good" do
VirtualBox::Command.expects(:version).returns("3.1.4") VirtualBox::Command.expects(:version).returns("3.1.4")
VirtualBox::Global.expects(:vboxconfig?).returns(true) VirtualBox::Global.expects(:vboxconfig?).returns(true)
Vagrant::Env.expects(:error_and_exit).never Vagrant::Environment.expects(:error_and_exit).never
Vagrant::Environment.check_virtualbox! Vagrant::Environment.check_virtualbox!
end end
@ -194,10 +194,10 @@ class EnvironmentTest < Test::Unit::TestCase
should "should set the path for the rootfile" do should "should set the path for the rootfile" do
path = "/foo" 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 @env.load_root_path!(Pathname.new(path))
assert_equal path, Vagrant::Env.root_path assert_equal path, @env.root_path
end end
end end
@ -214,7 +214,7 @@ class EnvironmentTest < Test::Unit::TestCase
end end
should "reset the configuration object" do should "reset the configuration object" do
Vagrant::Config.expects(:reset!).once Vagrant::Config.expects(:reset!).with(@env).once
@env.load_config! @env.load_config!
end end
@ -293,7 +293,7 @@ class EnvironmentTest < Test::Unit::TestCase
should "create each directory if it doesn't exist" do should "create each directory if it doesn't exist" do
create_seq = sequence("create_seq") create_seq = sequence("create_seq")
File.stubs(:directory?).returns(false) 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) FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq)
end end

View File

@ -6,7 +6,9 @@ class VMTest < Test::Unit::TestCase
mock_config mock_config
@persisted_vm = mock("persisted_vm") @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) Net::SSH.stubs(:start)
end end