diff options
author | danielsdeleo <dan@opscode.com> | 2012-10-25 16:12:45 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2012-10-25 16:12:45 -0700 |
commit | 265a1b898c7235198d60b14d83b75754133b0884 (patch) | |
tree | b7ade654e9c1ddd9100d2a56a73359414a6ff101 | |
parent | 06b4061054c2c1226cb1b0512e8e826ff1b721c9 (diff) | |
parent | 524860a0ad575af60169b4651d95c25dbb4d7a6e (diff) | |
download | chef-265a1b898c7235198d60b14d83b75754133b0884.tar.gz |
Merge branch 'CHEF-3561'
-rw-r--r-- | chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb | 6 | ||||
-rw-r--r-- | chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb | 32 |
2 files changed, 37 insertions, 1 deletions
diff --git a/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb b/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb index cfcd9b2f07..57d8de0ef9 100644 --- a/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb +++ b/chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb @@ -44,6 +44,12 @@ class Chef end error_description.section("Compiled Resource:", "#{resource.to_text}") + + # Template errors get wrapped in an exception class that can show the relevant template code, + # so add them to the error output. + if exception.respond_to?(:source_listing) + error_description.section("Template Context:", "#{exception.source_location}\n#{exception.source_listing}") + end end def recipe_snippet diff --git a/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb b/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb index 54a57d8077..52c719d2fb 100644 --- a/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb +++ b/chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb @@ -34,7 +34,8 @@ describe Chef::Formatters::ErrorInspectors::ResourceFailureInspector do before do @description = Chef::Formatters::ErrorDescription.new("Error Converging Resource:") - @outputter = Chef::Formatters::Outputter.new(StringIO.new, STDERR) + @stdout = StringIO.new + @outputter = Chef::Formatters::Outputter.new(@stdout, STDERR) #@outputter = Chef::Formatters::Outputter.new(STDOUT, STDERR) Chef::Config.stub!(:cookbook_path).and_return([ "/var/chef/cache" ]) @@ -77,6 +78,35 @@ describe Chef::Formatters::ErrorInspectors::ResourceFailureInspector do @description.display(@outputter) end + describe "and the error is a template error" do + before do + @description = Chef::Formatters::ErrorDescription.new("Error Converging Resource:") + @template_class = Class.new { include Chef::Mixin::Template } + @template = @template_class.new + @context = {:chef => "cool"} + + @resource = template("/tmp/foo.txt") do + mode "0644" + end + + @error = begin + @template.render_template("foo\nbar\nbaz\n<%= this_is_not_defined %>\nquin\nqunx\ndunno", @context) {|r| r} + rescue Chef::Mixin::Template::TemplateError => e + e + end + + @inspector = Chef::Formatters::ErrorInspectors::ResourceFailureInspector.new(@resource, :create, @error) + @inspector.add_explanation(@description) + end + + it "includes contextual info from the template error in the output" do + @description.display(@outputter) + @stdout.string.should include(@error.source_listing) + end + + + end + describe "recipe_snippet" do before do # fake code to run through #recipe_snippet |