class EcomDev::ChefSpec::Stub::IncludeRecipe

Attributes

allowed_recipes[RW]
loaded_recipes[RW]

Public Class Methods

new() click to toggle source
# File lib/ecomdev/chefspec/stub/include_recipe.rb, line 12
def initialize
  @allowed_recipes = []
  @loaded_recipes = []
end

Public Instance Methods

after_example(*) click to toggle source
# File lib/ecomdev/chefspec/stub/include_recipe.rb, line 58
def after_example(*)
  reset
end
allow_recipe(recipe) click to toggle source
# File lib/ecomdev/chefspec/stub/include_recipe.rb, line 17
def allow_recipe(recipe)
  @allowed_recipes << recipe
end
before_example(object) click to toggle source
# 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
reset() click to toggle source
# File lib/ecomdev/chefspec/stub/include_recipe.rb, line 21
def reset
  @allowed_recipes = []
  @loaded_recipes = []
end
stub_include(object) click to toggle source
# 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