From e8e2c136d3c78ecf70c6d48501838f7bff4c3265 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Mar 2010 23:56:36 -0700 Subject: [PATCH] Finally remove Vagrant::Env --- lib/vagrant/active_list.rb | 97 +++---- lib/vagrant/env.rb | 156 ----------- lib/vagrant/environment.rb | 11 +- test/vagrant/active_list_test.rb | 305 ++++++++++++---------- test/vagrant/env_test.rb | 427 ------------------------------- test/vagrant/environment_test.rb | 22 +- 6 files changed, 242 insertions(+), 776 deletions(-) delete mode 100644 lib/vagrant/env.rb delete mode 100644 test/vagrant/env_test.rb diff --git a/lib/vagrant/active_list.rb b/lib/vagrant/active_list.rb index 2979cded3..50d16a8c0 100644 --- a/lib/vagrant/active_list.rb +++ b/lib/vagrant/active_list.rb @@ -6,61 +6,68 @@ module Vagrant @@list = nil - class <] - def list(reload = false) - return @@list unless @@list.nil? || reload + # The environment this active list belongs to + attr_accessor :env - @@list ||= [] - return @@list unless File.file?(path) - File.open(path, "r") do |f| - @@list = JSON.parse(f.read) - end + # Creates the instance of the ActiveList, with the given environment + # if specified + def initialize(env=nil) + @env = env + end - @@list + # Parses and returns the list of UUIDs from the active VM + # JSON file. This will cache the result, which can be reloaded + # by setting the `reload` parameter to true. + # + # @return [Array] + def list(reload=false) + return @list unless @list.nil? || reload + + @list ||= [] + return @list unless File.file?(path) + File.open(path, "r") do |f| + @list = JSON.parse(f.read) end - # Returns an array of {Vagrant::VM} objects which are currently - # active. - def vms - list.collect { |uuid| Vagrant::VM.find(uuid) }.compact - end + @list + end - # Returns an array of UUIDs filtered so each is verified to exist. - def filtered_list - vms.collect { |vm| vm.uuid } - end + # Returns an array of {Vagrant::VM} objects which are currently + # active. + def vms + list.collect { |uuid| Vagrant::VM.find(uuid) }.compact + end - # Adds a virtual environment to the list of active virtual machines - def add(vm) - list << vm.uuid - list.uniq! - save - end + # Returns an array of UUIDs filtered so each is verified to exist. + def filtered_list + vms.collect { |vm| vm.uuid } + end - # Remove a virtual environment from the list of active virtual machines - def remove(vm) - vm = vm.uuid if vm.is_a?(Vagrant::VM) - list.delete(vm) - save - end + # Adds a virtual environment to the list of active virtual machines + def add(vm) + list << vm.uuid + list.uniq! + save + end - # Persists the list down to the JSON file. - def save - File.open(path, "w+") do |f| - f.write(filtered_list.to_json) - end - end + # Remove a virtual environment from the list of active virtual machines + def remove(vm) + vm = vm.uuid if vm.is_a?(Vagrant::VM) + list.delete(vm) + save + end - # Returns the path to the JSON file which holds the UUIDs of the - # active virtual machines managed by Vagrant. - def path - File.join(Env.home_path, FILENAME) + # Persists the list down to the JSON file. + def save + File.open(path, "w+") do |f| + f.write(filtered_list.to_json) end end + + # Returns the path to the JSON file which holds the UUIDs of the + # active virtual machines managed by Vagrant. + def path + File.join(env.home_path, FILENAME) + end end end \ No newline at end of file diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb deleted file mode 100644 index 58412f92a..000000000 --- a/lib/vagrant/env.rb +++ /dev/null @@ -1,156 +0,0 @@ -module Vagrant - class Env - ROOTFILE_NAME = "Vagrantfile" - HOME_SUBDIRS = ["tmp", "boxes"] - - # Initialize class variables used - @@persisted_vm = nil - @@root_path = nil - @@box = nil - - extend Vagrant::Util - - class << self - def box; @@box; end - def persisted_vm; @@persisted_vm; end - def root_path; @@root_path; end - def dotfile_path;File.join(root_path, Vagrant.config.vagrant.dotfile_name); end - def home_path; File.expand_path(Vagrant.config.vagrant.home); end - def tmp_path; File.join(home_path, "tmp"); end - def boxes_path; File.join(home_path, "boxes"); end - - def load! - load_root_path! - load_config! - load_home_directory! - load_box! - load_config! - check_virtualbox! - load_vm! - end - - def check_virtualbox! - version = VirtualBox::Command.version - if version.nil? - error_and_exit(:virtualbox_not_detected) - elsif version.to_f < 3.1 - error_and_exit(:virtualbox_invalid_version, :version => version.to_s) - end - - if !VirtualBox::Global.vboxconfig? - error_and_exit(:virtualbox_xml_not_detected) - end - end - - def load_config! - # Prepare load paths for config files - load_paths = [File.join(PROJECT_ROOT, "config", "default.rb")] - load_paths << File.join(box.directory, ROOTFILE_NAME) if box - load_paths << File.join(home_path, ROOTFILE_NAME) if Vagrant.config.vagrant.home - load_paths << File.join(root_path, ROOTFILE_NAME) if root_path - - # Then clear out the old data - Config.reset! - - load_paths.each do |path| - if File.exist?(path) - logger.info "Loading config from #{path}..." - load path - end - end - - # Execute the configurations - Config.execute! - end - - def load_home_directory! - home_dir = File.expand_path(Vagrant.config.vagrant.home) - - dirs = HOME_SUBDIRS.collect { |path| File.join(home_dir, path) } - dirs.unshift(home_dir) - - dirs.each do |dir| - next if File.directory?(dir) - - logger.info "Creating home directory since it doesn't exist: #{dir}" - FileUtils.mkdir_p(dir) - end - end - - def load_box! - return unless root_path - - @@box = Box.find(Vagrant.config.vm.box) if Vagrant.config.vm.box - end - - def load_vm! - return if !root_path || !File.file?(dotfile_path) - - File.open(dotfile_path) do |f| - @@persisted_vm = Vagrant::VM.find(f.read) - end - rescue Errno::ENOENT - @@persisted_vm = nil - end - - def persist_vm(vm) - # Save to the dotfile for this project - File.open(dotfile_path, 'w+') do |f| - f.write(vm.uuid) - end - - # Also add to the global store - ActiveList.add(vm) - end - - def depersist_vm(vm) - # Delete the dotfile if it exists - File.delete(dotfile_path) if File.exist?(dotfile_path) - - # Remove from the global store - ActiveList.remove(vm) - end - - def load_root_path!(path=nil) - path = Pathname.new(File.expand_path(path || Dir.pwd)) - - # Stop if we're at the root. - return false if path.root? - - file = "#{path}/#{ROOTFILE_NAME}" - if File.exist?(file) - @@root_path = path.to_s - return true - end - - load_root_path!(path.parent) - end - - def require_root_path - if !root_path - error_and_exit(:rootfile_not_found) - end - end - - def require_box - require_root_path - - if !box - if !Vagrant.config.vm.box - error_and_exit(:box_not_specified) - else - error_and_exit(:box_specified_doesnt_exist, :box_name => Vagrant.config.vm.box) - end - end - end - - def require_persisted_vm - require_root_path - - if !persisted_vm - error_and_exit(:environment_not_created) - end - end - end - end -end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 4a4ef1b9b..f82f45203 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -14,6 +14,7 @@ module Vagrant attr_reader :box attr_reader :vm attr_reader :ssh + attr_reader :active_list #--------------------------------------------------------------- # Class Methods @@ -96,6 +97,7 @@ module Vagrant self.class.check_virtualbox! load_vm! load_ssh! + load_active_list! self end @@ -185,6 +187,11 @@ module Vagrant @ssh = SSH.new(self) end + # Loads the activelist for this environment + def load_active_list! + @active_list = ActiveList.new(self) + end + #--------------------------------------------------------------- # Methods to manage VM #--------------------------------------------------------------- @@ -207,7 +214,7 @@ module Vagrant end # Also add to the global store - ActiveList.add(vm) + active_list.add(vm) end # Removes this environment's VM from the dotfile. @@ -216,7 +223,7 @@ module Vagrant File.delete(dotfile_path) if File.exist?(dotfile_path) # Remove from the global store - ActiveList.remove(vm) + active_list.remove(vm) end #--------------------------------------------------------------- diff --git a/test/vagrant/active_list_test.rb b/test/vagrant/active_list_test.rb index 9c9bb3a8e..79043bcac 100644 --- a/test/vagrant/active_list_test.rb +++ b/test/vagrant/active_list_test.rb @@ -3,167 +3,188 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class ActiveListTest < Test::Unit::TestCase setup do mock_config + + @env = mock_environment + @list = Vagrant::ActiveList.new(@env) end - context "class methods" do - context "loading" do - should "load if reload is given" do - File.stubs(:file?).returns(true) - File.expects(:open).once - Vagrant::ActiveList.list(true) - end - - should "not load if the active json file doesn't exist" do - File.expects(:file?).with(Vagrant::ActiveList.path).returns(false) - File.expects(:open).never - assert_equal [], Vagrant::ActiveList.list(true) - end - - should "parse the JSON by reading the file" do - file = mock("file") - data = mock("data") - result = mock("result") - File.expects(:file?).returns(true) - File.expects(:open).with(Vagrant::ActiveList.path, 'r').once.yields(file) - file.expects(:read).returns(data) - JSON.expects(:parse).with(data).returns(result) - assert_equal result, Vagrant::ActiveList.list(true) - end - - should "not load if reload flag is false and already loaded" do - File.expects(:file?).once.returns(false) - result = Vagrant::ActiveList.list(true) - assert result.equal?(Vagrant::ActiveList.list) - assert result.equal?(Vagrant::ActiveList.list) - assert result.equal?(Vagrant::ActiveList.list) - end + context "initializing" do + should "set the environment to nil if not specified" do + assert_nothing_raised { + list = Vagrant::ActiveList.new + assert list.env.nil? + } end - context "vms" do - setup do - @list = ["foo", "bar"] - Vagrant::ActiveList.stubs(:list).returns(@list) - end + should "set the environment to the given parameter if specified" do + env = mock("env") + list = Vagrant::ActiveList.new(env) + assert_equal env, list.env + end + end - should "return the list, but with each value as a VM" do - new_seq = sequence("new") - results = [] - @list.each do |item| - result = mock("result-#{item}") - Vagrant::VM.expects(:find).with(item).returns(result).in_sequence(new_seq) - results << result - end - - assert_equal results, Vagrant::ActiveList.vms - end - - should "compact out the nil values" do - Vagrant::VM.stubs(:find).returns(nil) - results = Vagrant::ActiveList.vms - assert results.empty? - end + context "listing" do + setup do + @path = "foo" + @list.stubs(:path).returns(@path) end - context "filtered list" do - should "return a list of UUIDs from the VMs" do - vms = [] - result = [] - 5.times do |i| - vm = mock("vm#{i}") - vm.expects(:uuid).returns(i) - result << i - vms << vm - end - - Vagrant::ActiveList.stubs(:vms).returns(vms) - assert_equal result, Vagrant::ActiveList.filtered_list - end + should "load if reload is given" do + File.stubs(:file?).returns(true) + File.expects(:open).once + @list.list(true) end - context "adding a VM to the list" do - setup do - @list = [] - Vagrant::ActiveList.stubs(:list).returns(@list) - Vagrant::ActiveList.stubs(:save) - - @uuid = "foo" - @vm = mock("vm") - @vm.stubs(:uuid).returns(@uuid) - end - - should "add the VMs UUID to the list" do - Vagrant::ActiveList.add(@vm) - assert_equal [@uuid], @list - end - - should "uniq the array so multiples never exist" do - @list << @uuid - assert_equal 1, @list.length - Vagrant::ActiveList.add(@vm) - assert_equal 1, @list.length - end - - should "save after adding" do - save_seq = sequence('save') - @list.expects(:<<).in_sequence(save_seq) - Vagrant::ActiveList.expects(:save).in_sequence(save_seq) - Vagrant::ActiveList.add(@vm) - end + should "not load if the active json file doesn't exist" do + File.expects(:file?).with(@list.path).returns(false) + File.expects(:open).never + assert_equal [], @list.list(true) end - context "deleting a VM from the list" do - setup do - @list = ["bar"] - Vagrant::ActiveList.stubs(:list).returns(@list) - Vagrant::ActiveList.stubs(:save) - - @uuid = "bar" - @vm = mock("vm") - @vm.stubs(:uuid).returns(@uuid) - @vm.stubs(:is_a?).with(Vagrant::VM).returns(true) - end - - should "delete the uuid from the list of a VM" do - Vagrant::ActiveList.remove(@vm) - assert @list.empty? - end - - should "delete just the string if a string is given" do - @list << "zoo" - Vagrant::ActiveList.remove("zoo") - assert !@list.include?("zoo") - end - - should "save after removing" do - save_seq = sequence('save') - @list.expects(:delete).in_sequence(save_seq) - Vagrant::ActiveList.expects(:save).in_sequence(save_seq) - Vagrant::ActiveList.remove(@vm) - end + should "parse the JSON by reading the file" do + file = mock("file") + data = mock("data") + result = mock("result") + File.expects(:file?).returns(true) + File.expects(:open).with(@list.path, 'r').once.yields(file) + file.expects(:read).returns(data) + JSON.expects(:parse).with(data).returns(result) + assert_equal result, @list.list(true) end - context "saving" do - setup do - @filtered = ["zoo"] - Vagrant::ActiveList.stubs(:filtered_list).returns(@filtered) - end + should "not load if reload flag is false and already loaded" do + File.expects(:file?).once.returns(false) + result = @list.list(true) + assert result.equal?(@list.list) + assert result.equal?(@list.list) + assert result.equal?(@list.list) + end + end - should "open the JSON path and save to it" do - file = mock("file") - File.expects(:open).with(Vagrant::ActiveList.path, "w+").yields(file) - file.expects(:write).with(@filtered.to_json) - Vagrant::ActiveList.save - end + context "vms" do + setup do + @the_list = ["foo", "bar"] + @list.stubs(:list).returns(@the_list) end - context "path" do - setup do - Vagrant::Env.stubs(:home_path).returns("foo") + should "return the list, but with each value as a VM" do + new_seq = sequence("new") + results = [] + @the_list.each do |item| + result = mock("result-#{item}") + Vagrant::VM.expects(:find).with(item).returns(result).in_sequence(new_seq) + results << result end - should "return the active file within the home path" do - assert_equal File.join(Vagrant::Env.home_path, Vagrant::ActiveList::FILENAME), Vagrant::ActiveList.path + assert_equal results, @list.vms + end + + should "compact out the nil values" do + Vagrant::VM.stubs(:find).returns(nil) + results = @list.vms + assert results.empty? + end + end + + context "filtered list" do + should "return a list of UUIDs from the VMs" do + vms = [] + result = [] + 5.times do |i| + vm = mock("vm#{i}") + vm.expects(:uuid).returns(i) + result << i + vms << vm end + + @list.stubs(:vms).returns(vms) + assert_equal result, @list.filtered_list + end + end + + context "adding a VM to the list" do + setup do + @the_list = [] + @list.stubs(:list).returns(@the_list) + @list.stubs(:save) + + @uuid = "foo" + @vm = mock("vm") + @vm.stubs(:uuid).returns(@uuid) + end + + should "add the VMs UUID to the list" do + @list.add(@vm) + assert_equal [@uuid], @the_list + end + + should "uniq the array so multiples never exist" do + @the_list << @uuid + assert_equal 1, @the_list.length + @list.add(@vm) + assert_equal 1, @the_list.length + end + + should "save after adding" do + save_seq = sequence('save') + @the_list.expects(:<<).in_sequence(save_seq) + @list.expects(:save).in_sequence(save_seq) + @list.add(@vm) + end + end + + context "deleting a VM from the list" do + setup do + @the_list = ["bar"] + @list.stubs(:list).returns(@the_list) + @list.stubs(:save) + + @uuid = "bar" + @vm = mock("vm") + @vm.stubs(:uuid).returns(@uuid) + @vm.stubs(:is_a?).with(Vagrant::VM).returns(true) + end + + should "delete the uuid from the list of a VM" do + @list.remove(@vm) + assert @the_list.empty? + end + + should "delete just the string if a string is given" do + @the_list << "zoo" + @list.remove("zoo") + assert !@the_list.include?("zoo") + end + + should "save after removing" do + save_seq = sequence('save') + @the_list.expects(:delete).in_sequence(save_seq) + @list.expects(:save).in_sequence(save_seq) + @list.remove(@vm) + end + end + + context "saving" do + setup do + @filtered = ["zoo"] + @list.stubs(:filtered_list).returns(@filtered) + end + + should "open the JSON path and save to it" do + file = mock("file") + File.expects(:open).with(@list.path, "w+").yields(file) + file.expects(:write).with(@filtered.to_json) + @list.save + end + end + + context "path" do + setup do + @env.stubs(:home_path).returns("foo") + end + + should "return the active file within the home path" do + assert_equal File.join(@env.home_path, Vagrant::ActiveList::FILENAME), @list.path end end end diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb deleted file mode 100644 index c25f64099..000000000 --- a/test/vagrant/env_test.rb +++ /dev/null @@ -1,427 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', 'test_helper') - -class EnvTest < Test::Unit::TestCase - def mock_persisted_vm(returnvalue="foovm") - filemock = mock("filemock") - filemock.expects(:read).returns("foo") - Vagrant::VM.expects(:find).with("foo").returns(returnvalue) - File.expects(:open).with(Vagrant::Env.dotfile_path).once.yields(filemock) - File.expects(:file?).with(Vagrant::Env.dotfile_path).once.returns(true) - Vagrant::Env.load_vm! - end - - setup do - mock_config - Vagrant::Box.stubs(:find).returns("foo") - end - - context "checking virtualbox version" do - setup do - VirtualBox::Command.stubs(:version).returns("3.1.4") - VirtualBox::Global.stubs(:vboxconfig?).returns(true) - end - - 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::Env.check_virtualbox! - end - - should "error and exit if VirtualBox is not installed or detected" do - Vagrant::Env.expects(:error_and_exit).with(:virtualbox_not_detected).once - VirtualBox::Command.expects(:version).returns(nil) - Vagrant::Env.check_virtualbox! - end - - should "error and exit if VirtualBox is lower than version 3.1" do - version = "3.0.12r1041" - Vagrant::Env.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once - VirtualBox::Command.expects(:version).returns(version) - Vagrant::Env.check_virtualbox! - end - - should "error and exit if the the vboxconfig is not set" do - VirtualBox::Global.expects(:vboxconfig?).returns(false) - Vagrant::Env.expects(:error_and_exit).with(:virtualbox_xml_not_detected).once - Vagrant::Env.check_virtualbox! - end - end - - context "requiring a VM" do - setup do - Vagrant::Env.stubs(:require_root_path) - Vagrant::Env.stubs(:error_and_exit) - end - - should "require root path" do - Vagrant::Env.expects(:require_root_path).once - Vagrant::Env.require_persisted_vm - end - - should "error and exit if no persisted VM was found" do - assert_nil Vagrant::Env.persisted_vm - Vagrant::Env.expects(:error_and_exit).with(:environment_not_created).once - Vagrant::Env.require_persisted_vm - end - - should "return and continue if persisted VM is found" do - mock_persisted_vm - Vagrant::Env.expects(:error_and_exit).never - Vagrant::Env.require_persisted_vm - end - end - - context "loading home directory" do - setup do - @home_dir = File.expand_path(Vagrant.config.vagrant.home) - - File.stubs(:directory?).returns(true) - FileUtils.stubs(:mkdir_p) - end - - 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| - FileUtils.expects(:mkdir_p).with(File.join(@home_dir, subdir)).in_sequence(create_seq) - end - - Vagrant::Env.load_home_directory! - end - - should "not create directories if they exist" do - File.stubs(:directory?).returns(true) - FileUtils.expects(:mkdir_p).never - Vagrant::Env.load_home_directory! - end - end - - context "loading config" do - setup do - @root_path = "/foo" - Vagrant::Env.stubs(:root_path).returns(@root_path) - Vagrant::Env.stubs(:box).returns(nil) - File.stubs(:exist?).returns(false) - Vagrant::Config.stubs(:execute!) - Vagrant::Config.stubs(:reset!) - end - - should "reset the configuration object" do - Vagrant::Config.expects(:reset!).once - Vagrant::Env.load_config! - end - - should "load from the project root" do - File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once - Vagrant::Env.load_config! - end - - should "load from the root path" do - File.expects(:exist?).with(File.join(@root_path, Vagrant::Env::ROOTFILE_NAME)).once - Vagrant::Env.load_config! - end - - should "load from the home directory" do - File.expects(:exist?).with(File.join(Vagrant::Env.home_path, Vagrant::Env::ROOTFILE_NAME)).once - Vagrant::Env.load_config! - end - - should "not load from the home directory if the home config is nil" do - mock_config do |config| - config.vagrant.home = nil - end - - File.expects(:exist?).with(File.join(Vagrant::Env.home_path, Vagrant::Env::ROOTFILE_NAME)).never - Vagrant::Env.load_config! - end - - should "not load from the root path if nil" do - Vagrant::Env.stubs(:root_path).returns(nil) - File.expects(:exist?).with(File.join(@root_path, Vagrant::Env::ROOTFILE_NAME)).never - Vagrant::Env.load_config! - end - - should "not load from the box directory if it is nil" do - Vagrant::Env.expects(:box).once.returns(nil) - Vagrant::Env.load_config! - end - - should "load from the box directory if it is not nil" do - dir = "foo" - box = mock("box") - box.stubs(:directory).returns(dir) - Vagrant::Env.expects(:box).twice.returns(box) - File.expects(:exist?).with(File.join(dir, Vagrant::Env::ROOTFILE_NAME)).once - Vagrant::Env.load_config! - end - - should "load the files only if exist? returns true" do - File.expects(:exist?).once.returns(true) - Vagrant::Env.expects(:load).once - Vagrant::Env.load_config! - end - - should "not load the files if exist? returns false" do - Vagrant::Env.expects(:load).never - Vagrant::Env.load_config! - end - - should "execute after loading" do - File.expects(:exist?).once.returns(true) - Vagrant::Env.expects(:load).once - Vagrant::Config.expects(:execute!).once - Vagrant::Env.load_config! - end - end - - context "initial load" do - should "load! should load the config and set the persisted_uid" do - call_seq = sequence("call_sequence") - Vagrant::Env.expects(:load_root_path!).once.in_sequence(call_seq) - Vagrant::Env.expects(:load_config!).once.in_sequence(call_seq) - Vagrant::Env.expects(:load_home_directory!).once.in_sequence(call_seq) - Vagrant::Env.expects(:load_box!).once.in_sequence(call_seq) - Vagrant::Env.expects(:load_config!).once.in_sequence(call_seq) - Vagrant::Env.expects(:check_virtualbox!).once.in_sequence(call_seq) - Vagrant::Env.expects(:load_vm!).once.in_sequence(call_seq) - Vagrant::Env.load! - end - end - - context "persisting the VM into a file" do - setup do - @vm = mock("vm") - @vm.stubs(:uuid).returns("foo") - - File.stubs(:open) - Vagrant::ActiveList.stubs(:add) - end - - should "should save it to the dotfile path" do - filemock = mock("filemock") - filemock.expects(:write).with(@vm.uuid) - File.expects(:open).with(Vagrant::Env.dotfile_path, 'w+').once.yields(filemock) - Vagrant::Env.persist_vm(@vm) - end - - should "add the VM to the activelist" do - Vagrant::ActiveList.expects(:add).with(@vm) - Vagrant::Env.persist_vm(@vm) - end - end - - context "depersisting the VM" do - setup do - File.stubs(:exist?).returns(false) - File.stubs(:delete) - - Vagrant::ActiveList.stubs(:remove) - - @dotfile_path = "foo" - Vagrant::Env.stubs(:dotfile_path).returns(@dotfile_path) - - @vm = mock("vm") - end - - should "remove the dotfile if it exists" do - File.expects(:exist?).with(Vagrant::Env.dotfile_path).returns(true) - File.expects(:delete).with(Vagrant::Env.dotfile_path).once - Vagrant::Env.depersist_vm(@vm) - end - - should "not remove the dotfile if it doesn't exist" do - File.expects(:exist?).returns(false) - File.expects(:delete).never - Vagrant::Env.depersist_vm(@vm) - end - - should "remove from the active list" do - Vagrant::ActiveList.expects(:remove).with(@vm) - Vagrant::Env.depersist_vm(@vm) - end - end - - context "loading the UUID out from the persisted file" do - setup do - File.stubs(:file?).returns(true) - end - - should "loading of the uuid from the dotfile" do - mock_persisted_vm - assert_equal 'foovm', Vagrant::Env.persisted_vm - end - - should "do nothing if the root path is nil" do - File.expects(:open).never - Vagrant::Env.stubs(:root_path).returns(nil) - Vagrant::Env.load_vm! - end - - should "do nothing if dotfile is not a file" do - File.expects(:file?).returns(false) - File.expects(:open).never - Vagrant::Env.load_vm! - end - - should "uuid should be nil if dotfile didn't exist" do - File.expects(:open).raises(Errno::ENOENT) - Vagrant::Env.load_vm! - assert_nil Vagrant::Env.persisted_vm - end - - should "should build up the dotfile out of the root path and the dotfile name" do - assert_equal File.join(Vagrant::Env.root_path, Vagrant.config.vagrant.dotfile_name), Vagrant::Env.dotfile_path - end - end - - context "loading the root path" do - should "default the path to the pwd if nil" do - @path = mock("path") - @path.stubs(:root?).returns(true) - Pathname.expects(:new).with(Dir.pwd).returns(@path) - Vagrant::Env.load_root_path!(nil) - end - - should "not default the path to pwd if its not nil" do - @path = mock("path") - @path.stubs(:to_s).returns("/") - File.expects(:expand_path).with(@path).returns("/") - Pathname.expects(:new).with("/").returns(@path) - @path.stubs(:root?).returns(true) - Vagrant::Env.load_root_path!(@path) - end - - should "should walk the parent directories looking for rootfile" do - paths = [ - Pathname.new("/foo/bar/baz"), - Pathname.new("/foo/bar"), - Pathname.new("/foo") - ] - - search_seq = sequence("search_seq") - paths.each do |path| - File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(false).in_sequence(search_seq) - end - - assert !Vagrant::Env.load_root_path!(paths.first) - end - - should "return false if not found" do - path = Pathname.new("/") - assert !Vagrant::Env.load_root_path!(path) - end - - should "return false if not found on windows-style root" do - # TODO: Is there _any_ way to test this on unix machines? The - # expand path doesn't work [properly for the test] on unix machines. - if RUBY_PLATFORM.downcase.include?("mswin") - # Note the escaped back slash - path = Pathname.new("C:\\") - assert !Vagrant::Env.load_root_path!(path) - end - end - - should "should set the path for the rootfile" do - path = "/foo" - File.expects(:exist?).with("#{path}/#{Vagrant::Env::ROOTFILE_NAME}").returns(true) - - assert Vagrant::Env.load_root_path!(Pathname.new(path)) - assert_equal path, Vagrant::Env.root_path - end - end - - context "home directory paths" do - should "return the expanded config for `home_path`" do - assert_equal File.expand_path(Vagrant.config.vagrant.home), Vagrant::Env.home_path - end - - should "return the home_path joined with tmp for a tmp path" do - @home_path = "foo" - Vagrant::Env.stubs(:home_path).returns(@home_path) - assert_equal File.join(@home_path, "tmp"), Vagrant::Env.tmp_path - end - - should "return the boxes path" do - @home_path = "foo" - Vagrant::Env.stubs(:home_path).returns(@home_path) - assert_equal File.join(@home_path, "boxes"), Vagrant::Env.boxes_path - end - end - - context "loading box" do - setup do - @box = mock("box") - - Vagrant::Env.stubs(:load_config!) - Vagrant::Env.stubs(:root_path).returns("foo") - end - - should "do nothing if the root path is nil" do - Vagrant::Box.expects(:find).never - Vagrant::Env.stubs(:root_path).returns(nil) - Vagrant::Env.load_vm! - end - - should "not load the box if its not set" do - mock_config do |config| - config.vm.box = nil - end - - Vagrant::Box.expects(:find).never - Vagrant::Env.load_box! - end - - should "set the box to what is found by the Box class" do - Vagrant::Box.expects(:find).with(Vagrant.config.vm.box).once.returns(@box) - Vagrant::Env.load_box! - assert @box.equal?(Vagrant::Env.box) - end - end - - context "requiring boxes" do - setup do - Vagrant::Env.stubs(:require_root_path) - Vagrant::Env.stubs(:error_and_exit) - end - - should "require root path" do - Vagrant::Env.expects(:require_root_path).once - Vagrant::Env.require_box - end - - should "error and exit if no box is specified" do - mock_config do |config| - config.vm.box = nil - end - - Vagrant::Env.expects(:box).returns(nil) - Vagrant::Env.expects(:error_and_exit).once.with(:box_not_specified) - Vagrant::Env.require_box - end - - should "error and exit if box is specified but doesn't exist" do - mock_config do |config| - config.vm.box = "foo" - end - - Vagrant::Env.expects(:box).returns(nil) - Vagrant::Env.expects(:error_and_exit).once.with(:box_specified_doesnt_exist, :box_name => "foo") - Vagrant::Env.require_box - end - end - - context "requiring root_path" do - should "error and exit if no root_path is set" do - Vagrant::Env.expects(:root_path).returns(nil) - Vagrant::Env.expects(:error_and_exit).with(:rootfile_not_found).once - Vagrant::Env.require_root_path - end - - should "not error and exit if root_path is set" do - Vagrant::Env.expects(:root_path).returns("foo") - Vagrant::Env.expects(:error_and_exit).never - Vagrant::Env.require_root_path - end - end -end diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index c3a33ff38..a0a581ff6 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -145,6 +145,7 @@ class EnvironmentTest < Test::Unit::TestCase Vagrant::Environment.expects(:check_virtualbox!).once.in_sequence(call_seq) @env.expects(:load_vm!).once.in_sequence(call_seq) @env.expects(:load_ssh!).once.in_sequence(call_seq) + @env.expects(:load_active_list!).once.in_sequence(call_seq) assert_equal @env, @env.load! end end @@ -416,6 +417,19 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal ssh, @env.ssh end end + + context "loading the active list" do + setup do + @env = mock_environment + end + + should "initialize the ActiveList object with the given environment" do + active_list = mock("active_list") + Vagrant::ActiveList.expects(:new).with(@env).returns(active_list) + @env.load_active_list! + assert_equal active_list, @env.active_list + end + end end context "requiring properties" do @@ -535,7 +549,7 @@ class EnvironmentTest < Test::Unit::TestCase mock_vm File.stubs(:open) - Vagrant::ActiveList.stubs(:add) + @env.active_list.stubs(:add) end should "should save it to the dotfile path" do @@ -546,7 +560,7 @@ class EnvironmentTest < Test::Unit::TestCase end should "add the VM to the activelist" do - Vagrant::ActiveList.expects(:add).with(@vm) + @env.active_list.expects(:add).with(@vm) @env.persist_vm end end @@ -558,7 +572,7 @@ class EnvironmentTest < Test::Unit::TestCase File.stubs(:exist?).returns(false) File.stubs(:delete) - Vagrant::ActiveList.stubs(:remove) + @env.active_list.stubs(:remove) end should "remove the dotfile if it exists" do @@ -574,7 +588,7 @@ class EnvironmentTest < Test::Unit::TestCase end should "remove from the active list" do - Vagrant::ActiveList.expects(:remove).with(@vm) + @env.active_list.expects(:remove).with(@vm) @env.depersist_vm end end