Skip to content

Commit c153566

Browse files
committed
refactor(git): fix tests & removed redundant checks
1 parent 6d22550 commit c153566

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

internal/cmd/git/instance/create/create.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/services/git/client"
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
18-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1918
"github.com/stackitcloud/stackit-sdk-go/services/git"
2019
"github.com/stackitcloud/stackit-sdk-go/services/git/wait"
2120
)
@@ -93,7 +92,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9392
s.Stop()
9493
}
9594

96-
return outputResult(params.Printer, model.OutputFormat, model.Async, model.Name, model.Id, result)
95+
return outputResult(params.Printer, model.OutputFormat, model.Async, model.Name, *model.Id, result)
9796
},
9897
}
9998

@@ -143,21 +142,17 @@ func createPayload(model *inputModel) git.CreateInstancePayload {
143142
}
144143
}
145144

146-
func outputResult(p *print.Printer, outputFormat string, async bool, instanceName string, modelId *string, resp *git.Instance) error {
145+
func outputResult(p *print.Printer, outputFormat string, async bool, instanceName, modelId string, resp *git.Instance) error {
147146
if resp == nil {
148147
return fmt.Errorf("API resp is nil")
149148
}
150-
id := utils.PtrString(modelId)
151-
if resp.Id != nil {
152-
id = *resp.Id
153-
}
154149

155150
return p.OutputResult(outputFormat, resp, func() error {
156151
operationState := "Created"
157152
if async {
158153
operationState = "Triggered creation of"
159154
}
160-
p.Outputf("%s instance %q with id %s\n", operationState, instanceName, id)
155+
p.Outputf("%s instance %q with id %s\n", operationState, instanceName, modelId)
161156
return nil
162157
})
163158
}

internal/cmd/git/instance/create/create_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,39 +175,47 @@ func TestBuildRequest(t *testing.T) {
175175

176176
func TestOutputResult(t *testing.T) {
177177
type args struct {
178-
model *inputModel
179-
resp *git.Instance
178+
outputFormat string
179+
async bool
180+
instanceName string
181+
modelId string
182+
resp *git.Instance
180183
}
181184
tests := []struct {
182185
name string
183186
args args
184187
wantErr bool
185188
}{
186189
{
187-
name: "nil",
190+
name: "nil response",
188191
args: args{
189-
model: nil,
190-
resp: nil,
192+
outputFormat: "",
193+
async: false,
194+
instanceName: "",
195+
modelId: "",
196+
resp: nil,
191197
},
192198
wantErr: true,
193199
},
194200
{
195201
name: "empty input",
196202
args: args{
197-
model: fixtureInputModel(),
198-
resp: &git.Instance{},
203+
outputFormat: "",
204+
async: false,
205+
instanceName: "",
206+
modelId: "",
207+
resp: &git.Instance{},
199208
},
200209
wantErr: false,
201210
},
202211
{
203212
name: "output json",
204213
args: args{
205-
model: &inputModel{
206-
GlobalFlagModel: &globalflags.GlobalFlagModel{
207-
OutputFormat: print.JSONOutputFormat,
208-
},
209-
},
210-
resp: nil,
214+
outputFormat: print.JSONOutputFormat,
215+
async: true,
216+
instanceName: testName,
217+
modelId: uuid.NewString(),
218+
resp: &git.Instance{},
211219
},
212220
wantErr: false,
213221
},
@@ -216,7 +224,7 @@ func TestOutputResult(t *testing.T) {
216224
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
217225
for _, tt := range tests {
218226
t.Run(tt.name, func(t *testing.T) {
219-
if err := outputResult(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr {
227+
if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.instanceName, tt.args.modelId, tt.args.resp); (err != nil) != tt.wantErr {
220228
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
221229
}
222230
})

0 commit comments

Comments
 (0)