install-lefthook.spec.ts 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. import { spawn, spawnSync } from 'node:child_process'
  2. import {
  3. chmodSync,
  4. existsSync,
  5. linkSync,
  6. mkdirSync,
  7. mkdtempSync,
  8. lstatSync,
  9. readFileSync,
  10. renameSync,
  11. rmSync,
  12. symlinkSync,
  13. writeFileSync,
  14. } from 'node:fs'
  15. import { tmpdir } from 'node:os'
  16. import { dirname, isAbsolute, join, resolve } from 'node:path'
  17. import { fileURLToPath } from 'node:url'
  18. import { afterEach, describe, expect, it } from 'vitest'
  19. import { removeFixtureSafely, unlinkFixtureLinks } from './test-fixture-cleanup.ts'
  20. const installer = fileURLToPath(new URL('./install-lefthook.mjs', import.meta.url))
  21. const pairingMergeDriver = 'scripts/merge-translation-pairing-driver.sh %O %A %B %P'
  22. const scriptsDirectory = fileURLToPath(new URL('.', import.meta.url))
  23. const tsxPackageDirectory = dirname(fileURLToPath(import.meta.resolve('tsx/package.json')))
  24. const fixtures: string[] = []
  25. // Multi-worktree cases spawn several Git and Node subprocesses; native Windows
  26. // coverage concurrency can delay them without changing installer behavior.
  27. const MULTI_PROCESS_TEST_TIMEOUT_MS = 30_000
  28. interface Fixture {
  29. container: string
  30. env: NodeJS.ProcessEnv
  31. linked: string
  32. main: string
  33. }
  34. interface CommandResult {
  35. status: number | null
  36. stderr: string
  37. stdout: string
  38. }
  39. afterEach(() => {
  40. for (const fixture of fixtures.splice(0)) removeFixtureSafely(fixture)
  41. })
  42. function commandResult(command: string, args: string[], cwd: string, env: NodeJS.ProcessEnv): CommandResult {
  43. const result = spawnSync(command, args, { cwd, encoding: 'utf8', env })
  44. return { status: result.status, stderr: result.stderr, stdout: result.stdout }
  45. }
  46. function gitResult(fixture: Fixture, cwd: string, args: string[]): CommandResult {
  47. return commandResult('git', args, cwd, fixture.env)
  48. }
  49. function git(fixture: Fixture, cwd: string, args: string[]): string {
  50. const result = gitResult(fixture, cwd, args)
  51. if (result.status !== 0) {
  52. throw new Error(`git ${args.join(' ')} failed: ${result.stderr}`)
  53. }
  54. return result.stdout.trim()
  55. }
  56. function write(path: string, content: string, mode?: number): void {
  57. mkdirSync(dirname(path), { recursive: true })
  58. writeFileSync(path, content, mode === undefined ? undefined : { mode })
  59. }
  60. function fakeLefthookSource(): string {
  61. return `#!/usr/bin/env node
  62. import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
  63. import { execFileSync } from 'node:child_process'
  64. import { join } from 'node:path'
  65. if (process.argv.slice(2).join(' ') !== 'install --force') process.exit(64)
  66. const rootOutput = execFileSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' })
  67. const root = rootOutput.endsWith('\\n') ? rootOutput.slice(0, -1) : rootOutput
  68. const forbiddenConfigKey = process.env.DSH_TEST_FORBIDDEN_GIT_CONFIG_KEY
  69. if (forbiddenConfigKey !== undefined) {
  70. try {
  71. execFileSync('git', ['config', '--get', forbiddenConfigKey], { encoding: 'utf8' })
  72. process.exit(92)
  73. } catch (error) {
  74. if (error === null || typeof error !== 'object' || !('status' in error) || error.status !== 1) throw error
  75. }
  76. }
  77. const hooksPath = execFileSync('git', ['config', '--get', 'core.hooksPath'], { encoding: 'utf8' }).trim()
  78. mkdirSync(hooksPath, { recursive: true })
  79. const running = join(hooksPath, '.fake-lefthook-running')
  80. try {
  81. writeFileSync(running, String(process.pid), { flag: 'wx' })
  82. } catch {
  83. process.exit(91)
  84. }
  85. const delay = Number(process.env.DSH_TEST_LEFTHOOK_DELAY_MS ?? 0)
  86. if (delay > 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay)
  87. const shouldFail = process.env.DSH_TEST_LEFTHOOK_FAIL === '1'
  88. if (!shouldFail) {
  89. const binary = join(root, 'node_modules', '.bin', process.platform === 'win32' ? 'lefthook.cmd' : 'lefthook')
  90. const config = readFileSync(join(root, 'lefthook.yml'), 'utf8').trim()
  91. const hook = \`#!/bin/sh\\n# root=\${root}\\n# binary=\${binary}\\n# config=\${config}\\nexit 0\\n\`
  92. for (const name of ['pre-commit', 'pre-merge-commit', 'pre-push']) writeFileSync(join(hooksPath, name), hook, { mode: 0o755 })
  93. }
  94. if (existsSync(running)) unlinkSync(running)
  95. if (process.env.DSH_TEST_LEFTHOOK_BREAK_WORKTREE_CONFIG === '1') {
  96. const configPath = execFileSync('git', ['rev-parse', '--git-path', 'config.worktree'], { encoding: 'utf8' }).trim()
  97. writeFileSync(configPath, '[invalid\\n')
  98. }
  99. if (shouldFail) process.exit(77)
  100. `
  101. }
  102. function installFakeLefthook(root: string): void {
  103. const binDirectory = join(root, 'node_modules/.bin')
  104. mkdirSync(binDirectory, { recursive: true })
  105. writeFileSync(join(binDirectory, 'fake-lefthook.mjs'), fakeLefthookSource())
  106. if (process.platform === 'win32') {
  107. writeFileSync(
  108. join(binDirectory, 'lefthook.cmd'),
  109. `@echo off\r\n"${process.execPath}" "%~dp0\\fake-lefthook.mjs" %*\r\n`,
  110. )
  111. return
  112. }
  113. const shim = join(binDirectory, 'lefthook')
  114. writeFileSync(shim, `#!/bin/sh\nexec "${process.execPath}" "$(dirname "$0")/fake-lefthook.mjs" "$@"\n`)
  115. chmodSync(shim, 0o755)
  116. }
  117. function installPairingProbeFixture(root: string): void {
  118. const linkType = process.platform === 'win32' ? 'junction' : 'dir'
  119. symlinkSync(scriptsDirectory, join(root, 'scripts'), linkType)
  120. symlinkSync(tsxPackageDirectory, join(root, 'node_modules/tsx'), linkType)
  121. }
  122. function createFixture(names: { main?: string; linked?: string } = {}): Fixture {
  123. const container = mkdtempSync(join(tmpdir(), 'dsh-lefthook-'))
  124. fixtures.push(container)
  125. const main = join(container, names.main ?? 'main')
  126. const linked = join(container, names.linked ?? 'linked')
  127. const env: NodeJS.ProcessEnv = {
  128. ...process.env,
  129. CI: 'false',
  130. GITHUB_ACTIONS: 'false',
  131. GIT_AUTHOR_EMAIL: 'hooks@example.test',
  132. GIT_AUTHOR_NAME: 'Hooks Test',
  133. GIT_COMMITTER_EMAIL: 'hooks@example.test',
  134. GIT_COMMITTER_NAME: 'Hooks Test',
  135. GIT_CONFIG_GLOBAL: join(container, 'global.gitconfig'),
  136. GIT_CONFIG_NOSYSTEM: '1',
  137. HOME: container,
  138. XDG_CONFIG_HOME: join(container, '.config'),
  139. }
  140. const fixture = { container, env, linked, main }
  141. mkdirSync(main)
  142. git(fixture, container, ['init', main])
  143. write(join(main, 'README.md'), '# fixture\n')
  144. git(fixture, main, ['add', 'README.md'])
  145. git(fixture, main, ['commit', '-m', 'fixture'])
  146. git(fixture, main, ['worktree', 'add', '-b', 'linked', linked])
  147. write(join(main, 'lefthook.yml'), 'main-worktree-config\n')
  148. write(join(linked, 'lefthook.yml'), 'linked-worktree-config\n')
  149. installFakeLefthook(main)
  150. installFakeLefthook(linked)
  151. installPairingProbeFixture(main)
  152. installPairingProbeFixture(linked)
  153. return fixture
  154. }
  155. function gitDirectory(fixture: Fixture, root: string): string {
  156. return git(fixture, root, ['rev-parse', '--absolute-git-dir'])
  157. }
  158. function commonDirectory(fixture: Fixture): string {
  159. const output = git(fixture, fixture.main, ['rev-parse', '--git-common-dir'])
  160. return isAbsolute(output) ? output : resolve(fixture.main, output)
  161. }
  162. function hooksPath(fixture: Fixture, root: string): string {
  163. return join(gitDirectory(fixture, root), 'dsh-hooks')
  164. }
  165. function installLockPath(fixture: Fixture): string {
  166. return join(commonDirectory(fixture), 'dsh-lefthook-install.lock')
  167. }
  168. async function waitForPath(path: string): Promise<void> {
  169. const deadline = Date.now() + 10_000
  170. while (!existsSync(path)) {
  171. if (Date.now() >= deadline) throw new Error(`timed out waiting for ${path}`)
  172. await new Promise(resolveWait => setTimeout(resolveWait, 10))
  173. }
  174. }
  175. function runInstaller(
  176. fixture: Fixture,
  177. root: string,
  178. extraEnv: NodeJS.ProcessEnv = {},
  179. ): Promise<CommandResult> {
  180. return new Promise((resolveResult, reject) => {
  181. const child = spawn(process.execPath, [installer], {
  182. cwd: root,
  183. env: { ...fixture.env, ...extraEnv },
  184. stdio: ['ignore', 'pipe', 'pipe'],
  185. })
  186. let stdout = ''
  187. let stderr = ''
  188. child.stdout.on('data', (chunk: Buffer) => { stdout += chunk.toString() })
  189. child.stderr.on('data', (chunk: Buffer) => { stderr += chunk.toString() })
  190. child.on('error', reject)
  191. child.on('close', (status) => { resolveResult({ status, stderr, stdout }) })
  192. })
  193. }
  194. describe('worktree-local Lefthook installer', { timeout: 30_000 }, () => {
  195. for (const [label, extraEnv] of [
  196. ['CI', { CI: 'true' }],
  197. ['GitHub Actions', { GITHUB_ACTIONS: 'true' }],
  198. ] satisfies [string, NodeJS.ProcessEnv][]) {
  199. it(`skips hook installation when ${label} marks an automated job`, async () => {
  200. const fixture = createFixture()
  201. const common = commonDirectory(fixture)
  202. const missingInclude = join(fixture.container, 'missing-ci-credentials.gitconfig')
  203. git(fixture, fixture.main, [
  204. 'config',
  205. '--local',
  206. 'includeIf.gitdir:/github/workspace/.git.path',
  207. missingInclude,
  208. ])
  209. const result = await runInstaller(fixture, fixture.main, extraEnv)
  210. expect(result.status, result.stderr).toBe(0)
  211. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  212. expect(git(fixture, fixture.main, ['config', '--get', 'core.repositoryFormatVersion'])).toBe('0')
  213. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  214. expect(existsSync(join(common, 'config.worktree'))).toBe(false)
  215. expect(gitResult(fixture, fixture.main, [
  216. 'config', '--get', 'merge.dsh-translation-pairing.driver',
  217. ]).status).toBe(1)
  218. })
  219. }
  220. it('isolates main and linked worktrees without changing legacy common hooks', async () => {
  221. const fixture = createFixture()
  222. const common = commonDirectory(fixture)
  223. const legacyHook = join(common, 'hooks/pre-commit')
  224. write(legacyHook, '#!/bin/sh\n# legacy hook\n', 0o755)
  225. const mainInstall = await runInstaller(fixture, fixture.main)
  226. const linkedInstall = await runInstaller(fixture, fixture.linked)
  227. expect(mainInstall.status, mainInstall.stderr).toBe(0)
  228. expect(linkedInstall.status, linkedInstall.stderr).toBe(0)
  229. const mainHooks = hooksPath(fixture, fixture.main)
  230. const linkedHooks = hooksPath(fixture, fixture.linked)
  231. expect(mainHooks).not.toBe(linkedHooks)
  232. expect(git(fixture, fixture.main, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(mainHooks)
  233. expect(git(fixture, fixture.linked, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(linkedHooks)
  234. expect(git(fixture, fixture.main, [
  235. 'config', '--worktree', '--get', 'merge.dsh-translation-pairing.driver',
  236. ])).toBe(pairingMergeDriver)
  237. expect(git(fixture, fixture.linked, [
  238. 'config', '--worktree', '--get', 'merge.dsh-translation-pairing.driver',
  239. ])).toBe(pairingMergeDriver)
  240. const mainHook = readFileSync(join(mainHooks, 'pre-commit'), 'utf8')
  241. const linkedHook = readFileSync(join(linkedHooks, 'pre-commit'), 'utf8')
  242. const canonicalMain = git(fixture, fixture.main, ['rev-parse', '--show-toplevel'])
  243. const canonicalLinked = git(fixture, fixture.linked, ['rev-parse', '--show-toplevel'])
  244. expect(mainHook).toContain(`# root=${canonicalMain}`)
  245. expect(mainHook).toContain('# config=main-worktree-config')
  246. expect(mainHook).not.toContain(canonicalLinked)
  247. expect(linkedHook).toContain(`# root=${canonicalLinked}`)
  248. expect(linkedHook).toContain('# config=linked-worktree-config')
  249. expect(linkedHook).not.toContain(canonicalMain)
  250. expect(existsSync(join(mainHooks, 'pre-merge-commit'))).toBe(true)
  251. expect(existsSync(join(linkedHooks, 'pre-merge-commit'))).toBe(true)
  252. expect(readFileSync(legacyHook, 'utf8')).toBe('#!/bin/sh\n# legacy hook\n')
  253. const commonConfig = join(common, 'config')
  254. expect(git(fixture, fixture.main, ['config', '--file', commonConfig, '--get', 'core.repositoryFormatVersion'])).toBe('1')
  255. expect(git(fixture, fixture.main, ['config', '--file', commonConfig, '--get', 'extensions.worktreeConfig'])).toBe('true')
  256. expect(gitResult(fixture, fixture.main, ['config', '--file', commonConfig, '--get', 'core.bare']).status).toBe(1)
  257. const mainHookBeforeRemoval = readFileSync(join(mainHooks, 'pre-commit'), 'utf8')
  258. // Windows Git follows the fixture's MOUNT_POINT junctions into their real
  259. // targets while removing a worktree; unlink them first so the removal
  260. // cannot delete the repository's scripts/ or tsx package.
  261. unlinkFixtureLinks(fixture.linked)
  262. git(fixture, fixture.main, ['worktree', 'remove', '--force', fixture.linked])
  263. expect(readFileSync(join(mainHooks, 'pre-commit'), 'utf8')).toBe(mainHookBeforeRemoval)
  264. expect(readFileSync(legacyHook, 'utf8')).toBe('#!/bin/sh\n# legacy hook\n')
  265. }, MULTI_PROCESS_TEST_TIMEOUT_MS)
  266. it('replaces the owned hook path Git copies into a newly added worktree', async () => {
  267. const fixture = createFixture()
  268. const mainInstall = await runInstaller(fixture, fixture.main)
  269. expect(mainInstall.status, mainInstall.stderr).toBe(0)
  270. const mainHooks = hooksPath(fixture, fixture.main)
  271. const mainHookBefore = readFileSync(join(mainHooks, 'pre-commit'), 'utf8')
  272. const lateLinked = join(fixture.container, 'late-linked')
  273. git(fixture, fixture.main, ['worktree', 'add', '-b', 'late-linked', lateLinked])
  274. write(join(lateLinked, 'lefthook.yml'), 'late-linked-worktree-config\n')
  275. installFakeLefthook(lateLinked)
  276. installPairingProbeFixture(lateLinked)
  277. expect(git(fixture, lateLinked, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(mainHooks)
  278. const linkedInstall = await runInstaller(fixture, lateLinked)
  279. expect(linkedInstall.status, linkedInstall.stderr).toBe(0)
  280. const linkedHooks = hooksPath(fixture, lateLinked)
  281. expect(linkedHooks).not.toBe(mainHooks)
  282. expect(git(fixture, lateLinked, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(linkedHooks)
  283. expect(readFileSync(join(linkedHooks, 'pre-commit'), 'utf8')).toContain(
  284. '# config=late-linked-worktree-config',
  285. )
  286. expect(readFileSync(join(mainHooks, 'pre-commit'), 'utf8')).toBe(mainHookBefore)
  287. }, MULTI_PROCESS_TEST_TIMEOUT_MS)
  288. it('serializes concurrent installs and keeps repeated output stable', async () => {
  289. const fixture = createFixture()
  290. const delayed = { DSH_TEST_LEFTHOOK_DELAY_MS: '150' }
  291. const first = await Promise.all([
  292. runInstaller(fixture, fixture.main, delayed),
  293. runInstaller(fixture, fixture.linked, delayed),
  294. ])
  295. for (const result of first) expect(result.status, result.stderr).toBe(0)
  296. const mainHookPath = join(hooksPath(fixture, fixture.main), 'pre-push')
  297. const initialHook = readFileSync(mainHookPath, 'utf8')
  298. const repeated = await Promise.all([
  299. runInstaller(fixture, fixture.main, delayed),
  300. runInstaller(fixture, fixture.main, delayed),
  301. ])
  302. for (const result of repeated) expect(result.status, result.stderr).toBe(0)
  303. expect(readFileSync(mainHookPath, 'utf8')).toBe(initialHook)
  304. expect(existsSync(join(commonDirectory(fixture), 'dsh-lefthook-install.lock'))).toBe(false)
  305. expect(existsSync(join(hooksPath(fixture, fixture.main), '.fake-lefthook-running'))).toBe(false)
  306. }, MULTI_PROCESS_TEST_TIMEOUT_MS)
  307. it('waits for a concurrent installer to finish publishing its lock record', async () => {
  308. const fixture = createFixture()
  309. const lockPath = installLockPath(fixture)
  310. const publishing = runInstaller(fixture, fixture.main, {
  311. DSH_TEST_LEFTHOOK_LOCK_WRITE_DELAY_MS: '200',
  312. })
  313. await waitForPath(lockPath)
  314. expect(readFileSync(lockPath, 'utf8')).toBe('')
  315. const waiting = runInstaller(fixture, fixture.linked)
  316. const results = await Promise.all([publishing, waiting])
  317. for (const result of results) expect(result.status, result.stderr).toBe(0)
  318. expect(existsSync(lockPath)).toBe(false)
  319. })
  320. it('repairs its owned absolute hook path after the checkout moves', async () => {
  321. const fixture = createFixture()
  322. const oldRoot = fixture.main
  323. const first = await runInstaller(fixture, oldRoot)
  324. expect(first.status, first.stderr).toBe(0)
  325. const oldHooks = hooksPath(fixture, oldRoot)
  326. const movedRoot = join(fixture.container, 'moved-main')
  327. renameSync(oldRoot, movedRoot)
  328. const moved = await runInstaller(fixture, movedRoot)
  329. expect(moved.status, moved.stderr).toBe(0)
  330. const movedHooks = hooksPath(fixture, movedRoot)
  331. expect(movedHooks).not.toBe(oldHooks)
  332. expect(git(fixture, movedRoot, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(movedHooks)
  333. const canonicalMoved = git(fixture, movedRoot, ['rev-parse', '--show-toplevel'])
  334. expect(readFileSync(join(movedHooks, 'pre-commit'), 'utf8')).toContain(`# root=${canonicalMoved}`)
  335. expect(readFileSync(join(movedHooks, '.dsh-lefthook-owned'), 'utf8')).toContain(
  336. JSON.stringify(movedHooks),
  337. )
  338. }, MULTI_PROCESS_TEST_TIMEOUT_MS)
  339. it.skipIf(process.platform === 'win32')('refuses a multiply linked ownership marker before relocation rewrites it', async () => {
  340. const fixture = createFixture()
  341. const oldRoot = fixture.main
  342. const first = await runInstaller(fixture, oldRoot)
  343. expect(first.status, first.stderr).toBe(0)
  344. const oldHooks = hooksPath(fixture, oldRoot)
  345. const markerName = '.dsh-lefthook-owned'
  346. const externalMarker = join(fixture.container, 'external-marker')
  347. linkSync(join(oldHooks, markerName), externalMarker)
  348. const externalContent = readFileSync(externalMarker, 'utf8')
  349. const movedRoot = join(fixture.container, 'moved-main')
  350. renameSync(oldRoot, movedRoot)
  351. const result = await runInstaller(fixture, movedRoot)
  352. expect(result.status).toBe(1)
  353. expect(result.stderr).toContain('invalid ownership marker')
  354. expect(readFileSync(externalMarker, 'utf8')).toBe(externalContent)
  355. })
  356. it.skipIf(process.platform === 'win32')('refuses aliased generated hooks before Lefthook can overwrite their targets', async () => {
  357. for (const kind of ['symlink', 'hardlink'] as const) {
  358. const fixture = createFixture()
  359. const first = await runInstaller(fixture, fixture.main)
  360. expect(first.status, first.stderr).toBe(0)
  361. const hook = join(hooksPath(fixture, fixture.main), 'pre-commit')
  362. const externalHook = join(fixture.container, `${kind}-external-hook`)
  363. rmSync(hook)
  364. write(externalHook, `external ${kind} target\n`)
  365. if (kind === 'symlink') symlinkSync(externalHook, hook)
  366. else linkSync(externalHook, hook)
  367. const externalContent = readFileSync(externalHook, 'utf8')
  368. const result = await runInstaller(fixture, fixture.main)
  369. expect(result.status).toBe(1)
  370. expect(result.stderr).toContain('non-regular or multiply linked hook entry')
  371. expect(readFileSync(externalHook, 'utf8')).toBe(externalContent)
  372. }
  373. }, MULTI_PROCESS_TEST_TIMEOUT_MS)
  374. it('restores the marker-backed stale hook path when relocation reinstall fails', async () => {
  375. const fixture = createFixture()
  376. const oldRoot = fixture.main
  377. const first = await runInstaller(fixture, oldRoot)
  378. expect(first.status, first.stderr).toBe(0)
  379. const oldHooks = hooksPath(fixture, oldRoot)
  380. const markerName = '.dsh-lefthook-owned'
  381. const previousMarker = readFileSync(join(oldHooks, markerName), 'utf8')
  382. const movedRoot = join(fixture.container, 'moved-main')
  383. renameSync(oldRoot, movedRoot)
  384. const failed = await runInstaller(fixture, movedRoot, { DSH_TEST_LEFTHOOK_FAIL: '1' })
  385. expect(failed.status).toBe(1)
  386. expect(failed.stderr).toContain('exit status 77')
  387. const movedHooks = hooksPath(fixture, movedRoot)
  388. expect(git(fixture, movedRoot, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(oldHooks)
  389. expect(readFileSync(join(movedHooks, markerName), 'utf8')).toBe(previousMarker)
  390. })
  391. it('refuses dormant repository extensions before upgrading the repository format', async () => {
  392. const fixture = createFixture()
  393. const commonConfig = join(commonDirectory(fixture), 'config')
  394. git(fixture, fixture.main, ['config', 'extensions.dshUnknown', 'true'])
  395. expect(gitResult(fixture, fixture.main, ['status', '--porcelain']).status).toBe(0)
  396. const result = await runInstaller(fixture, fixture.main)
  397. expect(result.status).toBe(1)
  398. expect(result.stderr).toContain('dormant repository extension extensions.dshunknown')
  399. expect(git(fixture, fixture.main, [
  400. 'config', '--file', commonConfig, '--get', 'core.repositoryFormatVersion',
  401. ])).toBe('0')
  402. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  403. expect(gitResult(fixture, fixture.main, ['status', '--porcelain']).status).toBe(0)
  404. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  405. })
  406. it('refuses direct core.worktree before enabling worktree config', async () => {
  407. const fixture = createFixture()
  408. const commonConfig = join(commonDirectory(fixture), 'config')
  409. git(fixture, fixture.main, ['config', '--file', commonConfig, 'core.worktree', fixture.main])
  410. const result = await runInstaller(fixture, fixture.linked)
  411. expect(result.status).toBe(1)
  412. expect(result.stderr).toContain('core.worktree is in the common config')
  413. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  414. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  415. })
  416. it.skipIf(process.platform === 'win32')('refuses a symlinked common repository config before writing through it', async () => {
  417. const fixture = createFixture()
  418. const commonConfig = join(commonDirectory(fixture), 'config')
  419. const externalConfig = join(fixture.container, 'external-common.gitconfig')
  420. renameSync(commonConfig, externalConfig)
  421. symlinkSync(externalConfig, commonConfig)
  422. const externalContent = readFileSync(externalConfig, 'utf8')
  423. const result = await runInstaller(fixture, fixture.main)
  424. expect(result.status).toBe(1)
  425. expect(result.stderr).toContain('common repository config')
  426. expect(result.stderr).toContain('not a regular file')
  427. expect(lstatSync(commonConfig).isSymbolicLink()).toBe(true)
  428. expect(readFileSync(externalConfig, 'utf8')).toBe(externalContent)
  429. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  430. })
  431. it('leaves stale installer locks for explicit recovery', async () => {
  432. const fixture = createFixture()
  433. const lockPath = installLockPath(fixture)
  434. const completed = spawnSync(process.execPath, ['-e', ''])
  435. expect(completed.status).toBe(0)
  436. const staleRecord = `${String(completed.pid)} 00000000-0000-4000-8000-000000000000\n`
  437. writeFileSync(lockPath, staleRecord)
  438. const results = await Promise.all(Array.from(
  439. { length: 4 },
  440. () => runInstaller(fixture, fixture.main),
  441. ))
  442. for (const result of results) {
  443. expect(result.status).toBe(1)
  444. expect(result.stderr).toContain('stale Lefthook installer lock')
  445. expect(result.stderr).toContain('remove it manually')
  446. }
  447. expect(readFileSync(lockPath, 'utf8')).toBe(staleRecord)
  448. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  449. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  450. })
  451. it('leaves invalid installer locks for explicit recovery', async () => {
  452. const fixture = createFixture()
  453. const lockPath = installLockPath(fixture)
  454. const invalidRecord = 'not an installer lock\n'
  455. writeFileSync(lockPath, invalidRecord)
  456. const result = await runInstaller(fixture, fixture.main)
  457. expect(result.status).toBe(1)
  458. expect(result.stderr).toContain('invalid Lefthook installer lock')
  459. expect(result.stderr).toContain('remove it manually')
  460. expect(readFileSync(lockPath, 'utf8')).toBe(invalidRecord)
  461. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  462. })
  463. it('does not release an installer lock whose ownership changed', async () => {
  464. const fixture = createFixture()
  465. const lockPath = installLockPath(fixture)
  466. const runningPath = join(hooksPath(fixture, fixture.main), '.fake-lefthook-running')
  467. const install = runInstaller(fixture, fixture.main, { DSH_TEST_LEFTHOOK_DELAY_MS: '250' })
  468. try {
  469. await waitForPath(runningPath)
  470. } catch (error) {
  471. await install
  472. throw error
  473. }
  474. const replacementRecord = 'replacement owner\n'
  475. writeFileSync(lockPath, replacementRecord)
  476. const result = await install
  477. expect(result.status).toBe(1)
  478. expect(result.stderr).toContain('installer lock ownership changed')
  479. expect(readFileSync(lockPath, 'utf8')).toBe(replacementRecord)
  480. })
  481. it.skipIf(process.platform === 'win32')('preserves trailing spaces in worktree paths', async () => {
  482. const fixture = createFixture({ main: 'main ', linked: 'linked ' })
  483. for (const root of [fixture.main, fixture.linked]) {
  484. const result = await runInstaller(fixture, root)
  485. expect(result.status, result.stderr).toBe(0)
  486. expect(git(fixture, root, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(hooksPath(fixture, root))
  487. }
  488. })
  489. it('preserves user-owned hook paths unless an inherited value is explicitly overridden', async () => {
  490. const fixture = createFixture()
  491. const customHook = join(fixture.main, 'custom-hooks/pre-commit')
  492. write(customHook, '#!/bin/sh\n# custom hook\n', 0o755)
  493. git(fixture, fixture.main, ['config', 'core.hooksPath', 'custom-hooks'])
  494. const refused = await runInstaller(fixture, fixture.main)
  495. expect(refused.status).toBe(1)
  496. expect(refused.stderr).toContain('refusing to replace user-owned core.hooksPath')
  497. expect(refused.stderr).toContain('DSH_LEFTHOOK_ALLOW_HOOKS_PATH_OVERRIDE=1')
  498. expect(git(fixture, fixture.main, ['config', '--get', 'core.hooksPath'])).toBe('custom-hooks')
  499. expect(readFileSync(customHook, 'utf8')).toBe('#!/bin/sh\n# custom hook\n')
  500. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  501. const optedIn = await runInstaller(fixture, fixture.main, {
  502. DSH_LEFTHOOK_ALLOW_HOOKS_PATH_OVERRIDE: '1',
  503. })
  504. expect(optedIn.status, optedIn.stderr).toBe(0)
  505. expect(git(fixture, fixture.main, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(hooksPath(fixture, fixture.main))
  506. expect(git(fixture, fixture.linked, ['config', '--get', 'core.hooksPath'])).toBe('custom-hooks')
  507. expect(gitResult(fixture, fixture.linked, ['config', '--worktree', '--get', 'core.hooksPath']).status).toBe(1)
  508. expect(readFileSync(customHook, 'utf8')).toBe('#!/bin/sh\n# custom hook\n')
  509. git(fixture, fixture.linked, ['config', '--worktree', 'core.hooksPath', 'linked-custom-hooks'])
  510. const explicitWorktreePath = await runInstaller(fixture, fixture.linked, {
  511. DSH_LEFTHOOK_ALLOW_HOOKS_PATH_OVERRIDE: '1',
  512. })
  513. expect(explicitWorktreePath.status).toBe(1)
  514. expect(git(fixture, fixture.linked, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe('linked-custom-hooks')
  515. })
  516. it('does not trust an ownership marker outside a registered worktree hook path', async () => {
  517. const fixture = createFixture()
  518. const mainInstall = await runInstaller(fixture, fixture.main)
  519. expect(mainInstall.status, mainInstall.stderr).toBe(0)
  520. const externalHooks = join(fixture.container, 'external-owned-hooks')
  521. write(
  522. join(externalHooks, '.dsh-lefthook-owned'),
  523. `${JSON.stringify({
  524. version: 1,
  525. owner: 'deepseek-harness worktree-local lefthook hooks',
  526. hooksPath: externalHooks,
  527. })}\n`,
  528. 0o600,
  529. )
  530. git(fixture, fixture.linked, ['config', '--worktree', 'core.hooksPath', externalHooks])
  531. const result = await runInstaller(fixture, fixture.linked)
  532. expect(result.status).toBe(1)
  533. expect(result.stderr).toContain('worktree-scoped core.hooksPath')
  534. expect(git(fixture, fixture.linked, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(externalHooks)
  535. expect(existsSync(hooksPath(fixture, fixture.linked))).toBe(false)
  536. })
  537. it('refuses to activate a sibling worktree dormant hook path', async () => {
  538. const fixture = createFixture()
  539. const linkedConfig = join(gitDirectory(fixture, fixture.linked), 'config.worktree')
  540. const linkedHooks = join(fixture.linked, 'custom-hooks')
  541. git(fixture, fixture.main, ['config', '--file', linkedConfig, 'core.hooksPath', linkedHooks])
  542. expect(gitResult(fixture, fixture.linked, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  543. const result = await runInstaller(fixture, fixture.main)
  544. expect(result.status).toBe(1)
  545. expect(result.stderr).toContain('sibling dormant worktree config')
  546. expect(result.stderr).toContain(JSON.stringify(linkedConfig))
  547. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  548. expect(gitResult(fixture, fixture.linked, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  549. expect(git(fixture, fixture.main, ['config', '--file', linkedConfig, '--get', 'core.hooksPath'])).toBe(linkedHooks)
  550. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  551. })
  552. it.skipIf(process.platform === 'win32')('refuses an active symlinked worktree config before writing through it', async () => {
  553. const fixture = createFixture()
  554. const commonConfig = join(commonDirectory(fixture), 'config')
  555. const worktreeConfig = join(gitDirectory(fixture, fixture.main), 'config.worktree')
  556. const externalConfig = join(fixture.container, 'external.gitconfig')
  557. const externalContent = '[user]\n\tname = External owner\n'
  558. write(externalConfig, externalContent)
  559. git(fixture, fixture.main, ['config', '--file', commonConfig, 'core.repositoryFormatVersion', '1'])
  560. git(fixture, fixture.main, ['config', '--file', commonConfig, 'extensions.worktreeConfig', 'true'])
  561. symlinkSync(externalConfig, worktreeConfig)
  562. const result = await runInstaller(fixture, fixture.main)
  563. expect(result.status).toBe(1)
  564. expect(result.stderr).toContain('active worktree config')
  565. expect(result.stderr).toContain('not a regular file')
  566. expect(lstatSync(worktreeConfig).isSymbolicLink()).toBe(true)
  567. expect(readFileSync(externalConfig, 'utf8')).toBe(externalContent)
  568. expect(gitResult(fixture, fixture.main, [
  569. 'config', '--file', externalConfig, '--get', 'core.hooksPath',
  570. ]).status).toBe(1)
  571. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  572. })
  573. for (const includeKey of ['include.path', 'includeIf.onbranch:conditional.path']) {
  574. for (const key of ['core.worktree', 'core.bare', 'extensions.dshunknown']) {
  575. it(`ignores ${key} loaded through ${includeKey}`, async () => {
  576. const fixture = createFixture()
  577. const commonConfig = join(commonDirectory(fixture), 'config')
  578. const includedConfig = join(fixture.container, `${includeKey.split('.')[0]}-${key.replace('.', '-')}.gitconfig`)
  579. const value = key === 'core.worktree' ? fixture.main : 'true'
  580. git(fixture, fixture.main, ['config', '--file', includedConfig, key, value])
  581. git(fixture, fixture.main, ['config', '--file', commonConfig, includeKey, includedConfig])
  582. const result = await runInstaller(fixture, fixture.linked)
  583. expect(result.status, result.stderr).toBe(0)
  584. expect(git(fixture, fixture.linked, ['config', '--worktree', '--get', 'core.hooksPath'])).toBe(
  585. hooksPath(fixture, fixture.linked),
  586. )
  587. expect(existsSync(join(hooksPath(fixture, fixture.linked), 'pre-commit'))).toBe(true)
  588. })
  589. }
  590. }
  591. it('ignores an inactive global includeIf that provides a hook path for another repository', async () => {
  592. const fixture = createFixture()
  593. const globalConfig = fixture.env.GIT_CONFIG_GLOBAL
  594. if (globalConfig === undefined) throw new Error('fixture global config path is missing')
  595. const includedConfig = join(fixture.container, 'other-repository.gitconfig')
  596. const includedHooks = join(fixture.container, 'other-repository-hooks')
  597. git(fixture, fixture.main, ['config', '--file', includedConfig, 'core.hooksPath', includedHooks])
  598. git(fixture, fixture.main, [
  599. 'config',
  600. '--file',
  601. globalConfig,
  602. `includeIf.gitdir:${join(fixture.container, 'other')}/.path`,
  603. includedConfig,
  604. ])
  605. const result = await runInstaller(fixture, fixture.linked)
  606. expect(result.status, result.stderr).toBe(0)
  607. expect(git(fixture, fixture.linked, ['config', '--get', 'core.hooksPath'])).toBe(hooksPath(fixture, fixture.linked))
  608. })
  609. it('never overrides a command-scoped hook path', async () => {
  610. const fixture = createFixture()
  611. const commandHooks = join(fixture.container, 'command-hooks')
  612. const sentinel = join(commandHooks, 'pre-commit')
  613. write(sentinel, '#!/bin/sh\n# command-scope sentinel\n', 0o755)
  614. const result = await runInstaller(fixture, fixture.main, {
  615. DSH_LEFTHOOK_ALLOW_HOOKS_PATH_OVERRIDE: '1',
  616. GIT_CONFIG_COUNT: '1',
  617. GIT_CONFIG_KEY_0: 'core.hooksPath',
  618. GIT_CONFIG_VALUE_0: commandHooks,
  619. })
  620. expect(result.status).toBe(1)
  621. expect(result.stderr).toContain('command-scoped core.hooksPath')
  622. expect(readFileSync(sentinel, 'utf8')).toBe('#!/bin/sh\n# command-scope sentinel\n')
  623. expect(gitResult(fixture, fixture.main, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  624. expect(gitResult(fixture, fixture.main, [
  625. 'config', '--get', 'merge.dsh-translation-pairing.driver',
  626. ]).status).toBe(1)
  627. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  628. })
  629. it('never replaces a custom worktree pairing merge driver', async () => {
  630. const fixture = createFixture()
  631. const commonConfig = join(commonDirectory(fixture), 'config')
  632. git(fixture, fixture.main, ['config', '--file', commonConfig, 'core.repositoryFormatVersion', '1'])
  633. git(fixture, fixture.main, ['config', '--file', commonConfig, 'extensions.worktreeConfig', 'true'])
  634. git(fixture, fixture.main, [
  635. 'config', '--worktree', 'merge.dsh-translation-pairing.driver', 'custom-driver %A',
  636. ])
  637. const result = await runInstaller(fixture, fixture.main)
  638. expect(result.status).toBe(1)
  639. expect(result.stderr).toContain('refusing to replace worktree merge.dsh-translation-pairing.driver')
  640. expect(git(fixture, fixture.main, [
  641. 'config', '--worktree', '--get', 'merge.dsh-translation-pairing.driver',
  642. ])).toBe('custom-driver %A')
  643. expect(gitResult(fixture, fixture.main, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  644. })
  645. it('never masks an inherited custom pairing merge driver', async () => {
  646. const fixture = createFixture()
  647. git(fixture, fixture.main, [
  648. 'config', '--local', 'merge.dsh-translation-pairing.driver', 'inherited-driver %A',
  649. ])
  650. const result = await runInstaller(fixture, fixture.main)
  651. expect(result.status).toBe(1)
  652. expect(result.stderr).toContain('refusing to mask inherited merge.dsh-translation-pairing.driver')
  653. expect(git(fixture, fixture.main, [
  654. 'config', '--local', '--get', 'merge.dsh-translation-pairing.driver',
  655. ])).toBe('inherited-driver %A')
  656. expect(gitResult(fixture, fixture.main, [
  657. 'config', '--worktree', '--get', 'merge.dsh-translation-pairing.driver',
  658. ]).status).toBe(1)
  659. expect(gitResult(fixture, fixture.main, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  660. })
  661. it('does not pass unrelated command-scoped Git config to Lefthook', async () => {
  662. const fixture = createFixture()
  663. const result = await runInstaller(fixture, fixture.main, {
  664. DSH_TEST_FORBIDDEN_GIT_CONFIG_KEY: 'dsh.testSentinel',
  665. GIT_CONFIG_COUNT: '1',
  666. GIT_CONFIG_KEY_0: 'dsh.testSentinel',
  667. GIT_CONFIG_VALUE_0: 'must-not-reach-lefthook',
  668. })
  669. expect(result.status, result.stderr).toBe(0)
  670. expect(existsSync(join(hooksPath(fixture, fixture.main), 'pre-commit'))).toBe(true)
  671. })
  672. it('never overrides a hook path included by worktree config', async () => {
  673. const fixture = createFixture()
  674. const commonConfig = join(commonDirectory(fixture), 'config')
  675. const worktreeConfig = join(gitDirectory(fixture, fixture.main), 'config.worktree')
  676. const includedConfig = join(fixture.container, 'included-worktree.gitconfig')
  677. const includedHooks = join(fixture.container, 'included-hooks')
  678. const sentinel = join(includedHooks, 'pre-commit')
  679. write(sentinel, '#!/bin/sh\n# included-worktree sentinel\n', 0o755)
  680. git(fixture, fixture.main, ['config', '--file', includedConfig, 'core.hooksPath', includedHooks])
  681. git(fixture, fixture.main, ['config', '--file', commonConfig, 'core.repositoryFormatVersion', '1'])
  682. git(fixture, fixture.main, ['config', '--file', commonConfig, 'extensions.worktreeConfig', 'true'])
  683. git(fixture, fixture.main, ['config', '--file', worktreeConfig, 'include.path', includedConfig])
  684. const result = await runInstaller(fixture, fixture.main, {
  685. DSH_LEFTHOOK_ALLOW_HOOKS_PATH_OVERRIDE: '1',
  686. })
  687. expect(result.status).toBe(1)
  688. expect(result.stderr).toContain('worktree-scoped core.hooksPath')
  689. expect(git(fixture, fixture.main, ['config', '--get', 'core.hooksPath'])).toBe(includedHooks)
  690. expect(readFileSync(sentinel, 'utf8')).toBe('#!/bin/sh\n# included-worktree sentinel\n')
  691. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  692. })
  693. it('restores the previous hook lookup when Lefthook installation fails', async () => {
  694. const fixture = createFixture()
  695. const common = commonDirectory(fixture)
  696. const legacyHook = join(common, 'hooks/pre-push')
  697. write(legacyHook, '#!/bin/sh\n# legacy pre-push\n', 0o755)
  698. const result = await runInstaller(fixture, fixture.main, { DSH_TEST_LEFTHOOK_FAIL: '1' })
  699. expect(result.status).toBe(1)
  700. expect(result.stderr).toContain('exit status 77')
  701. expect(gitResult(fixture, fixture.main, ['config', '--worktree', '--get', 'core.hooksPath']).status).toBe(1)
  702. expect(gitResult(fixture, fixture.main, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  703. expect(gitResult(fixture, fixture.main, [
  704. 'config', '--worktree', '--get', 'merge.dsh-translation-pairing.name',
  705. ]).status).toBe(1)
  706. expect(gitResult(fixture, fixture.main, [
  707. 'config', '--worktree', '--get', 'merge.dsh-translation-pairing.driver',
  708. ]).status).toBe(1)
  709. expect(readFileSync(legacyHook, 'utf8')).toBe('#!/bin/sh\n# legacy pre-push\n')
  710. })
  711. it('does not publish worktree integration when the pairing driver probe fails', async () => {
  712. const fixture = createFixture()
  713. rmSync(join(fixture.main, 'node_modules/tsx'), { recursive: true, force: true })
  714. const result = await runInstaller(fixture, fixture.main)
  715. expect(result.status).toBe(1)
  716. expect(result.stderr).toContain('merge-translation-pairing.ts --probe failed')
  717. expect(gitResult(fixture, fixture.main, ['config', '--get', 'core.hooksPath']).status).toBe(1)
  718. expect(gitResult(fixture, fixture.main, [
  719. 'config', '--get', 'merge.dsh-translation-pairing.driver',
  720. ]).status).toBe(1)
  721. })
  722. it('reports installation and hook-path rollback failures together', async () => {
  723. const fixture = createFixture()
  724. const result = await runInstaller(fixture, fixture.main, {
  725. DSH_TEST_LEFTHOOK_BREAK_WORKTREE_CONFIG: '1',
  726. DSH_TEST_LEFTHOOK_FAIL: '1',
  727. })
  728. expect(result.status).toBe(1)
  729. expect(result.stderr).toContain('Lefthook installation failed')
  730. expect(result.stderr).toContain('exit status 77')
  731. expect(result.stderr).toContain('worktree integration rollback also failed')
  732. expect(result.stderr).toContain('git config --worktree --unset-all core.hooksPath failed')
  733. expect(result.stderr).toContain('git config --worktree --unset-all merge.dsh-translation-pairing.driver failed')
  734. })
  735. it('refuses an unowned directory at the reserved worktree hook path', async () => {
  736. const fixture = createFixture()
  737. const reservedHook = join(hooksPath(fixture, fixture.main), 'pre-commit')
  738. write(reservedHook, '#!/bin/sh\n# user content\n', 0o755)
  739. const result = await runInstaller(fixture, fixture.main)
  740. expect(result.status).toBe(1)
  741. expect(result.stderr).toContain('refusing to overwrite unowned hooks directory')
  742. expect(readFileSync(reservedHook, 'utf8')).toBe('#!/bin/sh\n# user content\n')
  743. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  744. })
  745. it.skipIf(process.platform === 'win32')('rejects Git without config-scope support before mutation', async () => {
  746. const fixture = createFixture()
  747. const realGit = commandResult('which', ['git'], fixture.main, fixture.env).stdout.trim()
  748. const fakeBin = join(fixture.container, 'fake-bin')
  749. const fakeGit = join(fakeBin, 'git')
  750. write(
  751. fakeGit,
  752. `#!/bin/sh\nif [ "$1" = "--version" ]; then echo "git version 2.25.0"; exit 0; fi\nexec "${realGit}" "$@"\n`,
  753. 0o755,
  754. )
  755. const result = await runInstaller(fixture, fixture.main, {
  756. PATH: `${fakeBin}:${fixture.env.PATH ?? ''}`,
  757. })
  758. expect(result.status).toBe(1)
  759. expect(result.stderr).toContain('Git 2.26 or newer is required')
  760. expect(gitResult(fixture, fixture.main, ['config', '--get', 'extensions.worktreeConfig']).status).toBe(1)
  761. expect(existsSync(hooksPath(fixture, fixture.main))).toBe(false)
  762. })
  763. })