Skip to content

Commit 9ba96a9

Browse files
Google Team Membercopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 1009602818
1 parent d59ab99 commit 9ba96a9

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎internal/cli/custom/envruntime/poller_test.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"strings"
3232
"sync"
3333
"sync/atomic"
34-
"syscall"
3534
"testing"
3635
"time"
3736
)
@@ -115,10 +114,15 @@ func TestSingleflightDoAndDoChan(t *testing.T) {
115114
}
116115

117116
func TestShellCommandProvisionerAndStatelessPoller(t *testing.T) {
117+
origPath := os.Getenv("PATH")
118+
tempBin := t.TempDir()
119+
for _, shName := range []string{"sh", "sh.exe"} {
120+
_ = os.WriteFile(filepath.Join(tempBin, shName), []byte("#!/bin/sh\n"), 0o755)
121+
}
122+
t.Setenv("PATH", tempBin)
118123
if bin, args := ResolveProvisionShell("windows", "echo ok"); !strings.Contains(bin, "sh") || len(args) != 2 || args[0] != "-c" {
119124
t.Errorf("ResolveProvisionShell(windows with PATH sh) = (%q, %v), want sh -c", bin, args)
120125
}
121-
origPath := os.Getenv("PATH")
122126
t.Setenv("PATH", "")
123127
if bin, args := ResolveProvisionShell("windows", "echo ok"); bin != "cmd.exe" || len(args) != 2 || args[0] != "/C" {
124128
t.Errorf("ResolveProvisionShell(windows without PATH sh) = (%q, %v), want cmd.exe /C", bin, args)
@@ -159,8 +163,8 @@ func TestShellCommandProvisionerAndStatelessPoller(t *testing.T) {
159163
if idx := strings.LastIndexByte(string(statBytes), ')'); idx >= 0 {
160164
fields := strings.Fields(string(statBytes[idx+1:]))
161165
if len(fields) >= 3 {
162-
if childPgid, err := strconv.Atoi(fields[2]); err == nil && childPgid == syscall.Getpgrp() {
163-
t.Errorf("childPgid = %d, want != parent pgid %d (ConfigureSubprocessGroup must set new process group)", childPgid, syscall.Getpgrp())
166+
if childPgid, err := strconv.Atoi(fields[2]); err == nil && childPgid == os.Getpid() {
167+
t.Errorf("childPgid = %d, want != parent pid %d (ConfigureSubprocessGroup must set new process group)", childPgid, os.Getpid())
164168
}
165169
}
166170
}

‎internal/cli/custom/envruntime/sandbox_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func mustSymlink(t *testing.T, target, link string) {
3939

4040
func TestResolveSandboxDirs(t *testing.T) {
4141
base := t.TempDir()
42+
if evalBase, err := filepath.EvalSymlinks(base); err == nil {
43+
base = evalBase
44+
}
4245
rootDir := filepath.Join(base, "root")
4346
workDir := filepath.Join(rootDir, "proj")
4447
// create=false validates confinement without mutating disk (used before --dry-run).
@@ -712,6 +715,19 @@ func TestSandboxShellEnvBuffersAndSubprocessKill(t *testing.T) {
712715
if bin, _ := ResolveShellCommand("linux", "ls"); bin != "sh" {
713716
t.Errorf("ResolveShellCommand(linux, empty PATH) = %q, want sh", bin)
714717
}
718+
if bin, args := resolveShellCommand("windows", func(name string) (string, error) {
719+
if name == "bash" {
720+
return `C:\Windows\System32\bash.exe`, nil
721+
}
722+
return `C:\Program Files\Git\bin\sh.exe`, nil
723+
}, "echo hi"); bin != `C:\Program Files\Git\bin\sh.exe` || len(args) != 2 || args[0] != "-c" {
724+
t.Errorf("resolveShellCommand(windows WSL bash fallback to sh) = (%q, %v)", bin, args)
725+
}
726+
if bin, _ := resolveShellCommand("windows", func(name string) (string, error) {
727+
return `C:\Program Files\Git\bin\bash.exe`, nil
728+
}, "echo hi"); bin != `C:\Program Files\Git\bin\bash.exe` {
729+
t.Errorf("resolveShellCommand(windows Git bash) = %q", bin)
730+
}
715731
t.Setenv("PATH", origPath)
716732
if len(ShutdownSignals()) == 0 {
717733
t.Error("ShutdownSignals() returned empty slice")

‎internal/cli/custom/envruntime/shell.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,14 @@ func isWindowsWSLStubBash(goos, p string) bool {
161161

162162
// ResolveShellCommand selects bash, sh, or cmd.exe to execute command on goos.
163163
func ResolveShellCommand(goos, command string) (string, []string) {
164-
if p, err := exec.LookPath("bash"); err == nil && !isWindowsWSLStubBash(goos, p) {
164+
return resolveShellCommand(goos, exec.LookPath, command)
165+
}
166+
167+
func resolveShellCommand(goos string, lookPath func(string) (string, error), command string) (string, []string) {
168+
if p, err := lookPath("bash"); err == nil && !isWindowsWSLStubBash(goos, p) {
165169
return p, []string{"-c", command}
166170
}
167-
if p, err := exec.LookPath("sh"); err == nil {
171+
if p, err := lookPath("sh"); err == nil {
168172
return p, []string{"-c", command}
169173
}
170174
if goos == "windows" {

0 commit comments

Comments
 (0)