shell.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { ShellSelect } from "@opencode-ai/core/shell/select"
  4. import { FSUtil } from "@opencode-ai/util/fs-util"
  5. import { which } from "@opencode-ai/core/util/which"
  6. const withShell = async (shell: string | undefined, fn: () => void | Promise<void>) => {
  7. const prev = process.env.SHELL
  8. if (shell === undefined) delete process.env.SHELL
  9. else process.env.SHELL = shell
  10. ShellSelect.acceptable.reset()
  11. ShellSelect.preferred.reset()
  12. try {
  13. await fn()
  14. } finally {
  15. if (prev === undefined) delete process.env.SHELL
  16. else process.env.SHELL = prev
  17. ShellSelect.acceptable.reset()
  18. ShellSelect.preferred.reset()
  19. }
  20. }
  21. describe("shell", () => {
  22. test("normalizes shell names", () => {
  23. expect(ShellSelect.name("/bin/bash")).toBe("bash")
  24. if (process.platform === "win32") {
  25. expect(ShellSelect.name("C:/tools/NU.EXE")).toBe("nu")
  26. expect(ShellSelect.name("C:/tools/PWSH.EXE")).toBe("pwsh")
  27. }
  28. })
  29. test("detects login shells", () => {
  30. expect(ShellSelect.login("/bin/bash")).toBe(true)
  31. expect(ShellSelect.login("C:/tools/pwsh.exe")).toBe(false)
  32. })
  33. test("falls back when configured shell cannot be resolved", async () => {
  34. await withShell(undefined, async () => {
  35. const preferred = ShellSelect.preferred()
  36. const acceptable = ShellSelect.acceptable()
  37. expect(ShellSelect.preferred("opencode-missing-shell")).toBe(preferred)
  38. expect(ShellSelect.acceptable("opencode-missing-shell")).toBe(acceptable)
  39. })
  40. })
  41. test("falls back for terminal-only acceptable shells", () => {
  42. expect(ShellSelect.name(ShellSelect.acceptable("fish"))).not.toBe("fish")
  43. expect(ShellSelect.name(ShellSelect.acceptable("nu"))).not.toBe("nu")
  44. })
  45. test("builds command args per shell family", () => {
  46. expect(ShellSelect.args("/bin/sh", "echo hi")).toEqual(["-c", "echo hi"])
  47. expect(ShellSelect.args("/usr/bin/fish", "echo hi")).toEqual(["-c", "echo hi"])
  48. expect(ShellSelect.args("/bin/zsh", "echo hi")).toEqual(["-c", "echo hi"])
  49. expect(ShellSelect.args("/bin/bash", "echo hi")).toEqual(["-c", "echo hi"])
  50. expect(ShellSelect.args("pwsh", "Write-Output hi")).toEqual([
  51. "-NoLogo",
  52. "-NoProfile",
  53. "-NonInteractive",
  54. "-Command",
  55. "Write-Output hi",
  56. ])
  57. })
  58. if (process.platform === "win32") {
  59. test("rejects blacklisted shells case-insensitively", async () => {
  60. await withShell("NU.EXE", async () => {
  61. expect(ShellSelect.name(ShellSelect.acceptable())).not.toBe("nu")
  62. })
  63. })
  64. test("normalizes Git Bash shell paths from env", async () => {
  65. const shell = "/cygdrive/c/Program Files/Git/bin/bash.exe"
  66. await withShell(shell, async () => {
  67. expect(ShellSelect.preferred()).toBe(FSUtil.windowsPath(shell))
  68. })
  69. })
  70. test("resolves /usr/bin/bash from env to Git Bash", async () => {
  71. const bash = ShellSelect.gitbash()
  72. if (!bash) return
  73. await withShell("/usr/bin/bash", async () => {
  74. expect(ShellSelect.acceptable()).toBe(bash)
  75. expect(ShellSelect.preferred()).toBe(bash)
  76. })
  77. })
  78. test("resolves bare bash to Git Bash before PATH", async () => {
  79. const bash = ShellSelect.gitbash()
  80. if (!bash) return
  81. expect(ShellSelect.acceptable("bash")).toBe(bash)
  82. expect(ShellSelect.preferred("bash")).toBe(bash)
  83. await withShell("bash", async () => {
  84. expect(ShellSelect.acceptable()).toBe(bash)
  85. expect(ShellSelect.preferred()).toBe(bash)
  86. })
  87. })
  88. test("resolves bare PowerShell shells", async () => {
  89. const shell = which("pwsh") || which("powershell")
  90. if (!shell) return
  91. await withShell(path.win32.basename(shell), async () => {
  92. expect(ShellSelect.preferred()).toBe(shell)
  93. })
  94. })
  95. }
  96. })