Skip to content

Commit b94a0a0

Browse files
committed
Refactored tests for commands
1 parent 4f102f2 commit b94a0a0

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

pkg/commands/command_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
package commands
77

88
import (
9+
"bytes"
910
"os"
1011
"testing"
1112

13+
"github.com/Sirupsen/logrus"
1214
"github.com/scaleway/scaleway-cli/pkg/api"
15+
"github.com/scaleway/scaleway-cli/pkg/config"
1316
"github.com/scaleway/scaleway-cli/vendor/github.com/stretchr/testify/assert"
1417
)
1518

@@ -39,3 +42,34 @@ func TestCommandContext_Getenv(t *testing.T) {
3942
assert.Equal(t, ctx.Getenv("HOME"), os.Getenv("HOME"))
4043
assert.Equal(t, ctx.Getenv("DONTEXISTS"), "")
4144
}
45+
46+
func RealAPIContext() *CommandContext {
47+
config, err := config.GetConfig()
48+
if err != nil {
49+
logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
50+
return nil
51+
}
52+
53+
apiClient, err := api.NewScalewayAPI(config.ComputeAPI, config.AccountAPI, config.Organization, config.Token)
54+
if err != nil {
55+
logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err)
56+
return nil
57+
}
58+
59+
stdout := bytes.Buffer{}
60+
stderr := bytes.Buffer{}
61+
62+
ctx := CommandContext{
63+
Streams: Streams{
64+
Stdin: os.Stdin,
65+
Stdout: &stdout,
66+
Stderr: &stderr,
67+
},
68+
Env: []string{
69+
"HOME" + os.Getenv("HOME"),
70+
},
71+
RawArgs: []string{},
72+
API: apiClient,
73+
}
74+
return &ctx
75+
}

pkg/commands/version_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ import (
88
"bytes"
99
"testing"
1010

11-
"github.com/scaleway/scaleway-cli/vendor/github.com/stretchr/testify/assert"
11+
. "github.com/smartystreets/goconvey/convey"
1212
)
1313

14-
func TestToto(t *testing.T) {
15-
ctx := ExampleCommandContext()
16-
var buf bytes.Buffer
17-
ctx.Stdout = &buf
14+
func TestVersion(t *testing.T) {
15+
Convey("Testing Version()", t, func() {
16+
ctx := ExampleCommandContext()
17+
var buf bytes.Buffer
18+
ctx.Stdout = &buf
1819

19-
args := VersionArgs{}
20+
args := VersionArgs{}
21+
22+
err := Version(ctx, args)
2023

21-
err := Version(ctx, args)
24+
So(err, ShouldBeNil)
25+
So(buf.String(), ShouldContainSubstring, "Client version: ")
26+
So(buf.String(), ShouldContainSubstring, "Go version (client): ")
27+
So(buf.String(), ShouldContainSubstring, "Git commit (client): ")
28+
So(buf.String(), ShouldContainSubstring, "OS/Arch (client): ")
2229

23-
assert.Nil(t, err)
24-
assert.Contains(t, buf.String(), "Client version: ")
25-
assert.Contains(t, buf.String(), "Go version (client): ")
26-
assert.Contains(t, buf.String(), "Git commit (client): ")
27-
assert.Contains(t, buf.String(), "OS/Arch (client): ")
30+
})
2831
}
2932

3033
func ExampleVersion() {

0 commit comments

Comments
 (0)