From 29da74870200bc7e26e40b8a5d8f90e6eb063bf8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Jan 2014 20:51:00 -0800 Subject: [PATCH] commands/box/remove: update for new syntax --- plugins/commands/box/command/remove.rb | 46 +++++++------ templates/locales/en.yml | 11 ---- .../commands/box/command/remove_test.rb | 66 +++++++++++++++++++ 3 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 test/unit/plugins/commands/box/command/remove_test.rb diff --git a/plugins/commands/box/command/remove.rb b/plugins/commands/box/command/remove.rb index d58f5bcec..95fc5aabe 100644 --- a/plugins/commands/box/command/remove.rb +++ b/plugins/commands/box/command/remove.rb @@ -5,38 +5,42 @@ module VagrantPlugins module Command class Remove < Vagrant.plugin("2", :command) def execute + options = {} opts = OptionParser.new do |o| - o.banner = "Usage: vagrant box remove " + o.banner = "Usage: vagrant box remove " + o.separator "" + + o.on("--provider VALUE", String, + "The specific provider type for the box to remove.") do |p| + options[:provider] = p + end + + o.on("--version VALUE", String, + "The specific version of the box to remove.") do |v| + options[:version] = v + end end # Parse the options argv = parse_options(opts) return if !argv - raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length < 1 + if argv.empty? || argv.length > 2 + raise Vagrant::Errors::CLIInvalidUsage, + help: opts.help.chomp + end - if !argv[1] - # Try to automatically determine the provider. - providers = [] - @env.boxes.all.each do |name, provider| - if name == argv[0] - providers << provider - end - end - - if providers.length > 1 - @env.ui.error( - I18n.t("vagrant.commands.box.remove_must_specify_provider", - name: argv[0], - providers: providers.join(", "))) - return 1 - end - - argv[1] = providers[0] || "" + if argv.length == 2 + # @deprecated + @env.ui.warn("WARNING: The second argument to `vagrant box remove`") + @env.ui.warn("is deprecated. Please use the --provider flag. This") + @env.ui.warn("feature will stop working in the next version.") + options[:provider] = argv[1] end @env.action_runner.run(Vagrant::Action.action_box_remove, { :box_name => argv[0], - :box_provider => argv[1] + :box_provider => options[:provider], + :box_version => options[:version], }) # Success, exit status 0 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 4dd04d08f..47d80ead7 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -974,17 +974,6 @@ en: vm_not_created: "VM not created. Moving on..." vm_not_running: "VM is not currently running. Please, first bring it up with `vagrant up` then run this command." box: - remove_must_specify_provider: |- - Multiple providers were found for the box '%{name}'. Please specify - the specific provider for the box you want to remove. The list of - providers backing this box is: - - '%{providers}' - - To remove the box for a specific provider, run the following command, - filling in PROVIDER with one of the providers above: - - vagrant box remove '%{name}' PROVIDER no_installed_boxes: "There are no installed boxes! Use `vagrant box add` to add some." removing: |- Removing box '%{name}' with provider '%{provider}'... diff --git a/test/unit/plugins/commands/box/command/remove_test.rb b/test/unit/plugins/commands/box/command/remove_test.rb new file mode 100644 index 000000000..a13fd30fb --- /dev/null +++ b/test/unit/plugins/commands/box/command/remove_test.rb @@ -0,0 +1,66 @@ +require File.expand_path("../../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/commands/box/command/remove") + +describe VagrantPlugins::CommandBox::Command::Remove do + include_context "unit" + + let(:argv) { [] } + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + subject { described_class.new(argv, iso_env) } + + let(:action_runner) { double("action_runner") } + + before do + iso_env.stub(action_runner: action_runner) + end + + context "with no arguments" do + it "shows help" do + expect { subject.execute }. + to raise_error(Vagrant::Errors::CLIInvalidUsage) + end + end + + context "with one argument" do + let(:argv) { ["foo"] } + + it "invokes the action runner" do + action_runner.should_receive(:run).with do |action, opts| + expect(opts[:box_name]).to eq("foo") + true + end + + subject.execute + end + end + + context "with two arguments" do + let(:argv) { ["foo", "bar"] } + + it "uses the 2nd arg as a provider" do + action_runner.should_receive(:run).with do |action, opts| + expect(opts[:box_name]).to eq("foo") + expect(opts[:box_provider]).to eq("bar") + true + end + + subject.execute + end + end + + context "with more than two arguments" do + let(:argv) { ["one", "two", "three"] } + + it "shows help" do + expect { subject.execute }. + to raise_error(Vagrant::Errors::CLIInvalidUsage) + end + end +end