Skip to content

Commit

Permalink
Use test logger for client handler (#1178)
Browse files Browse the repository at this point in the history
This protects against logging in tests after they finish

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 authored Oct 7, 2024
1 parent b751858 commit 69fb6ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
12 changes: 8 additions & 4 deletions internal/lsp/server_aggregates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ import rego.v1

messages := createMessageChannels(files)

clientHandler := createClientHandler(t, messages)
logger := newTestLogger(t)

clientHandler := createClientHandler(t, logger, messages)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

tempDir := t.TempDir()

_, connClient, err := createAndInitServer(ctx, newTestLogger(t), tempDir, files, clientHandler)
_, connClient, err := createAndInitServer(ctx, logger, tempDir, files, clientHandler)
if err != nil {
t.Fatalf("failed to create and init language server: %s", err)
}
Expand Down Expand Up @@ -339,13 +341,15 @@ import rego.v1

messages := createMessageChannels(files)

clientHandler := createClientHandler(t, messages)
logger := newTestLogger(t)

clientHandler := createClientHandler(t, logger, messages)

// set up the server and client connections
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, connClient, err := createAndInitServer(ctx, newTestLogger(t), tempDir, files, clientHandler)
_, connClient, err := createAndInitServer(ctx, logger, tempDir, files, clientHandler)
if err != nil {
t.Fatalf("failed to create and init language server: %s", err)
}
Expand Down
6 changes: 4 additions & 2 deletions internal/lsp/server_multi_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ ignore:
`,
}

logger := newTestLogger(t)

messages := createMessageChannels(files)

clientHandler := createClientHandler(t, messages)
clientHandler := createClientHandler(t, logger, messages)

// set up the server and client connections
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, connClient, err := createAndInitServer(ctx, newTestLogger(t), tempDir, files, clientHandler)
_, connClient, err := createAndInitServer(ctx, logger, tempDir, files, clientHandler)
if err != nil {
t.Fatalf("failed to create and init language server: %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ func createAndInitServer(

func createClientHandler(
t *testing.T,
logger io.Writer,
messages map[string]chan []string,
) func(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
t.Helper()

return func(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
if req.Method != "textDocument/publishDiagnostics" {
t.Log("unexpected request method:", req.Method)
fmt.Fprintln(logger, "unexpected request method:", req.Method)

return struct{}{}, nil
}
Expand All @@ -200,7 +201,7 @@ func createClientHandler(
slices.Sort(violations)

fileBase := filepath.Base(requestData.URI)
t.Log("queue", fileBase, len(messages[fileBase]))
fmt.Fprintln(logger, "queue", fileBase, len(messages[fileBase]))

select {
case messages[fileBase] <- violations:
Expand Down

0 comments on commit 69fb6ca

Please sign in to comment.