-
Notifications
You must be signed in to change notification settings - Fork 897
chore: prevent nil dereferences on cmd handlers #8319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b453f7
chore: detect nil cmd handlers
Emyrk 9fa88f8
Fix typo
Emyrk 7de7e77
Parallelize tests
Emyrk 63073c4
Expand comment lanugage on the nil handler
Emyrk eaa2218
Merge remote-tracking branch 'origin/main' into stevenmasley/cmd_help
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package clitest | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coder/coder/cli/clibase" | ||
) | ||
|
||
// HandlersOK asserts that all commands have a handler. | ||
// Without a handler, the command has no default behavior. Even for | ||
// non-root commands (like 'groups' or 'users'), a handler is required. | ||
// These handlers are likely just the 'help' handler, but this must be | ||
// explicitly set. | ||
func HandlersOK(t *testing.T, cmd *clibase.Cmd) { | ||
cmd.Walk(func(cmd *clibase.Cmd) { | ||
if cmd.Handler == nil { | ||
// If you see this error, make the Handler a helper invoker. | ||
// Handler: func(inv *clibase.Invocation) error { | ||
// return inv.Command.HelpHandler(inv) | ||
// }, | ||
t.Errorf("command %q has no handler, change to a helper invoker using: 'inv.Command.HelpHandler(inv)'", cmd.Name()) | ||
} | ||
}) | ||
} |
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This explains what, but not why? I'm guessing it's the call to
h
below, but why don't we just default toh
beingfunc(inv *clibase.Invocation) error { return inv.Command.HelpHandler(inv) }
in this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mafredri I was wondering if defaulting to the help command would be a good solution.
Atm we manually invoke the help on all cmds except this one. So if I made that change it would be a new behavior that is probably fine, but being explicit is also pretty easy. Now that I have a unit test that will fail if you add a new cmd without a help, I figured I'd keep it being manual.
I'll update the comment with more explanation as to why. The unit test failure has that language near it