Skip to content

Commit e21b36b

Browse files
not-an-aardvarkplatinumazure
authored andcommitted
Chore: add integration tests for cache files (refs #7748) (#7794)
1 parent 2322733 commit e21b36b

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/bin/eslint.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,111 @@ describe("bin/eslint.js", () => {
142142
});
143143
});
144144

145+
describe("cache files", () => {
146+
const CACHE_PATH = ".temp-eslintcache";
147+
const SOURCE_PATH = "tests/fixtures/cache/src/test-file.js";
148+
const ARGS_WITHOUT_CACHE = ["--no-eslintrc", "--no-ignore", SOURCE_PATH, "--cache-location", CACHE_PATH];
149+
const ARGS_WITH_CACHE = ARGS_WITHOUT_CACHE.concat("--cache");
150+
151+
describe("when no cache file exists", () => {
152+
it("creates a cache file when the --cache flag is used", () => {
153+
const child = runESLint(ARGS_WITH_CACHE);
154+
155+
return assertExitCode(child, 0).then(() => {
156+
assert.isTrue(fs.existsSync(CACHE_PATH), "Cache file should exist at the given location");
157+
158+
assert.doesNotThrow(
159+
() => JSON.parse(fs.readFileSync(CACHE_PATH, "utf8")),
160+
SyntaxError,
161+
"Cache file should contain valid JSON"
162+
);
163+
});
164+
});
165+
});
166+
167+
describe("when a valid cache file already exists", () => {
168+
beforeEach(() => {
169+
const child = runESLint(ARGS_WITH_CACHE);
170+
171+
return assertExitCode(child, 0).then(() => {
172+
assert.isTrue(fs.existsSync(CACHE_PATH), "Cache file should exist at the given location");
173+
});
174+
});
175+
it("can lint with an existing cache file and the --cache flag", () => {
176+
const child = runESLint(ARGS_WITH_CACHE);
177+
178+
return assertExitCode(child, 0).then(() => {
179+
180+
// Note: This doesn't actually verify that the cache file is used for anything.
181+
assert.isTrue(fs.existsSync(CACHE_PATH), "Cache file should still exist after linting with --cache");
182+
});
183+
});
184+
it("updates the cache file when the source file is modified", () => {
185+
const initialCacheContent = fs.readFileSync(CACHE_PATH, "utf8");
186+
187+
// Update the file to change its mtime
188+
fs.writeFileSync(SOURCE_PATH, fs.readFileSync(SOURCE_PATH, "utf8"));
189+
190+
const child = runESLint(ARGS_WITH_CACHE);
191+
192+
return assertExitCode(child, 0).then(() => {
193+
const newCacheContent = fs.readFileSync(CACHE_PATH, "utf8");
194+
195+
assert.notStrictEqual(initialCacheContent, newCacheContent, "Cache file should change after source is modified");
196+
});
197+
});
198+
it("deletes the cache file when run without the --cache argument", () => {
199+
const child = runESLint(ARGS_WITHOUT_CACHE);
200+
201+
return assertExitCode(child, 0).then(() => {
202+
assert.isFalse(fs.existsSync(CACHE_PATH), "Cache file should be deleted after running ESLint without the --cache argument");
203+
});
204+
});
205+
});
206+
207+
// https://github.com/eslint/eslint/issues/7748
208+
describe("when an invalid cache file already exists", () => {
209+
beforeEach(() => {
210+
fs.writeFileSync(CACHE_PATH, "This is not valid JSON.");
211+
212+
// Sanity check
213+
assert.throws(
214+
() => JSON.parse(fs.readFileSync(CACHE_PATH, "utf8")),
215+
SyntaxError,
216+
/Unexpected token/,
217+
"Cache file should not contain valid JSON at the start"
218+
);
219+
});
220+
221+
it("overwrites the invalid cache file with a valid one when the --cache argument is used", () => {
222+
const child = runESLint(ARGS_WITH_CACHE);
223+
224+
return assertExitCode(child, 0).then(() => {
225+
assert.isTrue(fs.existsSync(CACHE_PATH), "Cache file should exist at the given location");
226+
assert.doesNotThrow(
227+
() => JSON.parse(fs.readFileSync(CACHE_PATH, "utf8")),
228+
SyntaxError,
229+
"Cache file should contain valid JSON"
230+
);
231+
});
232+
});
233+
234+
it("deletes the invalid cache file when the --cache argument is not used", () => {
235+
const child = runESLint(ARGS_WITHOUT_CACHE);
236+
237+
return assertExitCode(child, 0).then(() => {
238+
assert.isFalse(fs.existsSync(CACHE_PATH), "Cache file should be deleted after running ESLint without the --cache argument");
239+
});
240+
});
241+
});
242+
243+
afterEach(() => {
244+
if (fs.existsSync(CACHE_PATH)) {
245+
fs.unlinkSync(CACHE_PATH);
246+
}
247+
});
248+
});
249+
145250
afterEach(() => {
146251

147252
// Clean up all the processes after every test.

0 commit comments

Comments
 (0)