Skip to content

Commit c2f4379

Browse files
committed
Refactor CommandCacheKey as a string union
1 parent 41965b0 commit c2f4379

4 files changed

Lines changed: 36 additions & 35 deletions

File tree

‎src/cli/output-cache.test.ts‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
cacheCommandOutput,
1212
getCachedCommandOutput,
1313
resetCachedCommandOutputs,
14-
CommandCacheKey,
1514
} from "./output-cache";
1615

1716
setupTests(test);
@@ -52,14 +51,14 @@ test.serial(
5251
async (t) => {
5352
await withCacheDir((cacheFilePath) => {
5453
writeCacheFile(cacheFilePath, {
55-
[CommandCacheKey.Version]: {
54+
version: {
5655
cmd: "/path/to/codeql",
5756
output: { version: "2.20.0" },
5857
},
5958
});
6059
t.deepEqual(
6160
getCachedCommandOutput(
62-
CommandCacheKey.Version,
61+
"version",
6362
"/path/to/codeql",
6463
isVersionInfo,
6564
),
@@ -74,14 +73,14 @@ test.serial(
7473
async (t) => {
7574
await withCacheDir((cacheFilePath) => {
7675
writeCacheFile(cacheFilePath, {
77-
[CommandCacheKey.Version]: {
76+
version: {
7877
cmd: "/path/to/other-codeql",
7978
output: { version: "2.20.0" },
8079
},
8180
});
8281
t.is(
8382
getCachedCommandOutput(
84-
CommandCacheKey.Version,
83+
"version",
8584
"/path/to/codeql",
8685
isVersionInfo,
8786
),
@@ -98,7 +97,7 @@ test.serial(
9897
fs.writeFileSync(cacheFilePath, "not valid json");
9998
t.is(
10099
getCachedCommandOutput(
101-
CommandCacheKey.Version,
100+
"version",
102101
"/path/to/codeql",
103102
isVersionInfo,
104103
),
@@ -114,7 +113,7 @@ test.serial(
114113
await withCacheDir(() => {
115114
t.is(
116115
getCachedCommandOutput(
117-
CommandCacheKey.Version,
116+
"version",
118117
"/path/to/codeql",
119118
isVersionInfo,
120119
),
@@ -136,11 +135,11 @@ test.serial(
136135
]) {
137136
resetCachedCommandOutputs();
138137
writeCacheFile(cacheFilePath, {
139-
[CommandCacheKey.Version]: { cmd: "/path/to/codeql", output },
138+
version: { cmd: "/path/to/codeql", output },
140139
});
141140
t.is(
142141
getCachedCommandOutput(
143-
CommandCacheKey.Version,
142+
"version",
144143
"/path/to/codeql",
145144
isVersionInfo,
146145
),
@@ -157,11 +156,11 @@ test.serial(
157156
async (t) => {
158157
await withCacheDir((cacheFilePath) => {
159158
writeCacheFile(cacheFilePath, {
160-
[CommandCacheKey.Version]: { output: { version: "2.20.0" } },
159+
version: { output: { version: "2.20.0" } },
161160
});
162161
t.is(
163162
getCachedCommandOutput(
164-
CommandCacheKey.Version,
163+
"version",
165164
"/path/to/codeql",
166165
isVersionInfo,
167166
),
@@ -173,13 +172,13 @@ test.serial(
173172

174173
test.serial("cacheCommandOutput persists the output to the memo", async (t) => {
175174
await withCacheDir(() => {
176-
cacheCommandOutput(CommandCacheKey.Version, "/path/to/codeql", {
175+
cacheCommandOutput("version", "/path/to/codeql", {
177176
hello: "world",
178177
});
179178

180179
// Tier 1: the value is immediately available from the memo.
181180
t.deepEqual(
182-
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
181+
getCachedCommandOutput("version", "/path/to/codeql"),
183182
{ hello: "world" },
184183
);
185184
});
@@ -189,19 +188,19 @@ test.serial(
189188
"getCachedCommandOutput prefers the in-memory memo over the file",
190189
async (t) => {
191190
await withCacheDir((cacheFilePath) => {
192-
cacheCommandOutput(CommandCacheKey.Version, "/path/to/codeql", {
191+
cacheCommandOutput("version", "/path/to/codeql", {
193192
value: 1,
194193
});
195194

196195
// Overwrite the file with a different value; the memo (tier 1) should win.
197196
writeCacheFile(cacheFilePath, {
198-
[CommandCacheKey.Version]: {
197+
version: {
199198
cmd: "/path/to/codeql",
200199
output: { value: 2 },
201200
},
202201
});
203202
t.deepEqual(
204-
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
203+
getCachedCommandOutput("version", "/path/to/codeql"),
205204
{ value: 1 },
206205
);
207206
});

‎src/cli/output-cache.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,8 @@ import { getActionsLogger } from "../logging";
1111
*/
1212
const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json";
1313

14-
/** Named keys for the CLI command-output cache. */
15-
export const CommandCacheKey = {
16-
Version: "version",
17-
ResolveLanguages: "resolveLanguages",
18-
} as const;
19-
2014
/** A key used to identify cached command output. */
21-
export type CommandCacheKey =
22-
(typeof CommandCacheKey)[keyof typeof CommandCacheKey];
15+
export type CommandCacheKey = "version" | "resolve languages";
2316

2417
/** A single cached command output together with the CLI path it came from. */
2518
interface CommandCacheEntry {

‎src/codeql.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as api from "./api-client";
1515
import {
1616
cacheCommandOutput,
1717
getCachedCommandOutput,
18-
CommandCacheKey,
18+
type CommandCacheKey,
1919
} from "./cli/output-cache";
2020
import { CliError, wrapCliConfigurationError } from "./cli-errors";
2121
import { appendExtraQueryExclusions, type Config } from "./config-utils";
@@ -42,6 +42,11 @@ import { BuildMode, CleanupLevel, getErrorMessage } from "./util";
4242

4343
type Options = Array<string | number | boolean>;
4444

45+
type CommandCacheKeyOutputMap = {
46+
version: VersionInfo;
47+
"resolve languages": ResolveLanguagesOutput;
48+
};
49+
4550
/**
4651
* Extra command line options for the codeql commands.
4752
*/
@@ -547,13 +552,17 @@ export async function getCodeQLForTesting(
547552
* @param run Invokes the CLI to compute the output when it isn't cached.
548553
* @param validate Optional type guard for values loaded from the temporary file.
549554
*/
550-
async function getCachedOrRun<T>(
551-
key: CommandCacheKey,
555+
async function getCachedOrRun<K extends CommandCacheKey>(
556+
key: K,
552557
cmd: string,
553-
run: () => Promise<T>,
554-
validate?: (output: unknown) => output is T,
555-
): Promise<T> {
556-
let result = getCachedCommandOutput<T>(key, cmd, validate);
558+
run: () => Promise<CommandCacheKeyOutputMap[K]>,
559+
validate?: (output: unknown) => output is CommandCacheKeyOutputMap[K],
560+
): Promise<CommandCacheKeyOutputMap[K]> {
561+
let result = getCachedCommandOutput<CommandCacheKeyOutputMap[K]>(
562+
key,
563+
cmd,
564+
validate,
565+
);
557566
if (result === undefined) {
558567
result = await run();
559568
cacheCommandOutput(key, cmd, result);
@@ -579,7 +588,7 @@ async function getCodeQLForCmd(
579588
},
580589
async getVersion() {
581590
return getCachedOrRun(
582-
CommandCacheKey.Version,
591+
"version",
583592
cmd,
584593
() =>
585594
runCliJson<VersionInfo>(cmd, ["version", "--format=json"], {
@@ -786,7 +795,7 @@ async function getCodeQLForCmd(
786795
},
787796
async resolveLanguages() {
788797
return getCachedOrRun(
789-
CommandCacheKey.ResolveLanguages,
798+
"resolve languages",
790799
cmd,
791800
async () => {
792801
const isFilterToLanguagesWithQueriesSupported =

‎src/status-report.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
isSelfHostedRunner,
1313
} from "./actions-util";
1414
import { getAnalysisKey, getApiClient } from "./api-client";
15-
import { getCachedCommandOutput, CommandCacheKey } from "./cli/output-cache";
15+
import { getCachedCommandOutput } from "./cli/output-cache";
1616
import { isVersionInfo } from "./codeql";
1717
import { parseRegistriesWithoutCredentials, type Config } from "./config-utils";
1818
import { DependencyCacheRestoreStatusReport } from "./dependency-caching";
@@ -285,7 +285,7 @@ export async function createStatusReportBase(
285285
}
286286
const runnerOs = getRequiredEnvParam("RUNNER_OS");
287287
const codeQlCliVersion = getCachedCommandOutput(
288-
CommandCacheKey.Version,
288+
"version",
289289
undefined,
290290
isVersionInfo,
291291
);

0 commit comments

Comments
 (0)