From 324a08bd7570dcc3ea467daa4ca699017a49ac6d Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Wed, 9 Aug 2017 16:21:13 -0700 Subject: [PATCH] (#8719) Add force flag for box upgrade command Prior to this commit, if a state was reached where the action_box_add command needed a force flag, it would fail requesting the user to provide that flag to override adding a new box. However that flag did not exist on the box update command, and could not be passed onto the action_box_add action. This commit updates that to include a force flag, and if used, pass that value onto the action_box_add action. --- plugins/commands/box/command/update.rb | 19 ++++++++++------ .../commands/box/command/update_test.rb | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/plugins/commands/box/command/update.rb b/plugins/commands/box/command/update.rb index 7c72cca2f..30da1fcb6 100644 --- a/plugins/commands/box/command/update.rb +++ b/plugins/commands/box/command/update.rb @@ -33,6 +33,10 @@ module VagrantPlugins options[:provider] = p.to_sym end + o.on("-f", "--force", "Overwrite an existing box if it exists") do |f| + options[:force] = f + end + build_download_options(o, download_options) end @@ -40,15 +44,15 @@ module VagrantPlugins return if !argv if options[:box] - update_specific(options[:box], options[:provider], download_options) + update_specific(options[:box], options[:provider], download_options, options[:force]) else - update_vms(argv, options[:provider], download_options) + update_vms(argv, options[:provider], download_options, options[:force]) end 0 end - def update_specific(name, provider, download_options) + def update_specific(name, provider, download_options, force) boxes = {} @env.boxes.all.each do |n, v, p| boxes[n] ||= {} @@ -81,11 +85,11 @@ module VagrantPlugins to_update.each do |n, p, v| box = @env.boxes.find(n, p, v) - box_update(box, "> #{v}", @env.ui, download_options) + box_update(box, "> #{v}", @env.ui, download_options, force) end end - def update_vms(argv, provider, download_options) + def update_vms(argv, provider, download_options, force) machines = {} with_target_vms(argv, provider: provider) do |machine| @@ -120,11 +124,11 @@ module VagrantPlugins if download_options[:insecure].nil? download_options[:insecure] = machine.config.vm.box_download_insecure end - box_update(box, version, machine.ui, download_options) + box_update(box, version, machine.ui, download_options, force) end end - def box_update(box, version, ui, download_options) + def box_update(box, version, ui, download_options, force) ui.output(I18n.t("vagrant.box_update_checking", name: box.name)) ui.detail("Latest installed version: #{box.version}") ui.detail("Version constraints: #{version}") @@ -149,6 +153,7 @@ module VagrantPlugins box_provider: update[2].name, box_version: update[1].version, ui: ui, + box_force: force, box_client_cert: download_options[:client_cert], box_download_ca_cert: download_options[:ca_cert], box_download_ca_path: download_options[:ca_path], diff --git a/test/unit/plugins/commands/box/command/update_test.rb b/test/unit/plugins/commands/box/command/update_test.rb index d64d286f7..5a2f9153c 100644 --- a/test/unit/plugins/commands/box/command/update_test.rb +++ b/test/unit/plugins/commands/box/command/update_test.rb @@ -94,6 +94,7 @@ describe VagrantPlugins::CommandBox::Command::Update do allow(action_runner).to receive(:run) do |action, opts| if opts[:box_provider] action_called = true + expect(opts[:box_force]).to eq(nil) expect(opts[:box_url]).to eq(metadata_url.to_s) expect(opts[:box_provider]).to eq("virtualbox") expect(opts[:box_version]).to eq("1.1") @@ -356,6 +357,27 @@ describe VagrantPlugins::CommandBox::Command::Update do subject.execute end end + + context "force flag is specified on the command line" do + let(:argv) { ["--force"].concat(download_options) } + + it "passes force through to action_box_add as true" do + expect(box).to receive(:has_update?). + with(machine.config.vm.box_version, + {download_options: + {ca_cert: "foo", ca_path: "bar", client_cert: "baz", + insecure: true}}). + and_return([md, md.version("1.1"), + md.version("1.1").provider("virtualbox")]) + + expect(action_runner).to receive(:run).with(any_args) { |action, opts| + expect(opts[:box_force]).to be(true) + true + } + + subject.execute + end + end end end end