Class: EcomDev::ChefSpec::Stub::IncludeRecipe

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/ecomdev/chefspec/stub/include_recipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIncludeRecipe

Returns a new instance of IncludeRecipe.



12
13
14
15
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 12

def initialize
  @allowed_recipes = []
  @loaded_recipes = []
end

Instance Attribute Details

#allowed_recipesObject

Returns the value of attribute allowed_recipes.



10
11
12
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 10

def allowed_recipes
  @allowed_recipes
end

#loaded_recipesObject

Returns the value of attribute loaded_recipes.



10
11
12
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 10

def loaded_recipes
  @loaded_recipes
end

Instance Method Details

#after_exampleObject



58
59
60
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 58

def after_example(*)
  reset
end

#allow_recipe(recipe) ⇒ Object



17
18
19
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 17

def allow_recipe(recipe)
  @allowed_recipes << recipe
end

#before_example(object) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 26

def before_example(object)
  if object.respond_to?(:described_recipe) && object.described_recipe.match(/^[a-z_0-9]+::[a-z_0-9]+$/)
    allow_recipe(object.described_recipe)
  end

  stub_include(object) unless allowed_recipes.empty?
end

#resetObject



21
22
23
24
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 21

def reset
  @allowed_recipes = []
  @loaded_recipes = []
end

#stub_include(object) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ecomdev/chefspec/stub/include_recipe.rb', line 34

def stub_include(object)
  # Don't worry about external cookbook dependencies
  object.allow_any_instance_of(Chef::Cookbook::Metadata).to object.receive(:depends)

  # Test each recipe in isolation, regardless of includes
  object.allow_any_instance_of(Chef::RunContext).to object.receive(:loaded_recipe?) do |run_context, recipe_name|
    run_context.loaded_recipes.include?(recipe_name)
  end

  object.allow_any_instance_of(Chef::RunContext).to object.receive(:include_recipe) do |run_context, *recipe_names|
    recipe_names.flatten.each do |recipe_name|
      @loaded_recipes << recipe_name
      if allowed_recipes.include?(recipe_name)
        run_context.load_recipe(recipe_name)
      end
    end
    loaded_recipes
  end

  object.allow_any_instance_of(Chef::RunContext).to object.receive(:loaded_recipes) do
    loaded_recipes
  end
end