Module: ChefSpec::API::Core

Extended by:
ClassMethods, RSpec::SharedContext
Defined in:
lib/chefspec/api/core.rb

Overview

Module containing the core RSpec API for ChefSpec.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

automatic_attributes, chefspec_options, default_attributes, included, normal_attributes, override_attributes, platform, recipe, step_into

Instance Method Details

#chef_runner_classClass

This method is abstract.

Class of runner to use.

Returns:

  • (Class)


59
60
61
# File 'lib/chefspec/api/core.rb', line 59

def chef_runner_class
  ChefSpec::SoloRunner
end

#chef_runner_instanceChefSpec::SoloRunner

Create an instance of the runner.

This should only be used in cases where the ‘let()` cache would be a problem.



68
69
70
# File 'lib/chefspec/api/core.rb', line 68

def chef_runner_instance
  chef_runner_class.new(chef_runner_options)
end

#chef_runner_optionsHash<Symbol, Object>

This method is abstract.

Compute the options for the runner.

Returns:

  • (Hash<Symbol, Object>)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chefspec/api/core.rb', line 37

def chef_runner_options
  options = {
    step_into: chefspec_ancestor_gather([], :step_into) { |memo, val| memo | val },
    default_attributes: chefspec_default_attributes,
    normal_attributes: chefspec_normal_attributes,
    override_attributes: chefspec_override_attributes,
    automatic_attributes: chefspec_automatic_attributes,
    spec_declaration_locations: self.class.declaration_locations.last[0],
  }
  # Only specify these if set in the example so we don't override the
  # global settings.
  options[:platform] = chefspec_platform if chefspec_platform
  options[:version] = chefspec_platform_version if chefspec_platform_version
  # Merge in any final overrides.
  options.update(chefspec_attributes(:chefspec_options).symbolize_keys)
  options
end

#chefspec_ancestor_gather(start, method, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method for some of the nestable test value methods like ChefSpec::API::Core::ClassMethods#default_attributes and ChefSpec::API::Core::ClassMethods#step_into.

Parameters:

  • start (Object)

    Initial value for the reducer.

  • method (Symbol)

    Name of the group-level method to call on each ancestor.

  • block (Proc)

    Reducer callable.

Returns:

  • (Object)


87
88
89
90
91
92
# File 'lib/chefspec/api/core.rb', line 87

def chefspec_ancestor_gather(start, method, &block)
  candidate_ancestors = self.class.ancestors.select { |cls| cls.respond_to?(method) && cls != ChefSpec::API::Core }
  candidate_ancestors.reverse.inject(start) do |memo, cls|
    block.call(memo, cls.send(method))
  end
end

#chefspec_attributes(method) ⇒ Mash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Special case of #chefspec_ancestor_gather because we do it four times.

Parameters:

  • method (Symbol)

    Name of the group-level method to call on each ancestor.

Returns:

  • (Mash)


100
101
102
103
104
# File 'lib/chefspec/api/core.rb', line 100

def chefspec_attributes(method)
  chefspec_ancestor_gather(Mash.new, method) do |memo, val|
    Chef::Mixin::DeepMerge.merge(memo, val)
  end
end