Skip to content

Commit 8d561e2

Browse files
committed
Test stubbing exists?
1 parent d4560c1 commit 8d561e2

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@
2929
it 'has a method exists?' do
3030
expect(provider).to respond_to(:exists?)
3131
end
32+
context '#exists?' do
33+
it 'returns true when a matching config item is found' do
34+
config_data = [{ key: 'your_key', value: 'your_value' }]
35+
expect(provider).to receive(:parse_config).and_return(config_data)
36+
37+
expect(provider.exists?).to be true
38+
end
39+
40+
it 'returns false when no matching config item is found' do
41+
config_data = [{ key: 'other_key', value: 'other_value' }]
42+
expect(provider).to receive(:parse_config).and_return(config_data)
43+
44+
expect(provider.exists?).to be false
45+
end
46+
47+
it 'raises an error when multiple matching config items are found' do
48+
config_data = [{ key: 'your_key', value: 'value1' }, { key: 'your_key', value: 'value2' }]
49+
expect(provider).to receive(:parse_config).and_return(config_data)
50+
51+
expect { provider.exists? }.to raise_error(Puppet::Error, 'found multiple config items of your_key found, please fix this' )
52+
end
53+
end
3254

3355
it 'has a method create' do
3456
expect(provider).to respond_to(:create)

0 commit comments

Comments
 (0)