summaryrefslogtreecommitdiff
path: root/spec/unit
diff options
context:
space:
mode:
authorGabriel Mazetto <brodock@gmail.com>2019-05-17 05:31:51 +0200
committerGabriel Mazetto <brodock@gmail.com>2019-05-26 14:47:06 +0200
commitbf23769d7901a79623732d0db68d11a832b3ab40 (patch)
tree16715b2d80c6803a8f26921bf0731b69ce556763 /spec/unit
parentdf885a1e35366e6180632741918ab0e774c671a4 (diff)
downloadchef-bf23769d7901a79623732d0db68d11a832b3ab40.tar.gz
Improving error handling for template render
While Chef already implements a nice amount of information in `TemplateError#to_s`, when using ChefSpec with RSpec, the `#to_s` is not called, leaving us with no source information for template errors. By adding the filename to `Erubis::Eruby` initialization, we can get a better backtrace that will show up in RSpec output. Signed-off-by: Gabriel Mazetto <brodock@gmail.com>
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/mixin/template_spec.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/spec/unit/mixin/template_spec.rb b/spec/unit/mixin/template_spec.rb
index ab7ed5bc5a..96b983a9dd 100644
--- a/spec/unit/mixin/template_spec.rb
+++ b/spec/unit/mixin/template_spec.rb
@@ -182,6 +182,51 @@ describe Chef::Mixin::Template, "render_template" do
expect(output).to eq("before {partial one We could be diving for pearls! calling home} after")
end
+ describe "when an exception is raised in the template" do
+ let(:template_file) { File.expand_path(File.join(CHEF_SPEC_DATA, "templates", "failed.erb")) }
+
+ def do_raise
+ @template_context.render_template(template_file)
+ end
+
+ it "should catch and re-raise the exception as a TemplateError" do
+ expect { do_raise }.to raise_error(Chef::Mixin::Template::TemplateError)
+ end
+
+ describe "the raised TemplateError" do
+ subject(:exception) do
+ begin
+ do_raise
+ rescue Chef::Mixin::Template::TemplateError => e
+ e
+ end
+ end
+
+ it "should contain template file and line numbers" do
+ expect(exception.line_number).to eq(5)
+ end
+
+ it "should provide a source listing of the template around the exception" do
+ expect(exception.source_listing).to eq(" 3: Which includes some content\n 4: \n 5: And will fail <%= nil[] %>")
+ end
+
+ it "should provide a nice source location" do
+ expect(exception.source_location).to eq("on line #5")
+ end
+
+ it "should create a pretty output for the terminal" do
+ expect(exception.to_s).to match(/Chef::Mixin::Template::TemplateError/)
+ expect(exception.to_s).to match(/undefined method `\[\]' for nil:NilClass/)
+ expect(exception.to_s).to include(" 3: Which includes some content\n 4: \n 5: And will fail <%= nil[] %>")
+ expect(exception.to_s).to include(exception.original_exception.backtrace.first)
+ end
+
+ it "should include template file on original_exception backtrace" do
+ expect(exception.original_exception.backtrace).to include(/#{Regexp.escape(template_file)}/)
+ end
+ end
+ end
+
describe "when customizing the template context" do
it "extends the context to include modules" do