worktree-remove.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { $ } from "bun"
  2. import { describe, expect } from "bun:test"
  3. import * as fs from "fs/promises"
  4. import path from "path"
  5. import { Effect, Layer } from "effect"
  6. import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
  7. import { Worktree } from "../../src/worktree"
  8. import { provideTmpdirInstance } from "../fixture/fixture"
  9. import { testEffect } from "../lib/effect"
  10. const it = testEffect(Layer.mergeAll(Worktree.defaultLayer, CrossSpawnSpawner.defaultLayer))
  11. const wintest = process.platform === "win32" ? it.live : it.live.skip
  12. describe("Worktree.remove", () => {
  13. it.live("continues when git remove exits non-zero after detaching", () =>
  14. provideTmpdirInstance(
  15. (root) =>
  16. Effect.gen(function* () {
  17. const svc = yield* Worktree.Service
  18. const name = `remove-regression-${Date.now().toString(36)}`
  19. const branch = `opencode/${name}`
  20. const dir = path.join(root, "..", name)
  21. yield* Effect.promise(() => $`git worktree add --no-checkout -b ${branch} ${dir}`.cwd(root).quiet())
  22. yield* Effect.promise(() => $`git reset --hard`.cwd(dir).quiet())
  23. const real = (yield* Effect.promise(() => $`which git`.quiet().text())).trim()
  24. expect(real).toBeTruthy()
  25. const bin = path.join(root, "bin")
  26. const shim = path.join(bin, "git")
  27. yield* Effect.promise(() => fs.mkdir(bin, { recursive: true }))
  28. yield* Effect.promise(() =>
  29. Bun.write(
  30. shim,
  31. [
  32. "#!/bin/bash",
  33. `REAL_GIT=${JSON.stringify(real)}`,
  34. 'if [ "$1" = "worktree" ] && [ "$2" = "remove" ]; then',
  35. ' "$REAL_GIT" "$@" >/dev/null 2>&1',
  36. ' echo "fatal: failed to remove worktree: Directory not empty" >&2',
  37. " exit 1",
  38. "fi",
  39. 'exec "$REAL_GIT" "$@"',
  40. ].join("\n"),
  41. ),
  42. )
  43. yield* Effect.promise(() => fs.chmod(shim, 0o755))
  44. const prev = yield* Effect.acquireRelease(
  45. Effect.sync(() => {
  46. const prev = process.env.PATH ?? ""
  47. process.env.PATH = `${bin}${path.delimiter}${prev}`
  48. return prev
  49. }),
  50. (prev) =>
  51. Effect.sync(() => {
  52. process.env.PATH = prev
  53. }),
  54. )
  55. void prev
  56. const ok = yield* svc.remove({ directory: dir })
  57. expect(ok).toBe(true)
  58. expect(
  59. yield* Effect.promise(() =>
  60. fs
  61. .stat(dir)
  62. .then(() => true)
  63. .catch(() => false),
  64. ),
  65. ).toBe(false)
  66. const list = yield* Effect.promise(() => $`git worktree list --porcelain`.cwd(root).quiet().text())
  67. expect(list).not.toContain(`worktree ${dir}`)
  68. const ref = yield* Effect.promise(() =>
  69. $`git show-ref --verify --quiet refs/heads/${branch}`.cwd(root).quiet().nothrow(),
  70. )
  71. expect(ref.exitCode).not.toBe(0)
  72. }),
  73. { git: true },
  74. ),
  75. )
  76. wintest("stops fsmonitor before removing a worktree", () =>
  77. provideTmpdirInstance(
  78. (root) =>
  79. Effect.gen(function* () {
  80. const svc = yield* Worktree.Service
  81. const name = `remove-fsmonitor-${Date.now().toString(36)}`
  82. const branch = `opencode/${name}`
  83. const dir = path.join(root, "..", name)
  84. yield* Effect.promise(() => $`git worktree add --no-checkout -b ${branch} ${dir}`.cwd(root).quiet())
  85. yield* Effect.promise(() => $`git reset --hard`.cwd(dir).quiet())
  86. yield* Effect.promise(() => $`git config core.fsmonitor true`.cwd(dir).quiet())
  87. yield* Effect.promise(() => $`git fsmonitor--daemon stop`.cwd(dir).quiet().nothrow())
  88. yield* Effect.promise(() => Bun.write(path.join(dir, "tracked.txt"), "next\n"))
  89. yield* Effect.promise(() => $`git diff`.cwd(dir).quiet())
  90. const before = yield* Effect.promise(() => $`git fsmonitor--daemon status`.cwd(dir).quiet().nothrow())
  91. expect(before.exitCode).toBe(0)
  92. const ok = yield* svc.remove({ directory: dir })
  93. expect(ok).toBe(true)
  94. expect(
  95. yield* Effect.promise(() =>
  96. fs
  97. .stat(dir)
  98. .then(() => true)
  99. .catch(() => false),
  100. ),
  101. ).toBe(false)
  102. const ref = yield* Effect.promise(() =>
  103. $`git show-ref --verify --quiet refs/heads/${branch}`.cwd(root).quiet().nothrow(),
  104. )
  105. expect(ref.exitCode).not.toBe(0)
  106. }),
  107. { git: true },
  108. ),
  109. )
  110. })