Bug: user defined commands not appearing in usage#16
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to fix a CLI usability bug where user-defined profile commands were not being registered early enough to appear in raid help/usage output, by introducing centralized user-command registration and adding tests around that behavior.
Changes:
- Adds
registerUserCommandsto dynamically attach profile-defined commands to the Cobra root command (skipping reserved built-in names with a warning). - Adjusts CLI initialization to register user commands for all invocation types (including info/help flows).
- Adds tests for user command registration/help visibility and introduces
lib.GetContext/lib.SetContextto simplify test setup.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/cmd/raid.go |
Registers user-defined commands on the root Cobra command before execution; changes initialization timing. |
src/cmd/raid_test.go |
Adds unit tests/helpers for user command registration and help output assertions. |
src/internal/lib/lib.go |
Exposes context getters/setters to support test isolation by swapping the active context. |
Comment on lines
+41
to
+45
| // GetContext returns the active context. Intended for use in tests. | ||
| func GetContext() *Context { return context } | ||
|
|
||
| // SetContext replaces the active context. Intended for use in tests. | ||
| func SetContext(c *Context) { context = c } |
Comment on lines
84
to
86
| applyConfigFlag(os.Args) | ||
| if !info { | ||
| raid.Initialize() | ||
| } | ||
| raid.Initialize() | ||
|
|
Comment on lines
+190
to
+204
| // Regression: user commands must appear in help output for every invocation | ||
| // type, including info commands (no args, --help, help) and unknown commands. | ||
| func TestRegisterUserCommands_visibleForAllInvocationTypes(t *testing.T) { | ||
| withCommands(t, []lib.Command{{Name: "build", Usage: "Build services"}}) | ||
|
|
||
| invocations := []string{"(no args)", "--help", "help", "unknown-cmd"} | ||
| for _, inv := range invocations { | ||
| t.Run(inv, func(t *testing.T) { | ||
| root := newTestRoot() | ||
| registerUserCommands(root) | ||
| if !strings.Contains(helpOutput(root), "build") { | ||
| t.Errorf("'build' command missing from help output for invocation %q", inv) | ||
| } | ||
| }) | ||
| } |
…ate tests accordingly
…or all invocation types
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16 +/- ##
==========================================
- Coverage 72.78% 72.41% -0.38%
==========================================
Files 27 27
Lines 1459 1479 +20
==========================================
+ Hits 1062 1071 +9
- Misses 358 368 +10
- Partials 39 40 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
This pull request introduces a new mechanism for registering user-defined commands from the active profile into the CLI, along with comprehensive tests to ensure correct behavior. The main focus is on improving extensibility and reliability of command registration, especially for user commands and their visibility in help output.
User command registration improvements:
registerUserCommandsfunction tosrc/cmd/raid.gothat dynamically registers user-defined commands from the active profile to the root command, skipping reserved built-in names with a warning.src/cmd/raid.goto ensure user commands are registered regardless of command type (info or regular), improving consistency and visibility.Testing enhancements:
src/cmd/raid_test.goto verify user command registration, including checks for empty profiles, reserved name handling, help output visibility, and regression tests for all invocation types.src/cmd/raid_test.gofor test setup and help output capture, facilitating robust testing of command registration.Testability improvements in core library:
GetContextandSetContextfunctions insrc/internal/lib/lib.goto allow manipulation of the active context during tests, enabling more reliable and isolated test scenarios.