-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
index.ts
202 lines (174 loc) · 4.74 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
import fs from 'fs'
import cp from 'child_process'
import mkdirp from 'mkdirp'
import path from 'path'
import tempy from 'tempy'
import index, { Env } from '../'
let spy: jest.SpyInstance
// On AppVeyor $SHELL is not set
process.env.SHELL = process.env.SHELL || 'sh'
function getScriptPath(dir: string): string {
return path.join(dir, 'node_modules/husky/runner/index.js')
}
function expectSpawnSyncToHaveBeenCalledWith(
cwd: string,
cmd: string,
env: Env = {}
): void {
expect(cp.spawnSync).toHaveBeenCalledWith(process.env.SHELL, ['-c', cmd], {
cwd,
env: { ...process.env, ...env },
stdio: 'inherit'
})
}
describe('run', (): void => {
beforeEach((): void => {
spy = jest.spyOn(cp, 'spawnSync')
})
afterEach((): void => {
spy.mockReset()
spy.mockRestore()
})
it('should run working command and return 0 status', async (): Promise<
void
> => {
const dir = tempy.directory()
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-commit': 'echo success'
}
}
})
)
const status = await index(['', getScriptPath(dir), 'pre-commit'])
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success')
expect(status).toBe(0)
})
it('should run working command and return 0 status when husky is installed in a sub directory', async (): Promise<
void
> => {
const dir = tempy.directory()
const subDir = path.join(dir, 'A/B')
mkdirp.sync(subDir)
fs.writeFileSync(
path.join(subDir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-commit': 'echo success'
}
}
})
)
const status = await index(['', getScriptPath(subDir), 'pre-commit'])
expectSpawnSyncToHaveBeenCalledWith(subDir, 'echo success')
expect(status).toBe(0)
})
// This shouldn't happen since the shell script checks for command existence
// but in case there's a false positive, we're testing this also
it('should return 0 status if the command is undefined', async (): Promise<
void
> => {
const dir = tempy.directory()
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {}
})
)
const status = await index(['', getScriptPath(dir), 'pre-commit'])
expect(cp.spawnSync).not.toBeCalled()
expect(status).toBe(0)
})
it('should run failing command and return 1 status', async (): Promise<
void
> => {
const dir = tempy.directory()
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-commit': 'echo fail && exit 2'
}
}
})
)
const status = await index(['', getScriptPath(dir), 'pre-commit'])
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo fail && exit 2')
expect(status).toBe(2)
})
it('should support old scripts but show a deprecated message', async (): Promise<
void
> => {
const dir = tempy.directory()
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
scripts: {
precommit: 'echo success'
}
})
)
const status = await index(['', getScriptPath(dir), 'pre-commit'])
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success')
expect(status).toBe(0)
})
it('should set HUSKY_GIT_STDIN env for some hooks', async (): Promise<
void
> => {
const dir = tempy.directory()
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-push': 'echo success'
}
}
})
)
const status = await index(
['', getScriptPath(dir), 'pre-push'],
(): Promise<string> => Promise.resolve('foo')
)
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success', {
HUSKY_GIT_STDIN: 'foo'
})
expect(status).toBe(0)
})
it('should set HUSKY_GIT_PARAMS', async (): Promise<void> => {
const dir = tempy.directory()
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'commit-msg': 'echo success'
}
}
})
)
// 'commit-msg' takes one parameter from git
const status = await index([
'',
getScriptPath(dir),
'commit-msg',
'git fake param'
])
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success', {
HUSKY_GIT_PARAMS: 'git fake param'
})
expect(status).toBe(0)
})
it("should not throw if there's no package.json", async (): Promise<void> => {
const dir = tempy.directory()
await index(
['', getScriptPath(dir), 'pre-push'],
(): Promise<string> => Promise.resolve('foo')
)
})
})