/
standalone-cache.test.js
257 lines (221 loc) · 7.89 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
'use strict';
const hash = require('../utils/hash');
const path = require('path');
const standalone = require('../standalone');
const fixturesPath = path.join(__dirname, 'fixtures');
const fs = require('fs');
const replaceBackslashes = require('./replaceBackslashes');
const { promisify } = require('util');
const unlink = promisify(fs.unlink);
const fCache = require('file-entry-cache');
const copyFile = promisify(fs.copyFile);
const fileExists = (filePath) =>
new Promise((resolve) => fs.access(filePath, fs.F_OK, (error) => resolve(!error)));
const cwd = process.cwd();
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() {
return {
files: replaceBackslashes(path.join(fixturesPath, 'cache', '*.css')),
config: {
rules: { 'block-no-empty': true, 'color-no-invalid-hex': true },
},
cache: true,
};
}
describe('standalone cache', () => {
const expectedCacheFilePath = path.join(cwd, '.stylelintcache');
beforeEach(() => {
// Initial run to warm up the cache
return standalone(getConfig());
});
afterEach(() => {
// Clean up after each test case
return Promise.all([
unlink(expectedCacheFilePath).catch(() => {
// fs.unlink() throws an error if file doesn't exist and it's ok. We just
// want to make sure it's not there for next test.
}),
unlink(newFileDest).catch(() => {
// fs.unlink() throws an error if file doesn't exist and it's ok. We just
// want to make sure it's not there for next test.
}),
]);
});
it('cache file is created at $CWD/.stylelintcache', () => {
// Ensure cache file exists
return fileExists(expectedCacheFilePath).then((isFileExist) => {
expect(Boolean(isFileExist)).toBe(true);
const fileCache = fCache.createFromFile(expectedCacheFilePath);
const { cache } = fileCache;
// Ensure cache file contains only linted css file
expect(typeof cache.getKey(validFile) === 'object').toBe(true);
expect(typeof cache.getKey(newFileDest) === 'undefined').toBe(true);
});
});
it('only changed files are linted', () => {
// Add "changed" file
return copyFile(validFile, newFileDest)
.then(() => {
// Next run should lint only changed files
return standalone(getConfig());
})
.then((output) => {
// Ensure only changed files are linted
const isValidFileLinted = Boolean(output.results.find((file) => file.source === validFile));
const isNewFileLinted = Boolean(output.results.find((file) => file.source === newFileDest));
expect(isValidFileLinted).toBe(false);
expect(isNewFileLinted).toBe(true);
const fileCache = fCache.createFromFile(expectedCacheFilePath);
const { cache } = fileCache;
expect(typeof cache.getKey(validFile) === 'object').toBe(true);
expect(typeof cache.getKey(newFileDest) === 'object').toBe(true);
});
});
it('all files are linted on config change', () => {
const changedConfig = getConfig();
changedConfig.config.rules['block-no-empty'] = false;
return copyFile(validFile, newFileDest)
.then(() => {
// All file should be re-linted as config has changed
return standalone(changedConfig);
})
.then((output) => {
// Ensure all files are re-linted
const isValidFileLinted = Boolean(output.results.find((file) => file.source === validFile));
const isNewFileLinted = Boolean(output.results.find((file) => file.source === newFileDest));
expect(isValidFileLinted).toBe(true);
expect(isNewFileLinted).toBe(true);
});
});
it('invalid files are not cached', () => {
return copyFile(invalidFile, newFileDest)
.then(() => {
// Should lint only changed files
return standalone(getConfig());
})
.then((output) => {
expect(output.errored).toBe(true);
// Ensure only changed files are linted
const isValidFileLinted = Boolean(output.results.find((file) => file.source === validFile));
const isInvalidFileLinted = Boolean(
output.results.find((file) => file.source === newFileDest),
);
expect(isValidFileLinted).toBe(false);
expect(isInvalidFileLinted).toBe(true);
const fileCache = fCache.createFromFile(expectedCacheFilePath);
const { cache } = fileCache;
expect(typeof cache.getKey(validFile) === 'object').toBe(true);
expect(typeof cache.getKey(newFileDest) === 'undefined').toBe(true);
});
});
it('files with syntax errors are not cached', () => {
return copyFile(syntaxErrorFile, newFileDest)
.then(() => {
// Should lint only changed files
return standalone(getConfig());
})
.then(() => {
const fileCache = fCache.createFromFile(expectedCacheFilePath);
const { cache } = fileCache;
expect(typeof cache.getKey(validFile) === 'object').toBe(true);
expect(typeof cache.getKey(newFileDest) === 'undefined').toBe(true);
});
});
it('cache file is removed when cache is disabled', () => {
const noCacheConfig = getConfig();
noCacheConfig.cache = false;
let cacheFileExists = true;
return standalone(noCacheConfig).then(() => {
return fileExists(expectedCacheFilePath)
.then(() => {
throw new Error(
`Cache file is supposed to be removed, ${expectedCacheFilePath} is found instead`,
);
})
.catch(() => {
cacheFileExists = false;
expect(cacheFileExists).toBe(false);
});
});
});
it("cache doesn't do anything if string is passed", () => {
const config = {
code: '.foo {}',
codeFilename: 'foo.css',
cache: true,
config: {
rules: {
'color-no-invalid-hex': true,
},
},
};
let cacheFileExists = true;
return standalone(config).then((lintResults) => {
expect(lintResults.errored).toBe(false);
return fileExists(expectedCacheFilePath)
.then(() => {
throw new Error(
`Cache file should not be created if string is passed, ${expectedCacheFilePath} is found instead`,
);
})
.catch(() => {
cacheFileExists = false;
expect(cacheFileExists).toBe(false);
});
});
});
});
describe('standalone cache uses cacheLocation', () => {
const cacheLocationFile = path.join(fixturesPath, 'cache', '.cachefile');
const cacheLocationDir = path.join(fixturesPath, 'cache');
const expectedCacheFilePath = path.join(cacheLocationDir, `.stylelintcache_${hash(cwd)}`);
afterEach(() => {
// clean up after each test
return Promise.all([
unlink(expectedCacheFilePath).catch(() => {
// fs.unlink() throws an error if file doesn't exist and it's ok. We just
// want to make sure it's not there for next test.
}),
unlink(cacheLocationFile).catch(() => {
// fs.unlink() throws an error if file doesn't exist and it's ok. We just
// want to make sure it's not there for next test.
}),
]);
});
it('cacheLocation is a file', () => {
const config = getConfig();
config.cacheLocation = cacheLocationFile;
return standalone(config)
.then(() => {
// Ensure cache file is created
return fileExists(cacheLocationFile);
})
.then((fileStats) => {
expect(Boolean(fileStats)).toBe(true);
const fileCache = fCache.createFromFile(cacheLocationFile);
const { cache } = fileCache;
expect(typeof cache.getKey(validFile) === 'object').toBe(true);
});
});
it('cacheLocation is a directory', () => {
const config = getConfig();
config.cacheLocation = cacheLocationDir;
return standalone(config)
.then(() => {
return fileExists(expectedCacheFilePath);
})
.then((cacheFileStats) => {
// Ensure cache file is created
expect(Boolean(cacheFileStats)).toBe(true);
const fileCache = fCache.createFromFile(expectedCacheFilePath);
const { cache } = fileCache;
expect(typeof cache.getKey(validFile) === 'object').toBe(true);
});
});
});