/
generateTasks.spec.js
206 lines (187 loc) Β· 5.98 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
import os from 'os'
import normalize from 'normalize-path'
import path from 'path'
import generateTasks from '../lib/generateTasks'
import resolveGitRepo from '../lib/resolveGitRepo'
const normalizePath = path => normalize(path)
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('../lib/resolveGitRepo.js')
const gitDir = path.join(os.tmpdir(), 'tmp-lint-staged')
resolveGitRepo.mockResolvedValue({ gitDir })
const cwd = gitDir
const config = {
'*.js': 'root-js',
'**/*.js': 'any-js',
'deeper/*.js': 'deeper-js',
'.hidden/*.js': 'hidden-js',
'unknown/*.js': 'unknown-js',
'*.{css,js}': 'root-css-or-js',
'../*.{css,js}': 'parent-dir-css-or-js'
}
describe('generateTasks', () => {
it('should return absolute paths', async () => {
const [task] = await generateTasks({
config: {
'*': 'lint'
},
gitDir,
files
})
task.fileList.forEach(file => {
expect(path.isAbsolute(file)).toBe(true)
})
})
it('should not match non-children files', async () => {
const relPath = path.join(process.cwd(), '..')
const result = await generateTasks({ config, cwd, gitDir: relPath, 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.', async () => {
const result = await generateTasks({ config, cwd, gitDir, files })
result.forEach(task => {
if (task.commands === 'unknown-js' || task.commands === 'parent-dir-css-or-js') {
expect(task.fileList.length).toEqual(0)
} else {
expect(task.fileList.length).not.toEqual(0)
}
})
})
it('should match pattern "*.js"', async () => {
const result = await generateTasks({ config, cwd, gitDir, files })
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
commands: 'root-js',
fileList: [
`${gitDir}/test.js`,
`${gitDir}/deeper/test.js`,
`${gitDir}/deeper/test2.js`,
`${gitDir}/even/deeper/test.js`,
`${gitDir}/.hidden/test.js`
].map(normalizePath)
})
})
it('should match pattern "**/*.js"', async () => {
const result = await generateTasks({ config, cwd, gitDir, files })
const linter = result.find(item => item.pattern === '**/*.js')
expect(linter).toEqual({
pattern: '**/*.js',
commands: 'any-js',
fileList: [
`${gitDir}/test.js`,
`${gitDir}/deeper/test.js`,
`${gitDir}/deeper/test2.js`,
`${gitDir}/even/deeper/test.js`,
`${gitDir}/.hidden/test.js`
].map(normalizePath)
})
})
it('should match pattern "deeper/*.js"', async () => {
const result = await generateTasks({ config, cwd, gitDir, files })
const linter = result.find(item => item.pattern === 'deeper/*.js')
expect(linter).toEqual({
pattern: 'deeper/*.js',
commands: 'deeper-js',
fileList: [`${gitDir}/deeper/test.js`, `${gitDir}/deeper/test2.js`].map(normalizePath)
})
})
it('should match pattern ".hidden/*.js"', async () => {
const result = await generateTasks({ config, cwd, gitDir, files })
const linter = result.find(item => item.pattern === '.hidden/*.js')
expect(linter).toEqual({
pattern: '.hidden/*.js',
commands: 'hidden-js',
fileList: [`${gitDir}/.hidden/test.js`].map(normalizePath)
})
})
it('should match pattern "*.{css,js}"', async () => {
const result = await generateTasks({ config, cwd, gitDir, files })
const linter = result.find(item => item.pattern === '*.{css,js}')
expect(linter).toEqual({
pattern: '*.{css,js}',
commands: 'root-css-or-js',
fileList: [
`${gitDir}/test.js`,
`${gitDir}/deeper/test.js`,
`${gitDir}/deeper/test2.js`,
`${gitDir}/even/deeper/test.js`,
`${gitDir}/.hidden/test.js`,
`${gitDir}/test.css`,
`${gitDir}/deeper/test.css`,
`${gitDir}/deeper/test2.css`,
`${gitDir}/even/deeper/test.css`,
`${gitDir}/.hidden/test.css`
].map(normalizePath)
})
})
it('should not match files in parent directory by default', async () => {
const result = await generateTasks({
config,
cwd: path.join(gitDir, 'deeper'),
gitDir,
files
})
const linter = result.find(item => item.pattern === '*.js')
expect(linter).toEqual({
pattern: '*.js',
commands: 'root-js',
fileList: [`${gitDir}/deeper/test.js`, `${gitDir}/deeper/test2.js`].map(normalizePath)
})
})
it('should match files in parent directory when pattern starts with "../"', async () => {
const result = await generateTasks({
config,
cwd: path.join(gitDir, 'deeper'),
gitDir,
files
})
const linter = result.find(item => item.pattern === '../*.{css,js}')
expect(linter).toEqual({
commands: 'parent-dir-css-or-js',
fileList: [`${gitDir}/test.js`, `${gitDir}/test.css`].map(normalizePath),
pattern: '../*.{css,js}'
})
})
it('should be able to return relative paths for "*.{css,js}"', async () => {
const result = await generateTasks({ config, cwd, gitDir, files, relative: true })
const linter = result.find(item => item.pattern === '*.{css,js}')
expect(linter).toEqual({
pattern: '*.{css,js}',
commands: 'root-css-or-js',
fileList: [
'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'
].map(normalizePath)
})
})
})