Cleanup ProviderExampleGroup
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
class ProviderExampleGroup < Spec::Example::ExampleGroup
|
||||
|
||||
# Allow access to the current resource
|
||||
attr_reader :resource
|
||||
|
||||
|
||||
# Build up the values for the resource in this scope
|
||||
before :each do
|
||||
resource_hash = example_group_hierarchy.inject({}) do |memo, klass|
|
||||
memo.merge(klass.options[:resource] || {})
|
||||
@@ -9,42 +11,69 @@ class ProviderExampleGroup < Spec::Example::ExampleGroup
|
||||
full_hash = resource_hash.merge(:provider => described_class.name)
|
||||
@resource = described_class.resource_type.new(full_hash)
|
||||
end
|
||||
|
||||
|
||||
# Build the provider
|
||||
subject { described_class.new(@resource) }
|
||||
|
||||
# Allow access to it via +provider+
|
||||
alias :provider :subject
|
||||
|
||||
class << self
|
||||
# Generate a context for a provider operating on a resource with:
|
||||
#
|
||||
# call-seq:
|
||||
#
|
||||
# # A parameter/property set (when the value isn't important)
|
||||
# resource_with :source do
|
||||
# # ...
|
||||
# end
|
||||
#
|
||||
# # A parameter/property set to a specific value
|
||||
# resource_with :source => 'a-specific-value' do
|
||||
# # ...
|
||||
# end
|
||||
#
|
||||
# Note: Choose one or the other (mixing will create two separate contexts)
|
||||
#
|
||||
def self.resource_with(*params, &block)
|
||||
params_with_values = params.last.is_a?(Hash) ? params.pop : {}
|
||||
build_value_context(params_with_values, &block)
|
||||
build_existence_context(*params, &block)
|
||||
end
|
||||
|
||||
def field(field, &block)
|
||||
ResourceField.new(self, field, &block)
|
||||
def self.build_existence_context(*params, &block) #:nodoc:
|
||||
unless params.empty?
|
||||
text = params.join(', ')
|
||||
placeholders = params.inject({}) { |memo, key| memo.merge(key => 'an-unimportant-value') }
|
||||
context("and with a #{text}", {:resource => placeholders}, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def self.build_value_context(params = {}, &block) #:nodoc:
|
||||
unless params.empty?
|
||||
text = params.map { |k, v| "#{k} => #{v.inspect}" }.join(' and with ')
|
||||
context("and with #{text}", {:resource => params}, &block)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# call-seq:
|
||||
#
|
||||
# given(:ensure)
|
||||
# given(:ensure => :present)
|
||||
def resource_with(*args, &block)
|
||||
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||
if args.empty?
|
||||
text = options.map { |k, v| "#{k} => #{v.inspect}" }.join(' and with ')
|
||||
context("and with #{text}", {:resource => options}, &block)
|
||||
else
|
||||
text = args.join(', ')
|
||||
placeholders = args.inject({}) { |memo, key| memo.merge(key => 'an-unimportant-value') }
|
||||
context("and with a #{text}", {:resource => placeholders}, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def resource_without(field, &block)
|
||||
context("and without a #{field}", &block)
|
||||
end
|
||||
|
||||
# Generate a context for a provider operating on a resource without
|
||||
# a given parameter/property.
|
||||
#
|
||||
# call-seq:
|
||||
#
|
||||
# resource_without :source do
|
||||
# # ...
|
||||
# end
|
||||
#
|
||||
def self.resource_without(field, &block)
|
||||
context("and without a #{field}", &block)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Spec::Example::ExampleGroupFactory.register(:provider, ProviderExampleGroup)
|
||||
|
||||
# Outside wrapper to lookup a provider and start the spec using ProviderExampleGroup
|
||||
def describe_provider(type_name, provider_name, options = {}, &block)
|
||||
provider_class = Puppet::Type.type(type_name).provider(provider_name)
|
||||
describe(provider_class, options.merge(:type => :provider), &block)
|
||||
|
Reference in New Issue
Block a user