Skip to content

Commit

Permalink
(FM-7691) Allow pre-fabbed BaseTypeDefinition as argument to BaseContext
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS committed Jan 22, 2019
1 parent 994700f commit 73114a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/puppet/resource_api/base_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions spec/puppet/resource_api/base_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 73114a2

Please sign in to comment.