Skip to content

Proposed technique for mocking standalone functions from go-fastly#1686

Closed
kpfleming wants to merge 2 commits into
fastly:mainfrom
kpfleming:go-fastly-api-mocks
Closed

Proposed technique for mocking standalone functions from go-fastly#1686
kpfleming wants to merge 2 commits into
fastly:mainfrom
kpfleming:go-fastly-api-mocks

Conversation

@kpfleming

@kpfleming kpfleming commented Mar 11, 2026

Copy link
Copy Markdown
Member

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:

  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.

@kpfleming kpfleming added DO NOT MERGE YET Skip-Changelog do not add a changelog entry for this change labels Mar 11, 2026
@kpfleming kpfleming force-pushed the go-fastly-api-mocks branch 3 times, most recently from df49875 to 5b750d9 Compare March 12, 2026 15:09
Comment thread pkg/testutil/apihookscenarios.go Outdated
Comment thread pkg/testutil/apihookscenarios.go Outdated
Comment thread pkg/commands/compute/computeacl/create.go Outdated
Comment thread pkg/commands/compute/computeacl/computeacl_test.go Outdated
@kpfleming kpfleming force-pushed the go-fastly-api-mocks branch from 5b750d9 to a215f4f Compare March 12, 2026 15:19
Comment thread pkg/testutil/apihookscenarios.go Outdated
Comment thread pkg/commands/compute/computeacl/create.go Outdated
@kpfleming kpfleming force-pushed the go-fastly-api-mocks branch from a215f4f to 60b2085 Compare March 16, 2026 16:20
Comment thread pkg/commands/compute/computeacl/computeacl_test.go Outdated
@kpfleming kpfleming force-pushed the go-fastly-api-mocks branch 2 times, most recently from b928da5 to b0a19a6 Compare March 18, 2026 20:15
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.
@kpfleming kpfleming force-pushed the go-fastly-api-mocks branch from b0a19a6 to 2b25408 Compare March 18, 2026 20:30
Comment thread pkg/commands/compute/computeacl/computeacl_test.go Outdated
@kpfleming

Copy link
Copy Markdown
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.

@kpfleming kpfleming closed this Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DO NOT MERGE YET Skip-Changelog do not add a changelog entry for this change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants