forked from stylelint/stylelint
/
standalone-cache.test.js
318 lines (239 loc) · 9.58 KB
/
standalone-cache.test.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
'use strict';
const fCache = require('file-entry-cache');
const path = require('path');
const { promises: fs, existsSync } = require('fs');
const hash = require('../utils/hash');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const removeFile = require('../testUtils/removeFile');
const safeChdir = require('../testUtils/safeChdir');
const standalone = require('../standalone');
const isChanged = (file, targetFilePath) => {
return file.source === targetFilePath && !file.ignored;
};
const fixturesPath = path.join(__dirname, 'fixtures');
const invalidFile = path.join(fixturesPath, 'empty-block.css');
const syntaxErrorFile = path.join(fixturesPath, 'syntax_error.css');
const validFile = path.join(fixturesPath, 'cache', 'valid.css');
const newFileDest = path.join(fixturesPath, 'cache', 'newFile.css');
// Config object is getting mutated internally.
// Return new object of the same structure to
// make sure config doesn't change between runs.
function getConfig(additional = {}) {
return {
files: replaceBackslashes(path.join(fixturesPath, 'cache', '*.css')),
config: {
rules: { 'block-no-empty': true, 'color-no-invalid-hex': true },
},
cache: true,
...additional,
};
}
describe('standalone cache', () => {
const cwd = path.join(__dirname, 'tmp', 'standalone-cache');
safeChdir(cwd);
const expectedCacheFilePath = path.join(cwd, '.stylelintcache');
beforeEach(async () => {
// Initial run to warm up the cache
await standalone(getConfig());
});
afterEach(async () => {
// Clean up after each test case
await removeFile(expectedCacheFilePath);
await removeFile(newFileDest);
});
it('cache file is created at $CWD/.stylelintcache', async () => {
// Ensure cache file exists
expect(existsSync(expectedCacheFilePath)).toBe(true);
const { cache } = fCache.createFromFile(expectedCacheFilePath);
// Ensure cache file contains only linted css file
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
expect(cache.getKey(newFileDest)).toBeUndefined();
});
it('only changed files are linted', async () => {
// Add "changed" file
await fs.copyFile(validFile, newFileDest);
// Next run should lint only changed files
const { results } = await standalone(getConfig());
// Ensure only changed files are linted
expect(results.some((file) => isChanged(file, validFile))).toBe(false);
expect(results.some((file) => isChanged(file, newFileDest))).toBe(true);
const { cache } = fCache.createFromFile(expectedCacheFilePath);
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
expect(typeof cache.getKey(newFileDest)).toBe('object');
expect(cache.getKey(newFileDest)).toBeTruthy();
});
it('all files are linted on config change', async () => {
await fs.copyFile(validFile, newFileDest);
// All file should be re-linted as config has changed
const { results } = await standalone(
getConfig({
config: {
rules: { 'block-no-empty': false },
},
}),
);
// Ensure all files are re-linted
expect(results.some((file) => isChanged(file, validFile))).toBe(true);
expect(results.some((file) => isChanged(file, newFileDest))).toBe(true);
});
it('invalid files are not cached', async () => {
await fs.copyFile(invalidFile, newFileDest);
// Should lint only changed files
const { errored, results } = await standalone(getConfig());
expect(errored).toBe(true);
// Ensure only changed files are linted
expect(results.some((file) => isChanged(file, validFile))).toBe(false);
expect(results.some((file) => isChanged(file, newFileDest))).toBe(true);
const { cache } = fCache.createFromFile(expectedCacheFilePath);
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
expect(cache.getKey(newFileDest)).toBeUndefined();
});
it('warning files are not cached', async () => {
await fs.copyFile(invalidFile, newFileDest);
const { errored, results } = await standalone(
getConfig({
config: {
rules: { 'block-no-empty': [true, { severity: 'warning' }] },
},
}),
);
expect(errored).toBe(false);
// cache is discarded because config changed
expect(results.some((file) => isChanged(file, validFile))).toBe(true);
expect(results.some((file) => isChanged(file, newFileDest))).toBe(true);
const { cache } = fCache.createFromFile(expectedCacheFilePath);
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
expect(cache.getKey(newFileDest)).toBeUndefined();
});
it('files with syntax errors are not cached', async () => {
await fs.copyFile(syntaxErrorFile, newFileDest);
// Should lint only changed files
await standalone(getConfig());
const { cache } = fCache.createFromFile(expectedCacheFilePath);
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
expect(cache.getKey(newFileDest)).toBeUndefined();
});
it('cache file is removed when cache is disabled', async () => {
await standalone(getConfig({ cache: false }));
expect(existsSync(expectedCacheFilePath)).toBe(false);
});
it("cache doesn't do anything if string is passed", async () => {
// Ensure no cache file
await removeFile(expectedCacheFilePath);
const config = {
code: '.foo {}',
codeFilename: 'foo.css',
cache: true,
config: {
rules: {
'color-no-invalid-hex': true,
},
},
};
const lintResults = await standalone(config);
expect(lintResults.errored).toBe(false);
expect(existsSync(expectedCacheFilePath)).toBe(false);
});
});
describe('standalone cache uses cacheLocation', () => {
const cwd = path.join(__dirname, 'tmp', 'standalone-cache-uses-cacheLocation');
safeChdir(cwd);
const cacheLocationFile = path.join(fixturesPath, 'cache', '.cachefile');
const cacheLocationDir = path.join(fixturesPath, 'cache');
const expectedCacheFilePath = path.join(cacheLocationDir, `.stylelintcache_${hash(cwd)}`);
afterEach(async () => {
// clean up after each test
await removeFile(expectedCacheFilePath);
await removeFile(cacheLocationFile);
});
it('cacheLocation is a file', async () => {
await standalone(getConfig({ cacheLocation: cacheLocationFile }));
// Ensure cache file is created
expect(existsSync(cacheLocationFile)).toBe(true);
const { cache } = fCache.createFromFile(cacheLocationFile);
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
});
it('cacheLocation is a directory', async () => {
await standalone(getConfig({ cacheLocation: cacheLocationDir }));
// Ensure cache file is created
expect(existsSync(expectedCacheFilePath)).toBe(true);
const { cache } = fCache.createFromFile(expectedCacheFilePath);
expect(typeof cache.getKey(validFile)).toBe('object');
expect(cache.getKey(validFile)).toBeTruthy();
});
});
describe('standalone cache uses a config file', () => {
const cwd = path.join(__dirname, 'tmp', 'standalone-cache-use-config-file');
safeChdir(cwd);
const configFile = path.join(cwd, '.stylelintrc.json');
const lintedFile = path.join(cwd, 'a.css');
beforeEach(async () => {
await fs.writeFile(lintedFile, 'a {}');
});
afterEach(async () => {
await removeFile(configFile);
await removeFile(lintedFile);
});
it('cache is discarded when a config file is changed', async () => {
const config = { files: [lintedFile], config: undefined, cache: true };
// No warnings when a rule is disabled.
await fs.writeFile(
configFile,
JSON.stringify({
rules: { 'block-no-empty': null },
}),
);
const { results } = await standalone(config);
expect(results[0].warnings).toHaveLength(0);
// Some warnings when a rule becomes enabled by changing the config.
await fs.writeFile(
configFile,
JSON.stringify({
rules: { 'block-no-empty': true },
}),
);
const { results: resultsNew } = await standalone(config);
expect(resultsNew[0].warnings).toHaveLength(1);
});
});
describe('standalone cache uses cacheStrategy', () => {
const cwd = path.join(__dirname, 'tmp', 'standalone-cache-uses-cacheStrategy');
safeChdir(cwd);
const expectedCacheFilePath = path.join(cwd, '.stylelintcache');
afterEach(async () => {
// clean up after each test
await removeFile(expectedCacheFilePath);
await removeFile(newFileDest);
});
it('cacheStrategy is invalid', async () => {
await expect(standalone(getConfig({ cacheStrategy: 'foo' }))).rejects.toThrow(
'"foo" cache strategy is unsupported. Specify either "metadata" or "content"',
);
});
it('cacheStrategy is "metadata"', async () => {
const cacheStrategy = 'metadata';
await fs.copyFile(validFile, newFileDest);
const { results } = await standalone(getConfig({ cacheStrategy }));
expect(results.some((file) => isChanged(file, newFileDest))).toBe(true);
// No content change, but file metadata id changed
await fs.utimes(newFileDest, new Date(), new Date());
const { results: resultsCached } = await standalone(getConfig({ cacheStrategy }));
expect(resultsCached.some((file) => isChanged(file, newFileDest))).toBe(true);
});
it('cacheStrategy is "content"', async () => {
const cacheStrategy = 'content';
await fs.copyFile(validFile, newFileDest);
const { results } = await standalone(getConfig({ cacheStrategy }));
expect(results.some((file) => isChanged(file, newFileDest))).toBe(true);
// No content change, but file metadata id changed
await fs.utimes(newFileDest, new Date(), new Date());
const { results: resultsCached } = await standalone(getConfig({ cacheStrategy }));
expect(resultsCached.some((file) => isChanged(file, newFileDest))).toBe(false);
});
});