Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/render_file/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@
template '/tmp/partial' do
source 'partial.erb'
end

# A template and a file resource for the same path, as produced by a recipe
# that either renders or removes a config file depending on an attribute.
template '/tmp/template_or_delete' do
source 'template.erb'
end

file '/tmp/template_or_delete' do
action :delete
end
7 changes: 7 additions & 0 deletions examples/render_file/spec/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,11 @@
it { is_expected.to_not render_file('/tmp/partial').with_content(/^Not(.+)$/) }
end
end

context 'a path with both a template and a file resource' do
describe 'renders the template rather than matching the delete' do
it { is_expected.to render_file('/tmp/template_or_delete') }
it { is_expected.to render_file('/tmp/template_or_delete').with_content('This is content!') }
end
end
end
26 changes: 23 additions & 3 deletions lib/chefspec/matchers/render_file_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,18 @@ def expected_content_message
end

def resource
@resource ||= @runner.find_resource(:cookbook_file, @path) ||
@runner.find_resource(:file, @path) ||
@runner.find_resource(:template, @path)
return @resource if defined?(@resource)

candidates = %i{cookbook_file file template}.filter_map do |type|
@runner.find_resource(type, @path)
end

# A recipe may declare more than one resource for the same path, most
# commonly a template that creates the file and a file resource that
# deletes it, with guards selecting between them. Prefer whichever
# resource actually creates the file so that we do not report the file as
# unrendered just because a sibling delete resource was found first.
@resource = candidates.find { |candidate| create_action?(candidate) } || candidates.first
end

#
Expand All @@ -99,6 +108,17 @@ def resource
# @return [true, false]
#
def has_create_action?
create_action?(resource)
end

#
# Determines if the given resource has a create-like action.
#
# @param [Chef::Resource] resource
#
# @return [true, false]
#
def create_action?(resource)
%i{create create_if_missing}.any? { |action| resource.performed_action?(action) }
end

Expand Down
Loading