From f1d08aa629e3a33ca4759a154c0e50fe99ed84e0 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Wed, 31 May 2017 15:18:12 -0700 Subject: [PATCH] (#7118) Handle box names that are URLs Prior to this commit, if a user set a URL for the name of a box, vagrant would not warn the user about using box_url instead. This would lead to some difficult user experiences around the various box commands due to the box name being a full URL. This commit introduces a warning to the user and lets them know to instead use box_url. --- lib/vagrant/action/builtin/box_add.rb | 6 +++++ templates/locales/en.yml | 3 +++ .../vagrant/action/builtin/box_add_test.rb | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+) 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 f4e182a31..b13278fe4 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" }