-
Notifications
You must be signed in to change notification settings - Fork 10
/
builder_test.go
199 lines (161 loc) · 5.93 KB
/
builder_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package slackscot_test
import (
"fmt"
"github.com/alexandre-normand/slackscot"
"github.com/alexandre-normand/slackscot/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"strings"
"testing"
)
func TestNewSlackscotWithoutPlugins(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
Build()
require.NoError(t, err)
require.NotNil(t, b)
}
func TestNewSlackscotWithSimplePlugin(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPlugin(newPlugin()).
Build()
require.NoError(t, err)
require.NotNil(t, b)
}
func TestNewSlackscotWithPluginAndError(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPluginErr(newPluginWithErr("")).
Build()
require.NoError(t, err)
require.NotNil(t, b)
}
func TestNewSlackscotWithPluginAndErrorSet(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPluginErr(newPluginWithErr("error1")).
Build()
require.Error(t, err)
assert.EqualError(t, err, "error1")
assert.Nil(t, b)
}
func TestNewSlackscotWithPluginAndManyErrors(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPluginErr(newPluginWithErr("error1")).
WithPluginErr(newPluginWithErr("error2")).
WithPlugin(newPlugin()).
Build()
require.Error(t, err)
assert.EqualError(t, err, "error1")
assert.Nil(t, b)
}
func TestNewSlackscotWithCloserPluginClosingWithError(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPluginCloserErr(newPluginWithErrAndCloser("", CloseTester{errorMsg: "should be called"})).
Build()
require.NoError(t, err)
require.NotNil(t, b)
err = b.Close()
assert.EqualError(t, err, "should be called")
}
func TestNewSlackscotWithCloserPluginClosingWithoutError(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPluginCloserErr(newPluginWithErrAndCloser("", CloseTester{errorMsg: ""})).
Build()
require.NoError(t, err)
require.NotNil(t, b)
err = b.Close()
assert.NoError(t, err)
}
func TestNewSlackscotWithCloserAndErr(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithPluginCloserErr(newPluginWithErrAndCloser("error1", CloseTester{})).
WithPluginCloserErr(newPluginWithErrAndCloser("error2", CloseTester{})).
Build()
require.Error(t, err)
assert.EqualError(t, err, "error1")
assert.Nil(t, b)
}
func TestNewSlackscotWithConfigurablePluginMissingConfig(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithConfigurablePluginErr("tester", func(c *config.PluginConfig) (p *slackscot.Plugin, err error) { return newPluginWithErr("") }).
WithConfigurablePluginErr("testerClone", func(c *config.PluginConfig) (p *slackscot.Plugin, err error) { return newPluginWithErr("") }).
Build()
require.Error(t, err)
assert.EqualError(t, err, "Missing plugin configuration for plugin [tester] at [plugins.tester]")
assert.Nil(t, b)
}
func TestNewSlackscotWithConfigurablePluginValidConfig(t *testing.T) {
c := config.NewViperWithDefaults()
c.Set("plugins.tester", map[string]string{"enabled": "true"})
b, err := slackscot.NewBot("jane", c).
WithConfigurablePluginErr("tester", func(c *config.PluginConfig) (p *slackscot.Plugin, err error) { return newPluginWithErr("") }).
Build()
assert.NoError(t, err)
assert.NotNil(t, b)
}
func TestNewSlackscotWithConfigurableCloserPluginMissingConfig(t *testing.T) {
b, err := slackscot.NewBot("jane", config.NewViperWithDefaults()).
WithConfigurablePluginCloserErr("tester", func(conf *config.PluginConfig) (c io.Closer, p *slackscot.Plugin, err error) {
p, err = newPluginWithErr("")
return CloseTester{}, p, err
}).
WithConfigurablePluginCloserErr("testerClone", func(conf *config.PluginConfig) (c io.Closer, p *slackscot.Plugin, err error) {
p, err = newPluginWithErr("")
return CloseTester{}, p, err
}).
Build()
require.Error(t, err)
assert.EqualError(t, err, "Missing plugin configuration for plugin [tester] at [plugins.tester]")
assert.Nil(t, b)
}
func TestNewSlackscotWithConfigurableCloserPluginValidConfig(t *testing.T) {
c := config.NewViperWithDefaults()
c.Set("plugins.tester", map[string]string{"enabled": "true"})
b, err := slackscot.NewBot("jane", c).
WithConfigurablePluginCloserErr("tester", func(conf *config.PluginConfig) (c io.Closer, p *slackscot.Plugin, err error) {
p, err = newPluginWithErr("")
return CloseTester{}, p, err
}).
Build()
assert.NoError(t, err)
assert.NotNil(t, b)
}
// newPlugin returns a new tester plugin
func newPlugin() (p *slackscot.Plugin) {
p = new(slackscot.Plugin)
p.Name = "tester"
p.Commands = []slackscot.ActionDefinition{{
Match: func(m *slackscot.IncomingMessage) bool {
return strings.HasPrefix(m.NormalizedText, "make")
},
Usage: "make `<something>`",
Description: "Have the test bot make something for you",
Answer: func(m *slackscot.IncomingMessage) *slackscot.Answer {
return &slackscot.Answer{Text: "Ready"}
},
}}
return p
}
// newPluginWithErr returns the plugin along with an error if errorMsg is not empty
func newPluginWithErr(errorMsg string) (p *slackscot.Plugin, err error) {
if errorMsg != "" {
return nil, fmt.Errorf(errorMsg)
}
return newPlugin(), nil
}
// newPluginWithErr returns the plugin along with an error if errorMsg is not empty and the closer
func newPluginWithErrAndCloser(errorMsg string, closer io.Closer) (c io.Closer, p *slackscot.Plugin, err error) {
p, err = newPluginWithErr(errorMsg)
return closer, p, err
}
// CloseTester is an empty struct that has is a Closer that either doesn't do anything
// or returns the error set on the CloseTester
type CloseTester struct {
errorMsg string
}
// Close returns the CloseTester error if set, or just returns nil and does nothing otherwise
func (c CloseTester) Close() (err error) {
if c.errorMsg != "" {
return fmt.Errorf(c.errorMsg)
}
return nil
}