Proposed technique for mocking standalone functions from go-fastly#1686
Closed
kpfleming wants to merge 2 commits into
Closed
Proposed technique for mocking standalone functions from go-fastly#1686kpfleming wants to merge 2 commits into
kpfleming wants to merge 2 commits into
Conversation
df49875 to
5b750d9
Compare
kpfleming
commented
Mar 12, 2026
kpfleming
commented
Mar 12, 2026
kpfleming
commented
Mar 12, 2026
kpfleming
commented
Mar 12, 2026
5b750d9 to
a215f4f
Compare
kpfleming
commented
Mar 12, 2026
jsocol
reviewed
Mar 12, 2026
a215f4f to
60b2085
Compare
kpfleming
commented
Mar 16, 2026
b928da5 to
b0a19a6
Compare
This demonstrates a possible technique for mocking 'new style' API functions in go-fastly that are not methods on the Client object. The techique requires a few things: 1. A new parameter to app.Run which allows tests to modify argparser.Command structs after they have been created but before test scenario execution begins. The explicit intent of this is for injecting a mock function into the command-to-be-tested's structure. 2. A 'CommandHook' field in the testutil.CLIScenario struct which allows a scenario to indicate that it would like to inspect or modify the argparser.Command struct for the command that it tests. 3. New code in RunCLIScenario which detects the presence of a 'CommandHook' in a scenario and then locates the relevant argparser.Command struct, passing it to the 'CommandHook'. Example implementations for two test scenarios for 'fastly compute acl create' are included; one example demonstrates testing the content of the input structure passed to the mock function.
b0a19a6 to
2b25408
Compare
Member
Author
|
The implementation of this has changed substantially so I'm going to open a new PR with the cleaned-up branch, which will also have more documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
THIS PR IS FOR DISCUSSION ONLY, IT IS NOT A COMPLETE IMPLEMENTATION.
Change summary
This demonstrates a possible technique for mocking 'new style' API functions in go-fastly that are not methods on the Client object. The techique requires a few things:
A new parameter to
app.Runwhich allows tests to modifyargparser.Commandstructs after they have been created but before test scenario execution begins. The explicit intent of this is for injecting a mock function into the command-to-be-tested's structure.A
CommandHookfield in thetestutil.CLIScenariostruct which allows a scenario to indicate that it would like to inspect or modify theargparser.Commandstruct for the command that it tests.New code in
RunCLIScenariowhich detects the presence of aCommandHookin a scenario and then locates the relevantargparser.Commandstruct, passing it to theCommandHook.Example implementations for two test scenarios for
fastly compute acl createare included; one example demonstrates testing the content ofthe input structure passed to the mock function.