Raise an exception when the template cannot be found, and update tests accordingly

This commit is contained in:
Zachary Flower 2017-11-17 12:20:03 -07:00
parent 968fbb2399
commit abb1149190
4 changed files with 19 additions and 0 deletions

View File

@ -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

View File

@ -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],

View File

@ -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

View File

@ -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("") }