Skip to content

Commit 698e105

Browse files
committed
feature: refactored tests to reused actual cobra command
1 parent dc052dd commit 698e105

File tree

4 files changed

+94
-87
lines changed

4 files changed

+94
-87
lines changed

internal/cmd/beta/security-group/create/create_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/google/go-cmp/cmp"
1313
"github.com/google/go-cmp/cmp/cmpopts"
1414
"github.com/google/uuid"
15-
"github.com/spf13/cobra"
1615
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1716
)
1817

@@ -65,11 +64,11 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
6564
func fixtureRequest(mods ...func(request *iaas.ApiCreateSecurityGroupRequest)) iaas.ApiCreateSecurityGroupRequest {
6665
request := testClient.CreateSecurityGroup(testCtx, testProjectId)
6766
request = request.CreateSecurityGroupPayload(iaas.CreateSecurityGroupPayload{
68-
Description: utils.Ptr(testDescription),
67+
Description: &testDescription,
6968
Labels: &testLabels,
70-
Name: utils.Ptr(testName),
69+
Name: &testName,
7170
Rules: nil,
72-
Stateful: utils.Ptr(testStateful),
71+
Stateful: &testStateful,
7372
})
7473
for _, mod := range mods {
7574
mod(&request)
@@ -187,12 +186,8 @@ func TestParseInput(t *testing.T) {
187186

188187
for _, tt := range tests {
189188
t.Run(tt.description, func(t *testing.T) {
190-
cmd := &cobra.Command{}
191-
configureFlags(cmd)
192-
err := globalflags.Configure(cmd.Flags())
193-
if err != nil {
194-
t.Fatalf("configure global flags: %v", err)
195-
}
189+
p := print.NewPrinter()
190+
cmd := NewCmd(p)
196191

197192
for flag, value := range tt.flagValues {
198193
err := cmd.Flags().Set(flag, value)
@@ -204,15 +199,13 @@ func TestParseInput(t *testing.T) {
204199
}
205200
}
206201

207-
err = cmd.ValidateRequiredFlags()
208-
if err != nil {
202+
if err := cmd.ValidateRequiredFlags(); err != nil {
209203
if !tt.isValid {
210204
return
211205
}
212206
t.Fatalf("error validating flags: %v", err)
213207
}
214208

215-
p := print.NewPrinter()
216209
model, err := parseInput(p, cmd)
217210
if err != nil {
218211
if !tt.isValid {

internal/cmd/beta/security-group/delete/delete_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/google/go-cmp/cmp"
1111
"github.com/google/go-cmp/cmp/cmpopts"
1212
"github.com/google/uuid"
13-
"github.com/spf13/cobra"
1413
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1514
)
1615

@@ -95,11 +94,18 @@ func TestParseInput(t *testing.T) {
9594
args: []string{"foo","bar"},
9695
isValid: false,
9796
},
97+
{
98+
description: "invalid group id",
99+
flagValues: fixtureFlagValues(),
100+
args: []string{"foo"},
101+
isValid: false,
102+
},
98103
}
99104

100105
for _, tt := range tests {
101106
t.Run(tt.description, func(t *testing.T) {
102-
cmd := &cobra.Command{}
107+
p := print.NewPrinter()
108+
cmd:=NewCmd(p)
103109
err := globalflags.Configure(cmd.Flags())
104110
if err != nil {
105111
t.Fatalf("configure global flags: %v", err)
@@ -124,7 +130,6 @@ func TestParseInput(t *testing.T) {
124130
t.Fatalf("error validating flags: %v", err)
125131
}
126132

127-
p := print.NewPrinter()
128133
model, err := parseInput(p, cmd, tt.args)
129134
if err != nil {
130135
if !tt.isValid {

internal/cmd/beta/security-group/list/list_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/google/go-cmp/cmp"
1111
"github.com/google/go-cmp/cmp/cmpopts"
1212
"github.com/google/uuid"
13-
"github.com/spf13/cobra"
1413
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1514
)
1615

@@ -120,12 +119,8 @@ func TestParseInput(t *testing.T) {
120119

121120
for _, tt := range tests {
122121
t.Run(tt.description, func(t *testing.T) {
123-
cmd := &cobra.Command{}
124-
configureFlags(cmd)
125-
err := globalflags.Configure(cmd.Flags())
126-
if err != nil {
127-
t.Fatalf("configure global flags: %v", err)
128-
}
122+
p := print.NewPrinter()
123+
cmd := NewCmd(p)
129124

130125
for flag, value := range tt.flagValues {
131126
err := cmd.Flags().Set(flag, value)
@@ -137,15 +132,13 @@ func TestParseInput(t *testing.T) {
137132
}
138133
}
139134

140-
err = cmd.ValidateRequiredFlags()
141-
if err != nil {
135+
if err := cmd.ValidateRequiredFlags(); err != nil {
142136
if !tt.isValid {
143137
return
144138
}
145139
t.Fatalf("error validating flags: %v", err)
146140
}
147141

148-
p := print.NewPrinter()
149142
model, err := parseInput(p, cmd)
150143
if err != nil {
151144
if !tt.isValid {

0 commit comments

Comments
 (0)