From 73114a291012f8d98b01bffd3aaa10a0bf20c68d Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Tue, 22 Jan 2019 16:47:12 +0000 Subject: [PATCH] (FM-7691) Allow pre-fabbed BaseTypeDefinition as argument to BaseContext --- lib/puppet/resource_api/base_context.rb | 10 ++++++++-- spec/puppet/resource_api/base_context_spec.rb | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/puppet/resource_api/base_context.rb b/lib/puppet/resource_api/base_context.rb index 220ee8f6..68071a65 100644 --- a/lib/puppet/resource_api/base_context.rb +++ b/lib/puppet/resource_api/base_context.rb @@ -6,8 +6,14 @@ class Puppet::ResourceApi::BaseContext attr_reader :type def initialize(definition) - raise ArgumentError, 'BaseContext requires definition to be a Hash' unless definition.is_a?(Hash) - @type = Puppet::ResourceApi::TypeDefinition.new(definition) + if definition.is_a?(Hash) + # this is only for backwards compatibility + @type = Puppet::ResourceApi::TypeDefinition.new(definition) + elsif definition.is_a? Puppet::ResourceApi::BaseTypeDefinition + @type = definition + else + raise ArgumentError, 'BaseContext requires definition to be a child of Puppet::ResourceApi::BaseTypeDefinition' + end end def device diff --git a/spec/puppet/resource_api/base_context_spec.rb b/spec/puppet/resource_api/base_context_spec.rb index 34869f75..9072dbd4 100644 --- a/spec/puppet/resource_api/base_context_spec.rb +++ b/spec/puppet/resource_api/base_context_spec.rb @@ -13,10 +13,17 @@ def send_log(log, msg) TestContext.new(definition) end - let(:definition) { { name: 'some_resource', attributes: { name: { type: 'String', desc: 'message' } }, features: feature_support } } + let(:definition_hash) { { name: 'some_resource', attributes: { name: { type: 'String', desc: 'message' } }, features: feature_support } } + let(:definition) { Puppet::ResourceApi::TypeDefinition.new(definition_hash) } let(:feature_support) { [] } - it { expect { described_class.new(nil) }.to raise_error ArgumentError, %r{BaseContext requires definition to be a Hash} } + it { expect { described_class.new(nil) }.to raise_error ArgumentError, %r{BaseContext requires definition to be a child of Puppet::ResourceApi::BaseTypeDefinition} } + describe 'legacy hash defintion support' do + let(:definition) { definition_hash } + + it { expect { context }.not_to raise_error } + it { expect(context.type.name).to eq 'some_resource' } + end describe '#failed?' do it('defaults to false') { is_expected.not_to be_failed }