Skip to content

[test] Add tests for cmd.registerAllFlags, registerFlagCompletions, and OTLP flag defaults#3694

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/cmd-flags-completions-tracing-232a6db45d2963b7
Draft

[test] Add tests for cmd.registerAllFlags, registerFlagCompletions, and OTLP flag defaults#3694
github-actions[bot] wants to merge 1 commit intomainfrom
test-coverage/cmd-flags-completions-tracing-232a6db45d2963b7

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Test Coverage Improvement: cmd flags package

Function Analyzed

  • Package: internal/cmd
  • Primary Function: registerFlagCompletions
  • Supporting Functions: registerAllFlags, RegisterFlag, getDefaultOTLPEndpoint, getDefaultOTLPServiceName
  • Previous Coverage: 0% (none of these functions had any direct tests)
  • Complexity: Medium-High (registerFlagCompletions registers 6 flag completions + ValidArgsFunction)

Why This Function?

registerFlagCompletions in flags.go was completely untested despite being a non-trivial function that configures shell completion behavior for 6 flags (--config, --log-dir, --payload-dir, --env, --guards-mode, --allowonly-min-integrity) plus a ValidArgsFunction. Every other flags_*.go file in the package had corresponding tests (e.g., flags_logging_test.go, flags_difc_test.go) but flags.go and flags_tracing.go had none.

Tests Added

internal/cmd/flags_tracing_test.go

  • TestGetDefaultOTLPEndpoint: env var OTEL_EXPORTER_OTLP_ENDPOINT lookup (no env, custom value, empty → disabled, trailing slash)
  • TestGetDefaultOTLPServiceName: env var OTEL_SERVICE_NAME with config.DefaultTracingServiceName fallback (5 cases)
  • TestGetDefaultOTLPServiceName_DefaultIsCorrect: guards the "mcp-gateway" constant

internal/cmd/flags_test.go

  • TestRegisterAllFlags: verifies all expected flags are registered (core, logging, tracing, DIFC), flagRegistrars is populated
  • TestRegisterFlagCompletions: exercises all 6 flag completion functions and ValidArgsFunction:
    • --config.toml extension filter
    • --log-dir / --payload-dir → directory filter
    • --env.env extension filter
    • --guards-mode{strict, filter, propagate} with no-file-comp
    • --allowonly-min-integrity{none, unapproved, approved, merged} with no-file-comp
    • ValidArgsFunction → active help tip mentioning --config
  • TestRegisterFlag: verifies RegisterFlag appends to flagRegistrars and all registrars are invoked

Test Pattern

Follows the same table-driven style as flags_logging_test.go and flags_difc_test.go. Uses t.Setenv for env var isolation and cobra.GetFlagCompletionFunc (available since cobra v1.6.1, project uses v1.10.2) to directly verify completion function registrations.

Coverage Report

Before: 0% direct test coverage for registerFlagCompletions, registerAllFlags, 
        getDefaultOTLPEndpoint, getDefaultOTLPServiceName
After:  All branches covered by 3 test functions (14 sub-tests total)

Generated by Test Coverage Improver
Next run will target the next most complex under-tested function

Generated by Test Coverage Improver · ● 18.3M ·

… flag defaults

Cover previously untested functions in internal/cmd:
- getDefaultOTLPEndpoint: env var OTEL_EXPORTER_OTLP_ENDPOINT lookup
- getDefaultOTLPServiceName: env var OTEL_SERVICE_NAME with default fallback
- registerAllFlags: verifies all flag modules are called and flags registered
- registerFlagCompletions: verifies all completion functions and ValidArgsFunction
- RegisterFlag: verifies registered functions are invoked by registerAllFlags

These tests follow the same table-driven pattern as flags_logging_test.go
and flags_difc_test.go for consistent coverage of flag helpers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants