Move testlib to internal

This commit is contained in:
9seconds
2021-04-02 21:32:06 +03:00
parent 2fe04c7c9b
commit 24fdec6694
19 changed files with 14 additions and 14 deletions
+42
View File
@@ -0,0 +1,42 @@
package testlib
import (
"bytes"
"io"
"os"
"strings"
)
func CaptureStdout(callback func()) string {
return captureOutput(&os.Stdout, callback)
}
func CaptureStderr(callback func()) string {
return captureOutput(&os.Stderr, callback)
}
func captureOutput(filefp **os.File, callback func()) string {
oldFp := *filefp
defer func() {
*filefp = oldFp
}()
reader, writer, _ := os.Pipe()
buf := &bytes.Buffer{}
closeChan := make(chan bool)
go func() {
io.Copy(buf, reader) // nolint: errcheck
close(closeChan)
}()
*filefp = writer
callback()
writer.Close()
<-closeChan
return strings.TrimSpace(buf.String())
}
+7
View File
@@ -0,0 +1,7 @@
package testlib
import "github.com/stretchr/testify/mock"
type EventsObserverMock struct {
mock.Mock
}
@@ -0,0 +1,11 @@
package testlib
import "github.com/stretchr/testify/mock"
type MtglibAntiReplayCacheMock struct {
mock.Mock
}
func (m *MtglibAntiReplayCacheMock) SeenBefore(data []byte) bool {
return m.Called(data).Bool(0)
}
+30
View File
@@ -0,0 +1,30 @@
package testlib
import (
"context"
"net"
"net/http"
"github.com/stretchr/testify/mock"
)
type MtglibNetworkMock struct {
mock.Mock
}
func (m *MtglibNetworkMock) Dial(network, address string) (net.Conn, error) {
args := m.Called(network, address)
return args.Get(0).(net.Conn), args.Error(1)
}
func (m *MtglibNetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
args := m.Called(ctx, network, address)
return args.Get(0).(net.Conn), args.Error(1)
}
func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
network, address string) (net.Conn, error)) *http.Client {
return m.Called(dialFunc).Get(0).(*http.Client)
}
+48
View File
@@ -0,0 +1,48 @@
package testlib
import (
"net"
"time"
"github.com/stretchr/testify/mock"
)
type NetConnMock struct {
mock.Mock
}
func (n *NetConnMock) Read(b []byte) (int, error) {
args := n.Called(b)
return args.Int(0), args.Error(1)
}
func (n *NetConnMock) Write(b []byte) (int, error) {
args := n.Called(b)
return args.Int(0), args.Error(1)
}
func (n *NetConnMock) Close() error {
return n.Called().Error(0)
}
func (n *NetConnMock) LocalAddr() net.Addr {
return n.Called().Get(0).(net.Addr)
}
func (n *NetConnMock) RemoteAddr() net.Addr {
return n.Called().Get(0).(net.Addr)
}
func (n *NetConnMock) SetDeadline(t time.Time) error {
return n.Called(t).Error(0)
}
func (n *NetConnMock) SetReadDeadline(t time.Time) error {
return n.Called(t).Error(0)
}
func (n *NetConnMock) SetWriteDeadline(t time.Time) error {
return n.Called(t).Error(0)
}