From 279066d8b1975f96d11e09859f66c2f5828cd9fd Mon Sep 17 00:00:00 2001 From: Marcel Jacek Date: Wed, 19 Feb 2025 17:18:10 +0100 Subject: [PATCH 1/2] - add nil pointer checks - add tests for the outputResult functions within the quota command --- internal/cmd/beta/quota/list/list.go | 6 ++++ internal/cmd/beta/quota/list/list_test.go | 34 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/internal/cmd/beta/quota/list/list.go b/internal/cmd/beta/quota/list/list.go index 32a2be9d4..e0ce80f88 100644 --- a/internal/cmd/beta/quota/list/list.go +++ b/internal/cmd/beta/quota/list/list.go @@ -53,6 +53,9 @@ func NewCmd(p *print.Printer) *cobra.Command { p.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId } + if projectLabel == "" { + projectLabel = model.ProjectId + } // Call API request := buildRequest(ctx, model, apiClient) @@ -106,6 +109,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli } func outputResult(p *print.Printer, outputFormat string, quotas *iaas.QuotaList) error { + if quotas == nil { + return fmt.Errorf("quotas is nil") + } switch outputFormat { case print.JSONOutputFormat: details, err := json.MarshalIndent(quotas, "", " ") diff --git a/internal/cmd/beta/quota/list/list_test.go b/internal/cmd/beta/quota/list/list_test.go index b8fa5319c..1daa3fcff 100644 --- a/internal/cmd/beta/quota/list/list_test.go +++ b/internal/cmd/beta/quota/list/list_test.go @@ -162,3 +162,37 @@ func TestBuildRequest(t *testing.T) { }) } } + +func Test_outputResult(t *testing.T) { + type args struct { + outputFormat string + quotas *iaas.QuotaList + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: true, + }, + { + name: "set quota empty", + args: args{ + quotas: &iaas.QuotaList{}, + }, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(p) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.quotas); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 88dc52b0e6274d95b2952e80b4eb438ba663881d Mon Sep 17 00:00:00 2001 From: Marcel Jacek Date: Thu, 20 Feb 2025 10:27:02 +0100 Subject: [PATCH 2/2] fix: consistent `else if`-clause for labels --- internal/cmd/beta/quota/list/list.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/cmd/beta/quota/list/list.go b/internal/cmd/beta/quota/list/list.go index e0ce80f88..97e9f956b 100644 --- a/internal/cmd/beta/quota/list/list.go +++ b/internal/cmd/beta/quota/list/list.go @@ -52,8 +52,7 @@ func NewCmd(p *print.Printer) *cobra.Command { if err != nil { p.Debug(print.ErrorLevel, "get project name: %v", err) projectLabel = model.ProjectId - } - if projectLabel == "" { + } else if projectLabel == "" { projectLabel = model.ProjectId }