From abb114919033bd5ec2be507a6f5c09bcfe22e966 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Fri, 17 Nov 2017 12:20:03 -0700 Subject: [PATCH] Raise an exception when the template cannot be found, and update tests accordingly --- lib/vagrant/errors.rb | 4 ++++ plugins/commands/init/command.rb | 6 ++++++ templates/locales/en.yml | 3 +++ test/unit/plugins/commands/init/command_test.rb | 6 ++++++ 4 files changed, 19 insertions(+) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index ee0d8ea25..ab535640c 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -784,6 +784,10 @@ module Vagrant error_key(:vagrantfile_syntax_error) end + class VagrantfileTemplateNotFoundError < VagrantError + error_key(:vagrantfile_template_not_found_error) + end + class VagrantfileWriteError < VagrantError error_key(:vagrantfile_write_error) end diff --git a/plugins/commands/init/command.rb b/plugins/commands/init/command.rb index e28a58c74..a86c15494 100644 --- a/plugins/commands/init/command.rb +++ b/plugins/commands/init/command.rb @@ -71,6 +71,12 @@ module VagrantPlugins # Strip the .erb extension off the template if the user passes it in options[:template] = options[:template].chomp(".erb") + # Make sure the template actually exists + full_template_path = Vagrant::Util::TemplateRenderer.new(options[:template], template_root: template_root).full_template_path + if !File.file?(full_template_path) + raise Vagrant::Errors::VagrantfileTemplateNotFoundError, path: full_template_path + end + contents = Vagrant::Util::TemplateRenderer.render(options[:template], box_name: argv[0] || "base", box_url: argv[1], diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 0e673586a..5608c3814 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1379,6 +1379,9 @@ en: message is reproduced below for convenience: %{file} + vagrantfile_template_not_found_error: |- + The Vagrantfile template '%{path}' does not exist. Please double check + the template path and try again. vagrantfile_write_error: |- The user that is running Vagrant doesn't have the proper permissions to write a Vagrantfile to the specified location. Please ensure that diff --git a/test/unit/plugins/commands/init/command_test.rb b/test/unit/plugins/commands/init/command_test.rb index edc7ea3b1..e821c5a2e 100644 --- a/test/unit/plugins/commands/init/command_test.rb +++ b/test/unit/plugins/commands/init/command_test.rb @@ -61,6 +61,12 @@ describe VagrantPlugins::CommandInit::Command do expect(contents).to match(/config.vm.hostname = "vagrant.dev"/) end + it "raises an appropriate exception when the template file can't be found" do + expect { + described_class.new(["--template", "./a/b/c/template"], env).execute + }.to raise_error(Vagrant::Errors::VagrantfileTemplateNotFoundError) + end + it "does not overwrite an existing Vagrantfile" do # Create an existing Vagrantfile File.open(File.join(env.cwd, "Vagrantfile"), "w+") { |f| f.write("") }