class ChefSpec::Renderer

def content

Returns:
  • (String, nil) -
def content
  case resource_name(resource)
  when :template
    content_from_template(chef_run, resource)
  when :file
    content_from_file(chef_run, resource)
  when :cookbook_file
    content_from_cookbook_file(chef_run, resource)
  else
    nil
  end
end

def content_from_cookbook_file(chef_run, cookbook_file)

Parameters:
  • cookbook_file (Chef::Provider::CookbookFile) --
  • chef_run (Chef::RunContext) --
def content_from_cookbook_file(chef_run, cookbook_file)
  cookbook_name = cookbook_file.cookbook || cookbook_file.cookbook_name
  cookbook = cookbook_collection(chef_run.node)[cookbook_name]
  File.read(cookbook.preferred_filename_on_disk_location(chef_run.node, :files, cookbook_file.source))
end

def content_from_file(chef_run, file)

Returns:
  • (String) -

Parameters:
  • file (Chef::Provider::File) --
  • chef_run (Chef::RunContext) --
def content_from_file(chef_run, file)
  file.content
end

def content_from_template(chef_run, template)

Returns:
  • (String) -

Parameters:
  • template (Chef::Provider::Template) --
  • chef_run (Chef::RunContext) --
def content_from_template(chef_run, template)
  cookbook_name = template.cookbook || template.cookbook_name
  template_location = cookbook_collection(chef_run.node)[cookbook_name].preferred_filename_on_disk_location(chef_run.node, :templates, template.source)
  if Chef::Mixin::Template.const_defined?(:TemplateContext) # Chef 11+
    template_context = Chef::Mixin::Template::TemplateContext.new([])
    template_context.update({
      node: chef_run.node,
      template_finder: template_finder(chef_run, cookbook_name),
    }.merge(template.variables))
    if template.respond_to?(:helper_modules) # Chef 11.4+
      template_context._extend_modules(template.helper_modules)
    end
    template_context.render_template(template_location)
  else
    template.provider.new(template, chef_run.run_context).send(:render_with_context, template_location) do |file|
      File.read(file.path)
    end
  end
end

def cookbook_collection(node)

Returns:
  • (Array) -

Parameters:
  • node (Chef::Node) --
def cookbook_collection(node)
  if chef_run.respond_to?(:run_context)
    chef_run.run_context.cookbook_collection # Chef 11.8+
  elsif node.respond_to?(:run_context)
    node.run_context.cookbook_collection # Chef 11+
  else
    node.cookbook_collection # Chef 10
  end
end

def initialize(chef_run, resource)

Parameters:
  • resource (Chef::Resource) --
  • chef_run (Chef::Runner) --
def initialize(chef_run, resource)
  @chef_run = chef_run
  @resource = resource
end

def template_finder(chef_run, cookbook_name)

Returns:
  • (Chef::Provider::TemplateFinder, nil) -

Parameters:
  • cookbook_name (String) --
  • chef_run (Chef::RunContext) --
def template_finder(chef_run, cookbook_name)
  if Chef::Provider.const_defined?(:TemplateFinder) # Chef 11+
    Chef::Provider::TemplateFinder.new(chef_run.run_context, cookbook_name, chef_run.node)
  else
    nil
  end
end