Skip to content

Commit cd71147

Browse files
committed
Move cache-related util functions into dedicated module
1 parent 4dc327a commit cd71147

8 files changed

Lines changed: 1108 additions & 1085 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 904 additions & 899 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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import * as fs from "fs";
2+
import path from "path";
3+
4+
import test from "ava";
5+
6+
import { EnvVar } from "../environment";
7+
import { getTestEnv, setupTests } from "../testing-utils";
8+
import * as util from "../util";
9+
10+
import * as outputCache from "./output-cache";
11+
12+
setupTests(test);
13+
14+
test.serial(
15+
"getCachedCodeQlVersion reuses a version persisted by an earlier step",
16+
async (t) => {
17+
await util.withTmpDir(async (tmpDir: string) => {
18+
const cacheFile = path.join(tmpDir, "version.json");
19+
fs.writeFileSync(
20+
cacheFile,
21+
JSON.stringify({
22+
cmd: "/path/to/codeql",
23+
version: { version: "2.20.0" },
24+
}),
25+
"utf8",
26+
);
27+
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
28+
t.deepEqual(outputCache.getCachedCodeQlVersion("/path/to/codeql", env), {
29+
version: "2.20.0",
30+
});
31+
});
32+
},
33+
);
34+
35+
test.serial(
36+
"getCachedCodeQlVersion ignores a persisted version from a different CLI",
37+
async (t) => {
38+
await util.withTmpDir(async (tmpDir: string) => {
39+
const cacheFile = path.join(tmpDir, "version.json");
40+
fs.writeFileSync(
41+
cacheFile,
42+
JSON.stringify({
43+
cmd: "/path/to/other-codeql",
44+
version: { version: "2.20.0" },
45+
}),
46+
"utf8",
47+
);
48+
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
49+
t.is(
50+
outputCache.getCachedCodeQlVersion("/path/to/codeql", env),
51+
undefined,
52+
);
53+
});
54+
},
55+
);
56+
57+
test.serial(
58+
"getCachedCodeQlVersion ignores a malformed persisted value",
59+
async (t) => {
60+
await util.withTmpDir(async (tmpDir: string) => {
61+
const cacheFile = path.join(tmpDir, "version.json");
62+
fs.writeFileSync(cacheFile, "not valid json", "utf8");
63+
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
64+
t.is(
65+
outputCache.getCachedCodeQlVersion("/path/to/codeql", env),
66+
undefined,
67+
);
68+
});
69+
},
70+
);
71+
72+
test.serial(
73+
"getCachedCodeQlVersion ignores a persisted value with the wrong structure",
74+
async (t) => {
75+
await util.withTmpDir(async (tmpDir: string) => {
76+
const cacheFile = path.join(tmpDir, "version.json");
77+
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
78+
79+
const testValues = [
80+
{ cmd: "/path/to/codeql" },
81+
{ cmd: "/path/to/codeql", version: {} },
82+
{ cmd: "/path/to/codeql", version: { version: 2 } },
83+
{ version: { version: "2.20.0" } },
84+
{
85+
cmd: "/path/to/codeql",
86+
version: { version: "2.20.0", overlayVersion: "1" },
87+
},
88+
{
89+
cmd: "/path/to/codeql",
90+
version: { version: "2.20.0", features: "nope" },
91+
},
92+
].map((v) => JSON.stringify(v));
93+
94+
for (const value of testValues) {
95+
fs.writeFileSync(cacheFile, value, "utf8");
96+
t.is(
97+
outputCache.getCachedCodeQlVersion("/path/to/codeql", env),
98+
undefined,
99+
value,
100+
);
101+
}
102+
});
103+
},
104+
);
105+
106+
test.serial("getCachedCodeQlVersion ignores non-existent file", async (t) => {
107+
await util.withTmpDir(async (tmpDir: string) => {
108+
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
109+
t.is(outputCache.getCachedCodeQlVersion("/path/to/codeql", env), undefined);
110+
});
111+
});

‎src/cli/output-cache.ts‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,100 @@
1+
import * as fs from "fs";
12
import path from "path";
23

34
import { getTemporaryDirectory } from "../actions-util";
5+
import { VersionInfo } from "../codeql";
6+
import { Env, getEnv } from "../environment";
7+
import { isPersistedVersionInfo } from "../util";
48

59
/**
610
* The name of the temporary file that backs the on-disk cache of
711
* CLI responses between workflow steps.
812
*/
913
const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json";
1014

15+
/**
16+
* The module-global variable that caches the CodeQL CLI version in-memory.
17+
*/
18+
let cachedCodeQlVersion: undefined | VersionInfo = undefined;
19+
20+
/**
21+
* Resets the in-process cache of the CodeQL CLI version. Only for use in tests,
22+
* which exercise multiple "steps" within a single process.
23+
*/
24+
export function resetCachedCodeQlVersion(): void {
25+
cachedCodeQlVersion = undefined;
26+
}
27+
1128
/**
1229
* Returns the path to the temporary file that backs the
1330
* on-disk cache of CLI responses between workflow steps.
1431
*/
15-
function getCommandCacheFilePath(): string {
16-
return path.join(getTemporaryDirectory(), COMMAND_CACHE_FILENAME);
32+
function getCommandCacheFilePath(env: Env): string {
33+
return path.join(getTemporaryDirectory(env), COMMAND_CACHE_FILENAME);
34+
}
35+
36+
/**
37+
* Caches the CodeQL CLI version both in-memory and on disk.
38+
* @param cmd The path to the CodeQL CLI.
39+
* @param version The version information to cache.
40+
* @param env The environment variables to use.
41+
*/
42+
export function cacheCodeQlVersion(
43+
cmd: string,
44+
version: VersionInfo,
45+
env: Env = getEnv(),
46+
): void {
47+
if (cachedCodeQlVersion !== undefined) {
48+
throw new Error("cacheCodeQlVersion() should be called only once");
49+
}
50+
cachedCodeQlVersion = version;
51+
// Persist the version so that subsequent Actions steps, which run in separate
52+
// processes, can reuse it rather than invoking `codeql version` again. We
53+
// record the CLI path so that a different step using a different CodeQL bundle
54+
// doesn't pick up a stale version.
55+
fs.writeFileSync(
56+
getCommandCacheFilePath(env),
57+
JSON.stringify({ cmd, version }),
58+
"utf8",
59+
);
60+
}
61+
62+
/**
63+
* Returns the cached CodeQL CLI version, if any. If not cached,
64+
* attempts to read and parse it from disk.
65+
* @param cmd The path to the CodeQL CLI.
66+
* @param env The environment variables to use.
67+
*/
68+
export function getCachedCodeQlVersion(
69+
cmd?: string,
70+
env: Env = getEnv(),
71+
): undefined | VersionInfo {
72+
if (cachedCodeQlVersion !== undefined) {
73+
return cachedCodeQlVersion;
74+
}
75+
// Fall back to the value persisted by an earlier Actions step, if any. This is
76+
// best-effort: any malformed or mismatched value is ignored so that the caller
77+
// invokes `codeql version` instead.
78+
let serialized: string;
79+
try {
80+
serialized = fs.readFileSync(getCommandCacheFilePath(env), "utf8");
81+
} catch {
82+
return undefined;
83+
}
84+
let persisted: unknown;
85+
try {
86+
persisted = JSON.parse(serialized);
87+
} catch {
88+
return undefined;
89+
}
90+
if (
91+
!isPersistedVersionInfo(persisted) ||
92+
(cmd !== undefined && persisted.cmd !== cmd)
93+
) {
94+
return undefined;
95+
}
96+
// Memoize the parsed value so that subsequent calls in this process don't
97+
// re-parse the environment variable.
98+
cachedCodeQlVersion = persisted.version;
99+
return cachedCodeQlVersion;
17100
}

‎src/codeql.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
runTool,
1313
} from "./actions-util";
1414
import * as api from "./api-client";
15+
import * as outputCache from "./cli/output-cache";
1516
import { CliError, wrapCliConfigurationError } from "./cli-errors";
1617
import { appendExtraQueryExclusions, type Config } from "./config-utils";
1718
import { DocUrl } from "./doc-url";
@@ -502,7 +503,7 @@ async function getCodeQLForCmd(
502503
return cmd;
503504
},
504505
async getVersion() {
505-
let result = util.getCachedCodeQlVersion(cmd);
506+
let result = outputCache.getCachedCodeQlVersion(cmd);
506507
if (result === undefined) {
507508
result = await runCliJson<VersionInfo>(
508509
cmd,
@@ -511,7 +512,7 @@ async function getCodeQLForCmd(
511512
noStreamStdout: true,
512513
},
513514
);
514-
util.cacheCodeQlVersion(cmd, result);
515+
outputCache.cacheCodeQlVersion(cmd, result);
515516
}
516517
return result;
517518
},

‎src/status-report.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
isSelfHostedRunner,
1515
} from "./actions-util";
1616
import { getAnalysisKey, getApiClient } from "./api-client";
17+
import { getCachedCodeQlVersion } from "./cli/output-cache";
1718
import type { Config } from "./config/action-config";
1819
import type { ComputedInput, InputName } from "./config/inputs";
1920
import { parseRegistriesWithoutCredentials } from "./config/pack-registries";
@@ -30,7 +31,6 @@ import { registryBaseSchema } from "./start-proxy/types";
3031
import {
3132
ConfigurationError,
3233
getRequiredEnvParam,
33-
getCachedCodeQlVersion,
3434
isInTestMode,
3535
GITHUB_DOTCOM_URL,
3636
DiskUsage,

‎src/testing-utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { AnalysisKind } from "./analyses";
1818
import * as apiClient from "./api-client";
1919
import { GitHubApiDetails } from "./api-client";
2020
import { CachingKind } from "./caching-utils";
21+
import { resetCachedCodeQlVersion } from "./cli/output-cache";
2122
import * as codeql from "./codeql";
2223
import { Config } from "./config-utils";
2324
import * as defaults from "./defaults.json";
@@ -39,7 +40,6 @@ import {
3940
GitHubVariant,
4041
GitHubVersion,
4142
HTTPError,
42-
resetCachedCodeQlVersion,
4343
Result,
4444
Success,
4545
} from "./util";

‎src/util.test.ts‎

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as sinon from "sinon";
1010
import * as api from "./api-client";
1111
import { EnvVar } from "./environment";
1212
import { getRunnerLogger } from "./logging";
13-
import { getTestEnv, setupTests } from "./testing-utils";
13+
import { setupTests } from "./testing-utils";
1414
import * as util from "./util";
1515

1616
setupTests(test);
@@ -532,96 +532,3 @@ test("Failure.orElse returns the default value for a failure result", (t) => {
532532
const result = new util.Failure(new Error("test error"));
533533
t.is(result.orElse("default value"), "default value");
534534
});
535-
536-
test.serial(
537-
"getCachedCodeQlVersion reuses a version persisted by an earlier step",
538-
async (t) => {
539-
await util.withTmpDir(async (tmpDir: string) => {
540-
const cacheFile = path.join(tmpDir, "version.json");
541-
fs.writeFileSync(
542-
cacheFile,
543-
JSON.stringify({
544-
cmd: "/path/to/codeql",
545-
version: { version: "2.20.0" },
546-
}),
547-
"utf8",
548-
);
549-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
550-
t.deepEqual(util.getCachedCodeQlVersion("/path/to/codeql", env), {
551-
version: "2.20.0",
552-
});
553-
});
554-
},
555-
);
556-
557-
test.serial(
558-
"getCachedCodeQlVersion ignores a persisted version from a different CLI",
559-
async (t) => {
560-
await util.withTmpDir(async (tmpDir: string) => {
561-
const cacheFile = path.join(tmpDir, "version.json");
562-
fs.writeFileSync(
563-
cacheFile,
564-
JSON.stringify({
565-
cmd: "/path/to/other-codeql",
566-
version: { version: "2.20.0" },
567-
}),
568-
"utf8",
569-
);
570-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
571-
t.is(util.getCachedCodeQlVersion("/path/to/codeql", env), undefined);
572-
});
573-
},
574-
);
575-
576-
test.serial(
577-
"getCachedCodeQlVersion ignores a malformed persisted value",
578-
async (t) => {
579-
await util.withTmpDir(async (tmpDir: string) => {
580-
const cacheFile = path.join(tmpDir, "version.json");
581-
fs.writeFileSync(cacheFile, "not valid json", "utf8");
582-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
583-
t.is(util.getCachedCodeQlVersion("/path/to/codeql", env), undefined);
584-
});
585-
},
586-
);
587-
588-
test.serial(
589-
"getCachedCodeQlVersion ignores a persisted value with the wrong structure",
590-
async (t) => {
591-
await util.withTmpDir(async (tmpDir: string) => {
592-
const cacheFile = path.join(tmpDir, "version.json");
593-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
594-
595-
const testValues = [
596-
{ cmd: "/path/to/codeql" },
597-
{ cmd: "/path/to/codeql", version: {} },
598-
{ cmd: "/path/to/codeql", version: { version: 2 } },
599-
{ version: { version: "2.20.0" } },
600-
{
601-
cmd: "/path/to/codeql",
602-
version: { version: "2.20.0", overlayVersion: "1" },
603-
},
604-
{
605-
cmd: "/path/to/codeql",
606-
version: { version: "2.20.0", features: "nope" },
607-
},
608-
].map((v) => JSON.stringify(v));
609-
610-
for (const value of testValues) {
611-
fs.writeFileSync(cacheFile, value, "utf8");
612-
t.is(
613-
util.getCachedCodeQlVersion("/path/to/codeql", env),
614-
undefined,
615-
value,
616-
);
617-
}
618-
});
619-
},
620-
);
621-
622-
test.serial("getCachedCodeQlVersion ignores non-existent file", async (t) => {
623-
await util.withTmpDir(async (tmpDir: string) => {
624-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
625-
t.is(util.getCachedCodeQlVersion("/path/to/codeql", env), undefined);
626-
});
627-
});

0 commit comments

Comments
 (0)