/
generateTasks.spec.js
252 lines (229 loc) Β· 6.68 KB
/
generateTasks.spec.js
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
import path from 'path'
import os from 'os'
import generateTasks from '../src/generateTasks'
import resolveGitDir from '../src/resolveGitDir'
const files = [
'test.js',
'deeper/test.js',
'deeper/test2.js',
'even/deeper/test.js',
'.hidden/test.js',
'test.css',
'deeper/test.css',
'deeper/test2.css',
'even/deeper/test.css',
'.hidden/test.css',
'test.txt',
'deeper/test.txt',
'deeper/test2.txt',
'even/deeper/test.txt',
'.hidden/test.txt'
]
// Mocks get hoisted
jest.mock('../src/resolveGitDir.js')
const workDir = path.join(os.tmpdir(), 'tmp-lint-staged')
resolveGitDir.mockReturnValue(workDir)
const config = {
linters: {
'*.js': 'root-js',
'**/*.js': 'any-js',
'deeper/*.js': 'deeper-js',
'.hidden/*.js': 'hidden-js',
'unknown/*.js': 'unknown-js',
'*.{css,js}': 'root-css-or-js'
}
}
describe('generateTasks', () => {
beforeAll(() => {
jest.spyOn(process, 'cwd').mockReturnValue(workDir)
})
afterAll(() => {
process.cwd.mockRestore()
})
it('should work with simple configuration', () => {
const result = generateTasks(
{
'*.js': 'lint'
},
['test.js']
)
const commands = result.map(match => match.commands)
expect(commands).toEqual(['lint'])
})
it('should work with advanced configuration', () => {
const result = generateTasks(
{
linters: {
'*.js': 'lint'
}
},
['test.js']
)
const commands = result.map(match => match.commands)
expect(commands).toEqual(['lint'])
})
it('should return absolute paths', () => {
const [task] = generateTasks(
{
linters: {
'*': 'lint'
}
},
files
)
task.fileList.forEach(file => {
expect(path.isAbsolute(file)).toBe(true)
})
})
it('should not match non-children files', () => {
const relPath = path.join(process.cwd(), '..')
resolveGitDir.mockReturnValueOnce(relPath)
const result = generateTasks(Object.assign({}, config), files)
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
commands: 'root-js',
fileList: []
})
})
it('should return an empty file list for linters with no matches.', () => {
const result = generateTasks(config, files)
result.forEach(task => {
if (task.commands === 'unknown-js') {
expect(task.fileList.length).toEqual(0)
} else {
expect(task.fileList.length).not.toEqual(0)
}
})
})
it('should match pattern "*.js"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
commands: 'root-js',
fileList: [
`${workDir}/test.js`,
`${workDir}/deeper/test.js`,
`${workDir}/deeper/test2.js`,
`${workDir}/even/deeper/test.js`,
`${workDir}/.hidden/test.js`
].map(path.normalize)
})
})
it('should match pattern "**/*.js"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === '**/*.js')
expect(linter).toEqual({
pattern: '**/*.js',
commands: 'any-js',
fileList: [
`${workDir}/test.js`,
`${workDir}/deeper/test.js`,
`${workDir}/deeper/test2.js`,
`${workDir}/even/deeper/test.js`,
`${workDir}/.hidden/test.js`
].map(path.normalize)
})
})
it('should match pattern "deeper/*.js"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === 'deeper/*.js')
expect(linter).toEqual({
pattern: 'deeper/*.js',
commands: 'deeper-js',
fileList: [`${workDir}/deeper/test.js`, `${workDir}/deeper/test2.js`].map(path.normalize)
})
})
it('should match pattern ".hidden/*.js"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === '.hidden/*.js')
expect(linter).toEqual({
pattern: '.hidden/*.js',
commands: 'hidden-js',
fileList: [path.normalize(`${workDir}/.hidden/test.js`)]
})
})
it('should match pattern "*.{css,js}"', () => {
const result = generateTasks(config, files)
const linter = result.find(item => item.pattern === '*.{css,js}')
expect(linter).toEqual({
pattern: '*.{css,js}',
commands: 'root-css-or-js',
fileList: [
`${workDir}/test.js`,
`${workDir}/deeper/test.js`,
`${workDir}/deeper/test2.js`,
`${workDir}/even/deeper/test.js`,
`${workDir}/.hidden/test.js`,
`${workDir}/test.css`,
`${workDir}/deeper/test.css`,
`${workDir}/deeper/test2.css`,
`${workDir}/even/deeper/test.css`,
`${workDir}/.hidden/test.css`
].map(path.normalize)
})
})
it('should support globOptions specified in advanced configuration', () => {
const result = generateTasks(
{
globOptions: {
matchBase: false,
nocase: true
},
linters: {
'TeSt.*': 'lint'
}
},
files
)
const linter = result.find(item => item.pattern === 'TeSt.*')
expect(linter).toEqual({
pattern: 'TeSt.*',
commands: 'lint',
fileList: [`${workDir}/test.js`, `${workDir}/test.css`, `${workDir}/test.txt`].map(
path.normalize
)
})
})
it('should ignore patterns in the ignore array of configuration', () => {
const pattern = '**/*.js'
const commands = 'lint'
const result = generateTasks(
{
ignore: ['**/ignore/**', '**/ignore.*'],
linters: { [pattern]: commands }
},
['ignore/me.js', 'ignore.me.js', 'cool/js.js']
)
expect(result[0]).toEqual({
pattern,
commands,
fileList: [`${workDir}/cool/js.js`].map(path.normalize)
})
})
it('should not filter files for pattern which begins with `..`', () => {
jest.spyOn(process, 'cwd').mockReturnValueOnce(path.join(workDir, 'prj'))
const result = generateTasks(
{
linters: {
'*.js': 'my-cmd',
'../outside/*.js': 'my-cmd'
}
},
['root.js', 'prj/test.js', 'outside/test.js', 'outside/test2.js']
)
const prjTask = result.find(item => item.pattern === '*.js')
expect(prjTask).toEqual({
pattern: '*.js',
commands: 'my-cmd',
fileList: [`${workDir}/prj/test.js`].map(path.normalize)
})
const parentFolderTask = result.find(item => item.pattern === '../outside/*.js')
expect(parentFolderTask).toEqual({
pattern: '../outside/*.js',
commands: 'my-cmd',
fileList: [`${workDir}/outside/test.js`, `${workDir}/outside/test2.js`].map(path.normalize)
})
})
})