Box2 reads the metadata.json file.

This commit is contained in:
Mitchell Hashimoto 2012-06-29 19:29:06 -07:00
parent cd6c60775c
commit 2eaa850853
3 changed files with 29 additions and 3 deletions

View File

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

View File

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

View File

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