diff --git a/lib/vagrant/box2.rb b/lib/vagrant/box2.rb index f679217c7..61af1abe8 100644 --- a/lib/vagrant/box2.rb +++ b/lib/vagrant/box2.rb @@ -1,3 +1,5 @@ +require "json" + module Vagrant # Represents a "box," which is a package Vagrant environment that is used # as a base image when creating a new guest machine. @@ -22,6 +24,12 @@ module Vagrant # @return [Pathname] attr_reader :directory + # This is the metadata for the box. This is read from the "metadata.json" + # file that all boxes require. + # + # @return [Hash] + attr_reader :metadata + # This is used to initialize a box. # # @param [String] name Logical name of the box. @@ -32,6 +40,7 @@ module Vagrant @name = name @provider = provider @directory = directory + @metadata = JSON.parse(directory.join("metadata.json").read) end # This deletes the box. This is NOT undoable. diff --git a/test/unit/support/isolated_environment.rb b/test/unit/support/isolated_environment.rb index 16b8a45d8..b5b56d369 100644 --- a/test/unit/support/isolated_environment.rb +++ b/test/unit/support/isolated_environment.rb @@ -63,7 +63,7 @@ module Unit # Create a metadata.json file box_metadata_file = box_dir.join("metadata.json") box_metadata_file.open("w") do |f| - f.write("") + f.write("{}") end # Return the box directory diff --git a/test/unit/vagrant/box2_test.rb b/test/unit/vagrant/box2_test.rb index a5e006446..843419225 100644 --- a/test/unit/vagrant/box2_test.rb +++ b/test/unit/vagrant/box2_test.rb @@ -5,9 +5,11 @@ require "pathname" describe Vagrant::Box2 do include_context "unit" + let(:environment) { isolated_environment } + let(:name) { "foo" } let(:provider) { :virtualbox } - let(:directory) { temporary_dir } + let(:directory) { environment.box2("foo", :virtualbox) } let(:instance) { described_class.new(name, provider, directory) } it "provides the name" do @@ -22,6 +24,18 @@ describe Vagrant::Box2 do instance.directory.should == directory end + it "provides the metadata associated with a box" do + data = { "foo" => "bar" } + + # Write the metadata + directory.join("metadata.json").open("w") do |f| + f.write(JSON.generate(data)) + end + + # Verify the metadata + instance.metadata.should == data + end + describe "destroying" do it "should destroy an existing box" do # Verify that our "box" exists @@ -35,11 +49,14 @@ describe Vagrant::Box2 do end it "should not error destroying a non-existent box" do + # Get the instance so that it is instantiated + box = instance + # Delete the directory directory.rmtree # Destroy it - instance.destroy!.should be + box.destroy!.should be end end