diff --git a/internal/proc/env_test.go b/internal/proc/env_test.go index a794e28cc..f8d99fc1a 100644 --- a/internal/proc/env_test.go +++ b/internal/proc/env_test.go @@ -1,6 +1,7 @@ package proc import ( + "bufio" "context" "errors" "os" @@ -43,13 +44,15 @@ func TestParseEnviron(t *testing.T) { } } -// TestEnvOfHelper is TestEnvOfChild's child: it waits to be ended. macOS -// keeps a system binary's environment (sleep's) from ps, so the child is -// this test binary, as the Codex app's codex is its own. +// TestEnvOfHelper is TestEnvOfChild's child: it says it is running and +// waits to be ended. macOS keeps a system binary's environment (sleep's) +// from ps, so the child is this test binary, as the Codex app's codex is +// its own. func TestEnvOfHelper(t *testing.T) { if os.Getenv("MAGPIE_ENV_HELPER") != "1" { return } + os.Stdout.WriteString("running\n") time.Sleep(30 * time.Second) } @@ -60,11 +63,20 @@ func TestEnvOfChild(t *testing.T) { } ctx, cancel := context.WithCancel(context.Background()) cmd := CommandContext(ctx, os.Args[0], "-test.run=^TestEnvOfHelper$") - cmd.Env = []string{"MAGPIE_ENV_HELPER=1", "HOME=/sand box/h", "CODEX_HOME=/sand box/h/.codex"} + // TMPDIR is this test's: given no testenv.Marker, the helper makes a + // test home of its own there, which it never removes once killed + cmd.Env = []string{"MAGPIE_ENV_HELPER=1", "HOME=/sand box/h", "CODEX_HOME=/sand box/h/.codex", "TMPDIR=" + t.TempDir()} + out, _ := cmd.StdoutPipe() if err := cmd.Start(); err != nil { t.Fatal(err) } t.Cleanup(func() { cancel(); _ = cmd.Wait() }) + // running, so its exec is done: Start returns before Linux has written + // the new program's environment, and /proc//environ read then is + // empty (#3906) + if line, _ := bufio.NewReader(out).ReadString('\n'); line != "running\n" { + t.Fatalf("the helper: %q", line) + } got, err := EnvOf(context.Background(), cmd.Process.Pid, "HOME", "CODEX_HOME", "LANG") if err != nil { t.Fatal(err) @@ -81,11 +93,15 @@ func TestEnvOfChild(t *testing.T) { func TestEndExitsClean(t *testing.T) { cmd := CommandContext(context.Background(), os.Args[0], "-test.run=^TestEnvOfHelper$") cmd.Env = append(os.Environ(), "MAGPIE_ENV_HELPER=1") + out, _ := cmd.StdoutPipe() if err := cmd.Start(); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = cmd.Process.Kill() }) - time.Sleep(200 * time.Millisecond) // started, so the signal isn't taken before its handlers + // running, so the signal isn't taken before its handlers + if line, _ := bufio.NewReader(out).ReadString('\n'); line != "running\n" { + t.Fatalf("the helper: %q", line) + } if err := End(cmd.Process.Pid); err != nil { t.Fatal(err) } diff --git a/internal/provider/codex_app_server_test.go b/internal/provider/codex_app_server_test.go index 88959e4c8..ec54c0313 100644 --- a/internal/provider/codex_app_server_test.go +++ b/internal/provider/codex_app_server_test.go @@ -1,6 +1,7 @@ package provider import ( + "bufio" "context" "errors" "os" @@ -239,6 +240,7 @@ func TestCodexAppHomeHelper(t *testing.T) { if os.Getenv("MAGPIE_CODEX_HOME_HELPER") != "1" { return } + os.Stdout.WriteString("running\n") time.Sleep(30 * time.Second) } @@ -251,11 +253,20 @@ func TestCodexAppServersOnHome(t *testing.T) { start := func(env ...string) int { ctx, cancel := context.WithCancel(context.Background()) cmd := proc.CommandContext(ctx, os.Args[0], "-test.run=^TestCodexAppHomeHelper$") - cmd.Env = append([]string{"MAGPIE_CODEX_HOME_HELPER=1"}, env...) + // TMPDIR is this test's: given no testenv.Marker, the helper makes + // a test home of its own there, which it never removes once killed + cmd.Env = append([]string{"MAGPIE_CODEX_HOME_HELPER=1", "TMPDIR=" + t.TempDir()}, env...) + out, _ := cmd.StdoutPipe() if err := cmd.Start(); err != nil { t.Fatal(err) } t.Cleanup(func() { cancel(); _ = cmd.Wait() }) + // running, so its exec is done: Start returns before Linux has + // written the new program's environment, and /proc//environ + // read then is empty (#3906) + if line, _ := bufio.NewReader(out).ReadString('\n'); line != "running\n" { + t.Fatalf("the helper: %q", line) + } return cmd.Process.Pid } ctx := context.Background()