Skip to content

Commit 0ad7546

Browse files
committed
refactor: use MockExecute and assert.EqualError in tests
Using assert.EqualError instead of assert.Errorf fixes error message validation.
1 parent e1a27b6 commit 0ad7546

14 files changed

Lines changed: 22 additions & 46 deletions

File tree

internal/commands/account/show_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package account
22

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

76
"github.com/jedib0t/go-pretty/v6/text"
87

98
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
109
"github.com/UpCloudLtd/upcloud-cli/internal/config"
1110
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
12-
"github.com/UpCloudLtd/upcloud-cli/internal/output"
11+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1312

1413
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
15-
"github.com/gemalto/flume"
1614
"github.com/stretchr/testify/assert"
1715
)
1816

@@ -58,13 +56,9 @@ func TestShowCommand(t *testing.T) {
5856
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
5957

6058
command := commands.BuildCommand(testCmd, nil, conf)
61-
out, err := command.(commands.NoArgumentCommand).ExecuteWithoutArguments(commands.NewExecutor(conf, mService, flume.New("test")))
62-
assert.NoError(t, err)
59+
output, err := mockexecute.MockExecute(command, mService, conf)
6360

64-
buf := bytes.NewBuffer(nil)
65-
err = output.Render(buf, conf, out)
6661
assert.NoError(t, err)
67-
assert.Equal(t, expected, buf.String())
68-
62+
assert.Equal(t, expected, output)
6963
mService.AssertNumberOfCalls(t, "GetAccount", 1)
7064
}

internal/commands/database/plans_test.go

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

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

87
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
98
"github.com/UpCloudLtd/upcloud-cli/internal/config"
109
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
11-
"github.com/UpCloudLtd/upcloud-cli/internal/output"
10+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1211

1312
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
14-
"github.com/gemalto/flume"
1513
"github.com/jedib0t/go-pretty/v6/text"
1614
"github.com/stretchr/testify/assert"
1715
"github.com/stretchr/testify/mock"
@@ -38,16 +36,10 @@ func TestDatabasePlans_SortedHumanOutput(t *testing.T) {
3836

3937
command := commands.BuildCommand(PlansCommand(), nil, conf)
4038

41-
res, err := command.(commands.MultipleArgumentCommand).Execute(commands.NewExecutor(conf, &mService, flume.New("test")), "pg")
42-
43-
assert.Nil(t, err)
44-
45-
buf := bytes.NewBuffer(nil)
46-
err = output.Render(buf, conf, res)
47-
output := buf.String()
39+
command.Cobra().SetArgs([]string{"pg"})
40+
output, err := mockexecute.MockExecute(command, &mService, conf)
4841

4942
assert.NoError(t, err)
50-
5143
assert.Less(t, strings.Index(output, "test-plan-1"), strings.Index(output, "test-plan-2"))
5244
assert.Less(t, strings.Index(output, "test-plan-2"), strings.Index(output, "test-plan-3"))
5345
assert.Less(t, strings.Index(output, "test-plan-3"), strings.Index(output, "test-plan-4"))

internal/commands/network/delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestDeleteCommand(t *testing.T) {
4343
_, err = c.(commands.MultipleArgumentCommand).Execute(commands.NewExecutor(conf, &mService, flume.New("test")), test.arg)
4444

4545
if test.error != "" {
46-
assert.Errorf(t, err, test.error)
46+
assert.EqualError(t, err, test.error)
4747
} else {
4848
mService.AssertNumberOfCalls(t, targetMethod, 1)
4949
}

internal/commands/networkinterface/create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestCreateCommand(t *testing.T) {
127127
_, err = c.(commands.SingleArgumentCommand).ExecuteSingleArgument(commands.NewExecutor(conf, &mService, flume.New("test")), server.UUID)
128128

129129
if test.error != "" {
130-
assert.Errorf(t, err, test.error)
130+
assert.EqualError(t, err, test.error)
131131
} else {
132132
mService.AssertNumberOfCalls(t, targetMethod, 1)
133133
}

internal/commands/networkinterface/delete_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func TestDeleteCommand(t *testing.T) {
3535
{
3636
name: "server is missing",
3737
flags: []string{"--index", "4"},
38-
error: "at least one server uuid is required",
38+
error: "single server uuid is required",
3939
},
4040
{
4141
name: "index is missing",
4242
arg: server.UUID,
43-
error: "index is required",
43+
error: "interface index is required",
4444
},
4545
} {
4646
t.Run(test.name, func(t *testing.T) {
@@ -57,7 +57,7 @@ func TestDeleteCommand(t *testing.T) {
5757
_, err = c.(commands.SingleArgumentCommand).ExecuteSingleArgument(commands.NewExecutor(conf, &mService, flume.New("test")), test.arg)
5858

5959
if test.error != "" {
60-
assert.Errorf(t, err, test.error)
60+
assert.EqualError(t, err, test.error)
6161
} else {
6262
mService.AssertNumberOfCalls(t, targetMethod, 1)
6363
}

internal/commands/networkinterface/modify_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestModifyCommand(t *testing.T) {
7777
_, err = c.(commands.SingleArgumentCommand).ExecuteSingleArgument(commands.NewExecutor(conf, &mService, flume.New("test")), server.UUID)
7878

7979
if test.error != "" {
80-
assert.Errorf(t, err, test.error)
80+
assert.EqualError(t, err, test.error)
8181
} else {
8282
mService.AssertNumberOfCalls(t, targetMethod, 1)
8383
}

internal/commands/serverfirewall/create_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ func TestCreateFirewallRuleCommand(t *testing.T) {
101101
_, err := mockexecute.MockExecute(cc, &mService, conf)
102102

103103
if test.error != "" {
104-
assert.Error(t, err)
105-
assert.Equal(t, test.error, err.Error())
104+
assert.EqualError(t, err, test.error)
106105
} else {
107106
assert.NoError(t, err)
108107
mService.AssertNumberOfCalls(t, "CreateFirewallRule", 1)

internal/commands/serverstorage/attach_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ func TestAttachStorageCommand(t *testing.T) {
118118
_, err := mockexecute.MockExecute(c, mService, conf)
119119

120120
if test.error != "" {
121-
assert.Error(t, err)
122-
assert.Equal(t, test.error, err.Error())
121+
assert.EqualError(t, err, test.error)
123122
} else {
124123
assert.NoError(t, err)
125124
mService.AssertNumberOfCalls(t, targetMethod, 1)

internal/commands/serverstorage/detach_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ func TestDetachCommand(t *testing.T) {
7676
_, err := mockexecute.MockExecute(c, mService, conf)
7777

7878
if test.error != "" {
79-
assert.Error(t, err)
80-
assert.Equal(t, test.error, err.Error())
79+
assert.EqualError(t, err, test.error)
8180
} else {
8281
assert.NoError(t, err)
8382
mService.AssertNumberOfCalls(t, targetMethod, 1)

internal/commands/storage/backup_create_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ func TestCreateBackupCommand(t *testing.T) {
7272
_, err := mockexecute.MockExecute(c, mService, conf)
7373

7474
if test.error != "" {
75-
assert.Error(t, err)
76-
assert.Equal(t, test.error, err.Error())
75+
assert.EqualError(t, err, test.error)
7776
} else {
7877
assert.NoError(t, err)
7978
mService.AssertNumberOfCalls(t, targetMethod, 1)

0 commit comments

Comments
 (0)