Skip to content

Commit

Permalink
(#1532) Replace ParserError with Puppet::Error
Browse files Browse the repository at this point in the history
I'm not sure how we ended up with ParserError in the
provider. This exception doesn't exist in Ruby.
Puppet ships ther own exception, Puppet::Error. It
probably makes sense to raise that instead.

Fixes 179472b

Alternative implementation for #1538
  • Loading branch information
bastelfreak committed Oct 19, 2023
1 parent d4560c1 commit 1bfbee5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/puppet/provider/postgresql_conf/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def write_config(file, lines)
# check, if resource exists in postgresql.conf file
def exists?
select = parse_config.select { |hash| hash[:key] == resource[:key] }
raise ParserError, "found multiple config items of #{resource[:key]} found, please fix this" if select.length > 1
raise Puppet::Error, "found multiple config items of #{resource[:key]} found, please fix this" if select.length > 1
return false if select.empty?

@result = select.first
Expand Down
26 changes: 26 additions & 0 deletions spec/unit/provider/postgresql_conf/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@
it 'has a method exists?' do
expect(provider).to respond_to(:exists?)
end
context '#exists?' do
before(:each) do
# Stub the resource method to return a specific value
allow(provider).to receive(:resource).and_return(key: 'your_key')
end
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 found, please fix this')
end
end

it 'has a method create' do
expect(provider).to respond_to(:create)
Expand Down

0 comments on commit 1bfbee5

Please sign in to comment.