-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_utils.go
141 lines (128 loc) · 3.42 KB
/
test_utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
func createFile(t *testing.T, pathToFile string) {
testFileCreation, err := os.Create(pathToFile)
assert.Nilf(
t,
err,
"unexpected error when creating file at '%s': %s",
pathToFile,
err,
)
defer testFileCreation.Close()
}
// MockCommand holds a mock command that can be used in place of the
// actual Command struct for testing execution group calls
type MockCommand struct {
Command
}
func ensureFlag(t *testing.T, flag cli.Flag, hasType interface{}, hasName string) {
assert.IsType(t, hasType, flag)
assert.Regexp(t, regexp.MustCompile(hasName), flag.GetName())
}
func ensureCLICommand(t *testing.T, command cli.Command, expectedNames []string, expectedFlags []cli.Flag) {
assert.NotNil(t, command.Action)
assert.NotNil(t, command.Description)
assert.Equal(t, expectedFlags, command.Flags)
assert.Equal(t, expectedNames[0], command.Name)
for _, alias := range expectedNames[1:] {
assert.Contains(t, command.Aliases, alias)
}
assert.NotNil(t, command.Usage)
}
func ensureCLIFlags(t *testing.T, expectedFlags []string, actualFlags []cli.Flag) {
matchedFlagsCount := 0
for _, flag := range actualFlags {
for _, expected := range expectedFlags {
if strings.Contains(flag.GetName(), expected) {
matchedFlagsCount++
break
}
}
}
assert.Equal(t, matchedFlagsCount, len(expectedFlags))
}
func ensureCLIStartSetsRunFlag(t *testing.T, withArgs []string, runFlagName string, and ...func(bytes.Buffer)) {
cli := initCLI()
var logs bytes.Buffer
cli.rawLogger.SetOutput(&logs)
cli.Start(withArgs, func(config *Config) {
assert.NotNil(t, config)
configInstance := reflect.ValueOf(config)
flagValue := reflect.Indirect(configInstance).FieldByName(runFlagName)
assert.Equal(t, true, flagValue.Bool())
if len(and) > 0 {
and[0](logs)
}
})
}
func mockCommand(application string, arguments []string, logOutput *bytes.Buffer) *Command {
command := &Command{
id: fmt.Sprintf("%s%v", application, arguments),
config: &CommandConfig{
Application: application,
Arguments: arguments,
},
logger: InitLogger(&LoggerConfig{
Name: application,
Format: "production",
Level: "trace",
}),
signal: make(chan os.Signal),
}
command.logger.SetOutput(logOutput)
return command
}
func expectError(t *testing.T) func() {
return func() {
assert.NotNil(t, recover(), "expected an error but none was panicked")
}
}
func expectNoError(t *testing.T) func() {
return func() {
err := recover()
assert.Nilf(t, err, "expected no errors but '%s' was panicked", err)
}
}
func removeFile(t *testing.T, pathToFile string) {
err := os.Remove(pathToFile)
assert.Nilf(
t,
err,
"unexpected error when removing file at '%s': %s",
pathToFile,
err,
)
}
func removeDir(t *testing.T, pathToDirectory string) error {
directoryListing, err := ioutil.ReadDir(pathToDirectory)
assert.Nil(t, err)
for i := 0; i < len(directoryListing); i++ {
listing := directoryListing[i]
fullPath := path.Join(pathToDirectory, listing.Name())
listingInfo, err := os.Lstat(fullPath)
assert.Nil(t, err)
if listingInfo.IsDir() {
t.Logf("removing dir %s\n", fullPath)
err = removeDir(t, fullPath)
} else {
err = os.Remove(fullPath)
assert.Nil(t, err)
t.Logf("removed file %s\n", fullPath)
}
}
err = os.RemoveAll(pathToDirectory)
return err
}