Skip to content

Commit 68cc244

Browse files
authored
Merge pull request #1540 from bastelfreak/rubocop2
(#1532) Replace ParserError with Puppet::Error
2 parents 7e2d817 + b797bbd commit 68cc244

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/puppet/provider/postgresql_conf/ruby.rb

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

8080
@result = select.first

spec/unit/provider/postgresql_conf/ruby_spec.rb

+23-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
provider_class = Puppet::Type.type(:postgresql_conf).provider(:ruby)
55

66
describe provider_class do
7-
let(:resource) { Puppet::Type.type(:postgresql_conf).new(name: 'foo', value: 'bar') }
7+
let(:resource) { Puppet::Type.type(:postgresql_conf).new(name: 'foo', key: 'foo', value: 'bar') }
88
let(:provider) { resource.provider }
99

1010
before(:each) do
1111
allow(provider).to receive(:file_path).and_return('/tmp/foo')
1212
allow(provider).to receive(:read_file).and_return('foo = bar')
1313
allow(provider).to receive(:write_file).and_return(true)
14+
allow(provider).to receive(:resource).and_return(key: 'your_key', line_number: 1, value: 'foo')
1415
end
1516
# rubocop:enable RSpec/ReceiveMessages
1617

@@ -26,8 +27,27 @@
2627
expect(provider).to respond_to(:add_header)
2728
end
2829

29-
it 'has a method exists?' do
30-
expect(provider).to respond_to(:exists?)
30+
describe '#exists?' do
31+
it 'returns true when a matching config item is found' do
32+
config_data = [{ key: 'your_key', value: 'your_value' }]
33+
expect(provider).to receive(:parse_config).and_return(config_data)
34+
35+
expect(provider.exists?).to be true
36+
end
37+
38+
it 'returns false when no matching config item is found' do
39+
config_data = [{ key: 'other_key', value: 'other_value' }]
40+
expect(provider).to receive(:parse_config).and_return(config_data)
41+
42+
expect(provider.exists?).to be false
43+
end
44+
45+
it 'raises an error when multiple matching config items are found' do
46+
config_data = [{ key: 'your_key', value: 'value1' }, { key: 'your_key', value: 'value2' }]
47+
expect(provider).to receive(:parse_config).and_return(config_data)
48+
49+
expect { provider.exists? }.to raise_error(Puppet::Error, 'found multiple config items of your_key, please fix this')
50+
end
3151
end
3252

3353
it 'has a method create' do

0 commit comments

Comments
 (0)