Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6dae48a

Browse files
authored
fix: show --help message for CLI errors, add tests for delete (#1403)
* feat(cli): add test for deleteThis adds a new test for the `delete` command to ensure it works asexpected when provided the correct args.* fix(cli): use ExecuteC() to match CobraThis modifies the `cli.Root().Execute()` to `cli.Root).ExecuteC()` tomatch the default behavior of Cobra. We do this so errors will alwaysprint the "run --help" line.* feat(cli): add WithoutParameters test for deleteThis adds a new test to the `delete_test.go` suite to ensure the correctbehavior occurs when `delete` is called without an argument.* fixup! feat(cli): add WithoutParameters test for delete* refactor(cli): show --help error message on mainThis adds an error message which shows when there is an error with anycommands called to improve the UX.* fixup! refactor(cli): show --help error message on main* refactor(cli): handle err with FormatCobraErrorThis adds a new helper function called `FormatCobraError` to `root.go`so that we can colorize and add "--help" message to cobra command errorslike calling `delete`.* refactor(cli): add root_test.go, move delete test
1 parenta64ab65 commit6dae48a

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

‎cli/delete_test.go

Lines changed: 38 additions & 0 deletions
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+
funcTestDelete(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(chanstruct{})
27+
pty:=ptytest.New(t)
28+
cmd.SetIn(pty.Input())
29+
cmd.SetOut(pty.Output())
30+
gofunc() {
31+
deferclose(doneChan)
32+
err:=cmd.Execute()
33+
require.NoError(t,err)
34+
}()
35+
pty.ExpectMatch("Cleaning Up")
36+
<-doneChan
37+
})
38+
}

‎cli/root.go

Lines changed: 7 additions & 0 deletions
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
returntemplate
261262
}
263+
264+
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
265+
funcFormatCobraError(errerror,cmd*cobra.Command)string {
266+
helpErrMsg:=fmt.Sprintf("Run '%s %s --help' for usage.",cmd.Root().Name(),cmd.Name())
267+
returncliui.Styles.Error.Render(err.Error()+"\n"+helpErrMsg)
268+
}

‎cli/root_test.go

Lines changed: 22 additions & 0 deletions
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+
funcTestRoot(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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import (
1414

1515
funcmain() {
1616
dadjoke()
17-
err:=cli.Root().Execute()
17+
cmd,err:=cli.Root().ExecuteC()
1818
iferr!=nil {
1919
iferrors.Is(err,cliui.Canceled) {
2020
os.Exit(1)
2121
}
22-
_,_=fmt.Fprintln(os.Stderr,cliui.Styles.Error.Render(err.Error()))
22+
cobraErr:=cli.FormatCobraError(err,cmd)
23+
_,_=fmt.Fprintln(os.Stderr,cobraErr)
2324
os.Exit(1)
2425
}
2526
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp