Skip to content

fix(test): replace os.Args guard with cobra.ExactArgs(3) to prevent panic#391

Open
gyanranjanpanda wants to merge 1 commit into
microcks:masterfrom
gyanranjanpanda:fix/test-args-panic-with-global-flags
Open

fix(test): replace os.Args guard with cobra.ExactArgs(3) to prevent panic#391
gyanranjanpanda wants to merge 1 commit into
microcks:masterfrom
gyanranjanpanda:fix/test-args-panic-with-global-flags

Conversation

@gyanranjanpanda
Copy link
Copy Markdown

@gyanranjanpanda gyanranjanpanda commented May 15, 2026

Summary

Fixes #390

The test command was guarding positional argument count with len(os.Args) < 4. This check includes global persistent flags (--microcksURL, --keycloakClientId, --keycloakClientSecret) in the count. When the command is invoked with global flags but the <runner> argument is omitted - the standard CI invocation pattern shown in the README - os.Args has enough elements to pass the guard, but Cobra's args slice (flags stripped) has only 2 elements. args[2] then causes a runtime panic with a stack trace instead of a clean error message.

Changes

  • cmd/test.go: Use cobra.ExactArgs(3) on the command definition (idiomatic Cobra). The framework validates arg count and prints usage before Run is invoked - no manual guard needed.
    • cmd/test.go: Update Use field to "test <apiName:apiVersion> <testEndpoint> <runner>" so --help output shows the expected signature.
    • cmd/test.go: Remove three now-dead strings.HasPrefix(x, "-") guards - Cobra strips all flags before passing args to Run, so none of those conditions can ever be true.
    • cmd/test.go: Remove redundant var testResultID string pre-declaration immediately shadowed by :=.
    • cmd/test.go: Fix runner validation error message: listed SOAP (not in runnerChoices) instead of SOAP_HTTP.
    • cmd/test_test.go: Add regression test that simulates the CI invocation pattern (global flags + missing runner) and asserts exit code 1 with no panic: in output.

Before / After

Before (with global flags, missing <runner>):

goroutine 1 [running]:
runtime error: index out of range [2] with length 2
    cmd/test.go:57 +0x...

After:

Error: accepts 3 arg(s), received 2
Usage:
  microcks test <apiName:apiVersion> <testEndpoint> <runner> [flags]

Testing

go test ./cmd/... -run TestTestCommandMissingRunnerWithGlobalFlagsDoesNotPanic -v
=== RUN   TestTestCommandMissingRunnerWithGlobalFlagsDoesNotPanic
--- PASS: TestTestCommandMissingRunnerWithGlobalFlagsDoesNotPanic (0.01s)
PASS

@github-actions
Copy link
Copy Markdown

👋 @gyanranjanpanda

Welcome to the Microcks community! 💖

Thanks and congrats 🎉 for opening your first pull request here! Be sure to follow the pull request template or please update it accordingly.

Hope you have a great time there!

…anic

The test command validated argument count using len(os.Args) < 4, which
counts global flags like --microcksURL and --keycloakClientId as part of
the argument vector. When a user invokes the command with global flags
but omits the <runner> positional argument (the typical CI pattern from
the README), os.Args has enough entries to pass the guard, but cobra's
parsed args slice only has 2 elements. The subsequent access to args[2]
causes a runtime panic with a stack trace instead of a clean error.

Fix:
- Use cobra.ExactArgs(3) on the command definition — idiomatic and
  handles count validation before Run is ever called.
- Remove three now-dead HasPrefix guards that were unreachable since
  cobra strips flags before handing args to Run.
- Remove a redundant var testResultID string pre-declaration that was
  immediately shadowed by :=.
- Fix the runner error message which listed SOAP (not a valid value)
  instead of SOAP_HTTP.
- Update Use string to show positional args in help output.

Fixes microcks#390

Signed-off-by: gyanranjanpanda <sanupanda141@gmail.com>
@gyanranjanpanda gyanranjanpanda force-pushed the fix/test-args-panic-with-global-flags branch from 2fb3dc8 to 2c553b9 Compare May 15, 2026 19:52
@gyanranjanpanda
Copy link
Copy Markdown
Author

could @lbroudoux review this pr

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.

Bug: microcks test panics with index out of range when global flags are used and <runner> arg is omitted

1 participant