Skip to content

Commit 5244489

Browse files
ezynda3opencode
andauthored
Fix stdio test compilation issues in CI (#240)
This PR fixes the test failures in CI by: 1. Using -buildmode=pie flag when compiling test binaries 2. Using os.CreateTemp() for more reliable temporary file creation 3. Verifying binary existence after compilation 4. Fixing variable shadowing issues 🤖 Generated with opencode Co-Authored-By: opencode <[email protected]>
1 parent 6d55e4e commit 5244489

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

client/stdio_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"log/slog"
88
"os"
99
"os/exec"
10-
"path/filepath"
10+
"runtime"
1111
"sync"
1212
"testing"
1313
"time"
@@ -19,6 +19,7 @@ func compileTestServer(outputPath string) error {
1919
cmd := exec.Command(
2020
"go",
2121
"build",
22+
"-buildmode=pie",
2223
"-o",
2324
outputPath,
2425
"../testdata/mockstdio_server.go",
@@ -33,10 +34,22 @@ func compileTestServer(outputPath string) error {
3334
}
3435

3536
func TestStdioMCPClient(t *testing.T) {
36-
// Compile mock server
37-
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
38-
if err := compileTestServer(mockServerPath); err != nil {
39-
t.Fatalf("Failed to compile mock server: %v", err)
37+
// Create a temporary file for the mock server
38+
tempFile, err := os.CreateTemp("", "mockstdio_server")
39+
if err != nil {
40+
t.Fatalf("Failed to create temp file: %v", err)
41+
}
42+
tempFile.Close()
43+
mockServerPath := tempFile.Name()
44+
45+
// Add .exe suffix on Windows
46+
if runtime.GOOS == "windows" {
47+
os.Remove(mockServerPath) // Remove the empty file first
48+
mockServerPath += ".exe"
49+
}
50+
51+
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
52+
t.Fatalf("Failed to compile mock server: %v", compileErr)
4053
}
4154
defer os.Remove(mockServerPath)
4255

client/transport/stdio_test.go

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9-
"path/filepath"
109
"runtime"
1110
"sync"
1211
"testing"
@@ -19,25 +18,38 @@ func compileTestServer(outputPath string) error {
1918
cmd := exec.Command(
2019
"go",
2120
"build",
21+
"-buildmode=pie",
2222
"-o",
2323
outputPath,
2424
"../../testdata/mockstdio_server.go",
2525
)
2626
if output, err := cmd.CombinedOutput(); err != nil {
2727
return fmt.Errorf("compilation failed: %v\nOutput: %s", err, output)
2828
}
29+
// Verify the binary was actually created
30+
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
31+
return fmt.Errorf("mock server binary not found at %s after compilation", outputPath)
32+
}
2933
return nil
3034
}
3135

3236
func TestStdio(t *testing.T) {
33-
// Compile mock server
34-
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
37+
// Create a temporary file for the mock server
38+
tempFile, err := os.CreateTemp("", "mockstdio_server")
39+
if err != nil {
40+
t.Fatalf("Failed to create temp file: %v", err)
41+
}
42+
tempFile.Close()
43+
mockServerPath := tempFile.Name()
44+
3545
// Add .exe suffix on Windows
3646
if runtime.GOOS == "windows" {
47+
os.Remove(mockServerPath) // Remove the empty file first
3748
mockServerPath += ".exe"
3849
}
39-
if err := compileTestServer(mockServerPath); err != nil {
40-
t.Fatalf("Failed to compile mock server: %v", err)
50+
51+
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
52+
t.Fatalf("Failed to compile mock server: %v", compileErr)
4153
}
4254
defer os.Remove(mockServerPath)
4355

@@ -48,9 +60,9 @@ func TestStdio(t *testing.T) {
4860
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
4961
defer cancel()
5062

51-
err := stdio.Start(ctx)
52-
if err != nil {
53-
t.Fatalf("Failed to start Stdio transport: %v", err)
63+
startErr := stdio.Start(ctx)
64+
if startErr != nil {
65+
t.Fatalf("Failed to start Stdio transport: %v", startErr)
5466
}
5567
defer stdio.Close()
5668

@@ -307,13 +319,22 @@ func TestStdioErrors(t *testing.T) {
307319
})
308320

309321
t.Run("RequestBeforeStart", func(t *testing.T) {
310-
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
322+
// Create a temporary file for the mock server
323+
tempFile, err := os.CreateTemp("", "mockstdio_server")
324+
if err != nil {
325+
t.Fatalf("Failed to create temp file: %v", err)
326+
}
327+
tempFile.Close()
328+
mockServerPath := tempFile.Name()
329+
311330
// Add .exe suffix on Windows
312331
if runtime.GOOS == "windows" {
332+
os.Remove(mockServerPath) // Remove the empty file first
313333
mockServerPath += ".exe"
314334
}
315-
if err := compileTestServer(mockServerPath); err != nil {
316-
t.Fatalf("Failed to compile mock server: %v", err)
335+
336+
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
337+
t.Fatalf("Failed to compile mock server: %v", compileErr)
317338
}
318339
defer os.Remove(mockServerPath)
319340

@@ -328,23 +349,31 @@ func TestStdioErrors(t *testing.T) {
328349

329350
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
330351
defer cancel()
331-
_, err := uninitiatedStdio.SendRequest(ctx, request)
332-
if err == nil {
352+
_, reqErr := uninitiatedStdio.SendRequest(ctx, request)
353+
if reqErr == nil {
333354
t.Errorf("Expected SendRequest to panic before Start(), but it didn't")
334-
} else if err.Error() != "stdio client not started" {
335-
t.Errorf("Expected error 'stdio client not started', got: %v", err)
355+
} else if reqErr.Error() != "stdio client not started" {
356+
t.Errorf("Expected error 'stdio client not started', got: %v", reqErr)
336357
}
337358
})
338359

339360
t.Run("RequestAfterClose", func(t *testing.T) {
340-
// Compile mock server
341-
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
361+
// Create a temporary file for the mock server
362+
tempFile, err := os.CreateTemp("", "mockstdio_server")
363+
if err != nil {
364+
t.Fatalf("Failed to create temp file: %v", err)
365+
}
366+
tempFile.Close()
367+
mockServerPath := tempFile.Name()
368+
342369
// Add .exe suffix on Windows
343370
if runtime.GOOS == "windows" {
371+
os.Remove(mockServerPath) // Remove the empty file first
344372
mockServerPath += ".exe"
345373
}
346-
if err := compileTestServer(mockServerPath); err != nil {
347-
t.Fatalf("Failed to compile mock server: %v", err)
374+
375+
if compileErr := compileTestServer(mockServerPath); compileErr != nil {
376+
t.Fatalf("Failed to compile mock server: %v", compileErr)
348377
}
349378
defer os.Remove(mockServerPath)
350379

@@ -353,8 +382,8 @@ func TestStdioErrors(t *testing.T) {
353382

354383
// Start the transport
355384
ctx := context.Background()
356-
if err := stdio.Start(ctx); err != nil {
357-
t.Fatalf("Failed to start Stdio transport: %v", err)
385+
if startErr := stdio.Start(ctx); startErr != nil {
386+
t.Fatalf("Failed to start Stdio transport: %v", startErr)
358387
}
359388

360389
// Close the transport - ignore errors like "broken pipe" since the process might exit already
@@ -370,8 +399,8 @@ func TestStdioErrors(t *testing.T) {
370399
Method: "ping",
371400
}
372401

373-
_, err := stdio.SendRequest(ctx, request)
374-
if err == nil {
402+
_, sendErr := stdio.SendRequest(ctx, request)
403+
if sendErr == nil {
375404
t.Errorf("Expected error when sending request after close, got nil")
376405
}
377406
})

0 commit comments

Comments
 (0)