mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
refactor: rename keeltest folder
This commit is contained in:
parent
317647db12
commit
686ef11205
1
.github/dependabot.yml
vendored
1
.github/dependabot.yml
vendored
@ -1,4 +1,5 @@
|
||||
version: 2
|
||||
|
||||
updates:
|
||||
- package-ecosystem: github-actions
|
||||
directory: '/'
|
||||
|
||||
1
go.mod
1
go.mod
@ -38,6 +38,7 @@ require (
|
||||
go.temporal.io/api v1.8.0
|
||||
go.temporal.io/sdk v1.15.0
|
||||
go.temporal.io/sdk/contrib/opentelemetry v0.1.0
|
||||
go.uber.org/goleak v1.3.0
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/crypto v0.22.0
|
||||
golang.org/x/sync v0.7.0
|
||||
|
||||
@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/foomo/keel/keeltest"
|
||||
"github.com/foomo/keel/log"
|
||||
keeltestutil "github.com/foomo/keel/test/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/pretty"
|
||||
)
|
||||
@ -14,7 +14,7 @@ import (
|
||||
func InlineEqual(t *testing.T, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
t.Helper()
|
||||
|
||||
expected, ok := keeltestutil.Inline(t, 2, "%v", actual)
|
||||
expected, ok := keeltest.Inline(t, 2, "%v", actual)
|
||||
if ok {
|
||||
return assert.Equal(t, expected, fmt.Sprintf("%v", actual), msgAndArgs...)
|
||||
} else {
|
||||
@ -30,7 +30,7 @@ func InlineJSONEq(t *testing.T, actual interface{}, msgAndArgs ...interface{}) b
|
||||
t.Fatal("failed to marshal json", log.FError(err))
|
||||
}
|
||||
|
||||
expected, ok := keeltestutil.Inline(t, 2, string(actualBytes))
|
||||
expected, ok := keeltest.Inline(t, 2, string(actualBytes))
|
||||
if ok {
|
||||
return assert.Equal(t, string(pretty.Pretty([]byte(expected))), string(pretty.Pretty(actualBytes)), msgAndArgs...)
|
||||
} else {
|
||||
@ -3,7 +3,7 @@ package keelassert_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
keelassert "github.com/foomo/keel/test/assert"
|
||||
keelassert "github.com/foomo/keel/keeltest/assert"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/foomo/keel/keeltest"
|
||||
"github.com/foomo/keel/log"
|
||||
keeltestutil "github.com/foomo/keel/test/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/pretty"
|
||||
)
|
||||
@ -26,7 +26,7 @@ func New(t *testing.T) *Assertions { //nolint:thelper
|
||||
|
||||
func (a *Assertions) InlineEqual(actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
a.t.Helper()
|
||||
expected, ok := keeltestutil.Inline(a.t, 2, "%v", actual)
|
||||
expected, ok := keeltest.Inline(a.t, 2, "%v", actual)
|
||||
if ok {
|
||||
return assert.Equal(a.t, expected, fmt.Sprintf("%v", actual), msgAndArgs...)
|
||||
} else {
|
||||
@ -42,7 +42,7 @@ func (a *Assertions) InlineJSONEq(actual interface{}, msgAndArgs ...interface{})
|
||||
a.t.Fatal("failed to marshal json", log.FError(err))
|
||||
}
|
||||
|
||||
expected, ok := keeltestutil.Inline(a.t, 2, string(actualBytes))
|
||||
expected, ok := keeltest.Inline(a.t, 2, string(actualBytes))
|
||||
if ok {
|
||||
return assert.Equal(a.t, string(pretty.Pretty([]byte(expected))), string(pretty.Pretty(actualBytes)), msgAndArgs...)
|
||||
} else {
|
||||
@ -1,4 +1,4 @@
|
||||
package keeltestutil
|
||||
package keeltest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@ -1,27 +1,30 @@
|
||||
package keeltestutil_test
|
||||
package keeltest_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
keeltestutil "github.com/foomo/keel/test/util"
|
||||
"github.com/foomo/keel/keeltest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
|
||||
func TestInline(t *testing.T) {
|
||||
defer goleak.VerifyNone(t)
|
||||
|
||||
t.Run("read inline", func(t *testing.T) {
|
||||
value, ok := keeltestutil.Inline(t, 1) // INLINE: hello world
|
||||
value, ok := keeltest.Inline(t, 1) // INLINE: hello world
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "hello world", value)
|
||||
})
|
||||
|
||||
t.Run("read inline int", func(t *testing.T) {
|
||||
value, ok := keeltestutil.InlineInt(t, 1) // INLINE: 1
|
||||
value, ok := keeltest.InlineInt(t, 1) // INLINE: 1
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, value)
|
||||
})
|
||||
|
||||
t.Run("read inline float", func(t *testing.T) {
|
||||
value, ok := keeltestutil.InlineFloat64(t, 1) // INLINE: 1.5
|
||||
value, ok := keeltest.InlineFloat64(t, 1) // INLINE: 1.5
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1.5, value)
|
||||
})
|
||||
@ -30,12 +33,12 @@ func TestInline(t *testing.T) {
|
||||
var x struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
keeltestutil.InlineJSON(t, 1, &x) // INLINE: {"foo":"bar"}
|
||||
keeltest.InlineJSON(t, 1, &x) // INLINE: {"foo":"bar"}
|
||||
assert.Equal(t, "bar", x.Foo)
|
||||
})
|
||||
|
||||
t.Run("write inline", func(t *testing.T) {
|
||||
value, ok := keeltestutil.Inline(t, 1, "hello %s", "world") // INLINE: hello world
|
||||
value, ok := keeltest.Inline(t, 1, "hello %s", "world") // INLINE: hello world
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "hello world", value)
|
||||
})
|
||||
@ -5,8 +5,8 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/foomo/keel/keeltest"
|
||||
"github.com/foomo/keel/log"
|
||||
keeltestutil "github.com/foomo/keel/test/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/pretty"
|
||||
)
|
||||
@ -26,7 +26,7 @@ func New(t *testing.T) *Assertions { //nolint:thelper
|
||||
|
||||
func (a *Assertions) InlineEqual(actual interface{}, msgAndArgs ...interface{}) {
|
||||
a.t.Helper()
|
||||
if expected, ok := keeltestutil.Inline(a.t, 2, "%v", actual); ok {
|
||||
if expected, ok := keeltest.Inline(a.t, 2, "%v", actual); ok {
|
||||
require.Equal(a.t, expected, fmt.Sprintf("%v", actual), msgAndArgs...)
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,7 @@ func (a *Assertions) InlineJSONEq(actual interface{}, msgAndArgs ...interface{})
|
||||
if err != nil {
|
||||
a.t.Fatal("failed to marshal json", log.FError(err))
|
||||
}
|
||||
if expected, ok := keeltestutil.Inline(a.t, 2, string(actualBytes)); ok {
|
||||
if expected, ok := keeltest.Inline(a.t, 2, string(actualBytes)); ok {
|
||||
require.Equal(a.t, string(pretty.Pretty([]byte(expected))), string(pretty.Pretty(actualBytes)), msgAndArgs...)
|
||||
}
|
||||
}
|
||||
@ -5,15 +5,15 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/foomo/keel/keeltest"
|
||||
"github.com/foomo/keel/log"
|
||||
keeltestutil "github.com/foomo/keel/test/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/pretty"
|
||||
)
|
||||
|
||||
func InlineEqual(t *testing.T, actual interface{}, msgAndArgs ...interface{}) {
|
||||
t.Helper()
|
||||
if expected, ok := keeltestutil.Inline(t, 2, "%v", actual); ok {
|
||||
if expected, ok := keeltest.Inline(t, 2, "%v", actual); ok {
|
||||
require.Equal(t, expected, fmt.Sprintf("%v", actual), msgAndArgs...)
|
||||
}
|
||||
}
|
||||
@ -25,7 +25,7 @@ func InlineJSONEq(t *testing.T, actual interface{}, msgAndArgs ...interface{}) {
|
||||
if err != nil {
|
||||
t.Fatal("failed to marshal json", log.FError(err))
|
||||
}
|
||||
if expected, ok := keeltestutil.Inline(t, 2, string(actualBytes)); ok {
|
||||
if expected, ok := keeltest.Inline(t, 2, string(actualBytes)); ok {
|
||||
require.Equal(t, string(pretty.Pretty([]byte(expected))), string(pretty.Pretty(actualBytes)), msgAndArgs...)
|
||||
}
|
||||
}
|
||||
@ -5,9 +5,9 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
keelassert "github.com/foomo/keel/keeltest/assert"
|
||||
keelhttp "github.com/foomo/keel/net/http"
|
||||
"github.com/foomo/keel/net/http/middleware"
|
||||
keelassert "github.com/foomo/keel/test/assert"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
|
||||
@ -4,10 +4,10 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/foomo/keel/keeltest"
|
||||
"github.com/foomo/keel/log"
|
||||
httplog "github.com/foomo/keel/net/http/log"
|
||||
"github.com/foomo/keel/net/http/middleware"
|
||||
keeltest "github.com/foomo/keel/test"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
|
||||
@ -13,10 +13,11 @@ import (
|
||||
"github.com/foomo/keel/service"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/goleak"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"github.com/foomo/keel"
|
||||
keeltest "github.com/foomo/keel/test"
|
||||
)
|
||||
|
||||
type KeelTestSuite struct {
|
||||
@ -29,12 +30,12 @@ type KeelTestSuite struct {
|
||||
|
||||
// SetupSuite hook
|
||||
func (s *KeelTestSuite) SetupSuite() {
|
||||
s.l = keeltest.NewLogger(s.T()).Zap()
|
||||
s.l = zaptest.NewLogger(s.T())
|
||||
}
|
||||
|
||||
// BeforeTest hook
|
||||
func (s *KeelTestSuite) BeforeTest(suiteName, testName string) {
|
||||
s.l = keeltest.NewLogger(s.T()).Zap()
|
||||
s.l = zaptest.NewLogger(s.T())
|
||||
s.mux = http.NewServeMux()
|
||||
s.mux.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@ -74,7 +75,9 @@ func (s *KeelTestSuite) AfterTest(suiteName, testName string) {
|
||||
}
|
||||
|
||||
// TearDownSuite hook
|
||||
func (s *KeelTestSuite) TearDownSuite() {}
|
||||
func (s *KeelTestSuite) TearDownSuite() {
|
||||
goleak.VerifyNone(s.T())
|
||||
}
|
||||
|
||||
func (s *KeelTestSuite) TestServiceHTTP() {
|
||||
s.svr.AddServices(
|
||||
|
||||
105
test/log.go
105
test/log.go
@ -1,105 +0,0 @@
|
||||
package keeltest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
type (
|
||||
Logger struct {
|
||||
zap *zap.Logger
|
||||
writer *testingWriter
|
||||
}
|
||||
LoggerOptions struct {
|
||||
Level zapcore.LevelEnabler
|
||||
zapOptions []zap.Option
|
||||
}
|
||||
LoggerOption func(*LoggerOptions)
|
||||
)
|
||||
|
||||
// LoggerWithLevel adds zap.Option's to a test Logger built by NewLogger.
|
||||
func LoggerWithLevel(o zapcore.LevelEnabler) LoggerOption {
|
||||
return func(v *LoggerOptions) {
|
||||
v.Level = o
|
||||
}
|
||||
}
|
||||
|
||||
// LoggerWithZapOptions adds zap.Option's to a test Logger built by NewLogger.
|
||||
func LoggerWithZapOptions(o ...zap.Option) LoggerOption {
|
||||
return func(v *LoggerOptions) {
|
||||
v.zapOptions = o
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogger(t zaptest.TestingT, opts ...LoggerOption) *Logger {
|
||||
cfg := LoggerOptions{
|
||||
Level: zapcore.DebugLevel,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(&cfg)
|
||||
}
|
||||
|
||||
writer := newTestingWriter(t)
|
||||
zapOptions := []zap.Option{
|
||||
zap.AddCaller(),
|
||||
// zap.AddCallerSkip(1),
|
||||
// Send zap errors to the same writer and mark the test as failed if that happens.
|
||||
zap.ErrorOutput(writer),
|
||||
}
|
||||
zapOptions = append(zapOptions, cfg.zapOptions...)
|
||||
|
||||
e := zap.NewDevelopmentEncoderConfig()
|
||||
e.TimeKey = ""
|
||||
e.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
e.EncodeCaller = func(s zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(37), s.TrimmedPath()))
|
||||
}
|
||||
return &Logger{
|
||||
zap: zap.New(
|
||||
zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(e),
|
||||
writer,
|
||||
cfg.Level,
|
||||
),
|
||||
zapOptions...,
|
||||
),
|
||||
writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) T(t zaptest.TestingT) *Logger {
|
||||
l.writer.t = t
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *Logger) Zap() *zap.Logger {
|
||||
return l.zap
|
||||
}
|
||||
|
||||
// testingWriter is a WriteSyncer that writes to the given testing.TB.
|
||||
type testingWriter struct {
|
||||
t zaptest.TestingT
|
||||
}
|
||||
|
||||
func newTestingWriter(t zaptest.TestingT) *testingWriter {
|
||||
return &testingWriter{t: t}
|
||||
}
|
||||
|
||||
func (w *testingWriter) Write(p []byte) (int, error) {
|
||||
if w.t == nil {
|
||||
return fmt.Printf("%s", p) //nolint:forbidigo
|
||||
} else {
|
||||
// Note: t.Log is safe for concurrent use.
|
||||
// Strip trailing newline because t.Log always adds one.
|
||||
w.t.Logf("%s", bytes.TrimRight(p, "\n"))
|
||||
return len(p), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (w *testingWriter) Sync() error {
|
||||
return nil
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user