Skip to content

Commit e738d95

Browse files
committed
Add tests cases for outputResult
1 parent ccd59bd commit e738d95

File tree

8 files changed

+182
-8
lines changed

8 files changed

+182
-8
lines changed

internal/cmd/beta/key-pair/create/create.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8282
return fmt.Errorf("create key pair: %w", err)
8383
}
8484

85-
return outputResult(p, model, resp)
85+
return outputResult(p, model.GlobalFlagModel.OutputFormat, resp)
8686
},
8787
}
8888
configureFlags(cmd)
@@ -141,8 +141,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
141141
return req.CreateKeyPairPayload(payload)
142142
}
143143

144-
func outputResult(p *print.Printer, model *inputModel, item *iaas.Keypair) error {
145-
switch model.OutputFormat {
144+
func outputResult(p *print.Printer, outputFormat string, item *iaas.Keypair) error {
145+
if item == nil {
146+
return fmt.Errorf("no key pair found")
147+
}
148+
149+
switch outputFormat {
146150
case print.JSONOutputFormat:
147151
details, err := json.MarshalIndent(item, "", " ")
148152
if err != nil {

internal/cmd/beta/key-pair/create/create_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,42 @@ func TestBuildRequest(t *testing.T) {
194194
})
195195
}
196196
}
197+
198+
func Test_outputResult(t *testing.T) {
199+
type args struct {
200+
item *iaas.Keypair
201+
outputFormat string
202+
}
203+
tests := []struct {
204+
name string
205+
args args
206+
wantErr bool
207+
}{
208+
{
209+
name: "empty",
210+
args: args{
211+
item: nil,
212+
outputFormat: "",
213+
},
214+
wantErr: true,
215+
},
216+
{
217+
name: "base",
218+
args: args{
219+
item: &iaas.Keypair{},
220+
outputFormat: "",
221+
},
222+
wantErr: false,
223+
},
224+
}
225+
226+
p := print.NewPrinter()
227+
p.Cmd = NewCmd(p)
228+
for _, tt := range tests {
229+
t.Run(tt.name, func(t *testing.T) {
230+
if err := outputResult(p, tt.args.outputFormat, tt.args.item); (err != nil) != tt.wantErr {
231+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
232+
}
233+
})
234+
}
235+
}

internal/cmd/beta/key-pair/describe/describe.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
110110
}
111111

112112
func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool, keyPair *iaas.Keypair) error {
113+
if keyPair == nil {
114+
p.Outputln("No keypair found.")
115+
return nil
116+
}
113117
switch outputFormat {
114118
case print.JSONOutputFormat:
115119
details, err := json.MarshalIndent(keyPair, "", " ")
@@ -150,7 +154,7 @@ func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool,
150154
table.AddRow("KEY PAIR NAME", utils.PtrString(keyPair.Name))
151155
table.AddSeparator()
152156

153-
if *keyPair.Labels != nil && len(*keyPair.Labels) > 0 {
157+
if keyPair.Labels != nil && len(*keyPair.Labels) > 0 {
154158
var labels []string
155159
for key, value := range *keyPair.Labels {
156160
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
@@ -162,7 +166,11 @@ func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool,
162166
table.AddRow("FINGERPRINT", utils.PtrString(keyPair.Fingerprint))
163167
table.AddSeparator()
164168

165-
truncatedPublicKey := (*keyPair.PublicKey)[:maxLengthPublicKey] + "..."
169+
truncatedPublicKey := ""
170+
if keyPair.PublicKey != nil {
171+
truncatedPublicKey = (*keyPair.PublicKey)[:maxLengthPublicKey] + "..."
172+
}
173+
166174
table.AddRow("PUBLIC KEY", truncatedPublicKey)
167175
table.AddSeparator()
168176

internal/cmd/beta/key-pair/describe/describe_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,44 @@ func TestBuildRequest(t *testing.T) {
186186
})
187187
}
188188
}
189+
190+
func Test_outputResult(t *testing.T) {
191+
type args struct {
192+
outputFormat string
193+
showOnlyPublicKey bool
194+
keyPair *iaas.Keypair
195+
}
196+
tests := []struct {
197+
name string
198+
args args
199+
wantErr bool
200+
}{
201+
{
202+
name: "empty",
203+
args: args{
204+
outputFormat: "",
205+
showOnlyPublicKey: false,
206+
keyPair: nil,
207+
},
208+
wantErr: false,
209+
},
210+
{
211+
name: "base",
212+
args: args{
213+
outputFormat: "",
214+
showOnlyPublicKey: false,
215+
keyPair: &iaas.Keypair{},
216+
},
217+
wantErr: false,
218+
},
219+
}
220+
p := print.NewPrinter()
221+
p.Cmd = NewCmd(p)
222+
for _, tt := range tests {
223+
t.Run(tt.name, func(t *testing.T) {
224+
if err := outputResult(p, tt.args.outputFormat, tt.args.showOnlyPublicKey, tt.args.keyPair); (err != nil) != tt.wantErr {
225+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
226+
}
227+
})
228+
}
229+
}

internal/cmd/beta/key-pair/list/list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ func outputResult(p *print.Printer, outputFormat string, keyPairs []iaas.Keypair
160160
keyPair := keyPairs[idx]
161161

162162
var labels []string
163-
for key, value := range *keyPair.Labels {
164-
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
163+
if keyPair.Labels != nil {
164+
for key, value := range *keyPair.Labels {
165+
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
166+
}
165167
}
166168

167169
table.AddRow(

internal/cmd/beta/key-pair/list/list_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,37 @@ func TestBuildRequest(t *testing.T) {
186186
})
187187
}
188188
}
189+
190+
func Test_outputResult(t *testing.T) {
191+
type args struct {
192+
outputFormat string
193+
keyPairs []iaas.Keypair
194+
}
195+
tests := []struct {
196+
name string
197+
args args
198+
wantErr bool
199+
}{
200+
{
201+
name: "empty",
202+
args: args{
203+
outputFormat: "",
204+
keyPairs: []iaas.Keypair{
205+
{},
206+
},
207+
},
208+
wantErr: false,
209+
},
210+
}
211+
212+
for _, tt := range tests {
213+
t.Run(tt.name, func(t *testing.T) {
214+
p := print.NewPrinter()
215+
p.Cmd = NewCmd(p)
216+
217+
if err := outputResult(p, tt.args.outputFormat, tt.args.keyPairs); (err != nil) != tt.wantErr {
218+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
219+
}
220+
})
221+
}
222+
}

internal/cmd/beta/key-pair/update/update.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,18 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
123123
}
124124

125125
func outputResult(p *print.Printer, model *inputModel, keyPair *iaas.Keypair) error {
126-
switch model.OutputFormat {
126+
if model == nil {
127+
return fmt.Errorf("model is nil")
128+
}
129+
if keyPair == nil {
130+
return fmt.Errorf("keyPair is nil")
131+
}
132+
133+
var outputFormat string
134+
if model.GlobalFlagModel != nil {
135+
outputFormat = model.GlobalFlagModel.OutputFormat
136+
}
137+
switch outputFormat {
127138
case print.JSONOutputFormat:
128139
details, err := json.MarshalIndent(keyPair, "", " ")
129140
if err != nil {

internal/cmd/beta/key-pair/update/update_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,38 @@ func TestBuildRequest(t *testing.T) {
188188
})
189189
}
190190
}
191+
192+
func Test_outputResult(t *testing.T) {
193+
type args struct {
194+
model *inputModel
195+
keyPair *iaas.Keypair
196+
}
197+
tests := []struct {
198+
name string
199+
args args
200+
wantErr bool
201+
}{
202+
{
203+
name: "empty",
204+
args: args{},
205+
wantErr: true,
206+
},
207+
{
208+
name: "base",
209+
args: args{
210+
model: &inputModel{},
211+
keyPair: &iaas.Keypair{},
212+
},
213+
wantErr: false,
214+
},
215+
}
216+
p := print.NewPrinter()
217+
p.Cmd = NewCmd(p)
218+
for _, tt := range tests {
219+
t.Run(tt.name, func(t *testing.T) {
220+
if err := outputResult(p, tt.args.model, tt.args.keyPair); (err != nil) != tt.wantErr {
221+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
222+
}
223+
})
224+
}
225+
}

0 commit comments

Comments
 (0)