-
-
Notifications
You must be signed in to change notification settings - Fork 999
/
index.ts
249 lines (198 loc) · 6.66 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
import del from 'del'
import fs from 'fs'
import mkdirp from 'mkdirp'
import path from 'path'
import tempy from 'tempy'
import './__env__'
import * as installer from '../'
import { huskyIdentifier } from '../hooks'
// RandomId to verify that scripts get updated
const randomId = Math.random().toString()
// Temporary dir updated for each test
let tempDir: string
const pkg = JSON.stringify({})
// Helpers
function install({
gitCommonDir = '.git',
relativeUserPkgDir = '.',
userPkgDir = '.',
isCI = false,
}: {
gitCommonDir?: string
relativeUserPkgDir?: string
userPkgDir?: string
isCI?: boolean
} = {}): void {
installer.install({
absoluteGitCommonDir: path.join(tempDir, gitCommonDir),
relativeUserPkgDir,
userPkgDir: path.join(tempDir, userPkgDir),
pmName: 'npm',
isCI,
})
}
function uninstall({
gitCommonDir = '.git',
userPkgDir = '.',
}: {
gitCommonDir?: string
userPkgDir?: string
} = {}): void {
installer.uninstall({
absoluteGitCommonDir: path.join(tempDir, gitCommonDir),
userPkgDir,
})
}
function mkdir(dirs: string[]): void {
dirs.forEach((dir) => mkdirp.sync(path.join(tempDir, dir)))
}
function rmdir(dir: string): void {
fs.rmdirSync(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)
}
type HookManager = {
name: string
hookContent: string
}
const hooksManagers: HookManager[] = [
{
name: 'ghooks',
hookContent: '// Generated by ghooks. Do not edit this file.',
},
{ name: 'pre-commit', hookContent: './node_modules/pre-commit/hook' },
]
function testMigration({ name, hookContent }: HookManager): void {
it(`should migrate existing scripts (${name})`, (): void => {
writeFile('package.json', pkg)
writeFile('.git/hooks/pre-commit', hookContent)
install()
expectHookToExist('.git/hooks/pre-commit')
})
}
// Tests
describe('install', (): void => {
beforeEach((): void => {
delete process.env.INIT_CWD
tempDir = tempy.directory()
mkdir(['.git/hooks'])
})
afterEach((): Promise<string[]> => del(tempDir, { force: true }))
it('should install and uninstall', (): void => {
writeFile('package.json', pkg)
install()
expect(exists('.git/hooks/husky.sh')).toBeTruthy()
expect(exists('.git/hooks/husky.local.sh')).toBeTruthy()
expectHookToExist('.git/hooks/pre-commit')
const localScript = readFile('.git/hooks/husky.local.sh')
expect(localScript).toMatch('cd "."')
uninstall()
expect(exists('.git/hooks/husky.sh')).toBeFalsy()
expect(exists('.git/hooks/husky.local.sh')).toBeFalsy()
expect(exists('.git/hooks/pre-commit')).toBeFalsy()
})
it('should update existing husky hooks', (): void => {
writeFile('package.json', pkg)
// Create an existing husky hook
writeFile('.git/hooks/pre-commit', `# husky\n${randomId}`)
// Verify that it has been updated
install()
const hook = readFile('.git/hooks/pre-commit')
expect(hook).toContain('# husky')
expect(hook).not.toContain(randomId)
})
it('should update existing husky hooks (v0.14 and earlier)', (): void => {
writeFile('package.json', pkg)
// Create an existing husky hook
writeFile('.git/hooks/pre-commit', `#!/bin/sh\n#husky 0.14.3\n${randomId}`)
// Verify that it has been updated
install()
const hook = readFile('.git/hooks/pre-commit')
expect(hook).toContain('# husky')
expect(hook).not.toContain(randomId)
})
it('should not modify user hooks', (): void => {
writeFile('package.json', pkg)
writeFile('.git/hooks/pre-commit', 'foo')
// Verify that it's not overwritten
install()
const hook = readFile('.git/hooks/pre-commit')
expect(hook).toBe('foo')
// Verify that it's not deleted
uninstall()
expect(exists('.git/hooks/pre-commit')).toBeTruthy()
})
it('should support package.json installed in sub directory', (): void => {
const relativeUserPkgDir = 'A/B/'
mkdir([relativeUserPkgDir])
writeFile('A/B/package.json', pkg)
install({ relativeUserPkgDir, userPkgDir: relativeUserPkgDir })
const localScript = readFile('.git/hooks/husky.local.sh')
expect(localScript).toMatch('cd "A/B/"')
expectHookToExist('.git/hooks/pre-commit')
uninstall({ userPkgDir: relativeUserPkgDir })
expect(exists('.git/hooks/husky.local.sh')).toBeFalsy()
expect(exists('.git/hooks/pre-commit')).toBeFalsy()
})
it('should support git submodule', (): void => {
const gitCommonDir = '.git/modules/A/B'
const userPkgDir = 'A/B'
mkdir(['.git/modules/A/B/hooks', userPkgDir])
writeFile('A/B/package.json', pkg)
install({
gitCommonDir,
userPkgDir,
})
const localScript = readFile('.git/modules/A/B/hooks/husky.local.sh')
expect(localScript).toMatch('cd "."')
expectHookToExist('.git/modules/A/B/hooks/pre-commit')
uninstall({ gitCommonDir, userPkgDir })
expect(exists('.git/modules/A/B/hooks/husky.local.sh')).toBeFalsy()
expect(exists('.git/modules/A/B/hooks/pre-commit')).toBeFalsy()
})
it('should not install from node_modules/A', (): void => {
const userPkgDir = 'node_modules/A'
mkdir([userPkgDir])
writeFile('node_modules/A/package.json', '{}')
writeFile('package.json', pkg)
install({ userPkgDir })
expect(exists('.git/hooks/pre-commit')).toBeFalsy()
})
it('should not install hooks in CI server', (): void => {
writeFile('package.json', pkg)
// By default isCI is false in husky's test
install({ isCI: true })
expect(exists('.git/hooks/pre-commit')).toBeFalsy()
})
it('should install in CI server if skipCI is set to false', (): void => {
writeFile('package.json', JSON.stringify({ husky: { skipCI: false } }))
install()
expectHookToExist('.git/hooks/pre-commit')
})
it("should install even if .git/hooks doesn't exist", (): void => {
// Create only Git common dir but hooks dir
rmdir('.git/hooks')
writeFile('package.json', pkg)
install()
expectHookToExist('.git/hooks/pre-commit')
})
it('should not throw error during uninstall if some files do not exist', (): void => {
writeFile('package.json', pkg)
install()
fs.unlinkSync(path.join(tempDir, '.git/hooks/pre-commit'))
fs.unlinkSync(path.join(tempDir, '.git/hooks/husky.sh'))
expect(uninstall).not.toThrow()
})
hooksManagers.forEach(testMigration)
})