Skip to content

Commit eb9781b

Browse files
jsjoeiokylecarbs
authored andcommitted
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
1 parent d0a1c4e commit eb9781b

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

cli/delete_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cli_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/cli/clitest"
9+
"github.com/coder/coder/coderd/coderdtest"
10+
"github.com/coder/coder/pty/ptytest"
11+
)
12+
13+
func TestDelete(t *testing.T) {
14+
t.Run("WithParameter", func(t *testing.T) {
15+
t.Parallel()
16+
client := coderdtest.New(t, nil)
17+
user := coderdtest.CreateFirstUser(t, client)
18+
coderdtest.NewProvisionerDaemon(t, client)
19+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
20+
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
21+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
22+
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
23+
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
24+
cmd, root := clitest.New(t, "delete", workspace.Name)
25+
clitest.SetupConfig(t, client, root)
26+
doneChan := make(chan struct{})
27+
pty := ptytest.New(t)
28+
cmd.SetIn(pty.Input())
29+
cmd.SetOut(pty.Output())
30+
go func() {
31+
defer close(doneChan)
32+
err := cmd.Execute()
33+
require.NoError(t, err)
34+
}()
35+
pty.ExpectMatch("Cleaning Up")
36+
<-doneChan
37+
})
38+
}

cli/root.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"fmt"
45
"net/url"
56
"os"
67
"time"
@@ -259,3 +260,9 @@ func versionTemplate() string {
259260
template += "\r\n"
260261
return template
261262
}
263+
264+
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
265+
func FormatCobraError(err error, cmd *cobra.Command) string {
266+
helpErrMsg := fmt.Sprintf("Run '%s %s --help' for usage.", cmd.Root().Name(), cmd.Name())
267+
return cliui.Styles.Error.Render(err.Error() + "\n" + helpErrMsg)
268+
}

cli/root_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cli_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/cli"
9+
"github.com/coder/coder/cli/clitest"
10+
)
11+
12+
func TestRoot(t *testing.T) {
13+
t.Run("FormatCobraError", func(t *testing.T) {
14+
t.Parallel()
15+
16+
cmd, _ := clitest.New(t, "delete")
17+
18+
cmd, err := cmd.ExecuteC()
19+
errStr := cli.FormatCobraError(err, cmd)
20+
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
21+
})
22+
}

cmd/coder/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
)
1111

1212
func main() {
13-
err := cli.Root().Execute()
13+
cmd, err := cli.Root().ExecuteC()
1414
if err != nil {
1515
if errors.Is(err, cliui.Canceled) {
1616
os.Exit(1)
1717
}
18-
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()))
18+
cobraErr := cli.FormatCobraError(err, cmd)
19+
_, _ = fmt.Fprintln(os.Stderr, cobraErr)
1920
os.Exit(1)
2021
}
2122
}

0 commit comments

Comments
 (0)