/
test.go
70 lines (60 loc) · 1.57 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package commands
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/moul/anonuuid"
"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/config"
"github.com/scaleway/scaleway-cli/pkg/scwversion"
)
func shouldBeAnUUID(actual interface{}, expected ...interface{}) string {
input := actual.(string)
input = strings.TrimSpace(input)
if err := anonuuid.IsUUID(input); err != nil {
return fmt.Sprintf("%q should be an UUID", actual)
}
return ""
}
func getScopedCtx(sessionCtx *CommandContext) (*CommandContext, *bytes.Buffer, *bytes.Buffer) {
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
var newCtx CommandContext
newCtx = *sessionCtx
newCtx.Stdout = &stdout
newCtx.Stderr = &stderr
return &newCtx, &stdout, &stderr
}
// RealAPIContext returns a CommandContext with a configured API
func RealAPIContext() *CommandContext {
if os.Getenv("TEST_WITH_REAL_API") == "0" {
return nil
}
config, err := config.GetConfig()
if err != nil {
logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
return nil
}
apiClient, err := api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent(), "par1")
if err != nil {
logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err)
return nil
}
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
ctx := CommandContext{
Streams: Streams{
Stdin: os.Stdin,
Stdout: &stdout,
Stderr: &stderr,
},
Env: []string{
"HOME" + os.Getenv("HOME"),
},
RawArgs: []string{},
API: apiClient,
}
return &ctx
}