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.



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

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.



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

def example
  @example
end

#recipeObject (readonly)

Returns the value of attribute recipe.



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

def recipe
  @recipe
end

Instance Method Details

#clientObject



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cheffish/rspec/recipe_run_wrapper.rb', line 16

def client
  unless @client
    super
    example = self.example

    #
    # Support for both resources and rspec example's let variables:
    #
    # In 12.4, the elimination of a bunch of metaprogramming in 12.4
    # changed how Chef DSL is defined in code: resource methods are now
    # explicitly defined in `Chef::DSL::Recipe`. In 12.3, no actual
    # methods were defined and `respond_to?(:file)` would return false.
    # If we reach `method_missing` here, it means that we either have a
    # 12.3-ish resource or we want to call a `let` variable.
    #
    @client.instance_eval { @rspec_example = example }
    def @client.method_missing(name, *args, &block) # rubocop:disable Lint/NestedMethodDefinition
      # If there is a let variable, call it. This is because in 12.4,
      # the parent class is going to call respond_to?(name) to find out
      # if someone was doing weird things, and then call send(). This
      # would result in an infinite loop, coming right. Back. Here.
      # A fix to chef is incoming, but we still need this if we want to
      # work with Chef 12.4.
      if Gem::Version.new(Chef::VERSION) >= Gem::Version.new("12.4")
        if @rspec_example.respond_to?(name)
          return @rspec_example.public_send(name, *args, &block)
        end
      end

      # In 12.3 or below, method_missing was the only way to call
      # resources. If we are in 12.4, we still need to call the crazy
      # method_missing metaprogramming because backcompat.
      begin
        super
      rescue NameError
        if @rspec_example.respond_to?(name)
          @rspec_example.public_send(name, *args, &block)
        else
          raise
        end
      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.
    def @client.respond_to_missing?(name, include_private = false) # rubocop:disable Lint/NestedMethodDefinition
      @rspec_example.respond_to?(name, include_private) || super
    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
    def @client.is_a?(klass) # rubocop:disable Lint/NestedMethodDefinition
      klass == Chef::Provider || super(klass)
    end

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