Skip to content

Commit 904dc6c

Browse files
committed
refactor(server): use MockExecute helper to get command output
1 parent 59c3607 commit 904dc6c

4 files changed

Lines changed: 24 additions & 43 deletions

File tree

internal/commands/server/list_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package server
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
87
"github.com/UpCloudLtd/upcloud-cli/internal/config"
98
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
10-
"github.com/UpCloudLtd/upcloud-cli/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1110
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
1211

1312
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1413
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
15-
"github.com/gemalto/flume"
1614
"github.com/jedib0t/go-pretty/v6/text"
1715
"github.com/stretchr/testify/assert"
1816
)
@@ -128,7 +126,6 @@ func TestListServers(t *testing.T) {
128126
} {
129127
t.Run(test.name, func(t *testing.T) {
130128
conf := config.New()
131-
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
132129

133130
testCmd := ListCommand()
134131
mService := new(smock.Service)
@@ -138,23 +135,17 @@ func TestListServers(t *testing.T) {
138135
mService.On("GetServerNetworks", &request.GetServerNetworksRequest{ServerUUID: uuid}).Return(&serverNetworks, nil)
139136

140137
c := commands.BuildCommand(testCmd, nil, conf)
141-
err := c.Cobra().Flags().Parse(test.args)
142-
assert.NoError(t, err)
143-
144-
res, err := c.(commands.NoArgumentCommand).ExecuteWithoutArguments(commands.NewExecutor(conf, mService, flume.New("test")))
145-
assert.NoError(t, err)
138+
c.Cobra().SetArgs(test.args)
146139

147-
buf := bytes.NewBuffer(nil)
148-
err = output.Render(buf, conf, res)
140+
output, err := mockexecute.MockExecute(c, mService, conf)
149141
assert.NoError(t, err)
150-
str := buf.String()
151142

152143
for _, contains := range test.outputContains {
153-
assert.Contains(t, str, contains)
144+
assert.Contains(t, output, contains)
154145
}
155146

156147
for _, notContains := range test.outputNotContains {
157-
assert.NotContains(t, str, notContains)
148+
assert.NotContains(t, output, notContains)
158149
}
159150
})
160151
}

internal/commands/server/show_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package server
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
87
"github.com/UpCloudLtd/upcloud-cli/internal/config"
98
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
10-
"github.com/UpCloudLtd/upcloud-cli/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1110

1211
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1312
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
14-
"github.com/gemalto/flume"
1513
"github.com/jedib0t/go-pretty/v6/text"
1614
"github.com/stretchr/testify/assert"
1715
)
@@ -202,8 +200,6 @@ func TestServerHumanOutput(t *testing.T) {
202200
mService.On("GetFirewallRules", &request.GetFirewallRulesRequest{ServerUUID: uuid}).Return(firewallRules, nil)
203201

204202
conf := config.New()
205-
// force human output
206-
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
207203

208204
command := commands.BuildCommand(ShowCommand(), nil, conf)
209205

@@ -212,12 +208,10 @@ func TestServerHumanOutput(t *testing.T) {
212208
if err != nil {
213209
t.Fatal(err)
214210
}
215-
res, err := command.(commands.MultipleArgumentCommand).Execute(commands.NewExecutor(conf, &mService, flume.New("test")), uuid)
216211

217-
assert.Nil(t, err)
212+
command.Cobra().SetArgs([]string{uuid})
213+
output, err := mockexecute.MockExecute(command, &mService, conf)
218214

219-
buf := bytes.NewBuffer(nil)
220-
err = output.Render(buf, conf, res)
221215
assert.NoError(t, err)
222-
assert.Equal(t, expected, buf.String())
216+
assert.Equal(t, expected, output)
223217
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package zone
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
87
"github.com/UpCloudLtd/upcloud-cli/internal/config"
98
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
10-
"github.com/UpCloudLtd/upcloud-cli/internal/output"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1110

1211
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
13-
"github.com/gemalto/flume"
1412
"github.com/jedib0t/go-pretty/v6/text"
1513
"github.com/stretchr/testify/assert"
1614
)
@@ -28,18 +26,11 @@ func TestZoneListHumanOutput(t *testing.T) {
2826
mService.On("GetZones").Return(&zones, nil)
2927

3028
conf := config.New()
31-
// force human output
32-
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
33-
3429
command := commands.BuildCommand(ListCommand(), nil, conf)
3530

36-
res, err := command.(commands.NoArgumentCommand).ExecuteWithoutArguments(commands.NewExecutor(conf, &mService, flume.New("test")))
37-
38-
assert.Nil(t, err)
31+
output, err := mockexecute.MockExecute(command, &mService, conf)
3932

40-
buf := bytes.NewBuffer(nil)
41-
err = output.Render(buf, conf, res)
4233
assert.NoError(t, err)
43-
assert.Regexp(t, "ID\\s+Description\\s+Public", buf.String())
44-
assert.Regexp(t, "fi-hel1\\s+Helsinki #1\\s+yes", buf.String())
34+
assert.Regexp(t, "ID\\s+Description\\s+Public", output)
35+
assert.Regexp(t, "fi-hel1\\s+Helsinki #1\\s+yes", output)
4536
}

internal/mockexecute/mockexecute.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ import (
1212
"github.com/UpCloudLtd/upcloud-cli/internal/service"
1313
)
1414

15-
func MockExecute(command commands.Command, service service.AllServices, config *config.Config) (string, error) {
15+
func MockExecute(command commands.Command, service service.AllServices, conf *config.Config) (string, error) {
1616
buf := bytes.NewBuffer(nil)
1717
command.Cobra().SetErr(buf)
1818
command.Cobra().SetOut(buf)
1919

20+
// Use human output if nothing else is defined
21+
if !conf.IsSet(config.KeyOutput) {
22+
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
23+
}
24+
2025
command.Cobra().RunE = func(_ *cobra.Command, args []string) error {
21-
return mockRunE(command, service, config, args)
26+
return mockRunE(command, service, conf, args)
2227
}
2328
err := command.Cobra().Execute()
2429

@@ -38,8 +43,8 @@ func mockRunE(command commands.Command, service service.AllServices, config *con
3843
case commands.MultipleArgumentCommand:
3944
out, err = typedCommand.Execute(executor, args[0])
4045
}
41-
42-
_ = output.Render(command.Cobra().OutOrStdout(), config, out)
43-
44-
return err
46+
if err != nil {
47+
return err
48+
}
49+
return output.Render(command.Cobra().OutOrStdout(), config, out)
4550
}

0 commit comments

Comments
 (0)