diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 316420490..5f2aa907c 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -30,6 +30,12 @@ module Vagrant def call(env) @download_interrupted = false + unless env[:box_name].nil? + if URI.parse(env[:box_name]).kind_of?(URI::HTTP) + env[:ui].warn(I18n.t("vagrant.box_add_url_warn")) + end + end + url = Array(env[:box_url]).map do |u| u = u.gsub("\\", "/") if Util::Platform.windows? && u =~ /^[a-z]:/i diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 505a8f48a..0dd9472b7 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -22,6 +22,9 @@ en: Successfully added box '%{name}' (v%{version}) for '%{provider}'! box_adding_direct: |- Box file was not detected as metadata. Adding it directly... + box_add_url_warn: |- + It looks like you attempted to add a box with a URL for the name... + Instead, use box_url instead of box for box URLs. box_downloading: |- Downloading: %{url} box_download_error: |- diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index a1f966d4b..d7c6efa97 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -280,6 +280,31 @@ describe Vagrant::Action::Builtin::BoxAdd, :skip_windows do subject.call(env) end + context "with a box name accidentally set as a URL" do + it "displays a warning to the user" do + box_path = iso_env.box2_file(:virtualbox) + with_web_server(box_path) do |port| + + box_url_name = "http://127.0.0.1:#{port}/#{box_path.basename}" + env[:box_name] = box_url_name + + expect(box_collection).to receive(:add).with { |path, name, version, **opts| + expect(name).to eq(box_url_name) + expect(version).to eq("0") + expect(opts[:metadata_url]).to be_nil + true + }.and_return(box) + + expect(app).to receive(:call).with(env) + + expect(env[:ui]).to receive(:warn) + .with(/It looks like you attempted to add a box with a URL for the name/) + + subject.call(env) + end + end + end + context "with URL containing credentials" do let(:username){ "box-username" } let(:password){ "box-password" }