-
Notifications
You must be signed in to change notification settings - Fork 614
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
(#1532) Replace ParserError with Puppet::Error #1540
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -4,13 +4,14 @@ | |||
provider_class = Puppet::Type.type(:postgresql_conf).provider(:ruby) | ||||
|
||||
describe provider_class do | ||||
let(:resource) { Puppet::Type.type(:postgresql_conf).new(name: 'foo', value: 'bar') } | ||||
let(:resource) { Puppet::Type.type(:postgresql_conf).new(name: 'foo', key: 'foo', value: 'bar') } | ||||
let(:provider) { resource.provider } | ||||
|
||||
before(:each) do | ||||
allow(provider).to receive(:file_path).and_return('/tmp/foo') | ||||
allow(provider).to receive(:read_file).and_return('foo = bar') | ||||
allow(provider).to receive(:write_file).and_return(true) | ||||
allow(provider).to receive(:resource).and_return(key: 'your_key', line_number: 1, value: 'foo') | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant to change line 7 to: let(:resource) { Puppet::Type.type(:postgresql_conf).new(name: 'foo', key: 'foo', value: 'bar') } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah okay. will update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mhm this line is still requires to get the tests passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This confuses me and now I wonder what's wrong. Perhaps tomorrow I'll have some idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I think I know what is going on. Line 7 instantiates a new Puppet type with the desired state. The provider has the actual state. You're mocking that here. So by mocking this, you know it is already in place. So at the very least I'd expect you to only mock it when you want it to exist, not before each test. It also makes me wonder about something else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is now redundant, right?
Suggested change
|
||||
end | ||||
# rubocop:enable RSpec/ReceiveMessages | ||||
|
||||
|
@@ -26,8 +27,27 @@ | |||
expect(provider).to respond_to(:add_header) | ||||
end | ||||
|
||||
it 'has a method exists?' do | ||||
expect(provider).to respond_to(:exists?) | ||||
describe '#exists?' do | ||||
it 'returns true when a matching config item is found' do | ||||
config_data = [{ key: 'your_key', value: 'your_value' }] | ||||
expect(provider).to receive(:parse_config).and_return(config_data) | ||||
|
||||
expect(provider.exists?).to be true | ||||
end | ||||
|
||||
it 'returns false when no matching config item is found' do | ||||
config_data = [{ key: 'other_key', value: 'other_value' }] | ||||
expect(provider).to receive(:parse_config).and_return(config_data) | ||||
|
||||
expect(provider.exists?).to be false | ||||
end | ||||
|
||||
it 'raises an error when multiple matching config items are found' do | ||||
config_data = [{ key: 'your_key', value: 'value1' }, { key: 'your_key', value: 'value2' }] | ||||
expect(provider).to receive(:parse_config).and_return(config_data) | ||||
|
||||
expect { provider.exists? }.to raise_error(Puppet::Error, 'found multiple config items of your_key, please fix this') | ||||
end | ||||
end | ||||
|
||||
it 'has a method create' do | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, providers can call the
fail
method which will raisePuppet::Error
, for example see the base exec provider: https://github.com/puppetlabs/puppet/blob/fce49baa3767fffb000f1b26d3a7b4edbec58999/lib/puppet/provider/exec.rb#L105 There should really be a specification about what methods are available for providers...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to fix a simple typo. I don't know how to properly write tests for the
fail()
method. I cannot justify to the customer that I spent several days within a 6 month period on this fix. If someone wants to pick it up: feel free.