Skip to content

Commit

Permalink
Remove cache entry that not set
Browse files Browse the repository at this point in the history
  • Loading branch information
Milly committed Jun 20, 2022
1 parent 183cc7c commit d22b1fa
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/cli/format-results-cache.js
Expand Up @@ -88,6 +88,13 @@ class FormatResultsCache {
}
}

/**
* @param {string} filePath
*/
removeFormatResultsCache(filePath) {
this.fileEntryCache.removeEntry(filePath);
}

reconcile() {
this.fileEntryCache.reconcile();
}
Expand Down
2 changes: 2 additions & 0 deletions src/cli/format.js
Expand Up @@ -466,6 +466,8 @@ async function formatFiles(context) {

if (isCacheSet) {
formatResultsCache?.setFormatResultsCache(filename, options);
} else {
formatResultsCache?.removeFormatResultsCache(filename);
}

if (isDifferent) {
Expand Down
82 changes: 82 additions & 0 deletions tests/integration/__tests__/cache.js
Expand Up @@ -227,6 +227,47 @@ describe("--cache option", () => {
);
});

it("don't set cache that file is already cached and has different.", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--write",
"--cache",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
])
);

// Update `a.js`
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;");

const { stdout: secondStdout } = await runPrettier(dir, [
"--list-different",
"--cache",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual([
"a.js",
]);

const { stdout: thirdStdout } = await runPrettier(dir, [
"--list-different",
"--cache",
"--cache-strategy",
"metadata",
".",
]);
expect(stripAnsi(thirdStdout).split("\n").filter(Boolean)).toEqual([
"a.js",
]);
});

it("removes cache file when run Prettier without `--cache` option", async () => {
await runPrettier(dir, [
"--cache",
Expand Down Expand Up @@ -399,6 +440,47 @@ describe("--cache option", () => {
);
});

it("don't set cache that file is already cached and has different.", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
"--write",
"--cache",
"--cache-strategy",
"content",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
])
);

// Update `a.js`
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;");

const { stdout: secondStdout } = await runPrettier(dir, [
"--list-different",
"--cache",
"--cache-strategy",
"content",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual([
"a.js",
]);

const { stdout: thirdStdout } = await runPrettier(dir, [
"--list-different",
"--cache",
"--cache-strategy",
"content",
".",
]);
expect(stripAnsi(thirdStdout).split("\n").filter(Boolean)).toEqual([
"a.js",
]);
});

it("removes cache file when run Prettier without `--cache` option", async () => {
await runPrettier(dir, ["--cache", "--write", "."]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError();
Expand Down

0 comments on commit d22b1fa

Please sign in to comment.