Skip to content

Commit 73114a2

Browse files
committed
(FM-7691) Allow pre-fabbed BaseTypeDefinition as argument to BaseContext
1 parent 994700f commit 73114a2

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/puppet/resource_api/base_context.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ class Puppet::ResourceApi::BaseContext
66
attr_reader :type
77

88
def initialize(definition)
9-
raise ArgumentError, 'BaseContext requires definition to be a Hash' unless definition.is_a?(Hash)
10-
@type = Puppet::ResourceApi::TypeDefinition.new(definition)
9+
if definition.is_a?(Hash)
10+
# this is only for backwards compatibility
11+
@type = Puppet::ResourceApi::TypeDefinition.new(definition)
12+
elsif definition.is_a? Puppet::ResourceApi::BaseTypeDefinition
13+
@type = definition
14+
else
15+
raise ArgumentError, 'BaseContext requires definition to be a child of Puppet::ResourceApi::BaseTypeDefinition'
16+
end
1117
end
1218

1319
def device

spec/puppet/resource_api/base_context_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ def send_log(log, msg)
1313
TestContext.new(definition)
1414
end
1515

16-
let(:definition) { { name: 'some_resource', attributes: { name: { type: 'String', desc: 'message' } }, features: feature_support } }
16+
let(:definition_hash) { { name: 'some_resource', attributes: { name: { type: 'String', desc: 'message' } }, features: feature_support } }
17+
let(:definition) { Puppet::ResourceApi::TypeDefinition.new(definition_hash) }
1718
let(:feature_support) { [] }
1819

19-
it { expect { described_class.new(nil) }.to raise_error ArgumentError, %r{BaseContext requires definition to be a Hash} }
20+
it { expect { described_class.new(nil) }.to raise_error ArgumentError, %r{BaseContext requires definition to be a child of Puppet::ResourceApi::BaseTypeDefinition} }
21+
describe 'legacy hash defintion support' do
22+
let(:definition) { definition_hash }
23+
24+
it { expect { context }.not_to raise_error }
25+
it { expect(context.type.name).to eq 'some_resource' }
26+
end
2027

2128
describe '#failed?' do
2229
it('defaults to false') { is_expected.not_to be_failed }

0 commit comments

Comments
 (0)