Skip to content

Fix stdio test compilation issues in CI #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions client/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log/slog"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
Expand All @@ -19,6 +19,7 @@ func compileTestServer(outputPath string) error {
cmd := exec.Command(
"go",
"build",
"-buildmode=pie",
"-o",
outputPath,
"../testdata/mockstdio_server.go",
Expand All @@ -33,10 +34,22 @@ func compileTestServer(outputPath string) error {
}

func TestStdioMCPClient(t *testing.T) {
// Compile mock server
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
if err := compileTestServer(mockServerPath); err != nil {
t.Fatalf("Failed to compile mock server: %v", err)
// Create a temporary file for the mock server
tempFile, err := os.CreateTemp("", "mockstdio_server")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
tempFile.Close()
mockServerPath := tempFile.Name()

// Add .exe suffix on Windows
if runtime.GOOS == "windows" {
os.Remove(mockServerPath) // Remove the empty file first
mockServerPath += ".exe"
}

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

Expand Down
75 changes: 52 additions & 23 deletions client/transport/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
"testing"
Expand All @@ -19,25 +18,38 @@ func compileTestServer(outputPath string) error {
cmd := exec.Command(
"go",
"build",
"-buildmode=pie",
"-o",
outputPath,
"../../testdata/mockstdio_server.go",
)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("compilation failed: %v\nOutput: %s", err, output)
}
// Verify the binary was actually created
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
return fmt.Errorf("mock server binary not found at %s after compilation", outputPath)
}
return nil
}

func TestStdio(t *testing.T) {
// Compile mock server
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
// Create a temporary file for the mock server
tempFile, err := os.CreateTemp("", "mockstdio_server")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
tempFile.Close()
mockServerPath := tempFile.Name()

// Add .exe suffix on Windows
if runtime.GOOS == "windows" {
os.Remove(mockServerPath) // Remove the empty file first
mockServerPath += ".exe"
}
if err := compileTestServer(mockServerPath); err != nil {
t.Fatalf("Failed to compile mock server: %v", err)

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

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

err := stdio.Start(ctx)
if err != nil {
t.Fatalf("Failed to start Stdio transport: %v", err)
startErr := stdio.Start(ctx)
if startErr != nil {
t.Fatalf("Failed to start Stdio transport: %v", startErr)
}
defer stdio.Close()

Expand Down Expand Up @@ -307,13 +319,22 @@ func TestStdioErrors(t *testing.T) {
})

t.Run("RequestBeforeStart", func(t *testing.T) {
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
// Create a temporary file for the mock server
tempFile, err := os.CreateTemp("", "mockstdio_server")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
tempFile.Close()
mockServerPath := tempFile.Name()

// Add .exe suffix on Windows
if runtime.GOOS == "windows" {
os.Remove(mockServerPath) // Remove the empty file first
mockServerPath += ".exe"
}
if err := compileTestServer(mockServerPath); err != nil {
t.Fatalf("Failed to compile mock server: %v", err)

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

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

ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
_, err := uninitiatedStdio.SendRequest(ctx, request)
if err == nil {
_, reqErr := uninitiatedStdio.SendRequest(ctx, request)
if reqErr == nil {
t.Errorf("Expected SendRequest to panic before Start(), but it didn't")
} else if err.Error() != "stdio client not started" {
t.Errorf("Expected error 'stdio client not started', got: %v", err)
} else if reqErr.Error() != "stdio client not started" {
t.Errorf("Expected error 'stdio client not started', got: %v", reqErr)
}
})

t.Run("RequestAfterClose", func(t *testing.T) {
// Compile mock server
mockServerPath := filepath.Join(os.TempDir(), "mockstdio_server")
// Create a temporary file for the mock server
tempFile, err := os.CreateTemp("", "mockstdio_server")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
tempFile.Close()
mockServerPath := tempFile.Name()

// Add .exe suffix on Windows
if runtime.GOOS == "windows" {
os.Remove(mockServerPath) // Remove the empty file first
mockServerPath += ".exe"
}
if err := compileTestServer(mockServerPath); err != nil {
t.Fatalf("Failed to compile mock server: %v", err)

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

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

// Start the transport
ctx := context.Background()
if err := stdio.Start(ctx); err != nil {
t.Fatalf("Failed to start Stdio transport: %v", err)
if startErr := stdio.Start(ctx); startErr != nil {
t.Fatalf("Failed to start Stdio transport: %v", startErr)
}

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

_, err := stdio.SendRequest(ctx, request)
if err == nil {
_, sendErr := stdio.SendRequest(ctx, request)
if sendErr == nil {
t.Errorf("Expected error when sending request after close, got nil")
}
})
Expand Down