6
6
"fmt"
7
7
"os"
8
8
"os/exec"
9
- "path/filepath"
10
9
"runtime"
11
10
"sync"
12
11
"testing"
@@ -19,25 +18,38 @@ func compileTestServer(outputPath string) error {
19
18
cmd := exec .Command (
20
19
"go" ,
21
20
"build" ,
21
+ "-buildmode=pie" ,
22
22
"-o" ,
23
23
outputPath ,
24
24
"../../testdata/mockstdio_server.go" ,
25
25
)
26
26
if output , err := cmd .CombinedOutput (); err != nil {
27
27
return fmt .Errorf ("compilation failed: %v\n Output: %s" , err , output )
28
28
}
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
+ }
29
33
return nil
30
34
}
31
35
32
36
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
+
35
45
// Add .exe suffix on Windows
36
46
if runtime .GOOS == "windows" {
47
+ os .Remove (mockServerPath ) // Remove the empty file first
37
48
mockServerPath += ".exe"
38
49
}
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 )
41
53
}
42
54
defer os .Remove (mockServerPath )
43
55
@@ -48,9 +60,9 @@ func TestStdio(t *testing.T) {
48
60
ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
49
61
defer cancel ()
50
62
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 )
54
66
}
55
67
defer stdio .Close ()
56
68
@@ -307,13 +319,22 @@ func TestStdioErrors(t *testing.T) {
307
319
})
308
320
309
321
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
+
311
330
// Add .exe suffix on Windows
312
331
if runtime .GOOS == "windows" {
332
+ os .Remove (mockServerPath ) // Remove the empty file first
313
333
mockServerPath += ".exe"
314
334
}
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 )
317
338
}
318
339
defer os .Remove (mockServerPath )
319
340
@@ -328,23 +349,31 @@ func TestStdioErrors(t *testing.T) {
328
349
329
350
ctx , cancel := context .WithTimeout (context .Background (), 200 * time .Millisecond )
330
351
defer cancel ()
331
- _ , err := uninitiatedStdio .SendRequest (ctx , request )
332
- if err == nil {
352
+ _ , reqErr := uninitiatedStdio .SendRequest (ctx , request )
353
+ if reqErr == nil {
333
354
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 )
336
357
}
337
358
})
338
359
339
360
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
+
342
369
// Add .exe suffix on Windows
343
370
if runtime .GOOS == "windows" {
371
+ os .Remove (mockServerPath ) // Remove the empty file first
344
372
mockServerPath += ".exe"
345
373
}
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 )
348
377
}
349
378
defer os .Remove (mockServerPath )
350
379
@@ -353,8 +382,8 @@ func TestStdioErrors(t *testing.T) {
353
382
354
383
// Start the transport
355
384
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 )
358
387
}
359
388
360
389
// Close the transport - ignore errors like "broken pipe" since the process might exit already
@@ -370,8 +399,8 @@ func TestStdioErrors(t *testing.T) {
370
399
Method : "ping" ,
371
400
}
372
401
373
- _ , err := stdio .SendRequest (ctx , request )
374
- if err == nil {
402
+ _ , sendErr := stdio .SendRequest (ctx , request )
403
+ if sendErr == nil {
375
404
t .Errorf ("Expected error when sending request after close, got nil" )
376
405
}
377
406
})
0 commit comments