Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 617e4be

Browse files
authored
🍅 Prepare v0.11.9 release (#18)
* 🍅 Prepare v0.11.9 release - use constant errors - replace go-convey with `testify/require`
1 parent 1ddddcb commit 617e4be

File tree

7 files changed

+303
-219
lines changed

7 files changed

+303
-219
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/prometheus/client_golang v0.9.2
1515
github.com/prometheus/common v0.3.0 // indirect
1616
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 // indirect
17-
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa
17+
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
1818
github.com/spf13/viper v1.3.2
1919
github.com/stretchr/testify v1.3.0
2020
go.uber.org/atomic v1.3.2

helium_test.go

Lines changed: 172 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -15,182 +15,243 @@ import (
1515
"github.com/im-kulikov/helium/logger"
1616
"github.com/im-kulikov/helium/module"
1717
"github.com/im-kulikov/helium/settings"
18-
. "github.com/smartystreets/goconvey/convey"
18+
"github.com/stretchr/testify/require"
1919
"go.uber.org/dig"
2020
"go.uber.org/zap"
2121
)
2222

2323
type (
2424
heliumApp struct{}
2525
heliumErrApp struct{}
26+
27+
Error string
2628
)
2729

30+
const ErrTest = Error("test")
31+
32+
func (e Error) Error() string {
33+
return string(e)
34+
}
35+
2836
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 }
3038

3139
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+
})
3749

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+
})
4078

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 }},
4282
})
4383

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))
4791

48-
defer os.Remove(tmpFile.Name()) // clean up
92+
require.NotNil(t, h)
93+
require.NoError(t, err)
4994

50-
os.Setenv("ABC_CONFIG", tmpFile.Name())
51-
os.Setenv("ABC_CONFIG_TYPE", "toml")
95+
require.Error(t, h.Run())
96+
})
5297

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))
62102

63-
c.So(h, ShouldNotBeNil)
64-
c.So(err, ShouldBeNil)
103+
require.NotNil(t, h)
104+
require.NoError(t, err)
65105

66-
c.So(h.Run(), ShouldBeNil)
67-
})
106+
require.Error(t, h.Run())
107+
})
68108

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)
73115

74-
c.So(h, ShouldBeNil)
75-
c.So(err, ShouldBeError)
116+
require.Nil(t, h.Invoke(func() {}))
76117
})
77118

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))
82121

83-
c.So(h, ShouldNotBeNil)
84-
c.So(err, ShouldBeNil)
122+
require.NotNil(t, h)
123+
require.NoError(t, err)
85124

86-
c.So(h.Run(), ShouldBeError)
125+
require.Error(t, h.Invoke(func(string) {}))
87126
})
127+
})
88128

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
92132

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()
95137

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")
97140
})
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
98150

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 })
101153

102-
c.So(h, ShouldNotBeNil)
103-
c.So(err, ShouldBeNil)
154+
defer monkey.UnpatchAll()
104155

105-
c.So(h.Invoke(func(string) {}), ShouldBeError)
156+
monkey.Patch(fmt.Fprintf, func(io.Writer, string, ...interface{}) (int, error) {
157+
return 0, nil
106158
})
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)
107169
})
108170

109-
c.Convey("check catch", func(c C) {
171+
t.Run("shouldn't catch any", func(t *testing.T) {
110172
var exitCode int
111173

112174
monkey.Patch(os.Exit, func(code int) { exitCode = code })
113175
monkey.Patch(log.Fatal, func(...interface{}) { exitCode = 2 })
114176

115177
defer monkey.UnpatchAll()
116178

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+
})
122182

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
127185

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 })
133188

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()
138190

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
142193
})
143194

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"))
147198
})
148199

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
153205

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 })
158208

159-
c.So(exitCode, ShouldEqual, 0)
209+
defer monkey.UnpatchAll()
210+
211+
require.NotPanics(t, func() {
212+
CatchTrace(nil)
160213
})
161214

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+
})
166217

167-
c.So(exitCode, ShouldEqual, 0)
168-
})
218+
t.Run("should catch stacktrace on dig.Errors", func(t *testing.T) {
219+
var exitCode int
169220

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 })
173223

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()
180225

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+
}))
182234
})
183235

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 })
191244

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+
}))
193252
})
253+
254+
require.Empty(t, exitCode)
194255
})
195256
})
196257
}

0 commit comments

Comments
 (0)