-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
cache-location.js
112 lines (100 loc) · 3.12 KB
/
cache-location.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
"use strict";
const path = require("path");
const { promises: fs } = require("fs");
const rimraf = require("rimraf");
const { default: stripAnsi } = require("../../../vendors/strip-ansi.js");
const runPrettier = require("../run-prettier.js");
function resolveDir(dir) {
return path.join(__dirname, "..", dir);
}
function isJson(str) {
try {
JSON.parse(str);
return true;
} catch {
return false;
}
}
describe("--cache-location option", () => {
const dir = resolveDir("cli/cache-location");
const defaultCacheFile = path.join(dir, ".prettiercache");
const newCacheFile = path.join(dir, ".new_prettiercache");
const newCacheDir = path.join(dir, "cache-dir");
afterEach(() => {
rimraf.sync(newCacheFile);
rimraf.sync(newCacheDir);
});
it("doesn't create `.prettiercache` file when `--cache-location` is used.", async () => {
await runPrettier(dir, [
"--cache",
"--cache-location=.new_prettiercache",
".",
]);
await expect(fs.stat(defaultCacheFile)).rejects.toHaveProperty(
"code",
"ENOENT"
);
});
describe("specify file name", () => {
it("creates cache file at the specified file", async () => {
await runPrettier(dir, [
"--cache",
"--cache-location=.new_prettiercache",
".",
]);
const cacheFileData = await fs.readFile(newCacheFile, "utf8");
await expect(isJson(cacheFileData)).toBe(true);
});
it("skips formatting when cache is available", async () => {
await runPrettier(dir, [
"--cache",
"--cache-location=.new_prettiercache",
"--write",
".",
]);
const { stdout } = await runPrettier(dir, [
"--cache",
"--cache-location=.new_prettiercache",
"--write",
".",
]);
expect(stripAnsi(stdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
])
);
});
});
describe("specify director name", () => {
it("creates cache file into the specified directory", async () => {
await runPrettier(dir, ["--cache", "--cache-location=cache-dir/", "."]);
const fileNames = await fs.readdir(newCacheDir);
expect(fileNames.length).toBe(1);
const cacheFileName = fileNames[0];
const filePath = path.join(newCacheDir, cacheFileName);
const cacheFileData = await fs.readFile(filePath, "utf8");
await expect(isJson(cacheFileData)).toBe(true);
});
it("skips formatting when cache is available", async () => {
await runPrettier(dir, [
"--cache",
"--cache-location=cache-dir/",
"--write",
".",
]);
const { stdout } = await runPrettier(dir, [
"--cache",
"--cache-location=cache-dir/",
"--write",
".",
]);
expect(stripAnsi(stdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
])
);
});
});
});