diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 61d3a33..e82631a 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,4 +1,5 @@ version: 2 + updates: - package-ecosystem: github-actions directory: '/' diff --git a/go.mod b/go.mod index a833e6e..5e5a2a4 100644 --- a/go.mod +++ b/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 diff --git a/test/assert/assert.go b/keeltest/assert/assert.go similarity index 81% rename from test/assert/assert.go rename to keeltest/assert/assert.go index e024345..08333b5 100644 --- a/test/assert/assert.go +++ b/keeltest/assert/assert.go @@ -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 { diff --git a/test/assert/assert_test.go b/keeltest/assert/assert_test.go similarity index 95% rename from test/assert/assert_test.go rename to keeltest/assert/assert_test.go index 6d86152..4121070 100644 --- a/test/assert/assert_test.go +++ b/keeltest/assert/assert_test.go @@ -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" ) diff --git a/test/assert/assertions.go b/keeltest/assert/assertions.go similarity index 85% rename from test/assert/assertions.go rename to keeltest/assert/assertions.go index 6abcb76..cb37301 100644 --- a/test/assert/assertions.go +++ b/keeltest/assert/assertions.go @@ -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 { diff --git a/test/client.go b/keeltest/client.go similarity index 100% rename from test/client.go rename to keeltest/client.go diff --git a/test/util/inline.go b/keeltest/inline.go similarity index 99% rename from test/util/inline.go rename to keeltest/inline.go index b5b8532..53357c6 100644 --- a/test/util/inline.go +++ b/keeltest/inline.go @@ -1,4 +1,4 @@ -package keeltestutil +package keeltest import ( "encoding/json" diff --git a/test/util/inline_test.go b/keeltest/inline_test.go similarity index 61% rename from test/util/inline_test.go rename to keeltest/inline_test.go index 28944a8..c492dc4 100644 --- a/test/util/inline_test.go +++ b/keeltest/inline_test.go @@ -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) }) diff --git a/test/option.go b/keeltest/option.go similarity index 100% rename from test/option.go rename to keeltest/option.go diff --git a/test/require/assertions.go b/keeltest/require/assertions.go similarity index 83% rename from test/require/assertions.go rename to keeltest/require/assertions.go index a9956e5..0e43989 100644 --- a/test/require/assertions.go +++ b/keeltest/require/assertions.go @@ -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...) } } diff --git a/test/require/require.go b/keeltest/require/require.go similarity index 78% rename from test/require/require.go rename to keeltest/require/require.go index c26a819..5d38172 100644 --- a/test/require/require.go +++ b/keeltest/require/require.go @@ -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...) } } diff --git a/test/server.go b/keeltest/server.go similarity index 100% rename from test/server.go rename to keeltest/server.go diff --git a/test/service.go b/keeltest/service.go similarity index 100% rename from test/service.go rename to keeltest/service.go diff --git a/test/servicehttp.go b/keeltest/servicehttp.go similarity index 100% rename from test/servicehttp.go rename to keeltest/servicehttp.go diff --git a/net/http/middleware/cors_test.go b/net/http/middleware/cors_test.go index 67b671e..f1b15d1 100644 --- a/net/http/middleware/cors_test.go +++ b/net/http/middleware/cors_test.go @@ -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" ) diff --git a/net/http/middleware/logger_test.go b/net/http/middleware/logger_test.go index 78dd46c..386c5f3 100644 --- a/net/http/middleware/logger_test.go +++ b/net/http/middleware/logger_test.go @@ -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" ) diff --git a/server_test.go b/server_test.go index 0d5bd44..d90ff9c 100644 --- a/server_test.go +++ b/server_test.go @@ -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( diff --git a/test/log.go b/test/log.go deleted file mode 100644 index 82d2b5f..0000000 --- a/test/log.go +++ /dev/null @@ -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 -}