Skip to content

Commit d46cf85

Browse files
authored
refactor: simplify tests with the usetesting linter (#703)
1 parent 889fb05 commit d46cf85

File tree

5 files changed

+25
-80
lines changed

5 files changed

+25
-80
lines changed

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ version: "2"
22
linters:
33
enable:
44
- modernize
5+
- usetesting
6+
settings:
7+
usetesting:
8+
context-background: true
9+
context-todo: true
10+
os-temp-dir: true
511
exclusions:
612
presets:
713
- std-error-handling

client/issue583_test.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package client
33
import (
44
"context"
55
"os"
6-
"runtime"
76
"testing"
87
"time"
98

@@ -15,22 +14,16 @@ import (
1514
// where using transport.NewStdioWithOptions directly followed by client.Start()
1615
// would fail with "stdio client not started" error
1716
func TestDirectTransportCreation(t *testing.T) {
18-
tempFile, err := os.CreateTemp("", "mockstdio_server")
17+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
1918
if err != nil {
2019
t.Fatalf("Failed to create temp file: %v", err)
2120
}
2221
tempFile.Close()
23-
mockServerPath := tempFile.Name()
24-
25-
if runtime.GOOS == "windows" {
26-
os.Remove(mockServerPath)
27-
mockServerPath += ".exe"
28-
}
22+
mockServerPath := tempFile.Name() + ".exe"
2923

3024
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
3125
t.Fatalf("Failed to compile mock server: %v", compileErr)
3226
}
33-
defer os.Remove(mockServerPath)
3427

3528
// This is the pattern from issue #583 that was broken
3629
tport := transport.NewStdioWithOptions(mockServerPath, nil, nil)
@@ -68,22 +61,16 @@ func TestDirectTransportCreation(t *testing.T) {
6861

6962
// TestNewStdioMCPClientWithOptions tests that the old pattern still works
7063
func TestNewStdioMCPClientWithOptions(t *testing.T) {
71-
tempFile, err := os.CreateTemp("", "mockstdio_server")
64+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
7265
if err != nil {
7366
t.Fatalf("Failed to create temp file: %v", err)
7467
}
7568
tempFile.Close()
76-
mockServerPath := tempFile.Name()
77-
78-
if runtime.GOOS == "windows" {
79-
os.Remove(mockServerPath)
80-
mockServerPath += ".exe"
81-
}
69+
mockServerPath := tempFile.Name() + ".exe"
8270

8371
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
8472
t.Fatalf("Failed to compile mock server: %v", compileErr)
8573
}
86-
defer os.Remove(mockServerPath)
8774

8875
// This pattern was already working
8976
cli, err := NewStdioMCPClientWithOptions(mockServerPath, nil, nil)
@@ -123,22 +110,16 @@ func TestNewStdioMCPClientWithOptions(t *testing.T) {
123110

124111
// TestMultipleClientStartCalls tests that calling client.Start() multiple times is safe
125112
func TestMultipleClientStartCalls(t *testing.T) {
126-
tempFile, err := os.CreateTemp("", "mockstdio_server")
113+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
127114
if err != nil {
128115
t.Fatalf("Failed to create temp file: %v", err)
129116
}
130117
tempFile.Close()
131-
mockServerPath := tempFile.Name()
132-
133-
if runtime.GOOS == "windows" {
134-
os.Remove(mockServerPath)
135-
mockServerPath += ".exe"
136-
}
118+
mockServerPath := tempFile.Name() + ".exe"
137119

138120
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
139121
t.Fatalf("Failed to compile mock server: %v", compileErr)
140122
}
141-
defer os.Remove(mockServerPath)
142123

143124
tport := transport.NewStdioWithOptions(mockServerPath, nil, nil)
144125
cli := NewClient(tport)

client/stdio_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"log/slog"
88
"os"
99
"os/exec"
10-
"runtime"
1110
"sync"
1211
"testing"
1312
"time"
@@ -42,23 +41,16 @@ func compileTestServer(outputPath string) error {
4241

4342
func TestStdioMCPClient(t *testing.T) {
4443
// Create a temporary file for the mock server
45-
tempFile, err := os.CreateTemp("", "mockstdio_server")
44+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
4645
if err != nil {
4746
t.Fatalf("Failed to create temp file: %v", err)
4847
}
4948
tempFile.Close()
50-
mockServerPath := tempFile.Name()
51-
52-
// Add .exe suffix on Windows
53-
if runtime.GOOS == "windows" {
54-
os.Remove(mockServerPath) // Remove the empty file first
55-
mockServerPath += ".exe"
56-
}
49+
mockServerPath := tempFile.Name() + ".exe"
5750

5851
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
5952
t.Fatalf("Failed to compile mock server: %v", compileErr)
6053
}
61-
defer os.Remove(mockServerPath)
6254

6355
client, err := NewStdioMCPClient(mockServerPath, []string{})
6456
if err != nil {

client/transport/stdio_idempotent_test.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"os"
66
"os/exec"
7-
"runtime"
87
"testing"
98
"time"
109

@@ -13,22 +12,16 @@ import (
1312

1413
// TestStdio_StartIdempotency tests that calling Start() multiple times is safe
1514
func TestStdio_StartIdempotency(t *testing.T) {
16-
tempFile, err := os.CreateTemp("", "mockstdio_server")
15+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
1716
if err != nil {
1817
t.Fatalf("Failed to create temp file: %v", err)
1918
}
2019
tempFile.Close()
21-
mockServerPath := tempFile.Name()
22-
23-
if runtime.GOOS == "windows" {
24-
os.Remove(mockServerPath)
25-
mockServerPath += ".exe"
26-
}
20+
mockServerPath := tempFile.Name() + ".exe"
2721

2822
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
2923
t.Fatalf("Failed to compile mock server: %v", compileErr)
3024
}
31-
defer os.Remove(mockServerPath)
3225

3326
stdio := NewStdio(mockServerPath, nil)
3427

@@ -96,22 +89,16 @@ func TestStdio_StartFailureReset(t *testing.T) {
9689

9790
// TestStdio_StartWithOptions_Idempotent tests idempotency with custom options
9891
func TestStdio_StartWithOptions_Idempotent(t *testing.T) {
99-
tempFile, err := os.CreateTemp("", "mockstdio_server")
92+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
10093
if err != nil {
10194
t.Fatalf("Failed to create temp file: %v", err)
10295
}
10396
tempFile.Close()
104-
mockServerPath := tempFile.Name()
105-
106-
if runtime.GOOS == "windows" {
107-
os.Remove(mockServerPath)
108-
mockServerPath += ".exe"
109-
}
97+
mockServerPath := tempFile.Name() + ".exe"
11098

11199
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
112100
t.Fatalf("Failed to compile mock server: %v", compileErr)
113101
}
114-
defer os.Remove(mockServerPath)
115102

116103
cmdFuncCallCount := 0
117104
cmdFunc := func(ctx context.Context, command string, env []string, args []string) (*exec.Cmd, error) {

client/transport/stdio_test.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,16 @@ func compileTestServer(outputPath string) error {
4444

4545
func TestStdio(t *testing.T) {
4646
// Create a temporary file for the mock server
47-
tempFile, err := os.CreateTemp("", "mockstdio_server")
47+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
4848
if err != nil {
4949
t.Fatalf("Failed to create temp file: %v", err)
5050
}
5151
tempFile.Close()
52-
mockServerPath := tempFile.Name()
53-
54-
// Add .exe suffix on Windows
55-
if runtime.GOOS == "windows" {
56-
os.Remove(mockServerPath) // Remove the empty file first
57-
mockServerPath += ".exe"
58-
}
52+
mockServerPath := tempFile.Name() + ".exe"
5953

6054
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
6155
t.Fatalf("Failed to compile mock server: %v", compileErr)
6256
}
63-
defer os.Remove(mockServerPath)
6457

6558
// Create a new Stdio transport
6659
stdio := NewStdio(mockServerPath, nil)
@@ -425,7 +418,7 @@ func TestStdioErrors(t *testing.T) {
425418

426419
t.Run("RequestBeforeStart", func(t *testing.T) {
427420
// Create a temporary file for the mock server
428-
tempFile, err := os.CreateTemp("", "mockstdio_server")
421+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
429422
if err != nil {
430423
t.Fatalf("Failed to create temp file: %v", err)
431424
}
@@ -441,7 +434,6 @@ func TestStdioErrors(t *testing.T) {
441434
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
442435
t.Fatalf("Failed to compile mock server: %v", compileErr)
443436
}
444-
defer os.Remove(mockServerPath)
445437

446438
uninitiatedStdio := NewStdio(mockServerPath, nil)
447439

@@ -464,23 +456,16 @@ func TestStdioErrors(t *testing.T) {
464456

465457
t.Run("RequestAfterClose", func(t *testing.T) {
466458
// Create a temporary file for the mock server
467-
tempFile, err := os.CreateTemp("", "mockstdio_server")
459+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
468460
if err != nil {
469461
t.Fatalf("Failed to create temp file: %v", err)
470462
}
471463
tempFile.Close()
472-
mockServerPath := tempFile.Name()
473-
474-
// Add .exe suffix on Windows
475-
if runtime.GOOS == "windows" {
476-
os.Remove(mockServerPath) // Remove the empty file first
477-
mockServerPath += ".exe"
478-
}
464+
mockServerPath := tempFile.Name() + ".exe"
479465

480466
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
481467
t.Fatalf("Failed to compile mock server: %v", compileErr)
482468
}
483-
defer os.Remove(mockServerPath)
484469

485470
// Create a new Stdio transport
486471
stdio := NewStdio(mockServerPath, nil)
@@ -705,22 +690,16 @@ func TestStdio_NewStdioWithOptions_AppliesOptions(t *testing.T) {
705690
}
706691

707692
func TestStdio_LargeMessages(t *testing.T) {
708-
tempFile, err := os.CreateTemp("", "mockstdio_server")
693+
tempFile, err := os.CreateTemp(t.TempDir(), "mockstdio_server")
709694
if err != nil {
710695
t.Fatalf("Failed to create temp file: %v", err)
711696
}
712697
tempFile.Close()
713-
mockServerPath := tempFile.Name()
714-
715-
if runtime.GOOS == "windows" {
716-
os.Remove(mockServerPath)
717-
mockServerPath += ".exe"
718-
}
698+
mockServerPath := tempFile.Name() + ".exe"
719699

720700
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
721701
t.Fatalf("Failed to compile mock server: %v", compileErr)
722702
}
723-
defer os.Remove(mockServerPath)
724703

725704
stdio := NewStdio(mockServerPath, nil)
726705

0 commit comments

Comments
 (0)