Class: Cheffish::RSpec::RecipeRunWrapper

Inherits:
ChefRun
  • Object
show all
Defined in:
lib/cheffish/rspec/recipe_run_wrapper.rb

Instance Attribute Summary collapse

Attributes inherited from ChefRun

#chef_config

Instance Method Summary collapse

Methods inherited from ChefRun

#compile_recipe, #converge, #converge_failed?, #converged?, #event_sink, #logged_errors, #logged_info, #logged_warnings, #logs, #output_for_failure_message, #reset, #resources, #stderr, #stdout, #up_to_date?, #updated?

Constructor Details

#initialize(chef_config, example: nil, &recipe) ⇒ RecipeRunWrapper

Returns a new instance of RecipeRunWrapper.



6
7
8
9
10
# File 'lib/cheffish/rspec/recipe_run_wrapper.rb', line 6

def initialize(chef_config, example: nil, &recipe)
  super(chef_config)
  @recipe = recipe
  @example = example || recipe.binding.eval('self')
end

Instance Attribute Details

#exampleObject (readonly)

Returns the value of attribute example.



13
14
15
# File 'lib/cheffish/rspec/recipe_run_wrapper.rb', line 13

def example
  @example
end

#recipeObject (readonly)

Returns the value of attribute recipe.



12
13
14
# File 'lib/cheffish/rspec/recipe_run_wrapper.rb', line 12

def recipe
  @recipe
end

Instance Method Details

#clientObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cheffish/rspec/recipe_run_wrapper.rb', line 15

def client
  if !@client
    super
    example = self.example

    # Call into the rspec example's let variables and other methods
    @client.define_singleton_method(:method_missing) do |name, *args, &block|
      if example.respond_to?(name)
        example.public_send(name, *args, &block)
      else
        super(name, *args, &block)
      end
    end
    # This is called by respond_to?, and is required to make sure the
    # resource knows that we will in fact call the given method.
    @client.define_singleton_method(:respond_to_missing?) do |name, include_private = false|
      example.respond_to?(name)
    end
    # Respond true to is_a?(Chef::Provider) so that Chef::Recipe::DSL.build_resource
    # will hook resources up to the example let variables as well (via
    # enclosing_provider).
    # Please don't hurt me
    @client.define_singleton_method(:is_a?) do |klass|
      klass == Chef::Provider || super
    end

    @client.load_block(&recipe)
  end
  @client
end