git.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { $ } from "bun"
  2. import { describe, expect } from "bun:test"
  3. import fs from "fs/promises"
  4. import path from "path"
  5. import { Effect } from "effect"
  6. import { Git } from "../../src/git"
  7. import { tmpdir } from "../fixture/fixture"
  8. import { testEffect } from "../lib/effect"
  9. const weird = process.platform === "win32" ? "space file.txt" : "tab\tfile.txt"
  10. const it = testEffect(Git.defaultLayer)
  11. const scopedTmpdir = (options?: Parameters<typeof tmpdir>[0]) =>
  12. Effect.acquireRelease(
  13. Effect.promise(() => tmpdir(options)),
  14. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  15. )
  16. describe("Git", () => {
  17. it.live("branch() returns current branch name", () =>
  18. Effect.gen(function* () {
  19. const tmp = yield* scopedTmpdir({ git: true })
  20. const git = yield* Git.Service
  21. const branch = yield* git.branch(tmp.path)
  22. expect(branch).toBeDefined()
  23. expect(typeof branch).toBe("string")
  24. }),
  25. )
  26. it.live("branch() returns undefined for non-git directories", () =>
  27. Effect.gen(function* () {
  28. const tmp = yield* scopedTmpdir()
  29. const git = yield* Git.Service
  30. const branch = yield* git.branch(tmp.path)
  31. expect(branch).toBeUndefined()
  32. }),
  33. )
  34. it.live("branch() returns undefined for detached HEAD", () =>
  35. Effect.gen(function* () {
  36. const tmp = yield* scopedTmpdir({ git: true })
  37. const hash = (yield* Effect.promise(() => $`git rev-parse HEAD`.cwd(tmp.path).quiet().text())).trim()
  38. yield* Effect.promise(() => $`git checkout --detach ${hash}`.cwd(tmp.path).quiet())
  39. const git = yield* Git.Service
  40. const branch = yield* git.branch(tmp.path)
  41. expect(branch).toBeUndefined()
  42. }),
  43. )
  44. it.live("defaultBranch() uses init.defaultBranch when available", () =>
  45. Effect.gen(function* () {
  46. const tmp = yield* scopedTmpdir({ git: true })
  47. yield* Effect.promise(() => $`git branch -M trunk`.cwd(tmp.path).quiet())
  48. yield* Effect.promise(() => $`git config init.defaultBranch trunk`.cwd(tmp.path).quiet())
  49. const git = yield* Git.Service
  50. const branch = yield* git.defaultBranch(tmp.path)
  51. expect(branch?.name).toBe("trunk")
  52. expect(branch?.ref).toBe("trunk")
  53. }),
  54. )
  55. it.live("status() handles special filenames", () =>
  56. Effect.gen(function* () {
  57. const tmp = yield* scopedTmpdir({ git: true })
  58. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "hello\n", "utf-8"))
  59. const git = yield* Git.Service
  60. const status = yield* git.status(tmp.path)
  61. expect(status).toEqual(
  62. expect.arrayContaining([
  63. expect.objectContaining({
  64. file: weird,
  65. status: "added",
  66. }),
  67. ]),
  68. )
  69. }),
  70. )
  71. it.live("diff(), stats(), and mergeBase() parse tracked changes", () =>
  72. Effect.gen(function* () {
  73. const tmp = yield* scopedTmpdir({ git: true })
  74. yield* Effect.promise(() => $`git branch -M main`.cwd(tmp.path).quiet())
  75. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "before\n", "utf-8"))
  76. yield* Effect.promise(() => $`git add .`.cwd(tmp.path).quiet())
  77. yield* Effect.promise(() => $`git commit --no-gpg-sign -m "add file"`.cwd(tmp.path).quiet())
  78. yield* Effect.promise(() => $`git checkout -b feature/test`.cwd(tmp.path).quiet())
  79. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "after\n", "utf-8"))
  80. const git = yield* Git.Service
  81. const [base, diff, stats] = yield* Effect.all([
  82. git.mergeBase(tmp.path, "main"),
  83. git.diff(tmp.path, "HEAD"),
  84. git.stats(tmp.path, "HEAD"),
  85. ])
  86. expect(base).toBeTruthy()
  87. expect(diff).toEqual(
  88. expect.arrayContaining([
  89. expect.objectContaining({
  90. file: weird,
  91. status: "modified",
  92. }),
  93. ]),
  94. )
  95. expect(stats).toEqual(
  96. expect.arrayContaining([
  97. expect.objectContaining({
  98. file: weird,
  99. additions: 1,
  100. deletions: 1,
  101. }),
  102. ]),
  103. )
  104. }),
  105. )
  106. it.live("patch() returns capped native patch output", () =>
  107. Effect.gen(function* () {
  108. const tmp = yield* scopedTmpdir({ git: true })
  109. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "before\n", "utf-8"))
  110. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "other.txt"), "old\n", "utf-8"))
  111. yield* Effect.promise(() => $`git add .`.cwd(tmp.path).quiet())
  112. yield* Effect.promise(() => $`git commit --no-gpg-sign -m "add file"`.cwd(tmp.path).quiet())
  113. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "after\n", "utf-8"))
  114. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "other.txt"), "new\n", "utf-8"))
  115. const git = yield* Git.Service
  116. const [patch, all, capped] = yield* Effect.all([
  117. git.patch(tmp.path, "HEAD", weird, { context: 2_147_483_647 }),
  118. git.patchAll(tmp.path, "HEAD", { context: 2_147_483_647 }),
  119. git.patch(tmp.path, "HEAD", weird, { maxOutputBytes: 1 }),
  120. ])
  121. expect(patch.truncated).toBe(false)
  122. expect(patch.text).toContain("diff --git")
  123. expect(patch.text).toContain("-before")
  124. expect(patch.text).toContain("+after")
  125. expect(all.truncated).toBe(false)
  126. expect(all.text).toContain("diff --git")
  127. expect(all.text).toContain("other.txt")
  128. expect(all.text).toContain("+new")
  129. expect(capped.truncated).toBe(true)
  130. expect(capped.text).toBe("")
  131. }),
  132. )
  133. it.live("patchUntracked() and statUntracked() handle added files", () =>
  134. Effect.gen(function* () {
  135. const tmp = yield* scopedTmpdir({ git: true })
  136. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, weird), "one\ntwo\n", "utf-8"))
  137. const git = yield* Git.Service
  138. const [patch, stat] = yield* Effect.all([
  139. git.patchUntracked(tmp.path, weird, { context: 2_147_483_647 }),
  140. git.statUntracked(tmp.path, weird),
  141. ])
  142. expect(patch.truncated).toBe(false)
  143. expect(patch.text).toContain("diff --git")
  144. expect(patch.text).toContain("+one")
  145. expect(patch.text).toContain("+two")
  146. expect(stat).toEqual(expect.objectContaining({ file: weird, additions: 2, deletions: 0 }))
  147. }),
  148. )
  149. it.live("show() returns empty text for binary blobs", () =>
  150. Effect.gen(function* () {
  151. const tmp = yield* scopedTmpdir({ git: true })
  152. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "bin.dat"), new Uint8Array([0, 1, 2, 3])))
  153. yield* Effect.promise(() => $`git add .`.cwd(tmp.path).quiet())
  154. yield* Effect.promise(() => $`git commit --no-gpg-sign -m "add binary"`.cwd(tmp.path).quiet())
  155. const git = yield* Git.Service
  156. const text = yield* git.show(tmp.path, "HEAD", "bin.dat")
  157. expect(text).toBe("")
  158. }),
  159. )
  160. })