core: tests around the environment setup_version

This commit is contained in:
Mitchell Hashimoto 2014-01-22 09:36:22 -08:00
parent 86ede835ea
commit a03bc763f9
4 changed files with 114 additions and 56 deletions

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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)