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