Skip to content

Refactor: Improve error handling, add tests, and update deps#1

Merged
tlmanz merged 1 commit intomasterfrom
refactor/improvements-and-tests
May 23, 2025
Merged

Refactor: Improve error handling, add tests, and update deps#1
tlmanz merged 1 commit intomasterfrom
refactor/improvements-and-tests

Conversation

@tlmanz
Copy link
Owner

@tlmanz tlmanz commented May 23, 2025

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 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.
@tlmanz tlmanz merged commit 70d1031 into master May 23, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant