Skip to content

chore(project): Add nil pointer checks and tests for the outputResult functions #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions internal/cmd/project/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create project: %w", err)
}

return outputResult(p, model, resp)
return outputResult(p, *model, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -212,7 +212,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *resourceman
return req, nil
}

func outputResult(p *print.Printer, model *inputModel, resp *resourcemanager.Project) error {
func outputResult(p *print.Printer, model inputModel, resp *resourcemanager.Project) error {
if resp == nil {
return fmt.Errorf("response is empty")
}
if model.GlobalFlagModel == nil {
return fmt.Errorf("globalflags are empty")
}
switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
Expand Down
32 changes: 28 additions & 4 deletions internal/cmd/project/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
"github.com/zalando/go-keyring"
)
Expand Down Expand Up @@ -359,3 +358,28 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model inputModel
resp *resourcemanager.Project
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{model: inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}}}, true},
{"base", args{inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}}, &resourcemanager.Project{}}, 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.model, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
7 changes: 6 additions & 1 deletion internal/cmd/project/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *resourceman
}

func outputResult(p *print.Printer, outputFormat string, project *resourcemanager.GetProjectResponse) error {
if project == nil {
return fmt.Errorf("response not set")
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(project, "", " ")
Expand Down Expand Up @@ -146,7 +149,9 @@ func outputResult(p *print.Printer, outputFormat string, project *resourcemanage
table.AddSeparator()
table.AddRow("STATE", utils.PtrString(project.LifecycleState))
table.AddSeparator()
table.AddRow("PARENT ID", utils.PtrString(project.Parent.Id))
if project.Parent != nil {
table.AddRow("PARENT ID", utils.PtrString(project.Parent.Id))
}
err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
Expand Down
39 changes: 36 additions & 3 deletions internal/cmd/project/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package describe
import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
)

Expand Down Expand Up @@ -214,3 +215,35 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
project *resourcemanager.GetProjectResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, true},
{"base", args{"", &resourcemanager.GetProjectResponse{}}, false},
{"complete", args{"", &resourcemanager.GetProjectResponse{
ProjectId: utils.Ptr("4711"),
Name: utils.Ptr("name"),
CreationTime: utils.Ptr(time.Now()),
LifecycleState: utils.Ptr(resourcemanager.LIFECYCLESTATE_CREATING),
Parent: &resourcemanager.Parent{Id: utils.Ptr("parent id")},
},
}, 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.project); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
7 changes: 6 additions & 1 deletion internal/cmd/project/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,16 @@ func outputResult(p *print.Printer, outputFormat string, projects []resourcemana
table.SetHeader("ID", "NAME", "STATE", "PARENT ID")
for i := range projects {
p := projects[i]

var parentId *string
if p.Parent != nil {
parentId = p.Parent.Id
}
table.AddRow(
utils.PtrString(p.ProjectId),
utils.PtrString(p.Name),
utils.PtrString(p.LifecycleState),
utils.PtrString(p.Parent.Id),
utils.PtrString(parentId),
)
}

Expand Down
59 changes: 54 additions & 5 deletions internal/cmd/project/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/zalando/go-keyring"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
"github.com/zalando/go-keyring"
)

type testCtxKey struct{}
Expand Down Expand Up @@ -495,3 +494,53 @@ func TestFetchProjects(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
outputFormat string
projects []resourcemanager.Project
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, false},
{"base", args{"", []resourcemanager.Project{{}}}, false},
{"complete", args{"", []resourcemanager.Project{
{
ContainerId: utils.Ptr("container-id1"),
CreationTime: utils.Ptr(time.Now()),
Labels: &map[string]string{"foo": "bar"},
LifecycleState: utils.Ptr(resourcemanager.LIFECYCLESTATE_CREATING),
Name: utils.Ptr("some name"),
Parent: &resourcemanager.Parent{
Id: utils.Ptr("parent-id"),
},
ProjectId: utils.Ptr("project-id1"),
},
{
ContainerId: utils.Ptr("container-id2"),
CreationTime: utils.Ptr(time.Now()),
Labels: &map[string]string{"foo": "bar"},
LifecycleState: utils.Ptr(resourcemanager.LIFECYCLESTATE_CREATING),
Name: utils.Ptr("some name"),
Parent: &resourcemanager.Parent{
Id: utils.Ptr("parent-id"),
},
ProjectId: utils.Ptr("project-id2"),
},
}}, 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.projects); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
11 changes: 7 additions & 4 deletions internal/cmd/project/member/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
members = members[:*model.Limit]
}

return outputResult(p, model, members)
return outputResult(p, *model, members)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -145,13 +145,16 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati
return req
}

func outputResult(p *print.Printer, model *inputModel, members []authorization.Member) error {
func outputResult(p *print.Printer, model inputModel, members []authorization.Member) error {
if model.GlobalFlagModel == nil {
return fmt.Errorf("globalflags are empty")
}
sortFn := func(i, j int) bool {
switch model.SortBy {
case "subject":
return *members[i].Subject < *members[j].Subject
return utils.PtrString(members[i].Subject) < utils.PtrString(members[j].Subject)
case "role":
return *members[i].Role < *members[j].Role
return utils.PtrString(members[i].Role) < utils.PtrString(members[j].Role)
default:
return false
}
Expand Down
48 changes: 44 additions & 4 deletions internal/cmd/project/member/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
)

Expand Down Expand Up @@ -209,3 +208,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputResult(t *testing.T) {
type args struct {
model inputModel
members []authorization.Member
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{model: inputModel{GlobalFlagModel: &globalflags.GlobalFlagModel{}}}, false},
{"base", args{inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
Subject: utils.Ptr("subject"),
Limit: nil,
SortBy: "",
}, nil}, false},
{"complete", args{inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
Subject: utils.Ptr("subject"),
Limit: nil,
SortBy: "",
},
[]authorization.Member{
{Role: utils.Ptr("role1"), Subject: utils.Ptr("subject1")},
{Role: utils.Ptr("role2"), Subject: utils.Ptr("subject2")},
{Role: utils.Ptr("role3"), Subject: utils.Ptr("subject3")},
}},
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.model, tt.args.members); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 37 additions & 4 deletions internal/cmd/project/role/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
)

Expand Down Expand Up @@ -172,3 +171,37 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func Test_outputRolesResult(t *testing.T) {
type args struct {
outputFormat string
roles []authorization.Role
}
tests := []struct {
name string
args args
wantErr bool
}{
{"empty", args{}, false},
{"standard", args{"", nil}, false},
{"complete", args{"", []authorization.Role{
{
Description: utils.Ptr("description"),
Id: utils.Ptr("id"),
Name: utils.Ptr("name"),
Permissions: &[]authorization.Permission{
{Description: utils.Ptr("description"), Name: utils.Ptr("name")},
},
},
}}, false},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputRolesResult(p, tt.args.outputFormat, tt.args.roles); (err != nil) != tt.wantErr {
t.Errorf("outputRolesResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading