|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { |
| 3 | + chmodSync, |
| 4 | + existsSync, |
| 5 | + mkdirSync, |
| 6 | + mkdtempSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from 'node:fs'; |
| 10 | +import path from 'node:path'; |
| 11 | +import { afterEach, beforeEach } from 'rstack/test'; |
| 12 | +import { normalizeHelpOutput, RSTACK_BIN_PATH, test } from '#test-helpers'; |
| 13 | + |
| 14 | +const hooksPath = '.rstack/hooks/_'; |
| 15 | + |
| 16 | +let cwd: string; |
| 17 | +let env: NodeJS.ProcessEnv; |
| 18 | + |
| 19 | +const git = (args: string[]): string => { |
| 20 | + const result = spawnSync('git', args, { cwd, encoding: 'utf8', env }); |
| 21 | + if (result.status !== 0) { |
| 22 | + throw new Error(result.stderr || `Git exited with status ${result.status}`); |
| 23 | + } |
| 24 | + return result.stdout.trim(); |
| 25 | +}; |
| 26 | + |
| 27 | +const initRepository = (): void => { |
| 28 | + git(['init', '--quiet']); |
| 29 | +}; |
| 30 | + |
| 31 | +const runHooks = (args: string[], runCwd: string = cwd) => |
| 32 | + spawnSync(process.execPath, [RSTACK_BIN_PATH, 'hooks', ...args], { |
| 33 | + cwd: runCwd, |
| 34 | + encoding: 'utf8', |
| 35 | + env, |
| 36 | + }); |
| 37 | + |
| 38 | +const runHooksSuccessfully = (args: string[], runCwd: string = cwd): string => { |
| 39 | + const result = runHooks(args, runCwd); |
| 40 | + if (result.status !== 0) { |
| 41 | + throw new Error( |
| 42 | + result.stderr || result.error?.message || `Exited with ${result.status}`, |
| 43 | + ); |
| 44 | + } |
| 45 | + return `${result.stdout}${result.stderr}`; |
| 46 | +}; |
| 47 | + |
| 48 | +beforeEach(() => { |
| 49 | + cwd = mkdtempSync(path.join(import.meta.dirname, 'test-temp-rstack hooks ')); |
| 50 | + env = { |
| 51 | + ...process.env, |
| 52 | + // Keep Git from treating the fixture as part of this repository. |
| 53 | + GIT_CEILING_DIRECTORIES: import.meta.dirname, |
| 54 | + GIT_CONFIG_GLOBAL: path.join(cwd, 'global.gitconfig'), |
| 55 | + GIT_CONFIG_NOSYSTEM: '1', |
| 56 | + }; |
| 57 | +}); |
| 58 | + |
| 59 | +afterEach(() => { |
| 60 | + rmSync(cwd, { force: true, recursive: true }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('displays hooks help without installing hooks', ({ execCli, expect }) => { |
| 64 | + initRepository(); |
| 65 | + const output = execCli('hooks --help', { cwd, env }); |
| 66 | + |
| 67 | + expect(execCli('hooks -h', { cwd, env })).toBe(output); |
| 68 | + expect(normalizeHelpOutput(output)).toMatchSnapshot(); |
| 69 | + expect(existsSync(path.join(cwd, '.rstack'))).toBe(false); |
| 70 | +}); |
| 71 | + |
| 72 | +test('rejects unknown hooks positionals and options', ({ execCli, expect }) => { |
| 73 | + expect(() => execCli('hooks install', { cwd })).toThrow(); |
| 74 | + expect(() => execCli('hooks uninstall', { cwd })).toThrow(); |
| 75 | + expect(() => execCli('hooks --unknown', { cwd })).toThrow(); |
| 76 | + expect(() => execCli('hooks --dir custom-hooks', { cwd })).toThrow(); |
| 77 | + expect(() => execCli('hooks -d custom-hooks', { cwd })).toThrow(); |
| 78 | +}); |
| 79 | + |
| 80 | +test('reports missing and repeated hooks directory options', ({ expect }) => { |
| 81 | + const missing = runHooks(['--hooks-dir']); |
| 82 | + expect(missing.status).toBe(1); |
| 83 | + expect(missing.stderr).toContain('--hooks-dir'); |
| 84 | + |
| 85 | + const repeated = runHooks(['--hooks-dir', 'first', '--hooks-dir', 'second']); |
| 86 | + expect(repeated.status).toBe(1); |
| 87 | + expect(repeated.stderr).toContain( |
| 88 | + 'The --hooks-dir option cannot be specified more than once.', |
| 89 | + ); |
| 90 | +}); |
| 91 | + |
| 92 | +test('rejects invalid hooks directory options', ({ expect }) => { |
| 93 | + const empty = runHooks(['--hooks-dir', '']); |
| 94 | + expect(empty.status).toBe(1); |
| 95 | + expect(empty.stderr).toContain('Git hooks directory must not be empty.'); |
| 96 | + |
| 97 | + const absolute = runHooks(['--hooks-dir', path.join(cwd, 'hooks')]); |
| 98 | + expect(absolute.status).toBe(1); |
| 99 | + expect(absolute.stderr).toContain( |
| 100 | + 'Git hooks directory must be relative to the Git repository root.', |
| 101 | + ); |
| 102 | + |
| 103 | + const parent = runHooks(['--hooks-dir', '../hooks']); |
| 104 | + expect(parent.status).toBe(1); |
| 105 | + expect(parent.stderr).toContain('Git hooks directory must not contain "..".'); |
| 106 | +}); |
| 107 | + |
| 108 | +test('installs hooks silently without loading Rstack config', ({ |
| 109 | + execCli, |
| 110 | + expect, |
| 111 | +}) => { |
| 112 | + initRepository(); |
| 113 | + writeFileSync( |
| 114 | + path.join(cwd, 'rstack.config.ts'), |
| 115 | + 'throw new Error("must not load");\n', |
| 116 | + ); |
| 117 | + |
| 118 | + expect(execCli('hooks', { cwd, env })).toBe(''); |
| 119 | + expect(git(['config', '--local', '--get', 'core.hooksPath'])).toBe(hooksPath); |
| 120 | + expect(existsSync(path.join(cwd, hooksPath, 'runner'))).toBe(true); |
| 121 | + expect(existsSync(path.join(cwd, '.rstack', 'hooks', 'pre-commit'))).toBe( |
| 122 | + false, |
| 123 | + ); |
| 124 | + |
| 125 | + expect(execCli('hooks', { cwd, env })).toBe(''); |
| 126 | +}); |
| 127 | + |
| 128 | +test('guides and forces installation while preserving existing hooks', ({ |
| 129 | + expect, |
| 130 | +}) => { |
| 131 | + initRepository(); |
| 132 | + const existingHook = path.join(cwd, '.git', 'hooks', 'pre-commit'); |
| 133 | + writeFileSync( |
| 134 | + existingHook, |
| 135 | + "#!/usr/bin/env sh\nprintf 'ran\\n' > old-hook-ran\n", |
| 136 | + ); |
| 137 | + chmodSync(existingHook, 0o755); |
| 138 | + |
| 139 | + const skippedOutput = runHooksSuccessfully([]); |
| 140 | + expect(skippedOutput).toContain( |
| 141 | + 'Git hooks setup skipped: existing Git hooks were found: pre-commit.', |
| 142 | + ); |
| 143 | + expect(skippedOutput).toContain( |
| 144 | + 'To continue, run rs hooks --force. Existing hook files will be preserved but become inactive.', |
| 145 | + ); |
| 146 | + |
| 147 | + const forcedOutput = runHooksSuccessfully(['--force']); |
| 148 | + expect(forcedOutput).toContain( |
| 149 | + 'info Rstack now manages Git hooks at ".rstack/hooks/_".', |
| 150 | + ); |
| 151 | + expect(forcedOutput).toContain( |
| 152 | + 'Existing hooks in ".git/hooks" were preserved but will no longer run: pre-commit.', |
| 153 | + ); |
| 154 | + expect(forcedOutput).toContain('Unset core.hooksPath to restore them.'); |
| 155 | + expect(git(['config', '--local', '--get', 'core.hooksPath'])).toBe(hooksPath); |
| 156 | + |
| 157 | + git(['hook', 'run', 'pre-commit']); |
| 158 | + expect(existsSync(path.join(cwd, 'old-hook-ran'))).toBe(false); |
| 159 | + |
| 160 | + git(['config', '--local', '--unset', 'core.hooksPath']); |
| 161 | + git(['hook', 'run', 'pre-commit']); |
| 162 | + expect(existsSync(path.join(cwd, 'old-hook-ran'))).toBe(true); |
| 163 | + |
| 164 | + expect(runHooksSuccessfully(['-f'])).toContain( |
| 165 | + 'Existing hooks in ".git/hooks" were preserved but will no longer run: pre-commit.', |
| 166 | + ); |
| 167 | +}); |
| 168 | + |
| 169 | +test('reports how to restore a replaced hooks path', ({ expect }) => { |
| 170 | + initRepository(); |
| 171 | + const existingDirectory = path.join(cwd, '.husky', '_'); |
| 172 | + mkdirSync(existingDirectory, { recursive: true }); |
| 173 | + writeFileSync( |
| 174 | + path.join(existingDirectory, 'pre-commit'), |
| 175 | + '#!/usr/bin/env sh\n', |
| 176 | + ); |
| 177 | + git(['config', '--local', 'core.hooksPath', '.husky/_']); |
| 178 | + |
| 179 | + const output = runHooksSuccessfully(['--force']); |
| 180 | + expect(output).toContain( |
| 181 | + 'info Rstack now manages Git hooks at ".rstack/hooks/_".', |
| 182 | + ); |
| 183 | + expect(output).toContain( |
| 184 | + 'Existing hooks in ".husky/_" were preserved but will no longer run: pre-commit.', |
| 185 | + ); |
| 186 | + expect(output).toContain( |
| 187 | + 'Set core.hooksPath back to ".husky/_" to restore them.', |
| 188 | + ); |
| 189 | +}); |
| 190 | + |
| 191 | +test('installs root-relative hooks and reports owner conflicts', ({ |
| 192 | + execCli, |
| 193 | + expect, |
| 194 | +}) => { |
| 195 | + initRepository(); |
| 196 | + const frontend = path.join(cwd, 'frontend'); |
| 197 | + const docs = path.join(cwd, 'docs'); |
| 198 | + mkdirSync(frontend); |
| 199 | + mkdirSync(docs); |
| 200 | + |
| 201 | + expect( |
| 202 | + execCli('hooks --hooks-dir "custom hooks"', { cwd: frontend, env }), |
| 203 | + ).toBe(''); |
| 204 | + expect(git(['config', '--local', '--get', 'core.hooksPath'])).toBe( |
| 205 | + 'custom hooks/_', |
| 206 | + ); |
| 207 | + expect(existsSync(path.join(cwd, 'custom hooks', '_', 'runner'))).toBe(true); |
| 208 | + |
| 209 | + expect(runHooksSuccessfully(['--hooks-dir', 'custom hooks'], docs)).toContain( |
| 210 | + 'Git hooks are already managed by Rstack project "frontend"', |
| 211 | + ); |
| 212 | +}); |
| 213 | + |
| 214 | +test('skips non-Git directories without creating files', ({ |
| 215 | + execCli, |
| 216 | + expect, |
| 217 | +}) => { |
| 218 | + expect(execCli('hooks', { cwd, env })).toContain( |
| 219 | + 'info Git hooks setup skipped: not a Git repository.', |
| 220 | + ); |
| 221 | + expect(existsSync(path.join(cwd, '.rstack'))).toBe(false); |
| 222 | +}); |
| 223 | + |
| 224 | +test('skips installation when hooks are disabled', ({ execCli, expect }) => { |
| 225 | + const output = execCli('hooks', { |
| 226 | + cwd, |
| 227 | + env: { ...env, RSTACK_HOOKS: '0' }, |
| 228 | + }); |
| 229 | + |
| 230 | + expect(output).toContain( |
| 231 | + 'info Git hooks setup skipped: disabled by RSTACK_HOOKS.', |
| 232 | + ); |
| 233 | + expect(existsSync(path.join(cwd, '.rstack'))).toBe(false); |
| 234 | +}); |
| 235 | + |
| 236 | +test('exits with an error when Git is unavailable', ({ expect }) => { |
| 237 | + const result = spawnSync(process.execPath, [RSTACK_BIN_PATH, 'hooks'], { |
| 238 | + cwd, |
| 239 | + encoding: 'utf8', |
| 240 | + env: { ...env, PATH: '', Path: '' }, |
| 241 | + }); |
| 242 | + |
| 243 | + expect(result.status).toBe(1); |
| 244 | + expect(result.stderr).toContain('Git command not found.'); |
| 245 | +}); |
0 commit comments