Skip to content

Commit

Permalink
Use testify assert in log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed May 23, 2023
1 parent dc431ca commit c459d13
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 254 deletions.
77 changes: 20 additions & 57 deletions log/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/aws/smithy-go/logging"
"github.com/stretchr/testify/assert"
)

func TestAsGoaMiddlwareLogger(t *testing.T) {
Expand All @@ -19,9 +20,7 @@ func TestAsGoaMiddlwareLogger(t *testing.T) {
logger := AsGoaMiddlewareLogger(ctx)
logger.Log("msg", "hello world")
want := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)
}

func TestAsStdLogger(t *testing.T) {
Expand All @@ -35,36 +34,26 @@ func TestAsStdLogger(t *testing.T) {

logger.Print("hello world")
want := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)

buf.Reset()
logger.Println("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\\n\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)

buf.Reset()
logger.Printf("hello %s", "world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)

func() {
buf.Reset()
var msg string
defer func() { msg = recover().(string) }()
logger.Panic("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
if msg != "hello world" {
t.Errorf("got %q, want %q", msg, "hello world")
}
assert.Equal(t, buf.String(), want)
assert.Equal(t, msg, "hello world")
}()

func() {
Expand All @@ -73,12 +62,8 @@ func TestAsStdLogger(t *testing.T) {
defer func() { msg = recover().(string) }()
logger.Panicf("hello %s", "world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
if msg != "hello world" {
t.Errorf("got %q, want %q", msg, "hello world")
}
assert.Equal(t, buf.String(), want)
assert.Equal(t, msg, "hello world")
}()

func() {
Expand All @@ -87,12 +72,8 @@ func TestAsStdLogger(t *testing.T) {
defer func() { msg = recover().(string) }()
logger.Panicln("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\\n\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
if msg != "hello world\n" {
t.Errorf("got %q, want %q", msg, "hello world\n")
}
assert.Equal(t, buf.String(), want)
assert.Equal(t, msg, "hello world")
}()

osExitFunc := osExit
Expand All @@ -105,34 +86,22 @@ func TestAsStdLogger(t *testing.T) {
buf.Reset()
logger.Fatal("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
if exited != 1 {
t.Errorf("got %d, want %d", exited, 1)
}
assert.Equal(t, buf.String(), want)
assert.Equal(t, exited, 1)

exited = 0
buf.Reset()
logger.Fatalf("hello %s", "world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
if exited != 1 {
t.Errorf("got %d, want %d", exited, 1)
}
assert.Equal(t, buf.String(), want)
assert.Equal(t, exited, 1)

exited = 0
buf.Reset()
logger.Fatalln("hello world")
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello world\\n\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
if exited != 1 {
t.Errorf("got %d, want %d", exited, 1)
}
assert.Equal(t, buf.String(), want)
assert.Equal(t, exited, 1)
}

type ctxkey string
Expand All @@ -147,24 +116,18 @@ func TestAsAWSLogger(t *testing.T) {

logger.Logf(logging.Classification("INFO"), "hello %s", "world")
want := "time=2022-01-09T20:29:45Z level=info msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)

buf.Reset()
logger.Logf(logging.Classification("DEBUG"), "hello world")
want = "time=2022-01-09T20:29:45Z level=debug msg=\"hello world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)

buf.Reset()
key := ctxkey("key")
pctx := context.WithValue(context.Background(), key, "small")
logger = logger.(logging.ContextLogger).WithContext(pctx)
logger.Logf(logging.Classification("INFO"), "hello %v world", logger.(*AWSLogger).Context.Value(key))
want = "time=2022-01-09T20:29:45Z level=info msg=\"hello small world\"\n"
if buf.String() != want {
t.Errorf("got %q, want %q", buf.String(), want)
}
assert.Equal(t, buf.String(), want)
}
10 changes: 4 additions & 6 deletions log/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package log
import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDebugEnabled(t *testing.T) {
ctx := Context(context.Background())
if DebugEnabled(ctx) {
t.Errorf("expected debug logs to be disabled")
}
assert.False(t, DebugEnabled(ctx), "expected debug logs to be disabled")
ctx = Context(ctx, WithDebug())
if !DebugEnabled(ctx) {
t.Errorf("expected debug logs to be enabled")
}
assert.True(t, DebugEnabled(ctx), "expected debug logs to be enabled")
}
23 changes: 8 additions & 15 deletions log/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestFormat(t *testing.T) {
Expand Down Expand Up @@ -202,10 +204,7 @@ func TestFormat(t *testing.T) {
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithFormat(tc.format), WithDebug())
tc.logfn(ctx, kvList(tc.keyVals))
got := buf.String()
if got != tc.want {
t.Errorf("got %s, want %s", got, tc.want)
}
assert.Equal(t, tc.want, buf.String())
})
}

Expand All @@ -221,32 +220,29 @@ func TestFormat(t *testing.T) {
logfn: Error,
format: FormatText,
keyVals: keyVals,
want: "time=2022-01-09T20:29:45Z level=error " + formattedKeyVals + " err=error\n",
want: "time=2022-01-09T20:29:45Z level=error err=error " + formattedKeyVals + "\n",
},
{
name: "colored error",
logfn: Error,
format: FormatTerminal,
keyVals: keyVals,
want: "\033[1;31mERRO\033[0m[0000] " + coloredKeyVals("\033[1;31m") + " \033[1;31merr\033[0m=error\n",
want: "\033[1;31mERRO\033[0m[0000] " + "\033[1;31merr\033[0m=error " + coloredKeyVals("\033[1;31m") + "\n",
},
{
name: "json info",
logfn: Error,
format: FormatJSON,
keyVals: keyVals,
want: `{"time":"2022-01-09T20:29:45Z","level":"error",` + jsonKeyVals + `,"err":"error"}` + "\n",
want: `{"time":"2022-01-09T20:29:45Z","level":"error","err":"error",` + jsonKeyVals + `}` + "\n",
},
}
for _, tc := range errorCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithFormat(tc.format), WithDebug())
tc.logfn(ctx, errors.New("error"), kvList(tc.keyVals))
got := buf.String()
if got != tc.want {
t.Errorf("got %s, want %s", got, tc.want)
}
assert.Equal(t, tc.want, buf.String())
})
}

Expand Down Expand Up @@ -290,10 +286,7 @@ func TestFormat(t *testing.T) {
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithFormat(tc.format), WithDebug())
tc.logfn(ctx, kvList(tc.keyVals))
got := buf.String()
if got != tc.want {
t.Errorf("got %s, want %s", got, tc.want)
}
assert.Equal(t, tc.want, buf.String())
})
}
}
21 changes: 7 additions & 14 deletions log/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"goa.design/clue/internal/testsvc"
grpcmiddleware "goa.design/goa/v3/grpc/middleware"
"goa.design/goa/v3/middleware"
Expand Down Expand Up @@ -41,9 +42,7 @@ func TestUnaryServerInterceptor(t *testing.T) {
`"key1":"value1"`,
`"key2":"value2"`)

if buf.String() != expected {
t.Errorf("got %s, want %s", buf.String(), expected)
}
assert.Equal(t, expected, buf.String())
}

func TestStreamServerTrace(t *testing.T) {
Expand Down Expand Up @@ -80,15 +79,13 @@ func TestStreamServerTrace(t *testing.T) {
`"key1":"value1"`,
`"key2":"value2"`)

if buf.String() != expected {
t.Errorf("got %s, want %s", buf.String(), expected)
}
assert.Equal(t, expected, buf.String())
}

func TestUnaryClientInterceptor(t *testing.T) {
successLogs := `time=2022-01-09T20:29:45Z level=info msg="finished client unary call" grpc.service=test.Test grpc.method=GrpcMethod grpc.code=OK grpc.time_ms=42`
errorLogs := `time=2022-01-09T20:29:45Z level=error msg="finished client unary call" grpc.service=test.Test grpc.method=GrpcMethod grpc.status=error grpc.code=Unknown grpc.time_ms=42 err="rpc error: code = Unknown desc = error"`
statusLogs := `time=2022-01-09T20:29:45Z level=error msg="finished client unary call" grpc.service=test.Test grpc.method=GrpcMethod grpc.status=error grpc.code=Unknown grpc.time_ms=42 err="rpc error: code = Unknown desc = error"`
errorLogs := `time=2022-01-09T20:29:45Z level=error err="rpc error: code = Unknown desc = error" msg="finished client unary call" grpc.service=test.Test grpc.method=GrpcMethod grpc.status=error grpc.code=Unknown grpc.time_ms=42`
statusLogs := `time=2022-01-09T20:29:45Z level=error err="rpc error: code = Unknown desc = error" msg="finished client unary call" grpc.service=test.Test grpc.method=GrpcMethod grpc.status=error grpc.code=Unknown grpc.time_ms=42`
cases := []struct {
name string
noLog bool
Expand Down Expand Up @@ -128,9 +125,7 @@ func TestUnaryClientInterceptor(t *testing.T) {
cli.GRPCMethod(ctx, &testsvc.Fields{})
stop()

if strings.TrimSpace(buf.String()) != c.expected {
t.Errorf("got:\n%s\nwant:\n%s", strings.TrimSpace(buf.String()), c.expected)
}
assert.Equal(t, strings.TrimSpace(buf.String()), c.expected)
})
}
}
Expand Down Expand Up @@ -170,9 +165,7 @@ func TestStreamClientInterceptor(t *testing.T) {
}
stop()

if strings.TrimSpace(buf.String()) != c.expected {
t.Errorf("got:\n%s\nwant:\n%s", strings.TrimSpace(buf.String()), c.expected)
}
assert.Equal(t, strings.TrimSpace(buf.String()), c.expected)
})
}
}
Expand Down
21 changes: 7 additions & 14 deletions log/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"goa.design/goa/v3/middleware"
goa "goa.design/goa/v3/pkg"
)
Expand Down Expand Up @@ -41,9 +42,7 @@ func TestHTTP(t *testing.T) {
`"key1":"value1"`,
`"key2":"value2"`)

if buf.String() != expected {
t.Errorf("got %s, want %s", buf.String(), expected)
}
assert.Equal(t, expected, buf.String())
}

func TestEndpoint(t *testing.T) {
Expand Down Expand Up @@ -78,17 +77,15 @@ func TestEndpoint(t *testing.T) {

Endpoint(endpoint)(ctx, nil)

if buf.String() != c.expected+"\n" {
t.Errorf("got %s, want %s", buf.String(), c.expected)
}
assert.Equal(t, c.expected+"\n", buf.String())
})
}
}

func TestClient(t *testing.T) {
successLogs := `time=2022-01-09T20:29:45Z level=info msg="finished client HTTP request" http.method=GET http.url=$URL http.status="200 OK" http.time_ms=42`
errorLogs := `time=2022-01-09T20:29:45Z level=error msg="finished client HTTP request" http.method=GET http.url=$URL err=error`
statusLogs := `time=2022-01-09T20:29:45Z level=error msg="finished client HTTP request" http.method=GET http.url=$URL http.status="200 OK" http.time_ms=42 err="200 OK"`
errorLogs := `time=2022-01-09T20:29:45Z level=error err=error msg="finished client HTTP request" http.method=GET http.url=$URL`
statusLogs := `time=2022-01-09T20:29:45Z level=error err="200 OK" msg="finished client HTTP request" http.method=GET http.url=$URL http.status="200 OK" http.time_ms=42`
cases := []struct {
name string
noLog bool
Expand Down Expand Up @@ -133,9 +130,7 @@ func TestClient(t *testing.T) {
client.Do(req)

expected := strings.ReplaceAll(c.expected, "$URL", server.URL)
if strings.TrimSpace(buf.String()) != expected {
t.Errorf("got:\n%q\nwant:\n%q", strings.TrimSpace(buf.String()), expected)
}
assert.Equal(t, strings.TrimSpace(buf.String()), expected)
})
}
}
Expand All @@ -153,9 +148,7 @@ func TestWithPathFilter(t *testing.T) {

handler.ServeHTTP(nil, req)

if buf.String() != "" {
t.Errorf("got %s, want empty", buf.String())
}
assert.Empty(t, buf.String())
}

type errorClient struct {
Expand Down
Loading

0 comments on commit c459d13

Please sign in to comment.