From cbda9f09067b3f7c4fbbd74246a5c940262f8dfa Mon Sep 17 00:00:00 2001 From: Hasyimi Bahrudin Date: Tue, 7 Feb 2017 13:43:49 +0800 Subject: [PATCH 1/4] Add validate command for validating Vagrantfile --- plugins/commands/validate/command.rb | 31 ++++++++++++++++++++++++++++ plugins/commands/validate/plugin.rb | 17 +++++++++++++++ templates/locales/en.yml | 3 +++ 3 files changed, 51 insertions(+) create mode 100644 plugins/commands/validate/command.rb create mode 100644 plugins/commands/validate/plugin.rb diff --git a/plugins/commands/validate/command.rb b/plugins/commands/validate/command.rb new file mode 100644 index 000000000..9d6ad7bb2 --- /dev/null +++ b/plugins/commands/validate/command.rb @@ -0,0 +1,31 @@ +require 'optparse' + +module VagrantPlugins + module CommandValidate + class Command < Vagrant.plugin("2", :command) + def self.synopsis + "validates the Vagrantfile" + end + + def execute + opts = OptionParser.new do |o| + o.banner = "Usage: vagrant validate" + end + + # Parse the options + 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) + + @env.ui.info(I18n.t("vagrant.commands.validate.success")) + + # Success, exit status 0 + 0 + end + end + end +end diff --git a/plugins/commands/validate/plugin.rb b/plugins/commands/validate/plugin.rb new file mode 100644 index 000000000..31a558b77 --- /dev/null +++ b/plugins/commands/validate/plugin.rb @@ -0,0 +1,17 @@ +require "vagrant" + +module VagrantPlugins + module CommandValidate + class Plugin < Vagrant.plugin("2") + name "validate command" + description <<-DESC + The `validate` command validates the Vagrantfile. + DESC + + command("validate") do + require File.expand_path("../command", __FILE__) + Command + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 1dc166863..e6a034dce 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1726,6 +1726,9 @@ en: up: upping: |- Bringing machine '%{name}' up with '%{provider}' provider... + validate: + success: |- + Vagrantfile validated successfully. #------------------------------------------------------------------------------- # Translations for Vagrant middleware actions From e0fb8b1fad1131ee446f1d87576273ee4e028c8e Mon Sep 17 00:00:00 2001 From: Hasyimi Bahrudin Date: Tue, 7 Feb 2017 14:15:16 +0800 Subject: [PATCH 2/4] Add documentation for validate command --- website/source/docs/cli/validate.html.md | 20 ++++++++++++++++++++ website/source/layouts/docs.erb | 1 + 2 files changed, 21 insertions(+) create mode 100644 website/source/docs/cli/validate.html.md diff --git a/website/source/docs/cli/validate.html.md b/website/source/docs/cli/validate.html.md new file mode 100644 index 000000000..0c0646994 --- /dev/null +++ b/website/source/docs/cli/validate.html.md @@ -0,0 +1,20 @@ +--- +layout: "docs" +page_title: "vagrant validate - Command-Line Interface" +sidebar_current: "cli-validate" +description: |- + The "vagrant validate" command is used to validate your Vagrantfile. +--- + +# Up + +**Command: `vagrant validate`** + +This command validates your [Vagrantfile](/docs/vagrantfile/). + +## Examples + +```sh +$ vagrant validate +Vagrantfile validated successfully. +``` diff --git a/website/source/layouts/docs.erb b/website/source/layouts/docs.erb index ca0630679..ba0f9f0b1 100644 --- a/website/source/layouts/docs.erb +++ b/website/source/layouts/docs.erb @@ -62,6 +62,7 @@ >status >suspend >up + >validate >version >More Commands >Machine Readable Output From 3d089a62d12b470c38666d4e161973af7be082f8 Mon Sep 17 00:00:00 2001 From: Hasyimi Bahrudin Date: Tue, 7 Feb 2017 14:59:20 +0800 Subject: [PATCH 3/4] Add unit test for validate command --- .../plugins/commands/validate/command_test.rb | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/unit/plugins/commands/validate/command_test.rb diff --git a/test/unit/plugins/commands/validate/command_test.rb b/test/unit/plugins/commands/validate/command_test.rb new file mode 100644 index 000000000..79a5463d3 --- /dev/null +++ b/test/unit/plugins/commands/validate/command_test.rb @@ -0,0 +1,52 @@ +require_relative "../../../base" +require_relative "../../../../../plugins/commands/validate/command" + +describe VagrantPlugins::CommandValidate::Command do + include_context "unit" + include_context "command plugin helpers" + + let(:iso_env) do + isolated_environment + end + + let(:env) do + iso_env.create_vagrant_env + end + + let(:argv) { [] } + + before(:all) do + I18n.load_path << Vagrant.source_root.join("plugins/commands/port/locales/en.yml") + I18n.reload! + end + + subject { described_class.new(argv, env) } + + describe "#execute" do + it "validates correct Vagrantfile" do + iso_env.vagrantfile(<<-EOH) + Vagrant.configure("2") do |config| + config.vm.box = "hashicorp/precise64" + end + EOH + + expect(env.ui).to receive(:info).with { |message, _| + expect(message).to include("Vagrantfile validated successfully.") + } + + expect(subject.execute).to eq(0) + end + + it "validates the configuration" do + iso_env.vagrantfile <<-EOH + Vagrant.configure("2") do |config| + config.vm.bix = "hashicorp/precise64" + end + EOH + + expect { subject.execute }.to raise_error(Vagrant::Errors::ConfigInvalid) { |err| + expect(err.message).to include("The following settings shouldn't exist: bix") + } + end + end +end From 0044d560e0b5936f9ae9f1b2625a820390db858b Mon Sep 17 00:00:00 2001 From: Hasyimi Bahrudin Date: Wed, 8 Feb 2017 03:26:37 +0800 Subject: [PATCH 4/4] Fix typo in docs --- website/source/docs/cli/validate.html.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/source/docs/cli/validate.html.md b/website/source/docs/cli/validate.html.md index 0c0646994..0a8805637 100644 --- a/website/source/docs/cli/validate.html.md +++ b/website/source/docs/cli/validate.html.md @@ -6,7 +6,7 @@ description: |- The "vagrant validate" command is used to validate your Vagrantfile. --- -# Up +# Validate **Command: `vagrant validate`**