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\n Output: %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
3236func 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