|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { expect, test } from 'rstack/test'; |
| 6 | +import { createHookFiles } from '../../src/hooks.ts'; |
| 7 | + |
| 8 | +test('generates the dispatcher and all client-side Git hook shims', () => { |
| 9 | + const { runner, ...shims } = createHookFiles(); |
| 10 | + |
| 11 | + expect(Object.keys(shims)).toEqual([ |
| 12 | + 'pre-commit', |
| 13 | + 'pre-merge-commit', |
| 14 | + 'prepare-commit-msg', |
| 15 | + 'commit-msg', |
| 16 | + 'post-commit', |
| 17 | + 'applypatch-msg', |
| 18 | + 'pre-applypatch', |
| 19 | + 'post-applypatch', |
| 20 | + 'pre-rebase', |
| 21 | + 'post-rewrite', |
| 22 | + 'post-checkout', |
| 23 | + 'post-merge', |
| 24 | + 'pre-push', |
| 25 | + 'pre-auto-gc', |
| 26 | + ]); |
| 27 | + expect(runner).toBeTruthy(); |
| 28 | + expect(new Set(Object.values(shims)).size).toBe(1); |
| 29 | +}); |
| 30 | + |
| 31 | +test.runIf(process.platform !== 'win32')('runs generated hooks', () => { |
| 32 | + const directory = mkdtempSync(path.join(tmpdir(), 'rstack hooks ')); |
| 33 | + const hooksDirectory = path.join(directory, 'hooks with spaces'); |
| 34 | + const generatedDirectory = path.join(hooksDirectory, '_'); |
| 35 | + const generatedHook = path.join(generatedDirectory, 'pre-commit'); |
| 36 | + const userHook = path.join(hooksDirectory, 'pre-commit'); |
| 37 | + const files = createHookFiles(); |
| 38 | + |
| 39 | + try { |
| 40 | + mkdirSync(generatedDirectory, { recursive: true }); |
| 41 | + writeFileSync(path.join(generatedDirectory, 'runner'), files.runner); |
| 42 | + writeFileSync(generatedHook, files['pre-commit']); |
| 43 | + |
| 44 | + expect(spawnSync('sh', [generatedHook]).status).toBe(0); |
| 45 | + |
| 46 | + writeFileSync( |
| 47 | + userHook, |
| 48 | + `read -r input |
| 49 | +printf '%s\\n' "$1|$input" |
| 50 | +exit 23 |
| 51 | +`, |
| 52 | + ); |
| 53 | + const result = spawnSync('sh', [generatedHook, 'argument with spaces'], { |
| 54 | + encoding: 'utf8', |
| 55 | + input: 'standard input\n', |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result.status).toBe(23); |
| 59 | + expect(result.stdout).toBe('argument with spaces|standard input\n'); |
| 60 | + |
| 61 | + writeFileSync( |
| 62 | + userHook, |
| 63 | + `false |
| 64 | +printf 'unreachable\\n' |
| 65 | +`, |
| 66 | + ); |
| 67 | + const errexitResult = spawnSync('sh', [generatedHook], { encoding: 'utf8' }); |
| 68 | + |
| 69 | + expect(errexitResult.status).toBe(1); |
| 70 | + expect(errexitResult.stdout).toBe(''); |
| 71 | + } finally { |
| 72 | + rmSync(directory, { force: true, recursive: true }); |
| 73 | + } |
| 74 | +}); |
0 commit comments