Skip to content

Commit b14bc5c

Browse files
committed
refactor(server): simplify TestParseInput by using testutils helper function
1 parent d8f2ec2 commit b14bc5c

File tree

2 files changed

+4
-86
lines changed

2 files changed

+4
-86
lines changed

internal/cmd/server/security-group/attach/attach_test.go

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/google/go-cmp/cmp"
108
"github.com/google/go-cmp/cmp/cmpopts"
119
"github.com/google/uuid"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1412
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1513
)
1614

@@ -149,46 +147,7 @@ func TestParseInput(t *testing.T) {
149147

150148
for _, tt := range tests {
151149
t.Run(tt.description, func(t *testing.T) {
152-
p := print.NewPrinter()
153-
cmd := NewCmd(&types.CmdParams{Printer: p})
154-
err := globalflags.Configure(cmd.Flags())
155-
if err != nil {
156-
t.Fatalf("configure global flags: %v", err)
157-
}
158-
159-
for flag, value := range tt.flagValues {
160-
err := cmd.Flags().Set(flag, value)
161-
if err != nil {
162-
if !tt.isValid {
163-
return
164-
}
165-
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
166-
}
167-
}
168-
169-
err = cmd.ValidateRequiredFlags()
170-
if err != nil {
171-
if !tt.isValid {
172-
return
173-
}
174-
t.Fatalf("error validating flags: %v", err)
175-
}
176-
177-
model, err := parseInput(p, cmd, []string{})
178-
if err != nil {
179-
if !tt.isValid {
180-
return
181-
}
182-
t.Fatalf("error parsing input: %v", err)
183-
}
184-
185-
if !tt.isValid {
186-
t.Fatalf("did not fail on invalid input")
187-
}
188-
diff := cmp.Diff(model, tt.expectedModel)
189-
if diff != "" {
190-
t.Fatalf("Data does not match: %s", diff)
191-
}
150+
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, []string{}, tt.flagValues, tt.isValid)
192151
})
193152
}
194153
}

internal/cmd/server/security-group/detach/detach_test.go

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8-
97
"github.com/google/go-cmp/cmp"
108
"github.com/google/go-cmp/cmp/cmpopts"
119
"github.com/google/uuid"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13-
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
1412
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1513
)
1614

@@ -149,46 +147,7 @@ func TestParseInput(t *testing.T) {
149147

150148
for _, tt := range tests {
151149
t.Run(tt.description, func(t *testing.T) {
152-
p := print.NewPrinter()
153-
cmd := NewCmd(&types.CmdParams{Printer: p})
154-
err := globalflags.Configure(cmd.Flags())
155-
if err != nil {
156-
t.Fatalf("configure global flags: %v", err)
157-
}
158-
159-
for flag, value := range tt.flagValues {
160-
err := cmd.Flags().Set(flag, value)
161-
if err != nil {
162-
if !tt.isValid {
163-
return
164-
}
165-
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
166-
}
167-
}
168-
169-
err = cmd.ValidateRequiredFlags()
170-
if err != nil {
171-
if !tt.isValid {
172-
return
173-
}
174-
t.Fatalf("error validating flags: %v", err)
175-
}
176-
177-
model, err := parseInput(p, cmd, []string{})
178-
if err != nil {
179-
if !tt.isValid {
180-
return
181-
}
182-
t.Fatalf("error parsing input: %v", err)
183-
}
184-
185-
if !tt.isValid {
186-
t.Fatalf("did not fail on invalid input")
187-
}
188-
diff := cmp.Diff(model, tt.expectedModel)
189-
if diff != "" {
190-
t.Fatalf("Data does not match: %s", diff)
191-
}
150+
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, []string{}, tt.flagValues, tt.isValid)
192151
})
193152
}
194153
}

0 commit comments

Comments
 (0)