@@ -15,182 +15,243 @@ import (
15
15
"github.com/im-kulikov/helium/logger"
16
16
"github.com/im-kulikov/helium/module"
17
17
"github.com/im-kulikov/helium/settings"
18
- . "github.com/smartystreets/goconvey/convey "
18
+ "github.com/stretchr/testify/require "
19
19
"go.uber.org/dig"
20
20
"go.uber.org/zap"
21
21
)
22
22
23
23
type (
24
24
heliumApp struct {}
25
25
heliumErrApp struct {}
26
+
27
+ Error string
26
28
)
27
29
30
+ const ErrTest = Error ("test" )
31
+
32
+ func (e Error ) Error () string {
33
+ return string (e )
34
+ }
35
+
28
36
func (h heliumApp ) Run (ctx context.Context ) error { return nil }
29
- func (h heliumErrApp ) Run (ctx context.Context ) error { return errors . New ( "test" ) }
37
+ func (h heliumErrApp ) Run (ctx context.Context ) error { return ErrTest }
30
38
31
39
func TestHelium (t * testing.T ) {
32
- Convey ("Helium test suite" , t , func (c C ) {
33
- c .Convey ("create new helium without errors" , func (c C ) {
34
- h , err := New (& Settings {}, module.Module {
35
- {Constructor : func () App { return heliumApp {} }},
36
- }.Append (grace .Module , settings .Module , logger .Module ))
40
+ t .Run ("create new helium without errors" , func (t * testing.T ) {
41
+ h , err := New (& Settings {}, module.Module {
42
+ {Constructor : func () App { return heliumApp {} }},
43
+ }.Append (grace .Module , settings .Module , logger .Module ))
44
+
45
+ require .NotNil (t , h )
46
+ require .NoError (t , err )
47
+ require .NoError (t , h .Run ())
48
+ })
37
49
38
- c .So (h , ShouldNotBeNil )
39
- c .So (err , ShouldBeNil )
50
+ t .Run ("create new helium and setup ENV" , func (t * testing.T ) {
51
+ tmpFile , err := ioutil .TempFile ("" , "example" )
52
+ require .NoError (t , err )
53
+
54
+ defer func () {
55
+ require .NoError (t , os .Remove (tmpFile .Name ()))
56
+ }() // clean up
57
+
58
+ err = os .Setenv ("ABC_CONFIG" , tmpFile .Name ())
59
+ require .NoError (t , err )
60
+
61
+ err = os .Setenv ("ABC_CONFIG_TYPE" , "toml" )
62
+ require .NoError (t , err )
63
+
64
+ h , err := New (& Settings {
65
+ Name : "Abc" ,
66
+ }, module.Module {
67
+ {Constructor : func (cfg * settings.Core ) App {
68
+ require .Equal (t , tmpFile .Name (), cfg .File )
69
+ require .Equal (t , "toml" , cfg .Type )
70
+ return heliumApp {}
71
+ }},
72
+ }.Append (grace .Module , settings .Module , logger .Module ))
73
+
74
+ require .NotNil (t , h )
75
+ require .NoError (t , err )
76
+ require .NoError (t , h .Run ())
77
+ })
40
78
41
- c .So (h .Run (), ShouldBeNil )
79
+ t .Run ("create new helium should fail on new" , func (t * testing.T ) {
80
+ h , err := New (& Settings {}, module.Module {
81
+ {Constructor : func () error { return nil }},
42
82
})
43
83
44
- c .Convey ("create new helium and setup ENV" , func (c C ) {
45
- tmpFile , err := ioutil .TempFile ("" , "example" )
46
- c .So (err , ShouldBeNil )
84
+ require .Nil (t , h )
85
+ require .Error (t , err )
86
+ })
87
+ t .Run ("create new helium should fail on start" , func (t * testing.T ) {
88
+ h , err := New (& Settings {}, module.Module {
89
+ {Constructor : func () App { return heliumErrApp {} }},
90
+ }.Append (grace .Module , settings .Module , logger .Module ))
47
91
48
- defer os .Remove (tmpFile .Name ()) // clean up
92
+ require .NotNil (t , h )
93
+ require .NoError (t , err )
49
94
50
- os . Setenv ( "ABC_CONFIG" , tmpFile . Name ())
51
- os . Setenv ( "ABC_CONFIG_TYPE" , "toml" )
95
+ require . Error ( t , h . Run ())
96
+ } )
52
97
53
- h , err := New (& Settings {
54
- Name : "Abc" ,
55
- }, module.Module {
56
- {Constructor : func (cfg * settings.Core ) App {
57
- c .So (cfg .File , ShouldEqual , tmpFile .Name ())
58
- c .So (cfg .Type , ShouldEqual , "toml" )
59
- return heliumApp {}
60
- }},
61
- }.Append (grace .Module , settings .Module , logger .Module ))
98
+ t .Run ("create new helium should fail on start" , func (t * testing.T ) {
99
+ h , err := New (& Settings {}, module.Module {
100
+ {Constructor : func () App { return heliumErrApp {} }},
101
+ }.Append (grace .Module , settings .Module , logger .Module ))
62
102
63
- c . So ( h , ShouldNotBeNil )
64
- c . So ( err , ShouldBeNil )
103
+ require . NotNil ( t , h )
104
+ require . NoError ( t , err )
65
105
66
- c . So ( h .Run (), ShouldBeNil )
67
- })
106
+ require . Error ( t , h .Run ())
107
+ })
68
108
69
- c .Convey ("create new helium should fail on new" , func (c C ) {
70
- h , err := New (& Settings {}, module.Module {
71
- {Constructor : func () error { return nil }},
72
- })
109
+ t .Run ("invoke dependencies from helium container" , func (t * testing.T ) {
110
+ t .Run ("should be ok" , func (t * testing.T ) {
111
+ h , err := New (& Settings {}, grace .Module .Append (settings .Module , logger .Module ))
112
+
113
+ require .NotNil (t , h )
114
+ require .NoError (t , err )
73
115
74
- c .So (h , ShouldBeNil )
75
- c .So (err , ShouldBeError )
116
+ require .Nil (t , h .Invoke (func () {}))
76
117
})
77
118
78
- c .Convey ("create new helium should fail on start" , func (c C ) {
79
- h , err := New (& Settings {}, module.Module {
80
- {Constructor : func () App { return heliumErrApp {} }},
81
- }.Append (grace .Module , settings .Module , logger .Module ))
119
+ t .Run ("should fail" , func (t * testing.T ) {
120
+ h , err := New (& Settings {}, grace .Module .Append (settings .Module , logger .Module ))
82
121
83
- c . So ( h , ShouldNotBeNil )
84
- c . So ( err , ShouldBeNil )
122
+ require . NotNil ( t , h )
123
+ require . NoError ( t , err )
85
124
86
- c . So ( h . Run (), ShouldBeError )
125
+ require . Error ( t , h . Invoke ( func ( string ) {}) )
87
126
})
127
+ })
88
128
89
- c . Convey ( "invoke dependencies from helium container " , func (c C ) {
90
- c . Convey ("should be ok " , func (c C ) {
91
- h , err := New ( & Settings {}, grace . Module . Append ( settings . Module , logger . Module ))
129
+ t . Run ( "check catch " , func (t * testing. T ) {
130
+ t . Run ("should panic " , func (t * testing. T ) {
131
+ var exitCode int
92
132
93
- c .So (h , ShouldNotBeNil )
94
- c .So (err , ShouldBeNil )
133
+ monkey .Patch (os .Exit , func (code int ) { exitCode = code })
134
+ monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 })
135
+
136
+ defer monkey .UnpatchAll ()
95
137
96
- c .So (h .Invoke (func () {}), ShouldBeNil )
138
+ monkey .Patch (logger .NewLogger , func (* logger.Config , * settings.Core ) (* zap.Logger , error ) {
139
+ return nil , errors .New ("test" )
97
140
})
141
+ defer monkey .Unpatch (logger .NewLogger )
142
+
143
+ err := errors .New ("test" )
144
+ Catch (err )
145
+ require .Equal (t , 2 , exitCode )
146
+ })
147
+
148
+ t .Run ("should catch error" , func (t * testing.T ) {
149
+ var exitCode int
98
150
99
- c . Convey ( "should fail" , func (c C ) {
100
- h , err := New ( & Settings {}, grace . Module . Append ( settings . Module , logger . Module ) )
151
+ monkey . Patch ( os . Exit , func (code int ) { exitCode = code })
152
+ monkey . Patch ( log . Fatal , func ( ... interface {}) { exitCode = 2 } )
101
153
102
- c .So (h , ShouldNotBeNil )
103
- c .So (err , ShouldBeNil )
154
+ defer monkey .UnpatchAll ()
104
155
105
- c .So (h .Invoke (func (string ) {}), ShouldBeError )
156
+ monkey .Patch (fmt .Fprintf , func (io.Writer , string , ... interface {}) (int , error ) {
157
+ return 0 , nil
106
158
})
159
+ defer monkey .Unpatch (fmt .Fprintf )
160
+
161
+ monkey .Patch (logger .NewLogger , func (* logger.Config , * settings.Core ) (* zap.Logger , error ) {
162
+ return zap .NewNop (), nil
163
+ })
164
+ defer monkey .Unpatch (logger .NewLogger )
165
+
166
+ err := errors .New ("test" )
167
+ Catch (err )
168
+ require .Equal (t , 1 , exitCode )
107
169
})
108
170
109
- c . Convey ( "check catch" , func (c C ) {
171
+ t . Run ( "shouldn't catch any " , func (t * testing. T ) {
110
172
var exitCode int
111
173
112
174
monkey .Patch (os .Exit , func (code int ) { exitCode = code })
113
175
monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 })
114
176
115
177
defer monkey .UnpatchAll ()
116
178
117
- c .Convey ("should panic" , func (c C ) {
118
- monkey .Patch (logger .NewLogger , func (* logger.Config , * settings.Core ) (* zap.Logger , error ) {
119
- return nil , errors .New ("test" )
120
- })
121
- defer monkey .Unpatch (logger .NewLogger )
179
+ Catch (nil )
180
+ require .Empty (t , exitCode )
181
+ })
122
182
123
- err := errors .New ("test" )
124
- Catch (err )
125
- c .So (exitCode , ShouldEqual , 2 )
126
- })
183
+ t .Run ("should catch stacktrace simple error" , func (t * testing.T ) {
184
+ var exitCode int
127
185
128
- c .Convey ("should catch error" , func (c C ) {
129
- monkey .Patch (fmt .Fprintf , func (io.Writer , string , ... interface {}) (int , error ) {
130
- return 0 , nil
131
- })
132
- defer monkey .Unpatch (fmt .Fprintf )
186
+ monkey .Patch (os .Exit , func (code int ) { exitCode = code })
187
+ monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 })
133
188
134
- monkey .Patch (logger .NewLogger , func (* logger.Config , * settings.Core ) (* zap.Logger , error ) {
135
- return zap .NewNop (), nil
136
- })
137
- defer monkey .Unpatch (logger .NewLogger )
189
+ defer monkey .UnpatchAll ()
138
190
139
- err := errors .New ("test" )
140
- Catch (err )
141
- c .So (exitCode , ShouldEqual , 1 )
191
+ monkey .Patch (fmt .Printf , func (string , ... interface {}) (int , error ) {
192
+ return 0 , nil
142
193
})
143
194
144
- c . Convey ( "shouldn't catch any" , func (c C ) {
145
- Catch ( nil )
146
- c . So ( exitCode , ShouldBeZeroValue )
195
+ require . Panics ( t , func () {
196
+ CatchTrace (
197
+ errors . New ( "test" ) )
147
198
})
148
199
149
- c .Convey ("should catch stacktrace simple error" , func (c C ) {
150
- monkey .Patch (fmt .Printf , func (string , ... interface {}) (int , error ) {
151
- return 0 , nil
152
- })
200
+ require .Empty (t , exitCode )
201
+ })
202
+
203
+ t .Run ("should catch stacktrace on nil" , func (t * testing.T ) {
204
+ var exitCode int
153
205
154
- c .So (func () {
155
- CatchTrace (
156
- errors .New ("test" ))
157
- }, ShouldPanic )
206
+ monkey .Patch (os .Exit , func (code int ) { exitCode = code })
207
+ monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 })
158
208
159
- c .So (exitCode , ShouldEqual , 0 )
209
+ defer monkey .UnpatchAll ()
210
+
211
+ require .NotPanics (t , func () {
212
+ CatchTrace (nil )
160
213
})
161
214
162
- c .Convey ("should catch stacktrace on nil" , func (c C ) {
163
- c .So (func () {
164
- CatchTrace (nil )
165
- }, ShouldNotPanic )
215
+ require .Empty (t , exitCode )
216
+ })
166
217
167
- c . So ( exitCode , ShouldEqual , 0 )
168
- })
218
+ t . Run ( "should catch stacktrace on dig.Errors" , func ( t * testing. T ) {
219
+ var exitCode int
169
220
170
- c .Convey ("should catch stacktrace on dig.Errors" , func (c C ) {
171
- monkey .Patch (fmt .Fprintf , func (io.Writer , string , ... interface {}) (int , error ) { return 0 , nil })
172
- defer monkey .Unpatch (fmt .Fprintf )
221
+ monkey .Patch (os .Exit , func (code int ) { exitCode = code })
222
+ monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 })
173
223
174
- c .So (func () {
175
- di := dig .New ()
176
- CatchTrace (di .Invoke (func (log * zap.Logger ) error {
177
- return nil
178
- }))
179
- }, ShouldPanic )
224
+ defer monkey .UnpatchAll ()
180
225
181
- c .So (exitCode , ShouldEqual , 0 )
226
+ monkey .Patch (fmt .Fprintf , func (io.Writer , string , ... interface {}) (int , error ) { return 0 , nil })
227
+ defer monkey .Unpatch (fmt .Fprintf )
228
+
229
+ require .Panics (t , func () {
230
+ di := dig .New ()
231
+ CatchTrace (di .Invoke (func (log * zap.Logger ) error {
232
+ return nil
233
+ }))
182
234
})
183
235
184
- c .Convey ("should catch context.DeadlineExceeded on dig.Errors" , func (c C ) {
185
- c .So (func () {
186
- di := dig .New ()
187
- CatchTrace (di .Invoke (func () error {
188
- return context .DeadlineExceeded
189
- }))
190
- }, ShouldPanic )
236
+ require .Empty (t , exitCode )
237
+ })
238
+
239
+ t .Run ("should catch context.DeadlineExceeded on dig.Errors" , func (t * testing.T ) {
240
+ var exitCode int
241
+
242
+ monkey .Patch (os .Exit , func (code int ) { exitCode = code })
243
+ monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 })
191
244
192
- c .So (exitCode , ShouldEqual , 0 )
245
+ defer monkey .UnpatchAll ()
246
+
247
+ require .Panics (t , func () {
248
+ di := dig .New ()
249
+ CatchTrace (di .Invoke (func () error {
250
+ return context .DeadlineExceeded
251
+ }))
193
252
})
253
+
254
+ require .Empty (t , exitCode )
194
255
})
195
256
})
196
257
}
0 commit comments