1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package cli_test
import (
"os"
"path"
"testing"
"github.com/roadrunner-server/roadrunner/v2023/internal/cli"
"github.com/stretchr/testify/require"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestCommandSubcommands(t *testing.T) {
cmd := cli.NewCommand("unit test")
cases := []struct {
giveName string
}{
{giveName: "workers"},
{giveName: "reset"},
{giveName: "serve"},
}
// get all existing subcommands and put into the map
subcommands := make(map[string]*cobra.Command)
for _, sub := range cmd.Commands() {
subcommands[sub.Name()] = sub
}
for _, tt := range cases {
tt := tt
t.Run(tt.giveName, func(t *testing.T) {
if _, exists := subcommands[tt.giveName]; !exists {
assert.Failf(t, "command not found", "command [%s] was not found", tt.giveName)
}
})
}
}
func TestCommandFlags(t *testing.T) {
cmd := cli.NewCommand("unit test")
cases := []struct {
giveName string
wantShorthand string
wantDefault string
}{
{giveName: "config", wantShorthand: "c", wantDefault: ".rr.yaml"},
{giveName: "WorkDir", wantShorthand: "w", wantDefault: ""},
{giveName: "dotenv", wantShorthand: "", wantDefault: ""},
{giveName: "debug", wantShorthand: "d", wantDefault: "false"},
{giveName: "override", wantShorthand: "o", wantDefault: "[]"},
}
for _, tt := range cases {
tt := tt
t.Run(tt.giveName, func(t *testing.T) {
flag := cmd.Flag(tt.giveName)
if flag == nil {
assert.Failf(t, "flag not found", "flag [%s] was not found", tt.giveName)
return
}
assert.Equal(t, tt.wantShorthand, flag.Shorthand)
assert.Equal(t, tt.wantDefault, flag.DefValue)
})
}
}
func TestCommandSimpleExecuting(t *testing.T) {
cmd := cli.NewCommand("unit test")
cmd.SetArgs([]string{"-c", "./../../.rr.yaml"})
var executed bool
if cmd.Run == nil { // override "Run" property for test (if it was not set)
cmd.Run = func(_ *cobra.Command, _ []string) {
executed = true
}
}
assert.NoError(t, cmd.Execute())
assert.True(t, executed)
}
func TestCommandNoEnvFileError(t *testing.T) {
cmd := cli.NewCommand("unit test")
cmd.SetArgs([]string{"-c", "./../../.rr.yaml", "--dotenv", "foo/bar"})
var executed bool
if cmd.Run == nil { // override "Run" property for test (if it was not set)
cmd.Run = func(_ *cobra.Command, _ []string) {
executed = true
}
}
assert.Error(t, cmd.Execute())
assert.False(t, executed)
}
func TestCommandNoEnvFileNoError(t *testing.T) {
tmp := os.TempDir()
cmd := cli.NewCommand("unit test")
cmd.SetArgs([]string{"-c", path.Join(tmp, ".rr.yaml"), "--dotenv", path.Join(tmp, ".env")})
var executed bool
f, err := os.Create(path.Join(tmp, ".env"))
require.NoError(t, err)
f2, err := os.Create(path.Join(tmp, ".rr.yaml"))
require.NoError(t, err)
defer func() {
_ = f.Close()
_ = f2.Close()
}()
if cmd.Run == nil { // override "Run" property for test (if it was not set)
cmd.Run = func(_ *cobra.Command, _ []string) {
executed = true
}
}
assert.NoError(t, cmd.Execute())
assert.True(t, executed)
t.Cleanup(func() {
_ = os.RemoveAll(path.Join(tmp, ".env"))
_ = os.RemoveAll(path.Join(tmp, ".rr.yaml"))
})
}
func TestCommandWorkingDir(t *testing.T) {
tmp := os.TempDir()
cmd := cli.NewCommand("serve")
cmd.SetArgs([]string{"-w", tmp})
var executed bool
var wd string
f2, err := os.Create(path.Join(tmp, ".rr.yaml"))
require.NoError(t, err)
if cmd.Run == nil { // override "Run" property for test (if it was not set)
cmd.Run = func(_ *cobra.Command, _ []string) {
executed = true
wd, _ = os.Getwd()
}
}
assert.NoError(t, cmd.Execute())
assert.True(t, executed)
assert.Equal(t, tmp, wd)
t.Cleanup(func() {
_ = f2.Close()
_ = os.RemoveAll(path.Join(tmp, ".rr.yaml"))
})
}
|