package control import ( "os" "runtime" "strings" "testing" ) // execShellCommand 必须给出一个**本平台真实存在**的解释器。 // // 回归的是 2026-08-01 的线上故障:这段逻辑原本各平台共用 `os.Getenv("SHELL")` → 回落 // `/bin/sh`,而 Windows 上 SHELL 恒为空 —— 于是每次 /exec 都返回 // `exec: "/bin/sh": executable file not found in %PATH%`。偏偏 Windows 的 /term 依赖 // tmux.exe,那个文件一旦没铺到位,设备就一条远程通道都不剩(实测过这样一台在线设备)。 func TestExecShellCommandUsesRealInterpreter(t *testing.T) { shell, args := execShellCommand(`echo hi`) if shell == "" { t.Fatal("解释器不能为空") } if runtime.GOOS == "windows" { if strings.Contains(shell, "/bin/sh") { t.Fatalf("Windows 上绝不能回落到 /bin/sh,得 %q", shell) } for _, a := range args { if a == "-lc" { t.Fatal("-lc 是 POSIX shell 的参数,Windows 上非法") } } } else { if len(args) < 2 || args[0] != "-lc" { t.Fatalf("Unix 应走登录 shell -lc,得 %v", args) } } // 命令文本必须原样作为最后一个参数传下去,不能被拼进 shell 路径或丢失。 if args[len(args)-1] != `echo hi` { t.Fatalf("命令文本应原样传递,得 %q", args[len(args)-1]) } } // Unix:SHELL 为空时回落 /bin/sh;有值时用用户的 shell。 func TestExecShellCommandHonorsSHELL(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("Unix-only") } t.Setenv("SHELL", "") if sh, _ := execShellCommand("x"); sh != "/bin/sh" { t.Fatalf("SHELL 为空应回落 /bin/sh,得 %q", sh) } t.Setenv("SHELL", "/bin/zsh") if sh, _ := execShellCommand("x"); sh != "/bin/zsh" { t.Fatalf("应使用 $SHELL,得 %q", sh) } } // 解释器要么是绝对路径(LookPath 解出来的),要么至少能在本机找到 —— 别给一个不存在的名字。 func TestExecShellInterpreterExists(t *testing.T) { shell, _ := execShellCommand("x") if strings.ContainsAny(shell, `/\`) { if _, err := os.Stat(shell); err != nil { t.Fatalf("解释器 %q 在本机不存在: %v", shell, err) } } }