From 1ee058bcb5b9d5562db46d26f56aa85399384bcc Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Mon, 7 Oct 2024 10:33:01 +0100 Subject: [PATCH] Use test logger for client handler This protects against logging in tests after they finish Signed-off-by: Charlie Egan --- internal/lsp/server_aggregates_test.go | 12 ++++++++---- internal/lsp/server_multi_file_test.go | 6 ++++-- internal/lsp/server_test.go | 5 +++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/internal/lsp/server_aggregates_test.go b/internal/lsp/server_aggregates_test.go index 55c86733..6d0a4f8f 100644 --- a/internal/lsp/server_aggregates_test.go +++ b/internal/lsp/server_aggregates_test.go @@ -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) } @@ -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) } diff --git a/internal/lsp/server_multi_file_test.go b/internal/lsp/server_multi_file_test.go index 31720514..a72a1ae5 100644 --- a/internal/lsp/server_multi_file_test.go +++ b/internal/lsp/server_multi_file_test.go @@ -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) } diff --git a/internal/lsp/server_test.go b/internal/lsp/server_test.go index a96e30ef..58fdfa00 100644 --- a/internal/lsp/server_test.go +++ b/internal/lsp/server_test.go @@ -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 } @@ -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: