Skip to content

Commit da14981

Browse files
committed
Switch CommandCacheKey back to an enum
The code reads better/simpler as an `enum`.
1 parent 0d69817 commit da14981

5 files changed

Lines changed: 91 additions & 101 deletions

File tree

lib/entry-points.js

Lines changed: 51 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/output-cache.test.ts

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { setupTests } from "../testing-utils";
88

99
import {
1010
cacheCommandOutput,
11+
CommandCacheKey,
1112
getCachedCommandOutput,
1213
resetCachedCommandOutputs,
1314
type VersionInfo,
@@ -51,16 +52,13 @@ test.serial(
5152
async (t) => {
5253
await withCacheDir((cacheFilePath) => {
5354
writeCacheFile(cacheFilePath, {
54-
version: {
55+
[CommandCacheKey.Version]: {
5556
cmd: "/path/to/codeql",
5657
output: { version: "2.20.0" },
5758
},
5859
});
5960
t.deepEqual(
60-
getCachedCommandOutput(
61-
"version",
62-
"/path/to/codeql",
63-
),
61+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
6462
{ version: "2.20.0" },
6563
);
6664
});
@@ -72,16 +70,13 @@ test.serial(
7270
async (t) => {
7371
await withCacheDir((cacheFilePath) => {
7472
writeCacheFile(cacheFilePath, {
75-
version: {
73+
[CommandCacheKey.Version]: {
7674
cmd: "/path/to/other-codeql",
7775
output: { version: "2.20.0" },
7876
},
7977
});
8078
t.is(
81-
getCachedCommandOutput(
82-
"version",
83-
"/path/to/codeql",
84-
),
79+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
8580
undefined,
8681
);
8782
});
@@ -94,10 +89,7 @@ test.serial(
9489
await withCacheDir((cacheFilePath) => {
9590
fs.writeFileSync(cacheFilePath, "not valid json");
9691
t.is(
97-
getCachedCommandOutput(
98-
"version",
99-
"/path/to/codeql",
100-
),
92+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
10193
undefined,
10294
);
10395
});
@@ -109,10 +101,7 @@ test.serial(
109101
async (t) => {
110102
await withCacheDir(() => {
111103
t.is(
112-
getCachedCommandOutput(
113-
"version",
114-
"/path/to/codeql",
115-
),
104+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
116105
undefined,
117106
);
118107
});
@@ -131,13 +120,10 @@ test.serial(
131120
]) {
132121
resetCachedCommandOutputs();
133122
writeCacheFile(cacheFilePath, {
134-
version: { cmd: "/path/to/codeql", output },
123+
[CommandCacheKey.Version]: { cmd: "/path/to/codeql", output },
135124
});
136125
t.is(
137-
getCachedCommandOutput(
138-
"version",
139-
"/path/to/codeql",
140-
),
126+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
141127
undefined,
142128
JSON.stringify(output),
143129
);
@@ -151,13 +137,10 @@ test.serial(
151137
async (t) => {
152138
await withCacheDir((cacheFilePath) => {
153139
writeCacheFile(cacheFilePath, {
154-
version: { output: { version: "2.20.0" } },
140+
[CommandCacheKey.Version]: { output: { version: "2.20.0" } },
155141
});
156142
t.is(
157-
getCachedCommandOutput(
158-
"version",
159-
"/path/to/codeql",
160-
),
143+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
161144
undefined,
162145
);
163146
});
@@ -167,10 +150,13 @@ test.serial(
167150
test.serial("cacheCommandOutput persists the output to the memo", async (t) => {
168151
await withCacheDir(() => {
169152
const output: VersionInfo = { version: "2.20.0" };
170-
cacheCommandOutput("version", "/path/to/codeql", output);
153+
cacheCommandOutput(CommandCacheKey.Version, "/path/to/codeql", output);
171154

172155
// Tier 1: the value is immediately available from the memo.
173-
t.deepEqual(getCachedCommandOutput("version", "/path/to/codeql"), output);
156+
t.deepEqual(
157+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
158+
output,
159+
);
174160
});
175161
});
176162

@@ -179,16 +165,19 @@ test.serial(
179165
async (t) => {
180166
await withCacheDir((cacheFilePath) => {
181167
const output: VersionInfo = { version: "2.20.0", overlayVersion: 1 };
182-
cacheCommandOutput("version", "/path/to/codeql", output);
168+
cacheCommandOutput(CommandCacheKey.Version, "/path/to/codeql", output);
183169

184170
// Overwrite the file with a different value; the memo (tier 1) should win.
185171
writeCacheFile(cacheFilePath, {
186-
version: {
172+
[CommandCacheKey.Version]: {
187173
cmd: "/path/to/codeql",
188174
output: { version: "2.21.0" },
189175
},
190176
});
191-
t.deepEqual(getCachedCommandOutput("version", "/path/to/codeql"), output);
177+
t.deepEqual(
178+
getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"),
179+
output,
180+
);
192181
});
193182
},
194183
);

src/cli/output-cache.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { getActionsLogger } from "../logging";
1212
const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json";
1313

1414
/** A key used to identify cached command output. */
15-
export type CommandCacheKey = "version" | "resolve languages";
15+
export enum CommandCacheKey {
16+
Version = "version",
17+
ResolveLanguages = "resolve languages",
18+
}
1619

1720
export interface VersionInfo {
1821
version: string;
@@ -78,17 +81,17 @@ export function isResolveLanguagesOutput(
7881
}
7982

8083
export type CommandCacheKeyOutputMap = {
81-
version: VersionInfo;
82-
"resolve languages": ResolveLanguagesOutput;
84+
[CommandCacheKey.Version]: VersionInfo;
85+
[CommandCacheKey.ResolveLanguages]: ResolveLanguagesOutput;
8386
};
8487

8588
const commandCacheValidators: {
8689
[K in CommandCacheKey]: (
8790
output: unknown,
8891
) => output is CommandCacheKeyOutputMap[K];
8992
} = {
90-
version: isVersionInfo,
91-
"resolve languages": isResolveLanguagesOutput,
93+
[CommandCacheKey.Version]: isVersionInfo,
94+
[CommandCacheKey.ResolveLanguages]: isResolveLanguagesOutput,
9295
};
9396

9497
interface StoredCommandCacheEntry {
@@ -174,8 +177,8 @@ export function cacheCommandOutput<K extends CommandCacheKey>(
174177
*
175178
* Resolves tier 1 (in-memory memo) first, then tier 2 (temporary file). A value
176179
* loaded from the file is ignored unless its `cmd` matches the optional `cmd`
177-
* argument, and it satisfies the optional `validate` type guard; valid values
178-
* are memoized into tier 1 before being returned.
180+
* argument and its output satisfies the internal validator for `key`; valid
181+
* values are memoized into tier 1 before being returned.
179182
*
180183
* A return value of `undefined` signals the caller to fall back to tier 3 (the
181184
* CLI).

src/codeql.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
import * as api from "./api-client";
1515
import {
1616
cacheCommandOutput,
17+
CommandCacheKey,
1718
getCachedCommandOutput,
1819
type CommandCacheKeyOutputMap,
19-
type CommandCacheKey,
2020
type ResolveLanguagesOutput,
2121
type VersionInfo,
2222
} from "./cli/output-cache";
@@ -515,7 +515,7 @@ async function getCodeQLForCmd(
515515
return cmd;
516516
},
517517
async getVersion() {
518-
return getCachedOrRun("version", cmd, () =>
518+
return getCachedOrRun(CommandCacheKey.Version, cmd, () =>
519519
runCliJson<VersionInfo>(cmd, ["version", "--format=json"], {
520520
noStreamStdout: true,
521521
}),
@@ -718,7 +718,7 @@ async function getCodeQLForCmd(
718718
await runCli(cmd, args);
719719
},
720720
async resolveLanguages() {
721-
return getCachedOrRun("resolve languages", cmd, async () => {
721+
return getCachedOrRun(CommandCacheKey.ResolveLanguages, cmd, async () => {
722722
const isFilterToLanguagesWithQueriesSupported =
723723
await this.supportsFeature(
724724
ToolsFeature.BuiltinExtractorsSpecifyDefaultQueries,

0 commit comments

Comments
 (0)