[test-improver] Improve tests for internal/cmd (registerGuardsModeFlag) - #10646
Conversation
- Extract registerGuardsModeCompletion from registerGuardsModeFlag to make the completion-registration error path independently testable. - Add TestRegisterGuardsModeFlag covering flag defaults, successful completion registration, and the previously-untested error branch when RegisterFlagCompletionFunc returns 'already registered' (now only logged, not propagated). - registerGuardsModeFlag coverage: 66.7% -> 100.0%. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds targeted coverage for --guards-mode flag and completion registration.
Changes:
- Extracts completion registration into a testable helper.
- Adds flag registration and duplicate-completion tests.
Show a summary per file
| File | Description |
|---|---|
internal/cmd/flags_difc.go |
Extracts completion registration logic. |
internal/cmd/flags_difc_test.go |
Adds focused registration tests. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Balanced
| require.NoError(t, cmd.RegisterFlagCompletionFunc("guards-mode", cobra.FixedCompletions( | ||
| difc.ValidModes, cobra.ShellCompDirectiveNoFileComp))) | ||
|
|
||
| registerGuardsModeCompletion(cmd) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@copilot address review feedback |
|
@copilot address review feedback |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
🔒 mcpg Read-Only Stress — defaultSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Overall: PASS
References: §30918053207
|
🔒 mcpg Read-Only Stress — gVisorSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Overall: PASS Gateway enforcement: write tools absent from MCP registry ( References: §30918053221
|
🔒 mcpg Read-Only Stress — docker-sbxSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Overall: PASS
References: §30918053088
|
Summary
Improved test coverage for
internal/cmd/flags_difc.go, specificallyregisterGuardsModeFlag, which registers the--guards-modeCLI flag and its shell completion function.File analyzed
internal/cmd/flags_difc.gointernal/cmd/flags_difc_test.goAnalysis
Ran
go test -coverprofile=/tmp/cov.out ./internal/cmd/+go tool cover -funcand foundregisterGuardsModeFlagat only 66.7% coverage. The uncovered branch was the error path wherecmd.RegisterFlagCompletionFuncfails (e.g. when a completion func is already registered for that flag name) — this error was only logged viadebugLog.Printf, never returned, so it had no direct test.Changes
registerGuardsModeCompletion(cmd *cobra.Command)out ofregisterGuardsModeFlagso the completion-registration logic (and its failure path) can be exercised in isolation without needing to re-declare the underlying flag (which would panic via pflag's duplicate-flag registration).TestRegisterGuardsModeFlaginflags_difc_test.go):registers flag with default value and completion— verifies the flag is registered withdifc.DefaultEnforcementMode()as default.registers completion successfully— verifiesregisterGuardsModeCompletionsucceeds on a fresh flag.logs but does not panic when completion func already registered— pre-registers the completion func on the command, then callsregisterGuardsModeCompletionagain to force cobra's "flag completion already registered" error, asserting it's handled gracefully (logged, not propagated/panicking).All changes use testify (
require/assert) consistent with the rest of the file, and preserve all existing passing tests unchanged.Coverage
registerGuardsModeFlag: 66.7% → 100.0%registerGuardsModeCompletion(new): 100.0%Verification
Cache-memory updated with
internal/cmd/flags_difc_test.goto avoid re-selecting this file in future runs.