Skip to content

Commit a0c7312

Browse files
authored
Merge pull request #4096 from github/mario-campos/use-json-module
Use `json` module for JSON validation in `output-cache`
2 parents 6f53031 + fcd8d74 commit a0c7312

6 files changed

Lines changed: 121 additions & 87 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 39 additions & 18 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: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import path from "path";
33

44
import test from "ava";
55

6-
import { EnvVar } from "../environment";
76
import { getRunnerLogger } from "../logging";
8-
import { getTestEnv, setupTests } from "../testing-utils";
7+
import { setupTests } from "../testing-utils";
98
import * as util from "../util";
109

11-
import * as outputCache from "./output-cache";
10+
import { getCachedCodeQlVersion } from "./output-cache";
1211

1312
setupTests(test);
1413

@@ -18,18 +17,18 @@ test.serial(
1817
"getCachedCodeQlVersion reuses a version persisted by an earlier step",
1918
async (t) => {
2019
await util.withTmpDir(async (tmpDir: string) => {
21-
const cacheFile = path.join(tmpDir, "codeql-action-command-cache.json");
20+
const cacheFilePath = path.join(tmpDir, "cache.json");
21+
2222
fs.writeFileSync(
23-
cacheFile,
23+
cacheFilePath,
2424
JSON.stringify({
2525
cmd: "/path/to/codeql",
2626
entries: { version: { version: "2.20.0" } },
2727
}),
2828
"utf8",
2929
);
30-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
3130
t.deepEqual(
32-
outputCache.getCachedCodeQlVersion(logger, env, "/path/to/codeql"),
31+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
3332
{
3433
version: "2.20.0",
3534
},
@@ -42,18 +41,17 @@ test.serial(
4241
"getCachedCodeQlVersion ignores a persisted version from a different CLI",
4342
async (t) => {
4443
await util.withTmpDir(async (tmpDir: string) => {
45-
const cacheFile = path.join(tmpDir, "version.json");
44+
const cacheFilePath = path.join(tmpDir, "cache.json");
4645
fs.writeFileSync(
47-
cacheFile,
46+
cacheFilePath,
4847
JSON.stringify({
4948
cmd: "/path/to/other-codeql",
50-
version: { version: "2.20.0" },
49+
entries: { version: { version: "2.20.0" } },
5150
}),
5251
"utf8",
5352
);
54-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
5553
t.is(
56-
outputCache.getCachedCodeQlVersion(logger, env, "/path/to/codeql"),
54+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
5755
undefined,
5856
);
5957
});
@@ -64,11 +62,10 @@ test.serial(
6462
"getCachedCodeQlVersion ignores a malformed persisted value",
6563
async (t) => {
6664
await util.withTmpDir(async (tmpDir: string) => {
67-
const cacheFile = path.join(tmpDir, "version.json");
68-
fs.writeFileSync(cacheFile, "not valid json", "utf8");
69-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
65+
const cacheFilePath = path.join(tmpDir, "cache.json");
66+
fs.writeFileSync(cacheFilePath, "not valid json", "utf8");
7067
t.is(
71-
outputCache.getCachedCodeQlVersion(logger, env, "/path/to/codeql"),
68+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
7269
undefined,
7370
);
7471
});
@@ -79,9 +76,7 @@ test.serial(
7976
"getCachedCodeQlVersion ignores a persisted value with the wrong structure",
8077
async (t) => {
8178
await util.withTmpDir(async (tmpDir: string) => {
82-
const cacheFile = path.join(tmpDir, "version.json");
83-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
84-
79+
const cacheFilePath = path.join(tmpDir, "cache.json");
8580
const testValues = [
8681
{ cmd: "/path/to/codeql" },
8782
{ entries: { version: { version: "2.20.0" } } },
@@ -104,9 +99,9 @@ test.serial(
10499
].map((v) => JSON.stringify(v));
105100

106101
for (const value of testValues) {
107-
fs.writeFileSync(cacheFile, value, "utf8");
102+
fs.writeFileSync(cacheFilePath, value, "utf8");
108103
t.is(
109-
outputCache.getCachedCodeQlVersion(logger, env, "/path/to/codeql"),
104+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
110105
undefined,
111106
value,
112107
);
@@ -117,10 +112,10 @@ test.serial(
117112

118113
test.serial("getCachedCodeQlVersion ignores non-existent file", async (t) => {
119114
await util.withTmpDir(async (tmpDir: string) => {
120-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
115+
const cacheFilePath = path.join(tmpDir, "cache.json");
121116
t.notThrows(() => {
122117
t.is(
123-
outputCache.getCachedCodeQlVersion(logger, env, "/path/to/codeql"),
118+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
124119
undefined,
125120
);
126121
});

‎src/cli/output-cache.ts‎

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@ import path from "path";
33

44
import { getTemporaryDirectory } from "../actions-util";
55
import { Env } from "../environment";
6+
import * as json from "../json";
67
import { Logger } from "../logging";
78

8-
import type { VersionInfo } from "./types";
9+
import { VersionInfo, versionInfoBaseSchema } from "./types";
910

1011
/**
1112
* The keys of the command cache. Each key corresponds to a command whose output we cache.
1213
*/
1314
export type CommandCacheKey = string;
1415

1516
/**
16-
* The type of the command cache that is persisted to disk.
17+
* The JSON schema of the command cache that is persisted to disk.
1718
*/
18-
export interface OutputCache {
19-
cmd: string;
20-
entries: Record<CommandCacheKey, unknown>;
21-
}
19+
const outputCacheSchema = {
20+
cmd: json.string,
21+
entries: json.object({}),
22+
} as const satisfies json.Schema;
23+
24+
/**
25+
* The type that describes the command cache that is persisted to disk.
26+
*/
27+
export type OutputCache = json.FromSchema<typeof outputCacheSchema> & {
28+
entries: { version: VersionInfo };
29+
};
2230

2331
/**
2432
* The name of the temporary file that backs the on-disk cache of
@@ -43,18 +51,18 @@ export function resetCachedCodeQlVersion(): void {
4351
* Returns the path to the temporary file that backs the
4452
* on-disk cache of CLI responses between workflow steps.
4553
*/
46-
function getCommandCacheFilePath(env: Env): string {
54+
export function getCommandCacheFilePath(env: Env): string {
4755
return path.join(getTemporaryDirectory(env), COMMAND_CACHE_FILENAME);
4856
}
4957

5058
/**
5159
* Caches the CodeQL CLI version both in-memory and on disk.
52-
* @param env The environment variables to use.
60+
* @param cacheFilePath The path to the cache file.
5361
* @param cmd The path to the CodeQL CLI.
5462
* @param version The version information to cache.
5563
*/
5664
export function cacheCodeQlVersion(
57-
env: Env,
65+
cacheFilePath: string,
5866
cmd: string,
5967
version: VersionInfo,
6068
): void {
@@ -70,22 +78,18 @@ export function cacheCodeQlVersion(
7078
// processes, can reuse it rather than invoking `codeql version` again. We
7179
// record the CLI path so that a different step using a different CodeQL bundle
7280
// doesn't pick up a stale version.
73-
fs.writeFileSync(
74-
getCommandCacheFilePath(env),
75-
JSON.stringify(outputCache),
76-
"utf8",
77-
);
81+
fs.writeFileSync(cacheFilePath, JSON.stringify(outputCache), "utf8");
7882
}
7983

8084
/**
8185
* Returns the cached CodeQL CLI version, if any.
8286
* @param logger The logger to use for logging messages.
83-
* @param env The environment variables to use.
87+
* @param cacheFilePath The path to the cache file.
8488
* @param cmd The path to the CodeQL CLI.
8589
*/
8690
export function getCachedCodeQlVersion(
8791
logger: Logger,
88-
env: Env,
92+
cacheFilePath: string,
8993
cmd?: string,
9094
): undefined | VersionInfo {
9195
if (cachedCodeQlVersion !== undefined) {
@@ -96,11 +100,9 @@ export function getCachedCodeQlVersion(
96100
// invokes `codeql version` instead.
97101
let serialized: string;
98102
try {
99-
serialized = fs.readFileSync(getCommandCacheFilePath(env), "utf8");
103+
serialized = fs.readFileSync(cacheFilePath, "utf8");
100104
} catch (e) {
101-
logger.debug(
102-
`Cannot read CLI-cache file ${getCommandCacheFilePath(env)}: ${e}`,
103-
);
105+
logger.debug(`Cannot read CLI-cache file ${cacheFilePath}: ${e}`);
104106
return undefined;
105107
}
106108
let persisted: unknown;
@@ -127,30 +129,18 @@ export function getCachedCodeQlVersion(
127129
* @param x The value to test
128130
*/
129131
function isVersionInfo(x: unknown): x is VersionInfo {
130-
const candidate = x as Partial<VersionInfo> | null;
131-
return (
132-
typeof candidate === "object" &&
133-
candidate !== null &&
134-
typeof candidate.version === "string" &&
135-
(candidate.features === undefined ||
136-
(typeof candidate.features === "object" &&
137-
candidate.features !== null)) &&
138-
(candidate.overlayVersion === undefined ||
139-
typeof candidate.overlayVersion === "number")
140-
);
132+
return json.isObject(x) && json.validateSchema(versionInfoBaseSchema, x);
141133
}
142134

143135
/**
144136
* Determines whether a value is a `OutputCache` object.
145137
* @param x The value to test
146138
*/
147139
function isOutputCache(x: unknown): x is OutputCache {
148-
const candidate = x as Partial<OutputCache> | null;
149140
return (
150-
typeof candidate === "object" &&
151-
candidate !== null &&
152-
typeof candidate.cmd === "string" &&
153-
candidate.entries !== undefined &&
154-
isVersionInfo(candidate.entries.version)
141+
json.isObject(x) &&
142+
json.validateSchema(outputCacheSchema, x) &&
143+
json.isObject<{ version: unknown }>(x.entries) &&
144+
isVersionInfo(x.entries.version)
155145
);
156146
}

‎src/cli/types.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
export interface VersionInfo {
2-
version: string;
3-
features?: { [name: string]: boolean };
1+
import * as json from "../json";
2+
3+
/**
4+
* The JSON schema of the expected output of the `codeql version` command.
5+
*/
6+
export const versionInfoBaseSchema = {
7+
version: json.string,
8+
features: json.optional(json.object({})),
49
/**
510
* The overlay version helps deal with backward incompatible changes for
611
* overlay analysis. When a precompiled query pack reports the same overlay
@@ -9,5 +14,17 @@ export interface VersionInfo {
914
* or if either the pack or the CLI does not report an overlay version,
1015
* we need to revert to non-overlay analysis.
1116
*/
12-
overlayVersion?: number;
13-
}
17+
overlayVersion: json.optional(json.number),
18+
} as const satisfies json.Schema;
19+
20+
/**
21+
* The base type that describes the expected output of the `codeql version` command.
22+
*/
23+
export type VersionInfoBase = json.FromSchema<typeof versionInfoBaseSchema>;
24+
25+
/**
26+
* The full type that describes the expected output of the `codeql version` command.
27+
*/
28+
export type VersionInfo = Omit<VersionInfoBase, "features"> & {
29+
features?: { [name: string]: boolean };
30+
};

0 commit comments

Comments
 (0)