Add test and fix bug in default help/printing of duration#3
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 182 181 -1
=========================================
- Hits 182 181 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in the default value handling for duration flags and adds comprehensive test coverage. The fix ensures that duration flags properly display their default values in help text using the human-readable format (e.g., "1d3h") instead of the standard time.Duration format.
- Fixed initialization order issue in
FlagSetVarfunction to prevent incorrect default value display - Added test to verify default values are properly shown in help text
- Added test to ensure zero default values don't show "(default" text in help
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| duration.go | Fixed bug in FlagSetVar by reordering variable assignment and flag registration |
| duration_test.go | Added imports and two test functions to verify help text behavior with default values |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| // FlagSet is like [FlagVar] but for the specified flag set. Replacement for [flag.FlagSet.DurationVar]. | ||
| func FlagSetVar(fs *flag.FlagSet, p *time.Duration, name string, value time.Duration, usage string) { | ||
| d := Duration(value) | ||
| *p = value |
There was a problem hiding this comment.
The fix creates a potential issue where *p is set to the raw time.Duration value before registering the flag. This means the flag's String() method will return the standard Go duration format (e.g., '27h0m0s') instead of the human-readable format (e.g., '1d3h') for default values in help text. Consider creating a Duration instance first to ensure proper formatting: d := Duration(value); *p = time.Duration(d); fs.Var((*Duration)(p), name, usage)
| *p = value | |
| d := Duration(value) | |
| *p = time.Duration(d) |
There was a problem hiding this comment.
this is the fix actually - need value indeed to be set for default to work
No description provided.