Skip to content

Commit e4b5158

Browse files
committed
Add tests on cmd package
1 parent f560865 commit e4b5158

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

cmd/edit_test.go

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

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)