Refactor: Improve error handling, add tests, and update deps#1
Merged
Conversation
This commit introduces several improvements to the goconf package:
1. **Error Handling:**
- `printTable` now returns errors instead of calling `log.Fatal`, allowing callers to handle issues more gracefully.
- `printTable` now accepts an `io.Writer` for more flexible output destinations.
- The `Load` function now accumulates all errors encountered during the processing of configurations (Register, Validate, Print) and returns them as a single, consolidated error. This provides a more complete report of failures.
2. **Unit Tests:**
- Added a new test suite in `register_test.go`.
- Implemented tests for the `Load` function, covering success cases, registration failures, validation failures, and multiple error accumulation.
- Implemented tests for the (unexported) `printTable` function, verifying successful output to a buffer.
- Mock implementations for `Configer`, `Validater`, and `Printer` interfaces were created to facilitate testing.
- Noted that simulating errors from the `hush.Hush` library proved difficult with simple mock data, and tests were adjusted accordingly.
3. **Dependencies:**
- Updated `github.com/tlmanz/hush` from `v0.2.1` to `v0.2.3`.
- Other dependencies (`olekukonko/tablewriter`, `tryfix/log`) could not be updated due to their newer versions requiring a higher Go version than the project's current `go 1.21.7`.
4. **Minor Considerations:**
- The pattern of calling `hush.NewHush()` within `printTable` was reviewed. Given its likely lightweight nature, no changes were made to this for now, prioritizing code simplicity.
These changes enhance the robustness, testability, and maintainability of the goconf package.
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 commit introduces several improvements to the goconf package:
Error Handling:
printTablenow returns errors instead of callinglog.Fatal, allowing callers to handle issues more gracefully.printTablenow accepts anio.Writerfor more flexible output destinations.Loadfunction now accumulates all errors encountered during the processing of configurations (Register, Validate, Print) and returns them as a single, consolidated error. This provides a more complete report of failures.Unit Tests:
register_test.go.Loadfunction, covering success cases, registration failures, validation failures, and multiple error accumulation.printTablefunction, verifying successful output to a buffer.Configer,Validater, andPrinterinterfaces were created to facilitate testing.hush.Hushlibrary proved difficult with simple mock data, and tests were adjusted accordingly.Dependencies:
github.com/tlmanz/hushfromv0.2.1tov0.2.3.olekukonko/tablewriter,tryfix/log) could not be updated due to their newer versions requiring a higher Go version than the project's currentgo 1.21.7.Minor Considerations:
hush.NewHush()withinprintTablewas reviewed. Given its likely lightweight nature, no changes were made to this for now, prioritizing code simplicity.These changes enhance the robustness, testability, and maintainability of the goconf package.