From a6d7bd48fde6f568fdf74666437047c88a1326eb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 27 Jun 2012 17:15:08 -0700 Subject: [PATCH] Find boxes with new on-disk structure. The box collection can now find new-style boxes with providers and return proper Box objects. In the future, we'll also have to implement upgrading old style ones as well. --- lib/vagrant.rb | 1 + lib/vagrant/box_collection2.rb | 6 +++++ test/unit/support/isolated_environment.rb | 20 ++++++++++++++++ test/unit/vagrant/box_collection2_test.rb | 28 +++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 test/unit/vagrant/box_collection2_test.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index eb49bce2a..073aa2b9a 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -65,6 +65,7 @@ module Vagrant autoload :Box, 'vagrant/box' autoload :Box2, 'vagrant/box2' autoload :BoxCollection, 'vagrant/box_collection' + autoload :BoxCollection2, 'vagrant/box_collection2' autoload :CLI, 'vagrant/cli' autoload :Command, 'vagrant/command' autoload :Communication, 'vagrant/communication' diff --git a/lib/vagrant/box_collection2.rb b/lib/vagrant/box_collection2.rb index 573667a35..d5ef4508d 100644 --- a/lib/vagrant/box_collection2.rb +++ b/lib/vagrant/box_collection2.rb @@ -20,6 +20,12 @@ module Vagrant # @Param [String] provider Provider that the box implements. # @return [Box] The box found, or `nil` if not found. def find(name, provider) + # First look directly for the box we're asking for. + box_directory = @directory.join(name, provider.to_s, "metadata.json") + return Box2.new(name, provider, box_directory.dirname) if box_directory.file? + + # Didn't find it, return nil + nil end end end diff --git a/test/unit/support/isolated_environment.rb b/test/unit/support/isolated_environment.rb index c7435ea9a..66fb693cf 100644 --- a/test/unit/support/isolated_environment.rb +++ b/test/unit/support/isolated_environment.rb @@ -38,6 +38,26 @@ module Unit box_dir end + # Creates a fake box to exist in this environment. + # + # @param [String] name Name of the box + # @param [Symbol] provider Provider the box was built for. + # @return [Pathname] Path to the box directory. + def box2(name, provider) + # Make the box directory + box_dir = boxes_dir.join(name, provider.to_s) + box_dir.mkpath + + # Create a metadata.json file + box_metadata_file = box_dir.join("metadata.json") + box_metadata_file.open("w") do |f| + f.write("") + end + + # Return the box directory + box_dir + end + def boxes_dir dir = @homedir.join("boxes") dir.mkpath diff --git a/test/unit/vagrant/box_collection2_test.rb b/test/unit/vagrant/box_collection2_test.rb new file mode 100644 index 000000000..2ee60c8a6 --- /dev/null +++ b/test/unit/vagrant/box_collection2_test.rb @@ -0,0 +1,28 @@ +require File.expand_path("../../base", __FILE__) + +require "pathname" + +describe Vagrant::BoxCollection2 do + include_context "unit" + + let(:box_class) { Vagrant::Box2 } + let(:environment) { isolated_environment } + let(:instance) { described_class.new(environment.boxes_dir) } + + describe "finding" do + it "should return nil if the box does not exist" do + instance.find("foo", :i_dont_exist).should be_nil + end + + it "should return a box if the box does exist" do + # Create the "box" + environment.box2("foo", :virtualbox) + + # Actual test + result = instance.find("foo", :virtualbox) + result.should_not be_nil + result.should be_kind_of(box_class) + result.name.should == "foo" + end + end +end