Skip to content

Commit 5e8b314

Browse files
committed
Add tests on cmd package
1 parent f9a9472 commit 5e8b314

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed

cmd/edit_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewEditCmd(t *testing.T) {
11+
type scenario struct {
12+
name string
13+
setup func(*testing.T) (workspaceManager, []string)
14+
test func(error)
15+
}
16+
scenarios := []scenario{
17+
{
18+
"An error occurred when editing a workspace",
19+
func(t *testing.T) (workspaceManager, []string) {
20+
w := newMockWorkspaceManager(t)
21+
args := []string{"api"}
22+
w.Mock.On("Edit", args[0]).Return(errors.New("an error occurred"))
23+
return w, args
24+
},
25+
func(err error) {
26+
assert.Error(t, err)
27+
},
28+
},
29+
{
30+
"An error occurred when editing a workspace env",
31+
func(t *testing.T) (workspaceManager, []string) {
32+
w := newMockWorkspaceManager(t)
33+
args := []string{"api", "prod"}
34+
w.Mock.On("EditEnv", args[0], args[1]).Return(errors.New("an error occurred"))
35+
return w, args
36+
},
37+
func(err error) {
38+
assert.Error(t, err)
39+
},
40+
},
41+
{
42+
"Editing a workspace successfully",
43+
func(t *testing.T) (workspaceManager, []string) {
44+
w := newMockWorkspaceManager(t)
45+
args := []string{"api"}
46+
w.Mock.On("Edit", args[0]).Return(nil)
47+
return w, args
48+
},
49+
func(err error) {
50+
assert.NoError(t, err)
51+
},
52+
},
53+
{
54+
"Editing a workspace env successfully",
55+
func(t *testing.T) (workspaceManager, []string) {
56+
w := newMockWorkspaceManager(t)
57+
args := []string{"api", "prod"}
58+
w.Mock.On("EditEnv", args[0], args[1]).Return(nil)
59+
return w, args
60+
},
61+
func(err error) {
62+
assert.NoError(t, err)
63+
},
64+
},
65+
}
66+
for _, s := range scenarios {
67+
t.Run(s.name, func(t *testing.T) {
68+
w, args := s.setup(t)
69+
cmd := newEditCmd(w)
70+
cmd.SetArgs(args)
71+
s.test(cmd.Execute())
72+
})
73+
}
74+
}

cmd/mock_workspaceManager.go

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
2323
github.com/rivo/uniseg v0.4.7 // indirect
2424
github.com/spf13/pflag v1.0.5 // indirect
25+
github.com/stretchr/objx v0.5.2 // indirect
2526
golang.org/x/sys v0.12.0 // indirect
2627
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2728
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh
4444
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
4545
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
4646
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
47+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
48+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
4749
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4850
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4951
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=

0 commit comments

Comments
 (0)