/
index.ts
137 lines (117 loc) 路 3.11 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
import fs from 'fs'
import cp from 'child_process'
import path from 'path'
import tempy from 'tempy'
import index, { Env } from '../'
let spy: jest.SpyInstance
function expectSpawnSyncToHaveBeenCalledWith(
cwd: string,
cmd: string,
env: Env = {}
): void {
expect(cp.spawnSync).toHaveBeenCalledWith('sh', ['-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 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(['', '', 'pre-commit'], { cwd: dir })
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success')
expect(status).toBe(0)
})
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(['', '', 'pre-commit'], { cwd: dir })
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(['', '', 'pre-commit'], { cwd: dir })
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(['', '', 'pre-commit'], { cwd: dir })
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success')
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(['', '', 'commit-msg', 'git fake param'], {
cwd: dir
})
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(['', '', 'pre-push'], {
cwd: dir
})
})
})