-
Notifications
You must be signed in to change notification settings - Fork 943
fix: show --help message for CLI errors, add tests for delete #1403
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
Changes from 5 commits
758dceb
6ff347e
16c19c7
dae45ab
32cbc85
a7bd7bd
c28e93a
8ac2198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cli_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/pty/ptytest" | ||
) | ||
|
||
func TestDelete(t *testing.T) { | ||
t.Run("WithParameter", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
coderdtest.NewProvisionerDaemon(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID) | ||
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
cmd, root := clitest.New(t, "delete", workspace.Name) | ||
clitest.SetupConfig(t, client, root) | ||
doneChan := make(chan struct{}) | ||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
go func() { | ||
defer close(doneChan) | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
}() | ||
pty.ExpectMatch("Cleaning Up") | ||
<-doneChan | ||
}) | ||
t.Run("WithoutParameters", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cmd, _ := clitest.New(t, "delete") | ||
|
||
err := cmd.Execute() | ||
require.ErrorContains(t, err, "Run 'coder delete --help' for usage.") | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,13 @@ import ( | |
|
||
func main() { | ||
dadjoke() | ||
err := cli.Root().Execute() | ||
cmd, err := cli.Root().ExecuteC() | ||
if err != nil { | ||
if errors.Is(err, cliui.Canceled) { | ||
os.Exit(1) | ||
} | ||
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error())) | ||
helpErrMsg := fmt.Sprintf("Run '%s %s --help' for usage.", cmd.Root().Name(), cmd.Name()) | ||
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()+"/n"+helpErrMsg)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most errors are not usage errors and shouldn't show the usage. Stuff like HTTP errors would trigger this for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! @f0ssel mentioned the same thing. Maybe that's why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we can detect a cobra error like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked around the docs but didn't find anything. Seems like it just returns a regular error. Maybe someone sees something though. |
||
os.Exit(1) | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.