Skip to content

Commit 113b1ed

Browse files
Migrate from bou.ke/monkey to github.com/agiledragon/gomonkey/v2 (#78)
1 parent 772f028 commit 113b1ed

File tree

15 files changed

+210
-156
lines changed

15 files changed

+210
-156
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

1313
# Output of the go coverage tool, specifically when used with LiteIDE
1414
*.out
15-
15+
handler/fake_format_Mon Apr 7 1986

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
go test --race -gcflags=-l -v ./...

formatter/gelf_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ import (
77
"testing"
88
"time"
99

10-
"bou.ke/monkey"
11-
12-
"github.com/stretchr/testify/assert"
13-
10+
"github.com/agiledragon/gomonkey/v2"
1411
"github.com/gol4ng/logger"
1512
"github.com/gol4ng/logger/formatter"
13+
"github.com/stretchr/testify/assert"
1614
)
1715

1816
func TestNewGelf_WillPanic(t *testing.T) {
1917
err := errors.New("my_hostname_error")
20-
monkey.Patch(os.Hostname, func() (string, error) { return "", err })
18+
patch := gomonkey.NewPatches()
19+
patch.ApplyFunc(os.Hostname, func() (string, error) { return "", err })
20+
defer patch.Reset()
2121
assert.PanicsWithValue(t, err, func() { formatter.NewGelf() })
2222
}
2323

2424
func TestGelf_Format(t *testing.T) {
25-
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
26-
monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
27-
defer monkey.UnpatchAll()
25+
patch := gomonkey.NewPatches()
26+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(513216000, 0) })
27+
patch.ApplyFunc(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
28+
defer patch.Reset()
2829

2930
gelf := formatter.NewGelf()
3031

@@ -36,9 +37,10 @@ func TestGelf_Format(t *testing.T) {
3637
}
3738

3839
func TestGelfTCP_Format(t *testing.T) {
39-
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
40-
monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
41-
defer monkey.UnpatchAll()
40+
patch := gomonkey.NewPatches()
41+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(513216000, 0) })
42+
patch.ApplyFunc(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
43+
defer patch.Reset()
4244

4345
gelf := formatter.NewGelfTCP()
4446

@@ -54,9 +56,10 @@ func TestGelfTCP_Format(t *testing.T) {
5456
// =====================================================================================================================
5557

5658
func ExampleGelf_Format() {
57-
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
58-
monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
59-
defer monkey.UnpatchAll()
59+
patch := gomonkey.NewPatches()
60+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(513216000, 0) })
61+
patch.ApplyFunc(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
62+
defer patch.Reset()
6063

6164
gelfFormatter := formatter.NewGelf()
6265

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/gol4ng/logger
33
go 1.12
44

55
require (
6-
bou.ke/monkey v1.0.1
6+
github.com/agiledragon/gomonkey/v2 v2.3.1
77
github.com/davecgh/go-spew v1.1.1 // indirect
88
github.com/google/go-cmp v0.3.0 // indirect
99
github.com/kr/pretty v0.1.0 // indirect

go.sum

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U=
2-
bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg=
3-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1+
github.com/agiledragon/gomonkey/v2 v2.3.1 h1:k+UnUY0EMNYUFUAQVETGY9uUTxjMdnUkP0ARyJS1zzs=
2+
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
43
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
65
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
76
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
87
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
8+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
9+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
910
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1011
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1112
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -15,10 +16,17 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
1516
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1617
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1718
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
20+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
1821
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
1922
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2023
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2124
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
25+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
26+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
27+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
29+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
2230
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2331
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
2432
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

handler/gelf_test.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import (
88
"testing"
99
"time"
1010

11-
"bou.ke/monkey"
12-
13-
"github.com/stretchr/testify/assert"
14-
11+
"github.com/agiledragon/gomonkey/v2"
1512
"github.com/gol4ng/logger"
1613
"github.com/gol4ng/logger/handler"
1714
"github.com/gol4ng/logger/writer"
15+
"github.com/stretchr/testify/assert"
1816
)
1917

2018
func TestGelf(t *testing.T) {
@@ -35,13 +33,14 @@ func TestGelf(t *testing.T) {
3533
}
3634
for _, tt := range tests {
3735
t.Run(tt.name, func(t *testing.T) {
38-
monkey.Patch(tt.method, func(network, address string) logger.HandlerInterface {
36+
patch := gomonkey.NewPatches()
37+
patch.ApplyFunc(tt.method, func(network, address string) logger.HandlerInterface {
3938
assert.Equal(t, network, tt.network)
4039
assert.Equal(t, address, tt.address)
4140
return logger.NopHandler
4241
})
42+
defer patch.Reset()
4343
assert.IsType(t, logger.NopHandler, handler.Gelf(tt.network, tt.address))
44-
monkey.UnpatchAll()
4544
})
4645
}
4746
}
@@ -58,29 +57,31 @@ func (w *WrongConn) Read(_ []byte) (n int, err error) { return 0, nil }
5857
func (w *WrongConn) Write(_ []byte) (n int, err error) { return 0, nil }
5958
func (w *WrongConn) Close() error { return nil }
6059
func (w *WrongConn) LocalAddr() net.Addr { return nil }
61-
func (w *WrongConn) RemoteAddr() net.Addr { return nil }
62-
func (w *WrongConn) SetDeadline(_ time.Time) error { return nil }
63-
func (w *WrongConn) SetReadDeadline(_ time.Time) error { return nil }
60+
func (w *WrongConn) RemoteAddr() net.Addr { return nil }
61+
func (w *WrongConn) SetDeadline(_ time.Time) error { return nil }
62+
func (w *WrongConn) SetReadDeadline(_ time.Time) error { return nil }
6463
func (w *WrongConn) SetWriteDeadline(_ time.Time) error { return nil }
6564

6665
func TestGelfFromConnection(t *testing.T) {
6766

6867
t.Run("TestGelfFromConnection for TCPConn", func(t *testing.T) {
69-
monkey.Patch(handler.GelfTCPFromConnection, func(c *net.TCPConn) logger.HandlerInterface {
68+
patch := gomonkey.NewPatches()
69+
patch.ApplyFunc(handler.GelfTCPFromConnection, func(c *net.TCPConn) logger.HandlerInterface {
7070
//assert.Equal(t, conn, c)
7171
return logger.NopHandler
7272
})
73+
defer patch.Reset()
7374
assert.IsType(t, logger.NopHandler, handler.GelfFromConnection(&net.TCPConn{}))
74-
monkey.UnpatchAll()
7575
})
7676

7777
t.Run("TestGelfFromConnection for UDPConn", func(t *testing.T) {
78-
monkey.Patch(handler.GelfUDPFromConnection, func(c *net.UDPConn) logger.HandlerInterface {
78+
patch := gomonkey.NewPatches()
79+
patch.ApplyFunc(handler.GelfUDPFromConnection, func(c *net.UDPConn) logger.HandlerInterface {
7980
//assert.Equal(t, conn, c)
8081
return logger.NopHandler
8182
})
83+
defer patch.Reset()
8284
assert.IsType(t, logger.NopHandler, handler.GelfFromConnection(&net.UDPConn{}))
83-
monkey.UnpatchAll()
8485
})
8586
}
8687

@@ -93,24 +94,25 @@ func TestGelfFromConnection_withWrongConnection(t *testing.T) {
9394
func TestGelfTCP_Handle(t *testing.T) {
9495
var TCPAddr *net.TCPAddr
9596
var TCPConn *net.TCPConn
96-
monkey.Patch(net.ResolveTCPAddr, func(network, address string) (*net.TCPAddr, error) {
97+
patch := gomonkey.NewPatches()
98+
patch.ApplyFunc(net.ResolveTCPAddr, func(network, address string) (*net.TCPAddr, error) {
9799
assert.Equal(t, "fake_network", network)
98100
assert.Equal(t, "fake_address", address)
99101
return TCPAddr, nil
100102
})
101-
monkey.Patch(net.DialTCP, func(network string, laddr, raddr *net.TCPAddr) (*net.TCPConn, error) {
103+
patch.ApplyFunc(net.DialTCP, func(network string, laddr, raddr *net.TCPAddr) (*net.TCPConn, error) {
102104
assert.Equal(t, "fake_network", network)
103105
assert.Nil(t, laddr)
104106
assert.Equal(t, TCPAddr, raddr)
105107
return TCPConn, nil
106108
})
107-
monkey.PatchInstanceMethod(reflect.TypeOf(TCPConn), "Write", func(conn *net.TCPConn, b []byte) (n int, err error) {
109+
patch.ApplyMethod(reflect.TypeOf(TCPConn), "Write", func(conn *net.TCPConn, b []byte) (n int, err error) {
108110
assert.Equal(t, []byte(`{"version":"1.1","host":"my_fake_hostname","level":4,"timestamp":513216000.000,"short_message":"test message","full_message":"<warning> test message"}`+"\x00"), b)
109111
return 99, nil
110112
})
111-
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
112-
monkey.Patch(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
113-
defer monkey.UnpatchAll()
113+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(513216000, 0) })
114+
patch.ApplyFunc(os.Hostname, func() (string, error) { return "my_fake_hostname", nil })
115+
defer patch.Reset()
114116

115117
h := handler.GelfTCP("fake_network", "fake_address")
116118

@@ -122,33 +124,33 @@ func TestGelfUDP_Handle(t *testing.T) {
122124
var UDPAddr *net.UDPAddr
123125
var UDPConn *net.UDPConn
124126
var CompressWriter *writer.CompressWriter
125-
126-
monkey.Patch(net.ResolveUDPAddr, func(network, address string) (*net.UDPAddr, error) {
127+
patch := gomonkey.NewPatches()
128+
patch.ApplyFunc(net.ResolveUDPAddr, func(network, address string) (*net.UDPAddr, error) {
127129
assert.Equal(t, "fake_network", network)
128130
assert.Equal(t, "fake_address", address)
129131
return UDPAddr, nil
130132
})
131-
monkey.Patch(net.DialUDP, func(network string, laddr, raddr *net.UDPAddr) (*net.UDPConn, error) {
133+
patch.ApplyFunc(net.DialUDP, func(network string, laddr, raddr *net.UDPAddr) (*net.UDPConn, error) {
132134
assert.Equal(t, "fake_network", network)
133135
assert.Nil(t, laddr)
134136
assert.Equal(t, UDPAddr, raddr)
135137
return UDPConn, nil
136138
})
137-
monkey.Patch(writer.NewCompressWriter, func(w io.Writer, options ...writer.CompressOption) *writer.CompressWriter {
139+
patch.ApplyFunc(writer.NewCompressWriter, func(w io.Writer, options ...writer.CompressOption) *writer.CompressWriter {
138140
assert.IsType(t, &writer.GelfChunkWriter{}, w, "writer passed to CompressWriter must be a GelfCompressWriter")
139141
assert.Len(t, options, 2)
140142
return CompressWriter
141143
})
142-
monkey.PatchInstanceMethod(reflect.TypeOf(CompressWriter), "Write", func(w *writer.CompressWriter, p []byte) (int, error) {
144+
patch.ApplyMethod(reflect.TypeOf(CompressWriter), "Write", func(w *writer.CompressWriter, p []byte) (int, error) {
143145
assert.Equal(t, []byte(`{"version":"1.1","host":"`+osHostname+`","level":4,"timestamp":513216000.000,"short_message":"test message","full_message":"<warning> test message"}`+"\n"), p)
144146
return 99, nil
145147
})
146-
monkey.PatchInstanceMethod(reflect.TypeOf(UDPConn), "Write", func(conn *net.UDPConn, b []byte) (n int, err error) {
148+
patch.ApplyMethod(reflect.TypeOf(UDPConn), "Write", func(conn *net.UDPConn, b []byte) (n int, err error) {
147149
assert.Equal(t, []byte(`{"version":"1.1","host":"`+osHostname+`","level":4,"timestamp":513216000.000,"short_message":"test message","full_message":"<warning> test message"}`+"\n"), b)
148150
return 99, nil
149151
})
150-
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
151-
defer monkey.UnpatchAll()
152+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(513216000, 0) })
153+
defer patch.Reset()
152154

153155
h := handler.GelfUDP("fake_network", "fake_address")
154156
assert.Nil(t, h(logger.Entry{Message: "test message", Level: logger.WarningLevel, Context: nil}))

handler/rotate_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import (
77
"testing"
88
"time"
99

10-
"bou.ke/monkey"
11-
12-
"github.com/stretchr/testify/assert"
13-
"github.com/stretchr/testify/mock"
14-
10+
"github.com/agiledragon/gomonkey/v2"
1511
"github.com/gol4ng/logger"
1612
"github.com/gol4ng/logger/handler"
1713
"github.com/gol4ng/logger/mocks"
1814
"github.com/gol4ng/logger/writer"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/mock"
1917
)
2018

2119
func TestNewTimeRotateFileStream_Handle(t *testing.T) {
@@ -24,8 +22,9 @@ func TestNewTimeRotateFileStream_Handle(t *testing.T) {
2422
var f *os.File
2523
createdFile1 := &os.File{}
2624
createdFile2 := &os.File{}
25+
patch := gomonkey.NewPatches()
2726
// mock os.File::Write method
28-
monkey.PatchInstanceMethod(reflect.TypeOf(f), "Write", func(file *os.File, p []byte) (n int, err error) {
27+
patch.ApplyMethod(reflect.TypeOf(f), "Write", func(file *os.File, p []byte) (n int, err error) {
2928
if i == 0 {
3029
assert.Equal(t, createdFile1, file)
3130
assert.Equal(t, []byte("my formatter return\n"), p)
@@ -43,11 +42,11 @@ func TestNewTimeRotateFileStream_Handle(t *testing.T) {
4342
// on the second call of rotate, the TimeFileProvider will have a writer that is not nil (it will contain `createdFile1` that is technically nil
4443
// if we do not mock the `Close` method on `createdFile1` which will and up with a syscall.EINVAL error
4544
// as the code will pass here https://github.com/golang/go/blob/release-branch.go1.12/src/os/file_unix.go#L242
46-
monkey.PatchInstanceMethod(reflect.TypeOf(f), "Close", func(file *os.File) error {
45+
patch.ApplyMethod(reflect.TypeOf(f), "Close", func(file *os.File) error {
4746
return nil
4847
})
4948
// mock os.OpenFile method
50-
monkey.Patch(os.OpenFile, func(name string, flag int, perm os.FileMode) (*os.File, error) {
49+
patch.ApplyFunc(os.OpenFile, func(name string, flag int, perm os.FileMode) (*os.File, error) {
5150
if i == 0 {
5251
assert.Equal(t, "fake_format_Thu Jan 1 1970 00", name)
5352
assert.Equal(t, os.O_CREATE|os.O_APPEND|os.O_WRONLY, flag)
@@ -65,15 +64,16 @@ func TestNewTimeRotateFileStream_Handle(t *testing.T) {
6564
})
6665
// mock time.NewTicker method : override private ticker channel in order to be able to send ticks manually
6766
tickerChan := make(chan time.Time, 1)
68-
monkey.Patch(time.NewTicker, func(d time.Duration) *time.Ticker {
67+
patch.ApplyFunc(time.NewTicker, func(d time.Duration) *time.Ticker {
6968
assert.Equal(t, 10*time.Millisecond, d)
7069
return &time.Ticker{
7170
C: tickerChan,
7271
}
7372
})
7473
// mock time.Now method in order to return always the same time whenever the test is launched
75-
monkey.Patch(time.Now, func() time.Time { return time.Unix(i, 0) })
76-
defer monkey.UnpatchAll()
74+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(i, 0) })
75+
defer patch.Reset()
76+
7777
// mock a basic formatter that will return "my formatter return" on any call of `Format`
7878
mockFormatter := mocks.FormatterInterface{}
7979
mockFormatter.On("Format", mock.AnythingOfType("logger.Entry")).Return("my formatter return")
@@ -92,38 +92,40 @@ func TestNewTimeRotateFileStream_Handle(t *testing.T) {
9292

9393
func TestNewTimeRotateFileStream_Error(t *testing.T) {
9494
mockFormatter := mocks.FormatterInterface{}
95-
monkey.Patch(writer.NewTimeRotateFromProvider, func(provider writer.Provider, interval time.Duration) (*writer.TimeRotateWriter, error) {
95+
patch := gomonkey.NewPatches()
96+
patch.ApplyFunc(writer.NewTimeRotateFromProvider, func(provider writer.Provider, interval time.Duration) (*writer.TimeRotateWriter, error) {
9697
return nil, errors.New("my_fake_error")
9798
})
98-
defer monkey.UnpatchAll()
99+
defer patch.Reset()
99100

100101
_, err := handler.TimeRotateFileStream("fake_format_%s", "Mon Jan _2 2006 05", &mockFormatter, 10*time.Millisecond)
101102
assert.EqualError(t, err, "my_fake_error")
102103
}
103104

104105
func TestNewLogRotateFileStream_Handle(t *testing.T) {
105106
var f *os.File
106-
monkey.PatchInstanceMethod(reflect.TypeOf(f), "Write", func(_ *os.File, p []byte) (n int, err error) {
107+
patch := gomonkey.NewPatches()
108+
patch.ApplyMethod(reflect.TypeOf(f), "Write", func(_ *os.File, p []byte) (n int, err error) {
107109
assert.Equal(t, []byte("my formatter return\n"), p)
108110
return 99, nil
109111
})
110112

111113
file := os.File{}
112114

113-
monkey.Patch(os.OpenFile, func(name string, flag int, perm os.FileMode) (*os.File, error) {
115+
patch.ApplyFunc(os.OpenFile, func(name string, flag int, perm os.FileMode) (*os.File, error) {
114116
assert.Equal(t, "fake_format_test", name)
115117
assert.Equal(t, os.O_CREATE|os.O_APPEND|os.O_WRONLY, flag)
116118
assert.Equal(t, os.FileMode(0666), perm)
117119
return &file, nil
118120
})
119121

120-
monkey.Patch(os.Rename, func(oldpath, newpath string) error {
122+
patch.ApplyFunc(os.Rename, func(oldpath, newpath string) error {
121123
assert.Equal(t, "fake_format_test", oldpath)
122124
assert.Equal(t, "fake_format_Mon Apr 7 1986", newpath)
123125
return nil
124126
})
125-
monkey.Patch(time.Now, func() time.Time { return time.Unix(513216000, 0) })
126-
defer monkey.UnpatchAll()
127+
patch.ApplyFunc(time.Now, func() time.Time { return time.Unix(513216000, 0) })
128+
defer patch.Reset()
127129

128130
mockFormatter := mocks.FormatterInterface{}
129131
mockFormatter.On("Format", mock.AnythingOfType("logger.Entry")).Return("my formatter return")
@@ -138,10 +140,11 @@ func TestNewLogRotateFileStream_Handle(t *testing.T) {
138140

139141
func TestNewLogRotateFileStream_Error(t *testing.T) {
140142
mockFormatter := mocks.FormatterInterface{}
141-
monkey.Patch(writer.NewTimeRotateFromProvider, func(provider writer.Provider, interval time.Duration) (*writer.TimeRotateWriter, error) {
143+
patch := gomonkey.NewPatches()
144+
patch.ApplyFunc(writer.NewTimeRotateFromProvider, func(provider writer.Provider, interval time.Duration) (*writer.TimeRotateWriter, error) {
142145
return nil, errors.New("my_fake_error")
143146
})
144-
defer monkey.UnpatchAll()
147+
defer patch.Reset()
145148

146149
_, err := handler.LogRotateFileStream("test", "fake_format_%s", "Mon Jan _2 2006", &mockFormatter, 100*time.Millisecond)
147150
assert.EqualError(t, err, "my_fake_error")

0 commit comments

Comments
 (0)