Skip to content

Commit 1bfbee5

Browse files
committed
(#1532) Replace ParserError with Puppet::Error
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
1 parent d4560c1 commit 1bfbee5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/puppet/provider/postgresql_conf/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def write_config(file, lines)
7575
# check, if resource exists in postgresql.conf file
7676
def exists?
7777
select = parse_config.select { |hash| hash[:key] == resource[:key] }
78-
raise ParserError, "found multiple config items of #{resource[:key]} found, please fix this" if select.length > 1
78+
raise Puppet::Error, "found multiple config items of #{resource[:key]} found, please fix this" if select.length > 1
7979
return false if select.empty?
8080

8181
@result = select.first

spec/unit/provider/postgresql_conf/ruby_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@
2929
it 'has a method exists?' do
3030
expect(provider).to respond_to(:exists?)
3131
end
32+
context '#exists?' do
33+
before(:each) do
34+
# Stub the resource method to return a specific value
35+
allow(provider).to receive(:resource).and_return(key: 'your_key')
36+
end
37+
it 'returns true when a matching config item is found' do
38+
config_data = [{ key: 'your_key', value: 'your_value' }]
39+
expect(provider).to receive(:parse_config).and_return(config_data)
40+
41+
expect(provider.exists?).to be true
42+
end
43+
44+
it 'returns false when no matching config item is found' do
45+
config_data = [{ key: 'other_key', value: 'other_value' }]
46+
expect(provider).to receive(:parse_config).and_return(config_data)
47+
48+
expect(provider.exists?).to be false
49+
end
50+
51+
it 'raises an error when multiple matching config items are found' do
52+
config_data = [{ key: 'your_key', value: 'value1' }, { key: 'your_key', value: 'value2' }]
53+
expect(provider).to receive(:parse_config).and_return(config_data)
54+
55+
expect { provider.exists? }.to raise_error(Puppet::Error, 'found multiple config items of your_key found, please fix this')
56+
end
57+
end
3258

3359
it 'has a method create' do
3460
expect(provider).to respond_to(:create)

0 commit comments

Comments
 (0)