This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhelium_test.go
313 lines (242 loc) · 7.46 KB
/
helium_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package helium
import (
"context"
"fmt"
"io"
"log"
"os"
"strconv"
"testing"
"bou.ke/monkey"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"go.uber.org/dig"
"go.uber.org/zap"
"github.com/im-kulikov/helium/grace"
"github.com/im-kulikov/helium/internal"
"github.com/im-kulikov/helium/logger"
"github.com/im-kulikov/helium/module"
"github.com/im-kulikov/helium/settings"
)
type (
heliumApp struct{}
heliumErrApp struct{}
TestError struct {
Index int
Func interface{}
Reason error
}
)
const errTest = internal.Error("test")
func (e TestError) Error() string {
return "error level: " + strconv.Itoa(e.Index)
}
func (h heliumApp) Run(context.Context) error { return nil }
func (h heliumErrApp) Run(context.Context) error { return errTest }
func TestHelium(t *testing.T) {
t.Run("create new helium without errors", func(t *testing.T) {
h, err := New(&Settings{},
module.New(func() App { return heliumApp{} }),
grace.Module,
settings.Module,
logger.Module,
)
require.NotNil(t, h)
require.NoError(t, err)
require.NoError(t, h.Run())
})
t.Run("create new helium and setup ENV", func(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "example")
require.NoError(t, err)
err = os.Setenv("ABC_CONFIG", tmpFile.Name())
require.NoError(t, err)
err = os.Setenv("ABC_CONFIG_TYPE", "toml")
require.NoError(t, err)
h, err := New(&Settings{
Name: "Abc",
Defaults: func(cfg *settings.Core) {
require.NotNil(t, cfg)
cfg.Name = "TEST_NAME"
cfg.BuildTime = "TEST_BUILD_TIME"
cfg.BuildVersion = "TEST_BUILD_VERSION"
},
}, module.Module{
{Constructor: func(cfg *settings.Core) App {
require.Equal(t, tmpFile.Name(), cfg.File)
require.Equal(t, "toml", cfg.Type)
require.Equal(t, "TEST_NAME", cfg.Name)
require.Equal(t, "TEST_BUILD_TIME", cfg.BuildTime)
require.Equal(t, "TEST_BUILD_VERSION", cfg.BuildVersion)
return heliumApp{}
}},
}.Append(grace.Module, settings.Module, logger.Module))
require.NotNil(t, h)
require.NoError(t, err)
require.NoError(t, h.Run())
})
t.Run("start new helium default app with json logger and no_caller should not cause exceptions", func(t *testing.T) {
require.NotPanics(t, func() {
h, err := New(&Settings{
Defaults: func(v *viper.Viper) {
v.SetDefault("logger.format", "json")
v.SetDefault("logger.no_caller", true)
},
},
DefaultApp,
grace.Module,
settings.Module,
logger.Module,
)
require.NotNil(t, h)
require.NoError(t, err)
require.NoError(t, h.Run())
})
})
t.Run("create new helium should fail on new", func(t *testing.T) {
h, err := New(&Settings{}, module.Module{
{Constructor: func() error { return nil }},
})
require.Nil(t, h)
require.Error(t, err)
})
t.Run("create new helium should fail on start", func(t *testing.T) {
h, err := New(&Settings{}, module.Module{
{Constructor: func() App { return heliumErrApp{} }},
}.Append(grace.Module, settings.Module, logger.Module))
require.NotNil(t, h)
require.NoError(t, err)
require.Error(t, h.Run())
})
t.Run("create new helium should fail on start", func(t *testing.T) {
h, err := New(&Settings{}, module.Module{
{Constructor: func() App { return heliumErrApp{} }},
}.Append(grace.Module, settings.Module, logger.Module))
require.NotNil(t, h)
require.NoError(t, err)
require.Error(t, h.Run())
})
t.Run("invoke dependencies from helium container", func(t *testing.T) {
t.Run("should be ok", func(t *testing.T) {
h, err := New(&Settings{}, grace.Module.Append(settings.Module, logger.Module))
require.NotNil(t, h)
require.NoError(t, err)
require.Nil(t, h.Invoke(func() {}))
})
t.Run("should fail", func(t *testing.T) {
h, err := New(&Settings{}, grace.Module.Append(settings.Module, logger.Module))
require.NotNil(t, h)
require.NoError(t, err)
require.Error(t, h.Invoke(func(string) {}))
})
})
t.Run("check catch", func(t *testing.T) {
t.Run("should panic", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
monkey.Patch(logger.NewLogger, func(*logger.Config, *settings.Core) (*zap.Logger, error) {
return nil, errTest
})
defer monkey.Unpatch(logger.NewLogger)
err := errTest
Catch(err)
require.Equal(t, 2, exitCode)
})
t.Run("should catch error", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
monkey.Patch(fmt.Fprintf, func(io.Writer, string, ...interface{}) (int, error) {
return 0, nil
})
defer monkey.Unpatch(fmt.Fprintf)
monkey.Patch(logger.NewLogger, func(*logger.Config, *settings.Core) (*zap.Logger, error) {
return zap.NewNop(), nil
})
defer monkey.Unpatch(logger.NewLogger)
err := errTest
Catch(err)
require.Equal(t, 1, exitCode)
})
t.Run("shouldn't catch any", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
Catch(nil)
require.Empty(t, exitCode)
})
t.Run("should catch stacktrace simple error", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
// nolint:forbidigo
monkey.Patch(fmt.Printf, func(string, ...interface{}) (int, error) {
return 0, nil
})
require.Panics(t, func() {
CatchTrace(
errTest)
})
require.Empty(t, exitCode)
})
t.Run("should catch stacktrace on nil", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
require.NotPanics(t, func() {
CatchTrace(nil)
})
require.Empty(t, exitCode)
})
t.Run("should catch stacktrace on dig.Errors", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
monkey.Patch(fmt.Fprintf, func(io.Writer, string, ...interface{}) (int, error) { return 0, nil })
defer monkey.Unpatch(fmt.Fprintf)
require.Panics(t, func() {
di := dig.New()
CatchTrace(di.Invoke(func(log *zap.Logger) error {
return nil
}))
})
require.Empty(t, exitCode)
})
t.Run("should catch context.DeadlineExceeded on dig.Errors", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
defer monkey.UnpatchAll()
require.Panics(t, func() {
di := dig.New()
CatchTrace(di.Invoke(func() error {
return context.DeadlineExceeded
}))
})
require.Empty(t, exitCode)
})
t.Run("should catch multi level errors", func(t *testing.T) {
var exitCode int
monkey.Patch(os.Exit, func(code int) { exitCode = code })
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
require.Panics(t, func() {
CatchTrace(TestError{
Index: 1,
Reason: TestError{
Index: 2,
Reason: TestError{
Index: 3,
},
},
})
})
require.Empty(t, exitCode)
})
})
}