From a03bc763f9afe30421ab8fa40e9762bb3d6ce69f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 22 Jan 2014 09:36:22 -0800 Subject: [PATCH] core: tests around the environment setup_version --- lib/vagrant/environment.rb | 22 +++- lib/vagrant/errors.rb | 4 + templates/locales/en.yml | 4 + test/unit/vagrant/environment_test.rb | 140 ++++++++++++++++---------- 4 files changed, 114 insertions(+), 56 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 29e805659..2fc4143b1 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -14,6 +14,12 @@ module Vagrant # defined as basically a folder with a "Vagrantfile." This class allows # access to the VMs, CLI, etc. all in the scope of this environment. class Environment + # This is the current version that this version of Vagrant is + # compatible with in the home directory. + # + # @return [String] + CURRENT_SETUP_VERSION = "1.1" + DEFAULT_LOCAL_DATA = ".vagrant" # The `cwd` that this environment represents @@ -634,12 +640,24 @@ module Vagrant # we're using. version_file = @home_path.join("setup_version") if !version_file.file? - @logger.debug("Setting up the version file.") + @logger.debug( + "Creating home directory version file: #{CURRENT_SETUP_VERSION}") version_file.open("w") do |f| - f.write("1.1") + f.write(CURRENT_SETUP_VERSION) end end + # Determine if we need to update the directory structure + version = version_file.read + case version + when CURRENT_SETUP_VERSION + # We're already good, at the latest version. + else + raise Errors::HomeDirectoryUnknownVersion, + path: @home_path.to_s, + version: version + end + # Create the rgloader/loader file so we can use encoded files. loader_file = @home_path.join("rgloader", "loader.rb") if !loader_file.file? diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 0680de435..fe563694b 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -264,6 +264,10 @@ module Vagrant error_key(:home_dir_not_accessible) end + class HomeDirectoryUnknownVersion < VagrantError + error_key(:home_dir_unknown_version) + end + class ForwardPortAdapterNotFound < VagrantError error_key(:forward_port_adapter_not_found) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5c8fef2c6..9f44e0da3 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -437,6 +437,10 @@ en: directory that Vagrant uses must be both readable and writable. You specified: %{home_path} + home_dir_unknown_version: |- + The Vagrant app data directory (%{path}) is in a + structure Vagrant doesn't understand. This is a rare exception. + Please report an issue or ask the mailing list for help. host_explicit_not_detected: |- The host implementation explicitly specified in your Vagrantfile ("%{value}") could not be found. Please verify that the plugin is diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index d686c09ce..a5e670542 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -25,6 +25,92 @@ describe Vagrant::Environment do let(:instance) { env.create_vagrant_env } subject { instance } + describe "#home_path" do + it "is set to the home path given" do + Dir.mktmpdir do |dir| + instance = described_class.new(:home_path => dir) + instance.home_path.should == Pathname.new(dir) + end + end + + it "is set to the environmental variable VAGRANT_HOME" do + Dir.mktmpdir do |dir| + instance = with_temp_env("VAGRANT_HOME" => dir) do + described_class.new + end + + instance.home_path.should == Pathname.new(dir) + end + end + + it "throws an exception if inaccessible" do + expect { + described_class.new(:home_path => "/") + }.to raise_error(Vagrant::Errors::HomeDirectoryNotAccessible) + end + + context "default home path" do + it "is set to '~/.vagrant.d' by default" do + expected = Vagrant::Util::Platform.fs_real_path("~/.vagrant.d") + described_class.new.home_path.should == expected + end + + it "is set to '~/.vagrant.d' if on Windows but no USERPROFILE" do + Vagrant::Util::Platform.stub(:windows? => true) + + expected = Vagrant::Util::Platform.fs_real_path("~/.vagrant.d") + + with_temp_env("USERPROFILE" => nil) do + described_class.new.home_path.should == expected + end + end + + it "is set to '%USERPROFILE%/.vagrant.d' if on Windows and USERPROFILE is set" do + Vagrant::Util::Platform.stub(:windows? => true) + + Dir.mktmpdir do |dir| + expected = Vagrant::Util::Platform.fs_real_path("#{dir}/.vagrant.d") + + with_temp_env("USERPROFILE" => dir) do + described_class.new.home_path.should == expected + end + end + end + end + + context "setup version file" do + it "creates a setup version flie" do + path = subject.home_path.join("setup_version") + expect(path).to be_file + expect(path.read).to eq(Vagrant::Environment::CURRENT_SETUP_VERSION) + end + + it "is okay if it has the current version" do + Dir.mktmpdir do |dir| + Pathname.new(dir).join("setup_version").open("w") do |f| + f.write(Vagrant::Environment::CURRENT_SETUP_VERSION) + end + + instance = described_class.new(home_path: dir) + path = instance.home_path.join("setup_version") + expect(path).to be_file + expect(path.read).to eq(Vagrant::Environment::CURRENT_SETUP_VERSION) + end + end + + it "raises an exception if there is an unknown home directory version" do + Dir.mktmpdir do |dir| + Pathname.new(dir).join("setup_version").open("w") do |f| + f.write("0.7") + end + + expect { described_class.new(home_path: dir) }. + to raise_error(Vagrant::Errors::HomeDirectoryUnknownVersion) + end + end + end + end + describe "#host" do let(:plugin_hosts) { {} } let(:plugin_host_caps) { {} } @@ -211,60 +297,6 @@ describe Vagrant::Environment do end end - describe "home path" do - it "is set to the home path given" do - Dir.mktmpdir do |dir| - instance = described_class.new(:home_path => dir) - instance.home_path.should == Pathname.new(dir) - end - end - - it "is set to the environmental variable VAGRANT_HOME" do - Dir.mktmpdir do |dir| - instance = with_temp_env("VAGRANT_HOME" => dir) do - described_class.new - end - - instance.home_path.should == Pathname.new(dir) - end - end - - context "default home path" do - it "is set to '~/.vagrant.d' by default" do - expected = Vagrant::Util::Platform.fs_real_path("~/.vagrant.d") - described_class.new.home_path.should == expected - end - - it "is set to '~/.vagrant.d' if on Windows but no USERPROFILE" do - Vagrant::Util::Platform.stub(:windows? => true) - - expected = Vagrant::Util::Platform.fs_real_path("~/.vagrant.d") - - with_temp_env("USERPROFILE" => nil) do - described_class.new.home_path.should == expected - end - end - - it "is set to '%USERPROFILE%/.vagrant.d' if on Windows and USERPROFILE is set" do - Vagrant::Util::Platform.stub(:windows? => true) - - Dir.mktmpdir do |dir| - expected = Vagrant::Util::Platform.fs_real_path("#{dir}/.vagrant.d") - - with_temp_env("USERPROFILE" => dir) do - described_class.new.home_path.should == expected - end - end - end - end - - it "throws an exception if inaccessible" do - expect { - described_class.new(:home_path => "/") - }.to raise_error(Vagrant::Errors::HomeDirectoryNotAccessible) - end - end - describe "local data path" do it "is set to the proper default" do default = instance.root_path.join(described_class::DEFAULT_LOCAL_DATA)