refac(io) set IO once in main to allow overriding with in memory io i…#1363
Open
cgoetz-inovex wants to merge 2 commits intomainfrom
Open
refac(io) set IO once in main to allow overriding with in memory io i…#1363cgoetz-inovex wants to merge 2 commits intomainfrom
cgoetz-inovex wants to merge 2 commits intomainfrom
Conversation
…n tests `print.Printer` had a reference to a `cobra.Command` for using its IO streams. Each Command also used a Printer, resulting in an awkward circular dependency. Refactored Printer to use IO streams directly. When using the application these are set in `main`, when used in tests these can be set to `bytes.Buffer`s. Also replaced usages of `os.Args` with just a string slice. Also set in `main`. `cobra.Command`s `Args` and IO-streams are set in `NewRootCmd` with `traverseCommands`. `CmdParams` also has an `fs.FS`, currently unused but will allow using the real FS during regular use and an in-memory-FS during tests. This change prepares the application for integrative testing while keeping good isolation. Generally speaking the goal is to move all things with side effects into main (compare with https://grafana.com/blog/how-i-write-http-services-in-go-after-13-years/#func-main-only-calls-run)
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.
…n tests
print.Printerhad a reference to acobra.Commandfor using its IO streams. Each Command also used a Printer, resulting in an awkward circular dependency.Refactored Printer to use IO streams directly. When using the application these are set in
main, when used in tests these can be set tobytes.Buffers.Also replaced usages of
os.Argswith just a string slice. Also set inmain.cobra.CommandsArgsand IO-streams are set inNewRootCmdwithtraverseCommands.CmdParamsalso has anfs.FS, currently unused but will allow using the real FS during regular use and an in-memory-FS during tests.This change prepares the application for integrative testing while keeping good isolation. Generally speaking the goal is to move all things with side effects into main (compare with https://grafana.com/blog/how-i-write-http-services-in-go-after-13-years/#func-main-only-calls-run)
Description
relates to #1234
Checklist
make fmtmake generate-docs(will be checked by CI)make test(will be checked by CI)make lint(will be checked by CI)