|
1 | 1 | import { execSync } from 'node:child_process'; |
2 | | -import { existsSync, mkdtempSync, rmSync } from 'node:fs'; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
3 | 3 | import { tmpdir } from 'node:os'; |
4 | 4 | import { join } from 'node:path'; |
5 | 5 |
|
@@ -73,4 +73,86 @@ describe('hookScript', () => { |
73 | 73 | expect(countDirnameCalls(withDot)).toBe(countDirnameCalls(withoutDot)); |
74 | 74 | expect(countDirnameCalls(withDot)).toBe(3); |
75 | 75 | }); |
| 76 | + |
| 77 | + it.skipIf(process.platform === 'win32')( |
| 78 | + 'should add Vite+ managed bin to PATH as a fallback before running user hook', |
| 79 | + () => { |
| 80 | + const tmp = mkdtempSync(join(tmpdir(), 'hooks-path-test-')); |
| 81 | + try { |
| 82 | + const hooksDir = join(tmp, '.vite-hooks'); |
| 83 | + const internalHooksDir = join(hooksDir, '_'); |
| 84 | + const nodeModulesBin = join(tmp, 'node_modules', '.bin'); |
| 85 | + const vpHomeBin = join(tmp, 'vp-home', 'bin'); |
| 86 | + const systemBin = join(tmp, 'system-bin'); |
| 87 | + |
| 88 | + mkdirSync(internalHooksDir, { recursive: true }); |
| 89 | + mkdirSync(nodeModulesBin, { recursive: true }); |
| 90 | + mkdirSync(vpHomeBin, { recursive: true }); |
| 91 | + mkdirSync(systemBin, { recursive: true }); |
| 92 | + |
| 93 | + writeFileSync(join(internalHooksDir, 'h'), hookScript('.vite-hooks'), { mode: 0o755 }); |
| 94 | + writeFileSync( |
| 95 | + join(internalHooksDir, 'pre-commit'), |
| 96 | + '#!/usr/bin/env sh\n. "$(dirname "$0")/h"', |
| 97 | + { mode: 0o755 }, |
| 98 | + ); |
| 99 | + writeFileSync(join(hooksDir, 'pre-commit'), 'vp staged\n'); |
| 100 | + |
| 101 | + writeFileSync( |
| 102 | + join(nodeModulesBin, 'vp'), |
| 103 | + '#!/bin/sh\nbasedir=$(dirname "$0")\nexec node "$basedir/../vite-plus/bin/vp" "$@"\n', |
| 104 | + { mode: 0o755 }, |
| 105 | + ); |
| 106 | + writeFileSync( |
| 107 | + join(vpHomeBin, 'node'), |
| 108 | + '#!/bin/sh\necho "fake-node $*" > "$VP_HOME/node-used"\n', |
| 109 | + { mode: 0o755 }, |
| 110 | + ); |
| 111 | + writeFileSync( |
| 112 | + join(vpHomeBin, 'dirname'), |
| 113 | + '#!/bin/sh\necho "wrong dirname" > "$VP_HOME/dirname-used"\nexit 1\n', |
| 114 | + { mode: 0o755 }, |
| 115 | + ); |
| 116 | + writeFileSync( |
| 117 | + join(vpHomeBin, 'sh'), |
| 118 | + '#!/bin/sh\necho "wrong sh" > "$VP_HOME/sh-used"\nexit 1\n', |
| 119 | + { mode: 0o755 }, |
| 120 | + ); |
| 121 | + |
| 122 | + writeFileSync(join(systemBin, 'sh'), '#!/bin/sh\nexec /bin/sh "$@"\n', { |
| 123 | + mode: 0o755, |
| 124 | + }); |
| 125 | + writeFileSync(join(systemBin, 'dirname'), '#!/bin/sh\nexec /usr/bin/dirname "$@"\n', { |
| 126 | + mode: 0o755, |
| 127 | + }); |
| 128 | + writeFileSync(join(systemBin, 'basename'), '#!/bin/sh\nexec /usr/bin/basename "$@"\n', { |
| 129 | + mode: 0o755, |
| 130 | + }); |
| 131 | + |
| 132 | + execSync('sh .vite-hooks/_/pre-commit', { |
| 133 | + cwd: tmp, |
| 134 | + env: { |
| 135 | + HOME: join(tmp, 'home'), |
| 136 | + PATH: systemBin, |
| 137 | + VP_HOME: join(tmp, 'vp-home'), |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(existsSync(join(tmp, 'vp-home', 'node-used'))).toBe(true); |
| 142 | + expect(existsSync(join(tmp, 'vp-home', 'dirname-used'))).toBe(false); |
| 143 | + expect(existsSync(join(tmp, 'vp-home', 'sh-used'))).toBe(false); |
| 144 | + } finally { |
| 145 | + rmSync(tmp, { recursive: true, force: true }); |
| 146 | + } |
| 147 | + }, |
| 148 | + ); |
| 149 | + |
| 150 | + it('should compute root and shell before appending Vite+ managed bin', () => { |
| 151 | + const script = hookScript('.vite-hooks'); |
| 152 | + expect(script.indexOf('d=')).toBeLessThan(script.indexOf('export PATH="$PATH:$__vp_bin"')); |
| 153 | + expect(script.indexOf('__vp_shell=')).toBeLessThan( |
| 154 | + script.indexOf('export PATH="$PATH:$__vp_bin"'), |
| 155 | + ); |
| 156 | + expect(script).toContain('"$__vp_shell" -e "$s" "$@"'); |
| 157 | + }); |
76 | 158 | }); |
0 commit comments