diff --git a/lib/puppet/provider/postgresql_conf/ruby.rb b/lib/puppet/provider/postgresql_conf/ruby.rb index 984b42706b..7e5db35232 100644 --- a/lib/puppet/provider/postgresql_conf/ruby.rb +++ b/lib/puppet/provider/postgresql_conf/ruby.rb @@ -74,7 +74,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]}, please fix this" if select.length > 1 return false if select.empty? @result = select.first diff --git a/spec/unit/provider/postgresql_conf/ruby_spec.rb b/spec/unit/provider/postgresql_conf/ruby_spec.rb index 11800b0fc7..f625f94d54 100644 --- a/spec/unit/provider/postgresql_conf/ruby_spec.rb +++ b/spec/unit/provider/postgresql_conf/ruby_spec.rb @@ -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') 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 found, please fix this') + end end it 'has a method create' do