|
| 1 | +/** |
| 2 | + * Git Sync Hooks Tests |
| 3 | + * |
| 4 | + * Covers installing/removing the opt-in commit/merge/checkout hooks that |
| 5 | + * keep the index fresh when the live watcher is disabled (issue #199). |
| 6 | + * Exercises real git repos in temp dirs — no mocking. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 10 | +import { execFileSync } from 'child_process'; |
| 11 | +import * as fs from 'fs'; |
| 12 | +import * as path from 'path'; |
| 13 | +import * as os from 'os'; |
| 14 | +import { |
| 15 | + installGitSyncHook, |
| 16 | + removeGitSyncHook, |
| 17 | + isSyncHookInstalled, |
| 18 | + isGitRepo, |
| 19 | + DEFAULT_SYNC_HOOKS, |
| 20 | +} from '../src/sync/git-hooks'; |
| 21 | + |
| 22 | +function gitInit(dir: string): void { |
| 23 | + execFileSync('git', ['init', '-q'], { cwd: dir, stdio: 'ignore' }); |
| 24 | +} |
| 25 | + |
| 26 | +function isExecutable(file: string): boolean { |
| 27 | + if (process.platform === 'win32') return true; // mode bits not meaningful |
| 28 | + return (fs.statSync(file).mode & 0o111) !== 0; |
| 29 | +} |
| 30 | + |
| 31 | +describe('git sync hooks', () => { |
| 32 | + let repo: string; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + repo = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-githooks-')); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + if (fs.existsSync(repo)) fs.rmSync(repo, { recursive: true, force: true }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('installs all default hooks, executable, invoking codegraph sync', () => { |
| 43 | + gitInit(repo); |
| 44 | + const result = installGitSyncHook(repo); |
| 45 | + |
| 46 | + expect(result.installed.sort()).toEqual([...DEFAULT_SYNC_HOOKS].sort()); |
| 47 | + expect(result.skipped).toBeUndefined(); |
| 48 | + |
| 49 | + for (const hook of DEFAULT_SYNC_HOOKS) { |
| 50 | + const file = path.join(repo, '.git', 'hooks', hook); |
| 51 | + expect(fs.existsSync(file)).toBe(true); |
| 52 | + const body = fs.readFileSync(file, 'utf8'); |
| 53 | + expect(body).toContain('codegraph sync'); |
| 54 | + expect(body).toContain('command -v codegraph'); // no-op when not on PATH |
| 55 | + expect(isExecutable(file)).toBe(true); |
| 56 | + } |
| 57 | + expect(isSyncHookInstalled(repo)).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it('is idempotent — re-install does not duplicate the block', () => { |
| 61 | + gitInit(repo); |
| 62 | + installGitSyncHook(repo); |
| 63 | + installGitSyncHook(repo); |
| 64 | + |
| 65 | + const body = fs.readFileSync(path.join(repo, '.git', 'hooks', 'post-commit'), 'utf8'); |
| 66 | + const occurrences = body.split('# >>> codegraph sync hook >>>').length - 1; |
| 67 | + expect(occurrences).toBe(1); |
| 68 | + }); |
| 69 | + |
| 70 | + it('preserves a pre-existing user hook and appends our block', () => { |
| 71 | + gitInit(repo); |
| 72 | + const file = path.join(repo, '.git', 'hooks', 'post-commit'); |
| 73 | + fs.writeFileSync(file, '#!/bin/sh\necho "my custom hook"\n', { mode: 0o755 }); |
| 74 | + |
| 75 | + installGitSyncHook(repo, ['post-commit']); |
| 76 | + |
| 77 | + const body = fs.readFileSync(file, 'utf8'); |
| 78 | + expect(body).toContain('echo "my custom hook"'); |
| 79 | + expect(body).toContain('codegraph sync'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('remove strips our block; deletes a hook that was only ours', () => { |
| 83 | + gitInit(repo); |
| 84 | + installGitSyncHook(repo, ['post-commit']); |
| 85 | + const file = path.join(repo, '.git', 'hooks', 'post-commit'); |
| 86 | + expect(fs.existsSync(file)).toBe(true); |
| 87 | + |
| 88 | + const result = removeGitSyncHook(repo, ['post-commit']); |
| 89 | + expect(result.installed).toEqual(['post-commit']); |
| 90 | + expect(fs.existsSync(file)).toBe(false); // was ours-only → deleted |
| 91 | + expect(isSyncHookInstalled(repo)).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it('remove keeps user content when the hook is shared', () => { |
| 95 | + gitInit(repo); |
| 96 | + const file = path.join(repo, '.git', 'hooks', 'post-commit'); |
| 97 | + fs.writeFileSync(file, '#!/bin/sh\necho "keep me"\n', { mode: 0o755 }); |
| 98 | + installGitSyncHook(repo, ['post-commit']); |
| 99 | + |
| 100 | + removeGitSyncHook(repo, ['post-commit']); |
| 101 | + |
| 102 | + expect(fs.existsSync(file)).toBe(true); |
| 103 | + const body = fs.readFileSync(file, 'utf8'); |
| 104 | + expect(body).toContain('echo "keep me"'); |
| 105 | + expect(body).not.toContain('codegraph sync'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('honors core.hooksPath', () => { |
| 109 | + gitInit(repo); |
| 110 | + const customHooks = path.join(repo, '.husky'); |
| 111 | + fs.mkdirSync(customHooks); |
| 112 | + execFileSync('git', ['config', 'core.hooksPath', '.husky'], { cwd: repo, stdio: 'ignore' }); |
| 113 | + |
| 114 | + const result = installGitSyncHook(repo, ['post-commit']); |
| 115 | + expect(result.hooksDir).toBe(customHooks); |
| 116 | + expect(fs.existsSync(path.join(customHooks, 'post-commit'))).toBe(true); |
| 117 | + // The default .git/hooks dir should NOT have received the hook. |
| 118 | + expect(fs.existsSync(path.join(repo, '.git', 'hooks', 'post-commit'))).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it('skips cleanly when not a git repository', () => { |
| 122 | + expect(isGitRepo(repo)).toBe(false); |
| 123 | + const result = installGitSyncHook(repo); |
| 124 | + expect(result.installed).toEqual([]); |
| 125 | + expect(result.hooksDir).toBeNull(); |
| 126 | + expect(result.skipped).toMatch(/not a git repository/); |
| 127 | + expect(isSyncHookInstalled(repo)).toBe(false); |
| 128 | + }); |
| 129 | +}); |
0 commit comments