-
-
Notifications
You must be signed in to change notification settings - Fork 928
/
standalone-cache.test.js
195 lines (151 loc) · 6.04 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
'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 standalone = require('../standalone');
// NOTE: `fs.rm(file, { force: true })` will be available when we drop the support of older Node versions.
// See <https://nodejs.org/api/fs.html#fs_fs_rm_path_options_callback>
const removeFile = async (filePath) => {
if (existsSync(filePath)) {
await fs.unlink(filePath);
}
};
const cwd = process.cwd();
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 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) => file.source === validFile)).toBe(false);
expect(results.some((file) => file.source === 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) => file.source === validFile)).toBe(true);
expect(results.some((file) => file.source === 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) => file.source === validFile)).toBe(false);
expect(results.some((file) => file.source === 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 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();
});
});