Repository navigation
Expand file tree
/
Copy pathoutput-cache.test.ts
More file actions
220 lines (200 loc) · 5.93 KB
/
Copy pathoutput-cache.test.ts
File metadata and controls
220 lines (200 loc) · 5.93 KB
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
import * as fs from "fs";
import * as os from "os";
import path from "path";
import test from "ava";
import { setupTests } from "../testing-utils";
import {
cacheCommandOutput,
CommandCacheKey,
getCachedCommandOutput,
resetCachedCommandOutputs,
type VersionInfo,
} from "./output-cache";
setupTests(test);
const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json";
/**
* Runs `body` with a temporary directory configured as the cache's backing
* store (`RUNNER_TEMP`). `CODEQL_ACTION_TEMP` is cleared so that
* `getTemporaryDirectory()` falls back to `RUNNER_TEMP`.
*
* `setupTests` snapshots and restores `process.env` around every test, so we
* don't restore the environment variables we set here ourselves.
*/
async function withCacheDir(
body: (cacheFilePath: string) => Promise<void> | void,
): Promise<void> {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "cache-test-"));
process.env["RUNNER_TEMP"] = tmpDir;
delete process.env["CODEQL_ACTION_TEMP"];
resetCachedCommandOutputs();
try {
await body(path.join(tmpDir, COMMAND_CACHE_FILENAME));
} finally {
await fs.promises.rm(tmpDir, { force: true, recursive: true });
}
}
function writeCacheFile(
cacheFilePath: string,
contents: Record<string, unknown>,
): void {
fs.writeFileSync(cacheFilePath, JSON.stringify(contents));
}
test.serial(
"getCachedCommandOutput reuses an output persisted by an earlier step",
async (t) => {
await withCacheDir((cacheFilePath) => {
writeCacheFile(cacheFilePath, {
[CommandCacheKey.Version]: {
cmd: "/path/to/codeql",
output: { version: "2.20.0" },
},
});
t.deepEqual(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
{ version: "2.20.0" },
);
});
},
);
test.serial(
"getCachedCommandOutput ignores an output persisted from a different CLI",
async (t) => {
await withCacheDir((cacheFilePath) => {
writeCacheFile(cacheFilePath, {
[CommandCacheKey.Version]: {
cmd: "/path/to/other-codeql",
output: { version: "2.20.0" },
},
});
t.is(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
undefined,
);
});
},
);
test.serial(
"getCachedCommandOutput ignores a malformed cache file",
async (t) => {
await withCacheDir((cacheFilePath) => {
fs.writeFileSync(cacheFilePath, "not valid json");
t.is(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
undefined,
);
});
},
);
test.serial(
"getCachedCommandOutput returns undefined when there is no cache file",
async (t) => {
await withCacheDir(() => {
t.is(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
undefined,
);
});
},
);
test.serial(
"getCachedCommandOutput ignores an output that fails validation",
async (t) => {
await withCacheDir((cacheFilePath) => {
for (const output of [
{},
{ version: 2 },
{ version: "2.20.0", overlayVersion: "1" },
{ version: "2.20.0", features: "nope" },
]) {
resetCachedCommandOutputs();
writeCacheFile(cacheFilePath, {
[CommandCacheKey.Version]: { cmd: "/path/to/codeql", output },
});
t.is(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
undefined,
JSON.stringify(output),
);
}
});
},
);
test.serial(
"getCachedCommandOutput ignores an entry missing the cmd field",
async (t) => {
await withCacheDir((cacheFilePath) => {
writeCacheFile(cacheFilePath, {
[CommandCacheKey.Version]: { output: { version: "2.20.0" } },
});
t.is(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
undefined,
);
});
},
);
test.serial("cacheCommandOutput persists the output to the memo", async (t) => {
await withCacheDir(() => {
const output: VersionInfo = { version: "2.20.0" };
cacheCommandOutput(CommandCacheKey.Version, "/path/to/codeql", output);
// Tier 1: the value is immediately available from the memo.
t.deepEqual(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
output,
);
});
});
test.serial(
"getCachedCommandOutput treats a memoized output from another CLI as a miss",
async (t) => {
await withCacheDir(() => {
cacheCommandOutput(CommandCacheKey.Version, "/path/to/other-codeql", {
version: "2.20.0",
});
t.is(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
undefined,
);
});
},
);
test.serial(
"getCachedCommandOutput prefers the in-memory memo over the file",
async (t) => {
await withCacheDir((cacheFilePath) => {
const output: VersionInfo = { version: "2.20.0", overlayVersion: 1 };
cacheCommandOutput(CommandCacheKey.Version, "/path/to/codeql", output);
// Overwrite the file with a different value; the memo (tier 1) should win.
writeCacheFile(cacheFilePath, {
[CommandCacheKey.Version]: {
cmd: "/path/to/codeql",
output: { version: "2.21.0" },
},
});
t.deepEqual(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
output,
);
});
},
);
test.serial(
"getCachedCommandOutput falls back to file when memoized output comes from another CLI",
async (t) => {
await withCacheDir((cacheFilePath) => {
cacheCommandOutput(CommandCacheKey.Version, "/path/to/other-codeql", {
version: "2.19.0",
});
writeCacheFile(cacheFilePath, {
[CommandCacheKey.Version]: {
cmd: "/path/to/codeql",
output: { version: "2.20.0", overlayVersion: 1 },
},
});
t.deepEqual(
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
{ version: "2.20.0", overlayVersion: 1 },
);
});
},
);