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 Apr 8, 2024
1 parent 3904ab3 commit e02387e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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 @@ -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
Expand Down
26 changes: 23 additions & 3 deletions spec/unit/provider/postgresql_conf/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Check failure on line 45 in spec/unit/provider/postgresql_conf/ruby_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

Puppet::Type::Postgresql_conf::ProviderRuby#exists? raises an error when multiple matching config items are found Failure/Error: expect { provider.exists? }.to raise_error(Puppet::Error, 'found multiple config items of your_key found, please fix this') expected Puppet::Error with "found multiple config items of your_key found, please fix this", got #<Puppet::Error: found multiple config items of your_key, please fix this> with backtrace: # ./lib/puppet/provider/postgresql_conf/ruby.rb:77:in `exists?' # ./spec/unit/provider/postgresql_conf/ruby_spec.rb:49:in `block (4 levels) in <top (required)>' # ./spec/unit/provider/postgresql_conf/ruby_spec.rb:49:in `block (3 levels) in <top (required)>' # ./vendor/bundle/ruby/2.7.0/bin/rspec:23:in `load' # ./vendor/bundle/ruby/2.7.0/bin/rspec:23:in `<top (required)>' # /opt/hostedtoolcache/Ruby/2.7.8/x64/bin/bundle:23:in `load' # /opt/hostedtoolcache/Ruby/2.7.8/x64/bin/bundle:23:in `<main>'

Check failure on line 45 in spec/unit/provider/postgresql_conf/ruby_spec.rb

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

Puppet::Type::Postgresql_conf::ProviderRuby#exists? raises an error when multiple matching config items are found Failure/Error: expect { provider.exists? }.to raise_error(Puppet::Error, 'found multiple config items of your_key found, please fix this') expected Puppet::Error with "found multiple config items of your_key found, please fix this", got #<Puppet::Error: found multiple config items of your_key, please fix this> with backtrace: # ./lib/puppet/provider/postgresql_conf/ruby.rb:77:in `exists?' # ./spec/unit/provider/postgresql_conf/ruby_spec.rb:49:in `block (4 levels) in <top (required)>' # ./spec/unit/provider/postgresql_conf/ruby_spec.rb:49:in `block (3 levels) in <top (required)>' # ./vendor/bundle/ruby/3.2.0/bin/rspec:25:in `load' # ./vendor/bundle/ruby/3.2.0/bin/rspec:25:in `<top (required)>' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/cli/exec.rb:58:in `load' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/cli/exec.rb:58:in `kernel_load' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/cli/exec.rb:23:in `run' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/cli.rb:492:in `exec' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/cli.rb:34:in `dispatch' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/cli.rb:28:in `start' # /opt/hostedtoolcache/Ruby/3.2.3/x64/lib/ruby/3.2.0/bundler/friendly_errors.rb:117:in `with_friendly_errors' # /opt/hostedtoolcache/Ruby/3.2.3/x64/bin/bundle:25:in `load' # /opt/hostedtoolcache/Ruby/3.2.3/x64/bin/bundle:25:in `<main>'
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
Expand Down

0 comments on commit e02387e

Please sign in to comment.