Skip to content

Commit fb393e7

Browse files
committed
Add tests to cmd package
1 parent 7139fb6 commit fb393e7

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

cmd/create_test.go

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

cmd/remove_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"os"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestNewRemoveCmd(t *testing.T) {
13+
type scenario struct {
14+
name string
15+
setup func(*testing.T) (workspaceManager, []string)
16+
test func(*testing.T, error)
17+
}
18+
scenarios := []scenario{
19+
{
20+
"An error occurred when removing a workspace",
21+
func(t *testing.T) (workspaceManager, []string) {
22+
w := newMockWorkspaceManager(t)
23+
args := []string{"api"}
24+
w.Mock.On("Remove", args[0]).Return(errors.New("an error occurred"))
25+
return w, args
26+
},
27+
func(t *testing.T, err error) {
28+
assert.Error(t, err)
29+
},
30+
},
31+
{
32+
"Removing a workspace successfully",
33+
func(t *testing.T) (workspaceManager, []string) {
34+
w := newMockWorkspaceManager(t)
35+
args := []string{"api"}
36+
w.Mock.On("Remove", args[0]).Return(nil)
37+
return w, args
38+
},
39+
func(t *testing.T, err error) {
40+
assert.NoError(t, err)
41+
},
42+
},
43+
}
44+
for _, s := range scenarios {
45+
t.Run(s.name, func(t *testing.T) {
46+
os.Setenv("EDITOR", "emacs")
47+
os.Setenv("SHELL", "/bin/sh")
48+
w, args := s.setup(t)
49+
cmd := newRemoveCmd(w)
50+
cmd.SetArgs(args)
51+
cmd.SetErr(&bytes.Buffer{})
52+
cmd.SetOut(&bytes.Buffer{})
53+
s.test(t, cmd.Execute())
54+
})
55+
}
56+
}

cmd/run_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewRunCmd(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+
"Running a function successfully",
20+
func(t *testing.T) (workspaceManager, []string) {
21+
w := newMockWorkspaceManager(t)
22+
args := []string{"api", "start"}
23+
w.Mock.On("RunFunction", args[0], "", []string{args[1]}).Return(nil)
24+
return w, args
25+
},
26+
func(t *testing.T, err error) {
27+
assert.NoError(t, err)
28+
},
29+
},
30+
}
31+
for _, s := range scenarios {
32+
t.Run(s.name, func(t *testing.T) {
33+
os.Setenv("EDITOR", "emacs")
34+
os.Setenv("SHELL", "/bin/sh")
35+
w, args := s.setup(t)
36+
cmd := newRunCmd(w)
37+
cmd.SetArgs(args)
38+
cmd.SetErr(&bytes.Buffer{})
39+
cmd.SetOut(&bytes.Buffer{})
40+
s.test(t, cmd.Execute())
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)