1+ import { existsSync } from 'node:fs' ;
2+ import { join } from 'node:path' ;
3+
14import { describe , expect , it } from 'vitest' ;
25
3- import { hookScript } from '../hooks.js' ;
6+ import { hookScript , install } from '../hooks.js' ;
47
58function countDirnameCalls ( script : string ) : number {
69 // Count nested dirname calls in the `d=...` line
@@ -11,6 +14,34 @@ function countDirnameCalls(script: string): number {
1114 return ( match [ 1 ] . match ( / d i r n a m e / g) ?? [ ] ) . length ;
1215}
1316
17+ describe ( 'install' , ( ) => {
18+ it ( 'should create _/pre-commit but not pre-commit in hooks dir root' , ( ) => {
19+ const { execSync } = require ( 'node:child_process' ) ;
20+ const { mkdtempSync, rmSync } = require ( 'node:fs' ) ;
21+ const { tmpdir } = require ( 'node:os' ) ;
22+
23+ const tmp = mkdtempSync ( join ( tmpdir ( ) , 'hooks-test-' ) ) ;
24+ const originalCwd = process . cwd ( ) ;
25+ try {
26+ // Set up a temporary git repo
27+ execSync ( 'git init' , { cwd : tmp , stdio : 'ignore' } ) ;
28+ process . chdir ( tmp ) ;
29+
30+ const hooksDir = '.vite-hooks' ;
31+ const result = install ( hooksDir ) ;
32+ expect ( result . isError ) . toBe ( false ) ;
33+
34+ // install() creates the internal shim at _/pre-commit
35+ expect ( existsSync ( join ( tmp , hooksDir , '_' , 'pre-commit' ) ) ) . toBe ( true ) ;
36+ // install() does NOT create pre-commit at the hooks dir root
37+ expect ( existsSync ( join ( tmp , hooksDir , 'pre-commit' ) ) ) . toBe ( false ) ;
38+ } finally {
39+ process . chdir ( originalCwd ) ;
40+ rmSync ( tmp , { recursive : true , force : true } ) ;
41+ }
42+ } ) ;
43+ } ) ;
44+
1445describe ( 'hookScript' , ( ) => {
1546 it ( 'should compute correct depth for simple dir' , ( ) => {
1647 // ".vite-hooks" → 1 segment → depth 3
0 commit comments