/
index.ts
349 lines (277 loc) · 9.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import del from 'del'
import fs from 'fs'
import mkdirp from 'mkdirp'
import os from 'os'
import path from 'path'
import tempy from 'tempy'
import * as installer from '../'
import { huskyIdentifier } from '../getScript'
// Temporary dir updated for each test
let tempDir: string
// Default values
const defaultGitDir = '.git'
const defaultGitHooksDir = '.git/hooks'
const defaultHookFilename = '.git/hooks/pre-commit'
const defaultHuskyDir = 'node_modules/husky'
const pkg = JSON.stringify({})
// Helpers
function install({
topLevel,
gitDir = defaultGitDir,
huskyDir = defaultHuskyDir,
requireRunNodePath,
isCI = false
}: {
topLevel?: string
gitDir?: string
huskyDir?: string
requireRunNodePath?: string
isCI?: boolean
} = {}): void {
installer.install(
topLevel || tempDir,
path.join(tempDir, gitDir),
path.join(tempDir, huskyDir),
isCI,
requireRunNodePath && path.join(tempDir, requireRunNodePath)
)
}
function uninstall({
gitDir = defaultGitDir,
huskyDir = defaultHuskyDir
}: {
gitDir?: string
huskyDir?: string
} = {}): void {
installer.uninstall(path.join(tempDir, gitDir), path.join(tempDir, huskyDir))
}
function mkdir(...dirs: string[]): void {
dirs.forEach((dir): mkdirp.Made => mkdirp.sync(path.join(tempDir, dir)))
}
function writeFile(filename: string, data: string): void {
fs.writeFileSync(path.join(tempDir, filename), data)
}
function readFile(filename: string): string {
return fs.readFileSync(path.join(tempDir, filename), 'utf-8')
}
function exists(filename: string): boolean {
return fs.existsSync(path.join(tempDir, filename))
}
function expectHookToExist(filename: string): void {
const hook = readFile(filename)
expect(hook).toMatch(huskyIdentifier)
}
// Tests
describe('install', (): void => {
beforeEach((): void => {
delete process.env.INIT_CWD
delete process.env.HUSKY_SKIP_INSTALL
tempDir = tempy.directory()
})
afterEach((): Promise<string[]> => del(tempDir, { force: true }))
it('should install and uninstall', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
install()
expectHookToExist(defaultHookFilename)
const hook = readFile(defaultHookFilename)
const node =
os.platform() === 'win32' ? 'node' : 'node_modules/run-node/run-node'
expect(hook).toMatch(node)
uninstall()
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should install and uninstall with pnpm', (): void => {
// Pnpm installs husky in node_modules/[..]/node_modules/husky
// this tests ensures that husky will install from this path
process.env.INIT_CWD = tempDir
const huskyDir =
'node_modules/.registry.npmjs.org/husky/1.0.0/node_modules/husky'
mkdir(defaultGitHooksDir, huskyDir)
writeFile('package.json', pkg)
install({ huskyDir })
expectHookToExist(defaultHookFilename)
uninstall({ huskyDir })
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should update existing husky hooks', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
// Create an existing husky hook
writeFile(defaultHookFilename, '# husky\nfoo')
// Verify that it has been updated
install()
const hook = readFile(defaultHookFilename)
expect(hook).toContain('# husky')
expect(hook).not.toContain('foo')
})
it('should update existing husky hooks (v0.14 and earlier)', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
// Create an existing husky hook
writeFile(defaultHookFilename, '#!/bin/sh\n#husky 0.14.3\nfoo')
// Verify that it has been updated
install()
const hook = readFile(defaultHookFilename)
expect(hook).toContain('# husky')
expect(hook).not.toContain('foo')
})
it('should not modify user hooks', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
writeFile(defaultHookFilename, 'foo')
// Verify that it's not overwritten
install()
const hook = readFile(defaultHookFilename)
expect(hook).toBe('foo')
// Verify that it's not deleted
uninstall()
expect(exists(defaultHookFilename)).toBeTruthy()
})
it('should support package.json installed in sub directory', (): void => {
const huskyDir = 'A/B/node_modules/husky'
const requireRunNodePath = 'A/B/node_modules/run-node/run-node'
mkdir(defaultGitHooksDir, huskyDir)
writeFile('A/B/package.json', pkg)
install({
huskyDir,
requireRunNodePath
})
const hook = readFile('.git/hooks/pre-commit')
const node =
os.platform() === 'win32' ? 'node' : 'node_modules/run-node/run-node'
expect(hook).toMatch(node)
expect(hook).toMatch('A/B/node_modules/husky/run.js')
uninstall({ huskyDir })
expect(exists('.git/hooks/pre-commit')).toBeFalsy()
})
it('should support git submodule', (): void => {
const topLevel = 'A/B'
const gitDir = '.git/modules/A/B'
const huskyDir = 'A/B/node_modules/husky'
const requireRunNodePath = 'A/B/node_modules/run-node/run-node'
mkdir('.git/modules/A/B/hooks', huskyDir)
writeFile('A/B/package.json', pkg)
install({
topLevel,
gitDir,
huskyDir,
requireRunNodePath
})
const hook = readFile('.git/modules/A/B/hooks/pre-commit')
expect(hook).toMatch('node_modules/husky/run.js')
uninstall({ gitDir, huskyDir })
expect(exists('.git/modules/A/B/hooks/pre-commit')).toBeFalsy()
})
it('should support git submodule and sub directory', (): void => {
const topLevel = 'A/B'
const gitDir = '.git/modules/A/B'
const huskyDir = 'A/B/C/node_modules/husky'
const requireRunNodePath = 'A/B/C/node_modules/run-node/run-node'
mkdir('.git/modules/A/B/hooks', huskyDir)
writeFile('A/B/C/package.json', pkg)
install({ topLevel, gitDir, huskyDir, requireRunNodePath })
const hook = readFile('.git/modules/A/B/hooks/pre-commit')
expect(hook).toMatch('C/node_modules/husky/run.js')
uninstall({ gitDir, huskyDir })
expect(exists('.git/hooks/pre-push')).toBeFalsy()
})
it('should support git worktrees', (): void => {
// TopLevel A/B
const gitDir = '.git/worktrees/B'
const huskyDir = 'A/B/node_modules/husky'
const requireRunNodePath = 'A/B/node_modules/run-node/run-node'
mkdir(`${gitDir}/hooks`, huskyDir)
writeFile('A/B/package.json', pkg)
install({ gitDir, huskyDir, requireRunNodePath })
const hook = readFile(`${gitDir}/hooks/pre-commit`)
expect(hook).toMatch('node_modules/husky/run.js')
uninstall({ gitDir, huskyDir })
expect(exists('.git/worktrees/B/hooks/pre-commit')).toBeFalsy()
})
it('should support git worktrees with worktree outside', (): void => {
const topLevel = 'project/B'
const gitDir = 'project/A/.git/worktrees/B'
const huskyDir = 'project/B/node_modules/husky'
mkdir(gitDir, huskyDir)
writeFile('project/B/package.json', pkg)
install({
topLevel,
gitDir,
huskyDir,
requireRunNodePath: 'project/B/node_modules/run-node/run-node'
})
const hook = readFile('project/A/.git/worktrees/B/hooks/pre-commit')
expect(hook).toMatch('node_modules/husky/run.js')
uninstall({ gitDir, huskyDir })
expect(exists('.git/hooks/pre-commit')).toBeFalsy()
})
it('should not install from /node_modules/A/node_modules', (): void => {
const huskyDir = 'node_modules/A/node_modules/husky'
mkdir(defaultGitHooksDir, huskyDir)
writeFile('node_modules/A/package.json', '{}')
writeFile('package.json', pkg)
install({ huskyDir })
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should not install from node_modules using INIT_CWD', (): void => {
process.env.INIT_CWD = 'node_modules'
mkdir(defaultGitHooksDir)
writeFile('package.json', pkg)
install()
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should migrate existing scripts (ghooks)', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
writeFile(
defaultHookFilename,
'// Generated by ghooks. Do not edit this file.'
)
install()
const hook = readFile(defaultHookFilename)
expect(hook).toMatch(huskyIdentifier)
})
it('should migrate existing scripts (pre-commit)', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
writeFile(defaultHookFilename, './node_modules/pre-commit/hook')
install()
const hook = readFile(defaultHookFilename)
expect(hook).toMatch(huskyIdentifier)
})
it('should not install hooks if HUSKY_SKIP_INSTALL=1', (): void => {
mkdir(defaultGitDir, defaultHuskyDir)
writeFile('package.json', pkg)
process.env.HUSKY_SKIP_INSTALL = '1'
install()
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should not install hooks if HUSKY_SKIP_INSTALL=true', (): void => {
mkdir(defaultGitDir, defaultHuskyDir)
writeFile('package.json', pkg)
process.env.HUSKY_SKIP_INSTALL = 'true'
install()
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should not install hooks in CI server', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', pkg)
// By default isCI is false in husky's test
install({ isCI: true })
expect(exists(defaultHookFilename)).toBeFalsy()
})
it('should install in CI server if skipCI is set to false', (): void => {
mkdir(defaultGitHooksDir, defaultHuskyDir)
writeFile('package.json', JSON.stringify({ husky: { skipCI: false } }))
install()
expect(exists(defaultHookFilename)).toBeTruthy()
})
it("should install even if .git/hooks doesn't exist", (): void => {
mkdir(defaultGitDir, defaultHuskyDir)
writeFile('package.json', pkg)
install()
expect(exists(defaultHookFilename)).toBeTruthy()
expectHookToExist(defaultHookFilename)
})
})