🐛 fix(driver): 兼容跨平台 Go 路径回退测试

This commit is contained in:
Syngnat
2026-04-11 22:36:21 +08:00
parent 1a0ba9a499
commit 33b21cc5ee
2 changed files with 21 additions and 4 deletions

View File

@@ -36,6 +36,9 @@ var (
goBinaryCommand = func(name string, arg ...string) *exec.Cmd {
return exec.Command(name, arg...)
}
goBinaryCommandOutput = func(cmd *exec.Cmd) ([]byte, error) {
return cmd.Output()
}
)
// resolveGoBinaryPath 定位 Go 可执行文件,兼容 macOS 图形应用未继承 shell PATH 的场景 by AI.Coding
@@ -58,7 +61,7 @@ func resolveGoBinaryPath() (string, error) {
for _, shell := range candidateShellsForCommandLookup() {
cmd := goBinaryCommand(shell, "-lc", "command -v go")
output, err := cmd.Output()
output, err := goBinaryCommandOutput(cmd)
if err != nil {
continue
}

View File

@@ -33,10 +33,12 @@ func TestResolveGoBinaryPath_FallsBackToKnownLocation(t *testing.T) {
originalLookPath := goBinaryLookPath
originalStat := goBinaryStat
originalCommand := goBinaryCommand
originalCommandOutput := goBinaryCommandOutput
t.Cleanup(func() {
goBinaryLookPath = originalLookPath
goBinaryStat = originalStat
goBinaryCommand = originalCommand
goBinaryCommandOutput = originalCommandOutput
})
goBinaryLookPath = func(file string) (string, error) {
@@ -52,6 +54,10 @@ func TestResolveGoBinaryPath_FallsBackToKnownLocation(t *testing.T) {
t.Fatalf("shell fallback should not run when common path exists")
return nil
}
goBinaryCommandOutput = func(cmd *exec.Cmd) ([]byte, error) {
t.Fatalf("shell fallback should not run when common path exists")
return nil, nil
}
got, err := resolveGoBinaryPath()
if err != nil {
@@ -66,11 +72,13 @@ func TestResolveGoBinaryPath_FallsBackToShellOutput(t *testing.T) {
originalLookPath := goBinaryLookPath
originalStat := goBinaryStat
originalCommand := goBinaryCommand
originalCommandOutput := goBinaryCommandOutput
originalShell := os.Getenv("SHELL")
t.Cleanup(func() {
goBinaryLookPath = originalLookPath
goBinaryStat = originalStat
goBinaryCommand = originalCommand
goBinaryCommandOutput = originalCommandOutput
_ = os.Setenv("SHELL", originalShell)
})
if err := os.Setenv("SHELL", "/custom/shell"); err != nil {
@@ -89,10 +97,16 @@ func TestResolveGoBinaryPath_FallsBackToShellOutput(t *testing.T) {
var called []string
goBinaryCommand = func(name string, arg ...string) *exec.Cmd {
called = append(called, name)
if len(called) == 1 {
return exec.Command("/bin/sh", "-c", "printf 'welcome\\n/Users/test/go/bin/go\\n'")
return &exec.Cmd{
Path: name,
Args: append([]string{name}, arg...),
}
return exec.Command("/bin/sh", "-c", "exit 1")
}
goBinaryCommandOutput = func(cmd *exec.Cmd) ([]byte, error) {
if len(called) == 1 {
return []byte("welcome\n/Users/test/go/bin/go\n"), nil
}
return nil, exec.ErrNotFound
}
got, err := resolveGoBinaryPath()