fix(configuration): coerce typed flag values via spf13/cast#281
Open
matuszeg wants to merge 2 commits intogo-gremlins:mainfrom
Open
fix(configuration): coerce typed flag values via spf13/cast#281matuszeg wants to merge 2 commits intogo-gremlins:mainfrom
matuszeg wants to merge 2 commits intogo-gremlins:mainfrom
Conversation
…ins#216) Get[T] used an unchecked type assertion against viper.Get, which silently returned the zero value when the bound value originated from a pflag (pflag surfaces float64 values through Value.String()). The threshold-efficacy and threshold-mcover CLI flags therefore never triggered the threshold exit code, even though the same thresholds worked from a config file. Coerce numeric and string types through spf13/cast so flag-sourced and config-sourced values both resolve to the native Go type.
Satisfies the CRT-P0001 'can combine chain of 2 appends into one' lint on the PR's external code-review bot. Pre-existing pattern in the file; collapsed for parity with the bot's expectations now that the file is in the PR diff.
Author
|
After my first commit, I saw that "DeepSource: Go" was failing because of some append optimizations in code I didn't touch in onfiguration_test.go. I went ahead and fixed them to make it green, but if you don't want that as part of this PR to keep the changes isolated I can remove them. |
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.
Proposed changes
Closes #216.
--threshold-efficacyand--threshold-mcovernever produced a non-zero exit code, even when the actual efficacy/coverage was below the threshold. The same thresholds set via.gremlins.yamlworked correctly, so the bug only affected the CLI-flag path.Root cause
configuration.Get[T]used an unchecked type assertion againstviper.Get:When a value is bound into Viper through
viper.BindPFlag, pflag-resolvedfloat64values surface as their string representation (viapflag.Value.String()), so the assertion tofloat64silently failed and returned the zero value. The threshold check ininternal/report/report.gothen sawet == 0and skipped enforcement.Config-file values bypassed this because Viper stores YAML scalars as native Go types, so the assertion succeeded.
Fix
Coerce
float64,int,bool, andstringthroughspf13/cast(already an indirect dependency), and fall back to the assertion for slices and other types. This normalizes both pflag-sourced and config-sourced values to the native Go type before they leaveGet[T].Tests
Added three layered regression tests so the bug stays fixed regardless of where a future refactor lands:
internal/configuration:TestGetFromBoundPFlag—Get[T]over a bound pflag in isolation.cmd/internal/flags:TestSetBindsTypedValueFromCLI— fullflags.Set→ cobra parse →configuration.Get[T].cmd:TestUnleashFlagsPropagateToConfiguration— exercises the realunleashcommand surface.All three fail without the fix and pass with it.
End-to-end verified by rebuilding the binary and running
gremlins unleash --threshold-efficacy 50against a small module with 0% efficacy: exit code is now10(was0).Types of changes
Checklist
make all)Further comments
spf13/castis promoted from indirect to direct ingo.mod; it was already pulled in transitively throughviper, so no new dependency is added to the module graph.The original feature request in #216 also asked for
--fail-lived/--fail-not-coveredflags. That part is intentionally out of scope for this PR — this change only fixes the existing threshold flags so they actually work as documented. Per-status fail flags can be a follow-up.