Skip to content

Commit 9a6a177

Browse files
committed
Added helper to call scoped command
1 parent 3631753 commit 9a6a177

File tree

4 files changed

+75
-75
lines changed

4 files changed

+75
-75
lines changed

pkg/commands/command_test.go

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

88
import (
9-
"bytes"
109
"os"
1110
"testing"
1211

13-
"github.com/Sirupsen/logrus"
1412
"github.com/scaleway/scaleway-cli/pkg/api"
15-
"github.com/scaleway/scaleway-cli/pkg/config"
1613
"github.com/scaleway/scaleway-cli/vendor/github.com/stretchr/testify/assert"
1714
)
1815

@@ -42,34 +39,3 @@ func TestCommandContext_Getenv(t *testing.T) {
4239
assert.Equal(t, ctx.Getenv("HOME"), os.Getenv("HOME"))
4340
assert.Equal(t, ctx.Getenv("DONTEXISTS"), "")
4441
}
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/create_test.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package commands
66

77
import (
8-
"bytes"
98
"strings"
109
"testing"
1110

@@ -65,37 +64,27 @@ func TestRunCreate_realAPI(t *testing.T) {
6564
Name: "unittest-create-standard",
6665
Image: "ubuntu-vivid",
6766
}
68-
err := RunCreate(*ctx, args)
69-
So(err, ShouldBeNil)
7067

71-
stderr := ctx.Stderr.(*bytes.Buffer).String()
72-
stdout := ctx.Stdout.(*bytes.Buffer).String()
73-
So(stderr, ShouldBeEmpty)
74-
So(stdout, shouldBeAnUUID)
68+
scopedCtx, scopedStdout, scopedStderr := getScopedCtx(ctx)
69+
err := RunCreate(*scopedCtx, args)
70+
So(err, ShouldBeNil)
71+
So(scopedStderr.String(), ShouldBeEmpty)
72+
So(scopedStdout.String(), shouldBeAnUUID)
7573

76-
createdUUIDs = append(createdUUIDs, strings.TrimSpace(stdout))
74+
uuid := strings.TrimSpace(scopedStdout.String())
75+
createdUUIDs = append(createdUUIDs, uuid)
7776
})
7877

7978
Reset(func() {
80-
ctx.Stdout.(*bytes.Buffer).Reset()
81-
ctx.Stderr.(*bytes.Buffer).Reset()
82-
8379
if len(createdUUIDs) > 0 {
84-
err := RunRm(*ctx, RmArgs{
85-
Servers: createdUUIDs,
86-
})
87-
So(err, ShouldBeNil)
80+
rmCtx, rmStdout, rmStderr := getScopedCtx(ctx)
81+
rmErr := RunRm(*rmCtx, RmArgs{Servers: createdUUIDs})
82+
So(rmErr, ShouldBeNil)
83+
So(rmStderr.String(), ShouldBeEmpty)
8884

89-
stderr := ctx.Stderr.(*bytes.Buffer).String()
90-
stdout := ctx.Stdout.(*bytes.Buffer).String()
91-
So(stderr, ShouldBeEmpty)
92-
removedUUIDs := strings.Split(strings.TrimSpace(stdout), "\n")
85+
removedUUIDs := strings.Split(strings.TrimSpace(rmStdout.String()), "\n")
9386
So(removedUUIDs, ShouldResemble, createdUUIDs)
94-
9587
createdUUIDs = createdUUIDs[:0]
96-
97-
ctx.Stdout.(*bytes.Buffer).Reset()
98-
ctx.Stderr.(*bytes.Buffer).Reset()
9988
}
10089
})
10190
})

pkg/commands/images_test.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ func TestRunImages_realAPI(t *testing.T) {
7070
NoTrunc: false,
7171
Quiet: false,
7272
}
73-
err := RunImages(*ctx, args)
74-
So(err, ShouldBeNil)
7573

76-
stderr := ctx.Stderr.(*bytes.Buffer).String()
77-
So(stderr, ShouldBeEmpty)
74+
scopedCtx, scopedStdout, scopedStderr := getScopedCtx(ctx)
75+
err := RunImages(*scopedCtx, args)
76+
So(err, ShouldBeNil)
77+
So(scopedStderr.String(), ShouldBeEmpty)
7878

79-
stdout := ctx.Stdout.(*bytes.Buffer).String()
80-
lines := strings.Split(stdout, "\n")
79+
lines := strings.Split(scopedStdout.String(), "\n")
8180
So(len(lines), ShouldBeGreaterThan, 0)
8281

8382
firstLine := lines[0]
@@ -92,14 +91,13 @@ func TestRunImages_realAPI(t *testing.T) {
9291
NoTrunc: false,
9392
Quiet: false,
9493
}
95-
err := RunImages(*ctx, args)
96-
So(err, ShouldBeNil)
9794

98-
stderr := ctx.Stderr.(*bytes.Buffer).String()
99-
So(stderr, ShouldBeEmpty)
95+
scopedCtx, scopedStdout, scopedStderr := getScopedCtx(ctx)
96+
err := RunImages(*scopedCtx, args)
97+
So(err, ShouldBeNil)
98+
So(scopedStderr.String(), ShouldBeEmpty)
10099

101-
stdout := ctx.Stdout.(*bytes.Buffer).String()
102-
lines := strings.Split(stdout, "\n")
100+
lines := strings.Split(scopedStdout.String(), "\n")
103101
So(len(lines), ShouldBeGreaterThan, 0)
104102

105103
firstLine := lines[0]
@@ -116,14 +114,13 @@ func TestRunImages_realAPI(t *testing.T) {
116114
NoTrunc: false,
117115
Quiet: true,
118116
}
119-
err := RunImages(*ctx, args)
120-
So(err, ShouldBeNil)
121117

122-
stderr := ctx.Stderr.(*bytes.Buffer).String()
123-
So(stderr, ShouldBeEmpty)
118+
scopedCtx, scopedStdout, scopedStderr := getScopedCtx(ctx)
119+
err := RunImages(*scopedCtx, args)
120+
So(err, ShouldBeNil)
121+
So(scopedStderr.String(), ShouldBeEmpty)
124122

125-
stdout := ctx.Stdout.(*bytes.Buffer).String()
126-
lines := strings.Split(stdout, "\n")
123+
lines := strings.Split(scopedStdout.String(), "\n")
127124
// So(len(lines), ShouldBeGreaterThan, 0)
128125

129126
if len(lines) > 0 {

pkg/commands/test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package commands
22

33
import (
4+
"bytes"
45
"fmt"
6+
"os"
57
"strings"
68

9+
"github.com/Sirupsen/logrus"
710
"github.com/pborman/uuid"
11+
"github.com/scaleway/scaleway-cli/pkg/api"
12+
"github.com/scaleway/scaleway-cli/pkg/config"
813
)
914

1015
func shouldBeAnUUID(actual interface{}, expected ...interface{}) string {
@@ -16,3 +21,46 @@ func shouldBeAnUUID(actual interface{}, expected ...interface{}) string {
1621
}
1722
return ""
1823
}
24+
25+
func getScopedCtx(sessionCtx *CommandContext) (*CommandContext, *bytes.Buffer, *bytes.Buffer) {
26+
stdout := bytes.Buffer{}
27+
stderr := bytes.Buffer{}
28+
29+
var newCtx CommandContext
30+
newCtx = *sessionCtx
31+
newCtx.Stdout = &stdout
32+
newCtx.Stderr = &stderr
33+
34+
return &newCtx, &stdout, &stderr
35+
}
36+
37+
func RealAPIContext() *CommandContext {
38+
config, err := config.GetConfig()
39+
if err != nil {
40+
logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
41+
return nil
42+
}
43+
44+
apiClient, err := api.NewScalewayAPI(config.ComputeAPI, config.AccountAPI, config.Organization, config.Token)
45+
if err != nil {
46+
logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err)
47+
return nil
48+
}
49+
50+
stdout := bytes.Buffer{}
51+
stderr := bytes.Buffer{}
52+
53+
ctx := CommandContext{
54+
Streams: Streams{
55+
Stdin: os.Stdin,
56+
Stdout: &stdout,
57+
Stderr: &stderr,
58+
},
59+
Env: []string{
60+
"HOME" + os.Getenv("HOME"),
61+
},
62+
RawArgs: []string{},
63+
API: apiClient,
64+
}
65+
return &ctx
66+
}

0 commit comments

Comments
 (0)