summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2012-10-25 16:03:24 -0700
committerdanielsdeleo <dan@opscode.com>2012-10-25 16:13:56 -0700
commitb091d56dc49646ca3bdee95f21f2c575df301ba6 (patch)
treeb7cf6290bc5ef291244ce36b32ae973e00b19efa
parent5b771324a7b4d244cbabe996804664d004efce4a (diff)
downloadchef-b091d56dc49646ca3bdee95f21f2c575df301ba6.tar.gz
[CHEF-3561] add template context for template errors
ResourceFailureInspector will now notice TemplateErrors and use them to display the relevant lines from a template.
-rw-r--r--chef/lib/chef/formatters/error_inspectors/resource_failure_inspector.rb6
-rw-r--r--chef/spec/unit/formatters/error_inspectors/resource_failure_inspector_spec.rb32
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 b4064db4b6..fac7feb465 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