Expose Chef's template helper variables when rendering - #51
Open
tas50 wants to merge 1 commit into
Open
Conversation
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
force-pushed
the
fix/template-helper-variables
branch
from
August 22, 2026 00:09
28bd2b1 to
9fb1a87
Compare
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:ChefSpec's renderer only sets
nodeandtemplate_finder, so a template containing<%= cookbook_name %>renders an empty string under test while working correctly during a real Chef run: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_nameandrecipe_nameto therender_fileacceptance example.56 examples, 0 failures56 examples, 3 failures197 examples, 0 failuresThe
resourcedouble inspec/unit/renderer_spec.rbis extended with the attributes the renderer now reads.