Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-924) Throw when SimpleProvider is used with unensurable type #73

Merged
merged 2 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contrib/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Code modified from: https://gist.github.com/hanloong/9849098
require 'English'

ADDED_OR_MODIFIED = %r{A|AM|M|^M}
ADDED = %r{A|AM}

changed_files = `git status --porcelain`.split(%r{\n})
changed_files = changed_files.select do |file_name_with_status|
file_name_with_status =~ ADDED_OR_MODIFIED
file_name_with_status =~ ADDED
end
changed_files = changed_files.map do |file_name_with_status|
file_name_with_status.split(' ')[1]
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/resource_api/simple_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def set(context, changes)

should = change[:should]

raise 'SimpleProvider cannot be used with a Type that is not ensurable' unless context.type.ensurable?

is = { name: name, ensure: 'absent' } if is.nil?
should = { name: name, ensure: 'absent' } if should.nil?

Expand Down
26 changes: 25 additions & 1 deletion spec/puppet/resource_api/simple_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def delete(context, _name); end

let(:provider) { provider_class.new }

before(:each) do
allow(context).to receive(:type).and_return(type_def)
allow(type_def).to receive(:ensurable?).and_return(true)
end

context 'without overriding the crud methods' do
it 'create fails' do
expect { described_class.new.create(context, nil, nil) }.to raise_error StandardError, %r{has not implemented.*create}
Expand Down Expand Up @@ -172,7 +177,6 @@ def delete(context, _name); end
allow(context).to receive(:creating).with('to create').and_yield
allow(context).to receive(:updating).with('to update').and_yield
allow(context).to receive(:deleting).with('to delete').and_yield
allow(context).to receive(:type).and_return(type_def).exactly(3).times
allow(type_def).to receive(:feature?).with('simple_get_filter').exactly(3).times
end

Expand All @@ -183,4 +187,24 @@ def delete(context, _name); end
provider.set(context, changes)
end
end

context 'with a type that does not implement ensurable' do
let(:is_values) { { name: 'title', content: 'foo' } }
let(:should_values) { { name: 'title', content: 'bar' } }
let(:changes) do
{ 'title' =>
{
is: is_values,
should: should_values,
} }
end

before(:each) do
allow(context).to receive(:updating).with('title').and_yield
allow(type_def).to receive(:feature?).with('simple_get_filter')
allow(type_def).to receive(:ensurable?).and_return(false)
end

it { expect { provider.set(context, changes) }.to raise_error %r{SimpleProvider cannot be used with a Type that is not ensurable} }
end
end