Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1532) Replace ParserError with Puppet::Error #1540

Merged
merged 1 commit into from
Apr 11, 2024

Conversation

bastelfreak
Copy link
Collaborator

@bastelfreak bastelfreak commented Oct 19, 2023

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 bastelfreak@179472b

Alternative implementation for #1538

Summary

Provide a detailed description of all the changes present in this pull request.

Additional Context

Add any additional context about the problem here.

  • Root cause and the steps to reproduce. (If applicable)
  • Thought process behind the implementation.

Related Issues (if any)

Mention any related issues or pull requests.

Checklist

  • 🟢 Spec tests.
  • 🟢 Acceptance tests.
  • Manually verified. (For example puppet apply)

@bastelfreak bastelfreak changed the title Test stubbing exists? (#1532) Replace ParserError with Puppet::Error Oct 19, 2023
@@ -11,6 +11,7 @@
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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to change line 7 to:

let(:resource) { Puppet::Type.type(:postgresql_conf).new(name: 'foo', key: 'foo', value: 'bar') }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay. will update

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhm this line is still requires to get the tests passing

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This confuses me and now I wonder what's wrong. Perhaps tomorrow I'll have some idea.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think I know what is going on. Line 7 instantiates a new Puppet type with the desired state. The provider has the actual state. You're mocking that here. So by mocking this, you know it is already in place.

So at the very least I'd expect you to only mock it when you want it to exist, not before each test.

It also makes me wonder about something else. key is a parameter, not a property. So I'm not sure it should be loaded from the provider.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is now redundant, right?

Suggested change
allow(provider).to receive(:resource).and_return(key: 'your_key', line_number: 1, value: 'foo')

@bastelfreak bastelfreak force-pushed the rubocop2 branch 2 times, most recently from 7bc3141 to 481eb3f Compare October 19, 2023 19:46
@@ -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
Copy link

@joshcooper joshcooper Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, providers can call the fail method which will raise Puppet::Error, for example see the base exec provider: https://github.com/puppetlabs/puppet/blob/fce49baa3767fffb000f1b26d3a7b4edbec58999/lib/puppet/provider/exec.rb#L105 There should really be a specification about what methods are available for providers...

Copy link
Collaborator Author

@bastelfreak bastelfreak Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to fix a simple typo. I don't know how to properly write tests for the fail() method. I cannot justify to the customer that I spent several days within a 6 month period on this fix. If someone wants to pick it up: feel free.

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 puppetlabs#1538
@Ramesh7 Ramesh7 merged commit 68cc244 into puppetlabs:main Apr 11, 2024
40 of 44 checks passed
@bastelfreak bastelfreak deleted the rubocop2 branch April 24, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Postgresql::Server::Config_entry uninitialized constant ParserError
7 participants