Skip to content

Expose Chef's template helper variables when rendering - #51

Open
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/template-helper-variables
Open

Expose Chef's template helper variables when rendering#51
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/template-helper-variables

Conversation

@tas50

@tas50 tas50 commented Aug 22, 2026

Copy link
Copy Markdown

Fixes chefspec#795

Problem

Chef's template provider sets helper variables on the template context before rendering, in Chef::Provider::Template::Content#file_for_provider:

context[:cookbook_name] = new_resource.cookbook_name unless context.keys.include?(:cookbook_name)
context[:recipe_name] = new_resource.recipe_name unless context.keys.include?(:recipe_name)
context[:recipe_line_string] = new_resource.source_line unless context.keys.include?(:recipe_line_string)
context[:recipe_path] = new_resource.source_line_file unless context.keys.include?(:recipe_path)
context[:recipe_line] = new_resource.source_line_number unless context.keys.include?(:recipe_line)
context[:template_name] = new_resource.source unless context.keys.include?(:template_name)
context[:template_path] = template_location unless context.keys.include?(:template_path)

ChefSpec's renderer only sets node and template_finder, so a template containing <%= cookbook_name %> renders an empty string under test while working correctly during a real Chef run:

expected Chef run to render "/tmp/cbname" matching:
I am from the repro cookbook
but got:
I am from the  cookbook

Fix

Set the same helper variables Chef does, keeping Chef's rule that variables supplied on the resource take precedence.

Testing

Adds a template referencing cookbook_name, template_name and recipe_name to the render_file acceptance example.

  • With the fix: 56 examples, 0 failures
  • With the fix reverted: 56 examples, 3 failures
  • Unit suite: 197 examples, 0 failures

The resource double in spec/unit/renderer_spec.rb is extended with the attributes the renderer now reads.

@tas50
tas50 requested review from a team and jaymzh as code owners August 22, 2026 00:07
Chef's template provider sets a group of helper variables on the
template context before rendering, so templates can reference
cookbook_name, template_name, recipe_name and friends.

ChefSpec's renderer only set node and template_finder, so any template
using those helpers rendered them as empty strings and render_file
comparisons failed against content that works during a real Chef run.

Set the same helper variables Chef does, letting variables defined on the
resource win, matching Chef::Provider::Template::Content.

Fixes chefspec#795

Signed-off-by: Tim Smith <tsmith84@proton.me>
@tas50
tas50 force-pushed the fix/template-helper-variables branch from 28bd2b1 to 9fb1a87 Compare August 22, 2026 00:09
@tas50

tas50 commented Aug 22, 2026

Copy link
Copy Markdown
Author

CI on this PR is red for an unrelated reason: main's bundle cannot be installed on Linux, which breaks every job before any test runs. That is fixed separately in #56, which is green across Ruby 3.1 to 3.4. This PR should go green once #56 lands and this branch is rebased.

Locally, against a working bundle, this branch passes rake unit and the acceptance examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cookbook_name is nil when used in template

1 participant