Skip to content

Commit a90d86e

Browse files
authored
fix: stackit beta quota command - add nil pointer checks and tests for the outputResult functions (#610)
* add nil pointer checks * add tests for the outputResult functions within the quota command
1 parent 908703a commit a90d86e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

internal/cmd/beta/quota/list/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
5252
if err != nil {
5353
p.Debug(print.ErrorLevel, "get project name: %v", err)
5454
projectLabel = model.ProjectId
55+
} else if projectLabel == "" {
56+
projectLabel = model.ProjectId
5557
}
5658

5759
// Call API
@@ -106,6 +108,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
106108
}
107109

108110
func outputResult(p *print.Printer, outputFormat string, quotas *iaas.QuotaList) error {
111+
if quotas == nil {
112+
return fmt.Errorf("quotas is nil")
113+
}
109114
switch outputFormat {
110115
case print.JSONOutputFormat:
111116
details, err := json.MarshalIndent(quotas, "", " ")

internal/cmd/beta/quota/list/list_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,37 @@ func TestBuildRequest(t *testing.T) {
162162
})
163163
}
164164
}
165+
166+
func Test_outputResult(t *testing.T) {
167+
type args struct {
168+
outputFormat string
169+
quotas *iaas.QuotaList
170+
}
171+
tests := []struct {
172+
name string
173+
args args
174+
wantErr bool
175+
}{
176+
{
177+
name: "empty",
178+
args: args{},
179+
wantErr: true,
180+
},
181+
{
182+
name: "set quota empty",
183+
args: args{
184+
quotas: &iaas.QuotaList{},
185+
},
186+
wantErr: false,
187+
},
188+
}
189+
p := print.NewPrinter()
190+
p.Cmd = NewCmd(p)
191+
for _, tt := range tests {
192+
t.Run(tt.name, func(t *testing.T) {
193+
if err := outputResult(p, tt.args.outputFormat, tt.args.quotas); (err != nil) != tt.wantErr {
194+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
195+
}
196+
})
197+
}
198+
}

0 commit comments

Comments
 (0)