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

Add information on mocking an external function #51

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rnelson0
Copy link

When not testing the function itself, you still need to mock it so it's available to the calling class.

When not testing the function itself, you still need to mock it so it's available to the calling class.
@rnelson0 rnelson0 requested a review from a team as a code owner April 24, 2023 19:25
@CLAassistant
Copy link

CLAassistant commented Apr 24, 2023

CLA assistant check
All committers have signed the CLA.


You may not need to test a function itself, but a calling class may require the function
to return a value. You can mock the function once and make it available to all test.
Modify *spec/spec_helper_local.rb* and use `Puppet::Parser::Functions.newfunction()`
Copy link

Choose a reason for hiding this comment

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

rspec-puppet really doesn't mention spec_helper_local.rb anywhere. That's really a PDK-thing and I'd avoid it here.

RSpec.configure do | c|
c.before :each do
# The vault_lookup function takes a single argument and returns a hash with three keys
Puppet::Parser::Functions.newfunction(:vault_lookup, type: :rvalue) do |_args|
Copy link

Choose a reason for hiding this comment

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

This is the legacy function API. The current standard should be used everywhere.

Copy link
Author

Choose a reason for hiding this comment

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

There's some discussion of that in slack and how it would apply to tests, there's not a lot of clarity there and the "obvious" code doesn't seem to work. Very happy to move to that if we can determine the correct syntax to use.

Copy link

Choose a reason for hiding this comment

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

I'm looking at the function testing code and Puppet.override looks promising. It is a private API of Puppet, but looks promising.

def find_function(function_name = self.class.top_level_description)
with_vardir do
env = adapter.current_environment
if Puppet.version.to_f >= 4.0
context_overrides = compiler.context_overrides
func = nil
loaders = Puppet.lookup(:loaders)
Puppet.override(context_overrides, 'rspec-test scope') do
func = V4FunctionWrapper.new(function_name,
loaders.private_environment_loader.load(:function, function_name), context_overrides)
@scope = context_overrides[:global_scope]
end
return func if func.func
end
if Puppet::Parser::Functions.function(function_name)
V3FunctionWrapper.new(function_name, scope.method("function_#{function_name}".intern))
end
end
end

Another problem you may have with the modern API is that it prefers namespaced functions, or there's a difference in priority.

@gavindidrichsen
Copy link

Thanks for your contributions here @rnelson0 , @ekohl.

As it seems there's still a bit of discussion about the implementation details, I'm going to move this draft. We'll certainly have a look again at this whenever it comes back up.

@gavindidrichsen gavindidrichsen marked this pull request as draft June 6, 2024 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants