core: BoxAdd checks if box already exists for metadata

unless force
This commit is contained in:
Mitchell Hashimoto 2014-01-23 15:31:54 -08:00
parent c1989603be
commit 75cffe53d1
4 changed files with 59 additions and 9 deletions

View File

@ -114,8 +114,17 @@ module Vagrant
version: metadata_version.version, version: metadata_version.version,
provider: metadata_provider.name)) provider: metadata_provider.name))
# TODO(mitchellh): verify that the box we're adding # Verify the box we're adding doesn't already exist
# doesn't already exist. if !env[:box_force]
box = env[:box_collection].find(
metadata.name, metadata_provider.name, metadata_version.version)
if box
raise Errors::BoxAlreadyExists,
name: metadata.name,
provider: metadata_provider.name,
version: metadata_version.version
end
end
# Now we have a URL, we have to download this URL. # Now we have a URL, we have to download this URL.
box = nil box = nil

View File

@ -129,7 +129,7 @@ module Vagrant
end end
class BoxAlreadyExists < VagrantError class BoxAlreadyExists < VagrantError
error_key(:already_exists, "vagrant.actions.box.unpackage") error_key(:box_add_exists)
end end
class BoxChecksumInvalidType < VagrantError class BoxChecksumInvalidType < VagrantError

View File

@ -4,6 +4,13 @@ en:
Machine booted and ready! Machine booted and ready!
boot_waiting: |- boot_waiting: |-
Waiting for machine to boot. This may take a few minutes... Waiting for machine to boot. This may take a few minutes...
box_add_exists: |-
The box you're attempting to add already exists. Remove it before
adding it again or add it with the `--force` flag.
Name: %{name}
Provider: %{provider}
Version: %{version}
box_add_with_version: |- box_add_with_version: |-
Adding box '%{name}' (v%{version}) for '%{provider}' provider... Adding box '%{name}' (v%{version}) for '%{provider}' provider...
box_added: |- box_added: |-
@ -1252,12 +1259,6 @@ en:
output from attempting to unpackage (if any): output from attempting to unpackage (if any):
%{output} %{output}
already_exists: |-
The box you're attempting to add already exists:
Name: %{name}
Version: %{version}
Provider: %{providers}
add: add:
adding: |- adding: |-
Extracting box... Extracting box...

View File

@ -32,6 +32,10 @@ describe Vagrant::Action::Builtin::BoxAdd do
FileChecksum.new(path, Digest::SHA1).checksum FileChecksum.new(path, Digest::SHA1).checksum
end end
before do
box_collection.stub(find: nil)
end
context "with box file directly" do context "with box file directly" do
it "adds it" do it "adds it" do
box_path = iso_env.box2_file(:virtualbox) box_path = iso_env.box2_file(:virtualbox)
@ -369,6 +373,41 @@ describe Vagrant::Action::Builtin::BoxAdd do
to raise_error(Vagrant::Errors::BoxAddNoMatchingProvider) to raise_error(Vagrant::Errors::BoxAddNoMatchingProvider)
end end
it "raises an error if a box already exists" do
box_path = iso_env.box2_file(:virtualbox)
tf = Tempfile.new("vagrant").tap do |f|
f.write(<<-RAW)
{
"name": "foo/bar",
"versions": [
{
"version": "0.5"
},
{
"version": "0.7",
"providers": [
{
"name": "virtualbox",
"url": "#{box_path}"
}
]
}
]
}
RAW
f.close
end
env[:box_url] = tf.path
box_collection.should_receive(:find).
with("foo/bar", "virtualbox", "0.7").and_return(box)
box_collection.should_receive(:add).never
app.should_receive(:call).never
expect { subject.call(env) }.
to raise_error(Vagrant::Errors::BoxAlreadyExists)
end
it "force adds a box if specified" do it "force adds a box if specified" do
box_path = iso_env.box2_file(:virtualbox) box_path = iso_env.box2_file(:virtualbox)
tf = Tempfile.new("vagrant").tap do |f| tf = Tempfile.new("vagrant").tap do |f|
@ -396,6 +435,7 @@ describe Vagrant::Action::Builtin::BoxAdd do
env[:box_force] = true env[:box_force] = true
env[:box_url] = tf.path env[:box_url] = tf.path
box_collection.stub(find: box)
box_collection.should_receive(:add).with do |path, name, version, **opts| box_collection.should_receive(:add).with do |path, name, version, **opts|
expect(checksum(path)).to eq(checksum(box_path)) expect(checksum(path)).to eq(checksum(box_path))
expect(name).to eq("foo/bar") expect(name).to eq("foo/bar")