From 4bd95cbfe225f71f9dff80a8a19353229e91825f Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 15 Aug 2017 13:52:35 -0700 Subject: [PATCH] (#8864) Improve `vagrant validate` command Prior to this commit, the `vagrant validate` command would only validate the first machine in a vagrant file. This commit improves that by validating all known machines in the environment. If one is not found, it will properly throw an exception instead of a stacktrace. --- plugins/commands/validate/command.rb | 8 ++-- .../plugins/commands/validate/command_test.rb | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/plugins/commands/validate/command.rb b/plugins/commands/validate/command.rb index 9d6ad7bb2..bccbc5755 100644 --- a/plugins/commands/validate/command.rb +++ b/plugins/commands/validate/command.rb @@ -16,10 +16,10 @@ module VagrantPlugins argv = parse_options(opts) return if !argv - # Validate the configuration - @env.machine(@env.machine_names.first, @env.default_provider).action_raw( - :config_validate, - Vagrant::Action::Builtin::ConfigValidate) + # Validate the configuration of all machines + with_target_vms() do |machine| + machine.action_raw(:config_validate, Vagrant::Action::Builtin::ConfigValidate) + end @env.ui.info(I18n.t("vagrant.commands.validate.success")) diff --git a/test/unit/plugins/commands/validate/command_test.rb b/test/unit/plugins/commands/validate/command_test.rb index 66941d72c..2dfd93e75 100644 --- a/test/unit/plugins/commands/validate/command_test.rb +++ b/test/unit/plugins/commands/validate/command_test.rb @@ -48,5 +48,51 @@ describe VagrantPlugins::CommandValidate::Command do expect(err.message).to include("The following settings shouldn't exist: bix") } end + + it "validates correct Vagrantfile of all vms" do + iso_env.vagrantfile <<-EOH + Vagrant.configure("2") do |config| + config.vm.box = "hashicorp/precise64" + + config.vm.define "test" do |vm| + vm.vm.provider :virtualbox + end + + config.vm.define "machine" do |vm| + vm.vm.provider :virtualbox + end + end + EOH + + expect(env.ui).to receive(:info).with(any_args) { |message, _| + expect(message).to include("Vagrantfile validated successfully.") + } + + expect(subject.execute).to eq(0) + end + + it "validates the configuration of all vms" do + iso_env.vagrantfile <<-EOH + Vagrant.configure("2") do |config| + config.vm.box = "hashicorp/precise64" + + config.vm.define "test" do |vm| + vm.vm.provider :virtualbox + end + + config.vm.define "machine" do |vm| + vm.vm.not_provider :virtualbox + end + end + EOH + + expect { subject.execute }.to raise_error(Vagrant::Errors::ConfigInvalid) { |err| + expect(err.message).to include("The following settings shouldn't exist: not_provider") + } + end + + it "throws an exception if there's no Vagrantfile" do + expect { subject.execute }.to raise_error(Vagrant::Errors::NoEnvironmentError) + end end end