66 "fmt"
77 "os"
88 "os/exec"
9- "path/filepath"
109 "runtime"
1110 "sync"
1211 "testing"
@@ -19,25 +18,41 @@ 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 )
26+ tmpCache , _ := os .MkdirTemp ("" , "gocache" )
27+ cmd .Env = append (os .Environ (), "GOCACHE=" + tmpCache )
28+
2629 if output , err := cmd .CombinedOutput (); err != nil {
2730 return fmt .Errorf ("compilation failed: %v\n Output: %s" , err , output )
2831 }
32+ // Verify the binary was actually created
33+ if _ , err := os .Stat (outputPath ); os .IsNotExist (err ) {
34+ return fmt .Errorf ("mock server binary not found at %s after compilation" , outputPath )
35+ }
2936 return nil
3037}
3138
3239func TestStdio (t * testing.T ) {
33- // Compile mock server
34- mockServerPath := filepath .Join (os .TempDir (), "mockstdio_server" )
40+ // Create a temporary file for the mock server
41+ tempFile , err := os .CreateTemp ("" , "mockstdio_server" )
42+ if err != nil {
43+ t .Fatalf ("Failed to create temp file: %v" , err )
44+ }
45+ tempFile .Close ()
46+ mockServerPath := tempFile .Name ()
47+
3548 // Add .exe suffix on Windows
3649 if runtime .GOOS == "windows" {
50+ os .Remove (mockServerPath ) // Remove the empty file first
3751 mockServerPath += ".exe"
3852 }
39- if err := compileTestServer (mockServerPath ); err != nil {
40- t .Fatalf ("Failed to compile mock server: %v" , err )
53+
54+ if compileErr := compileTestServer (mockServerPath ); compileErr != nil {
55+ t .Fatalf ("Failed to compile mock server: %v" , compileErr )
4156 }
4257 defer os .Remove (mockServerPath )
4358
@@ -48,9 +63,9 @@ func TestStdio(t *testing.T) {
4863 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
4964 defer cancel ()
5065
51- err := stdio .Start (ctx )
52- if err != nil {
53- t .Fatalf ("Failed to start Stdio transport: %v" , err )
66+ startErr := stdio .Start (ctx )
67+ if startErr != nil {
68+ t .Fatalf ("Failed to start Stdio transport: %v" , startErr )
5469 }
5570 defer stdio .Close ()
5671
@@ -307,13 +322,22 @@ func TestStdioErrors(t *testing.T) {
307322 })
308323
309324 t .Run ("RequestBeforeStart" , func (t * testing.T ) {
310- mockServerPath := filepath .Join (os .TempDir (), "mockstdio_server" )
325+ // Create a temporary file for the mock server
326+ tempFile , err := os .CreateTemp ("" , "mockstdio_server" )
327+ if err != nil {
328+ t .Fatalf ("Failed to create temp file: %v" , err )
329+ }
330+ tempFile .Close ()
331+ mockServerPath := tempFile .Name ()
332+
311333 // Add .exe suffix on Windows
312334 if runtime .GOOS == "windows" {
335+ os .Remove (mockServerPath ) // Remove the empty file first
313336 mockServerPath += ".exe"
314337 }
315- if err := compileTestServer (mockServerPath ); err != nil {
316- t .Fatalf ("Failed to compile mock server: %v" , err )
338+
339+ if compileErr := compileTestServer (mockServerPath ); compileErr != nil {
340+ t .Fatalf ("Failed to compile mock server: %v" , compileErr )
317341 }
318342 defer os .Remove (mockServerPath )
319343
@@ -328,23 +352,31 @@ func TestStdioErrors(t *testing.T) {
328352
329353 ctx , cancel := context .WithTimeout (context .Background (), 200 * time .Millisecond )
330354 defer cancel ()
331- _ , err := uninitiatedStdio .SendRequest (ctx , request )
332- if err == nil {
355+ _ , reqErr := uninitiatedStdio .SendRequest (ctx , request )
356+ if reqErr == nil {
333357 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 )
358+ } else if reqErr .Error () != "stdio client not started" {
359+ t .Errorf ("Expected error 'stdio client not started', got: %v" , reqErr )
336360 }
337361 })
338362
339363 t .Run ("RequestAfterClose" , func (t * testing.T ) {
340- // Compile mock server
341- mockServerPath := filepath .Join (os .TempDir (), "mockstdio_server" )
364+ // Create a temporary file for the mock server
365+ tempFile , err := os .CreateTemp ("" , "mockstdio_server" )
366+ if err != nil {
367+ t .Fatalf ("Failed to create temp file: %v" , err )
368+ }
369+ tempFile .Close ()
370+ mockServerPath := tempFile .Name ()
371+
342372 // Add .exe suffix on Windows
343373 if runtime .GOOS == "windows" {
374+ os .Remove (mockServerPath ) // Remove the empty file first
344375 mockServerPath += ".exe"
345376 }
346- if err := compileTestServer (mockServerPath ); err != nil {
347- t .Fatalf ("Failed to compile mock server: %v" , err )
377+
378+ if compileErr := compileTestServer (mockServerPath ); compileErr != nil {
379+ t .Fatalf ("Failed to compile mock server: %v" , compileErr )
348380 }
349381 defer os .Remove (mockServerPath )
350382
@@ -353,8 +385,8 @@ func TestStdioErrors(t *testing.T) {
353385
354386 // Start the transport
355387 ctx := context .Background ()
356- if err := stdio .Start (ctx ); err != nil {
357- t .Fatalf ("Failed to start Stdio transport: %v" , err )
388+ if startErr := stdio .Start (ctx ); startErr != nil {
389+ t .Fatalf ("Failed to start Stdio transport: %v" , startErr )
358390 }
359391
360392 // Close the transport - ignore errors like "broken pipe" since the process might exit already
@@ -370,8 +402,8 @@ func TestStdioErrors(t *testing.T) {
370402 Method : "ping" ,
371403 }
372404
373- _ , err := stdio .SendRequest (ctx , request )
374- if err == nil {
405+ _ , sendErr := stdio .SendRequest (ctx , request )
406+ if sendErr == nil {
375407 t .Errorf ("Expected error when sending request after close, got nil" )
376408 }
377409 })
0 commit comments