2
0

shell.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { Shell } from "../../src/shell/shell"
  4. import { Filesystem } from "../../src/util/filesystem"
  5. const withShell = async (shell: string | undefined, fn: () => void | Promise<void>) => {
  6. const prev = process.env.SHELL
  7. if (shell === undefined) delete process.env.SHELL
  8. else process.env.SHELL = shell
  9. Shell.acceptable.reset()
  10. Shell.preferred.reset()
  11. try {
  12. await fn()
  13. } finally {
  14. if (prev === undefined) delete process.env.SHELL
  15. else process.env.SHELL = prev
  16. Shell.acceptable.reset()
  17. Shell.preferred.reset()
  18. }
  19. }
  20. describe("shell", () => {
  21. test("normalizes shell names", () => {
  22. expect(Shell.name("/bin/bash")).toBe("bash")
  23. if (process.platform === "win32") {
  24. expect(Shell.name("C:/tools/NU.EXE")).toBe("nu")
  25. expect(Shell.name("C:/tools/PWSH.EXE")).toBe("pwsh")
  26. }
  27. })
  28. test("detects login shells", () => {
  29. expect(Shell.login("/bin/bash")).toBe(true)
  30. expect(Shell.login("C:/tools/pwsh.exe")).toBe(false)
  31. })
  32. test("detects posix shells", () => {
  33. expect(Shell.posix("/bin/bash")).toBe(true)
  34. expect(Shell.posix("/bin/fish")).toBe(false)
  35. expect(Shell.posix("C:/tools/pwsh.exe")).toBe(false)
  36. })
  37. if (process.platform === "win32") {
  38. test("rejects blacklisted shells case-insensitively", async () => {
  39. await withShell("NU.EXE", async () => {
  40. expect(Shell.name(Shell.acceptable())).not.toBe("nu")
  41. })
  42. })
  43. test("normalizes Git Bash shell paths from env", async () => {
  44. const shell = "/cygdrive/c/Program Files/Git/bin/bash.exe"
  45. await withShell(shell, async () => {
  46. expect(Shell.preferred()).toBe(Filesystem.windowsPath(shell))
  47. })
  48. })
  49. test("resolves /usr/bin/bash from env to Git Bash", async () => {
  50. const bash = Shell.gitbash()
  51. if (!bash) return
  52. await withShell("/usr/bin/bash", async () => {
  53. expect(Shell.acceptable()).toBe(bash)
  54. expect(Shell.preferred()).toBe(bash)
  55. })
  56. })
  57. test("resolves bare PowerShell shells", async () => {
  58. const shell = Bun.which("pwsh") || Bun.which("powershell")
  59. if (!shell) return
  60. await withShell(path.win32.basename(shell), async () => {
  61. expect(Shell.preferred()).toBe(shell)
  62. })
  63. })
  64. }
  65. })