1 class ProviderExampleGroup < Spec::Example::ExampleGroup
3 # Allow access to the current resource
6 # Build up the values for the resource in this scope
8 resource_hash = example_group_hierarchy.inject({}) do |memo, klass|
9 memo.merge(klass.options[:resource] || {})
11 full_hash = resource_hash.merge(:provider => described_class.name)
12 @resource = described_class.resource_type.new(full_hash)
16 subject { described_class.new(@resource) }
18 # Allow access to it via +provider+
19 alias :provider :subject
21 # Generate a context for a provider operating on a resource with:
25 # # A parameter/property set (when the value isn't important)
26 # resource_with :source do
30 # # A parameter/property set to a specific value
31 # resource_with :source => 'a-specific-value' do
35 # Note: Choose one or the other (mixing will create two separate contexts)
37 def self.resource_with(*params, &block)
38 params_with_values = params.last.is_a?(Hash) ? params.pop : {}
39 build_value_context(params_with_values, &block)
40 build_existence_context(*params, &block)
43 def self.build_existence_context(*params, &block) #:nodoc:
45 text = params.join(', ')
46 placeholders = params.inject({}) { |memo, key| memo.merge(key => 'an-unimportant-value') }
47 context("and with a #{text}", {:resource => placeholders}, &block)
51 def self.build_value_context(params = {}, &block) #:nodoc:
53 text = params.map { |k, v| "#{k} => #{v.inspect}" }.join(' and with ')
54 context("and with #{text}", {:resource => params}, &block)
59 # Generate a context for a provider operating on a resource without
60 # a given parameter/property.
64 # resource_without :source do
68 def self.resource_without(field, &block)
69 context("and without a #{field}", &block)
74 Spec::Example::ExampleGroupFactory.register(:provider, ProviderExampleGroup)
76 # Outside wrapper to lookup a provider and start the spec using ProviderExampleGroup
77 def describe_provider(type_name, provider_name, options = {}, &block)
78 provider_class = Puppet::Type.type(type_name).provider(provider_name)
79 describe(provider_class, options.merge(:type => :provider), &block)