From 6dae48a1a86fa66c490262509183f77faf111bfe Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 19 May 2022 15:35:59 -0700 Subject: [PATCH] fix: show --help message for CLI errors, add tests for delete (#1403) * feat(cli): add test for delete This adds a new test for the `delete` command to ensure it works as expected when provided the correct args. * fix(cli): use ExecuteC() to match Cobra This modifies the `cli.Root().Execute()` to `cli.Root).ExecuteC()` to match the default behavior of Cobra. We do this so errors will always print the "run --help" line. * feat(cli): add WithoutParameters test for delete This adds a new test to the `delete_test.go` suite to ensure the correct behavior occurs when `delete` is called without an argument. * fixup! feat(cli): add WithoutParameters test for delete * refactor(cli): show --help error message on main This adds an error message which shows when there is an error with any commands called to improve the UX. * fixup! refactor(cli): show --help error message on main * refactor(cli): handle err with FormatCobraError This adds a new helper function called `FormatCobraError` to `root.go` so that we can colorize and add "--help" message to cobra command errors like calling `delete`. * refactor(cli): add root_test.go, move delete test --- cli/delete_test.go | 38 ++++++++++++++++++++++++++++++++++++++ cli/root.go | 7 +++++++ cli/root_test.go | 22 ++++++++++++++++++++++ cmd/coder/main.go | 5 +++-- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 cli/delete_test.go create mode 100644 cli/root_test.go diff --git a/cli/delete_test.go b/cli/delete_test.go new file mode 100644 index 0000000000..2f67fc52bd --- /dev/null +++ b/cli/delete_test.go @@ -0,0 +1,38 @@ +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 + }) +} diff --git a/cli/root.go b/cli/root.go index c2c9a9d47c..ddd0c2c2d2 100644 --- a/cli/root.go +++ b/cli/root.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "net/url" "os" "time" @@ -259,3 +260,9 @@ func versionTemplate() string { template += "\r\n" return template } + +// FormatCobraError colorizes and adds "--help" docs to cobra commands. +func FormatCobraError(err error, cmd *cobra.Command) string { + helpErrMsg := fmt.Sprintf("Run '%s %s --help' for usage.", cmd.Root().Name(), cmd.Name()) + return cliui.Styles.Error.Render(err.Error() + "\n" + helpErrMsg) +} diff --git a/cli/root_test.go b/cli/root_test.go new file mode 100644 index 0000000000..28206cc467 --- /dev/null +++ b/cli/root_test.go @@ -0,0 +1,22 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/cli" + "github.com/coder/coder/cli/clitest" +) + +func TestRoot(t *testing.T) { + t.Run("FormatCobraError", func(t *testing.T) { + t.Parallel() + + cmd, _ := clitest.New(t, "delete") + + cmd, err := cmd.ExecuteC() + errStr := cli.FormatCobraError(err, cmd) + require.Contains(t, errStr, "Run 'coder delete --help' for usage.") + }) +} diff --git a/cmd/coder/main.go b/cmd/coder/main.go index 1c09c69851..f6c2b511ab 100644 --- a/cmd/coder/main.go +++ b/cmd/coder/main.go @@ -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())) + cobraErr := cli.FormatCobraError(err, cmd) + _, _ = fmt.Fprintln(os.Stderr, cobraErr) os.Exit(1) } }