mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 15:44:01 +03:00
Move testlib to internal
This commit is contained in:
@@ -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())
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user