Skip to content

Commit fcd8d74

Browse files
committed
Refactor output-caching functions to accept file path dependency
This makes it easier to test. Credit to @mbg.
1 parent b5d3438 commit fcd8d74

5 files changed

Lines changed: 70 additions & 57 deletions

File tree

lib/entry-points.js

Lines changed: 16 additions & 14 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: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import * as fs from "fs";
2+
import path from "path";
23

34
import test from "ava";
45

5-
import { EnvVar } from "../environment";
66
import { getRunnerLogger } from "../logging";
7-
import { getTestEnv, setupTests } from "../testing-utils";
7+
import { setupTests } from "../testing-utils";
88
import * as util from "../util";
99

10-
import {
11-
getCachedCodeQlVersion,
12-
getCommandCacheFilePath,
13-
} from "./output-cache";
10+
import { getCachedCodeQlVersion } from "./output-cache";
1411

1512
setupTests(test);
1613

@@ -20,19 +17,22 @@ test.serial(
2017
"getCachedCodeQlVersion reuses a version persisted by an earlier step",
2118
async (t) => {
2219
await util.withTmpDir(async (tmpDir: string) => {
23-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
24-
const cacheFile = getCommandCacheFilePath(env);
20+
const cacheFilePath = path.join(tmpDir, "cache.json");
21+
2522
fs.writeFileSync(
26-
cacheFile,
23+
cacheFilePath,
2724
JSON.stringify({
2825
cmd: "/path/to/codeql",
2926
entries: { version: { version: "2.20.0" } },
3027
}),
3128
"utf8",
3229
);
33-
t.deepEqual(getCachedCodeQlVersion(logger, env, "/path/to/codeql"), {
34-
version: "2.20.0",
35-
});
30+
t.deepEqual(
31+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
32+
{
33+
version: "2.20.0",
34+
},
35+
);
3636
});
3737
},
3838
);
@@ -41,17 +41,19 @@ test.serial(
4141
"getCachedCodeQlVersion ignores a persisted version from a different CLI",
4242
async (t) => {
4343
await util.withTmpDir(async (tmpDir: string) => {
44-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
45-
const cacheFile = getCommandCacheFilePath(env);
44+
const cacheFilePath = path.join(tmpDir, "cache.json");
4645
fs.writeFileSync(
47-
cacheFile,
46+
cacheFilePath,
4847
JSON.stringify({
4948
cmd: "/path/to/other-codeql",
5049
entries: { version: { version: "2.20.0" } },
5150
}),
5251
"utf8",
5352
);
54-
t.is(getCachedCodeQlVersion(logger, env, "/path/to/codeql"), undefined);
53+
t.is(
54+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
55+
undefined,
56+
);
5557
});
5658
},
5759
);
@@ -60,10 +62,12 @@ test.serial(
6062
"getCachedCodeQlVersion ignores a malformed persisted value",
6163
async (t) => {
6264
await util.withTmpDir(async (tmpDir: string) => {
63-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
64-
const cacheFile = getCommandCacheFilePath(env);
65-
fs.writeFileSync(cacheFile, "not valid json", "utf8");
66-
t.is(getCachedCodeQlVersion(logger, env, "/path/to/codeql"), undefined);
65+
const cacheFilePath = path.join(tmpDir, "cache.json");
66+
fs.writeFileSync(cacheFilePath, "not valid json", "utf8");
67+
t.is(
68+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
69+
undefined,
70+
);
6771
});
6872
},
6973
);
@@ -72,8 +76,7 @@ test.serial(
7276
"getCachedCodeQlVersion ignores a persisted value with the wrong structure",
7377
async (t) => {
7478
await util.withTmpDir(async (tmpDir: string) => {
75-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
76-
const cacheFile = getCommandCacheFilePath(env);
79+
const cacheFilePath = path.join(tmpDir, "cache.json");
7780
const testValues = [
7881
{ cmd: "/path/to/codeql" },
7982
{ entries: { version: { version: "2.20.0" } } },
@@ -96,9 +99,9 @@ test.serial(
9699
].map((v) => JSON.stringify(v));
97100

98101
for (const value of testValues) {
99-
fs.writeFileSync(cacheFile, value, "utf8");
102+
fs.writeFileSync(cacheFilePath, value, "utf8");
100103
t.is(
101-
getCachedCodeQlVersion(logger, env, "/path/to/codeql"),
104+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
102105
undefined,
103106
value,
104107
);
@@ -109,9 +112,12 @@ test.serial(
109112

110113
test.serial("getCachedCodeQlVersion ignores non-existent file", async (t) => {
111114
await util.withTmpDir(async (tmpDir: string) => {
112-
const env = getTestEnv({ [EnvVar.TEMP]: tmpDir });
115+
const cacheFilePath = path.join(tmpDir, "cache.json");
113116
t.notThrows(() => {
114-
t.is(getCachedCodeQlVersion(logger, env, "/path/to/codeql"), undefined);
117+
t.is(
118+
getCachedCodeQlVersion(logger, cacheFilePath, "/path/to/codeql"),
119+
undefined,
120+
);
115121
});
116122
});
117123
});

src/cli/output-cache.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export function getCommandCacheFilePath(env: Env): string {
5757

5858
/**
5959
* Caches the CodeQL CLI version both in-memory and on disk.
60-
* @param env The environment variables to use.
60+
* @param cacheFilePath The path to the cache file.
6161
* @param cmd The path to the CodeQL CLI.
6262
* @param version The version information to cache.
6363
*/
6464
export function cacheCodeQlVersion(
65-
env: Env,
65+
cacheFilePath: string,
6666
cmd: string,
6767
version: VersionInfo,
6868
): void {
@@ -78,22 +78,18 @@ export function cacheCodeQlVersion(
7878
// processes, can reuse it rather than invoking `codeql version` again. We
7979
// record the CLI path so that a different step using a different CodeQL bundle
8080
// doesn't pick up a stale version.
81-
fs.writeFileSync(
82-
getCommandCacheFilePath(env),
83-
JSON.stringify(outputCache),
84-
"utf8",
85-
);
81+
fs.writeFileSync(cacheFilePath, JSON.stringify(outputCache), "utf8");
8682
}
8783

8884
/**
8985
* Returns the cached CodeQL CLI version, if any.
9086
* @param logger The logger to use for logging messages.
91-
* @param env The environment variables to use.
87+
* @param cacheFilePath The path to the cache file.
9288
* @param cmd The path to the CodeQL CLI.
9389
*/
9490
export function getCachedCodeQlVersion(
9591
logger: Logger,
96-
env: Env,
92+
cacheFilePath: string,
9793
cmd?: string,
9894
): undefined | VersionInfo {
9995
if (cachedCodeQlVersion !== undefined) {
@@ -104,11 +100,9 @@ export function getCachedCodeQlVersion(
104100
// invokes `codeql version` instead.
105101
let serialized: string;
106102
try {
107-
serialized = fs.readFileSync(getCommandCacheFilePath(env), "utf8");
103+
serialized = fs.readFileSync(cacheFilePath, "utf8");
108104
} catch (e) {
109-
logger.debug(
110-
`Cannot read CLI-cache file ${getCommandCacheFilePath(env)}: ${e}`,
111-
);
105+
logger.debug(`Cannot read CLI-cache file ${cacheFilePath}: ${e}`);
112106
return undefined;
113107
}
114108
let persisted: unknown;

src/codeql.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,12 @@ async function getCodeQLForCmd(
491491
return cmd;
492492
},
493493
async getVersion() {
494-
let result = outputCache.getCachedCodeQlVersion(logger, getEnv(), cmd);
494+
const cacheFilePath = outputCache.getCommandCacheFilePath(getEnv());
495+
let result = outputCache.getCachedCodeQlVersion(
496+
logger,
497+
cacheFilePath,
498+
cmd,
499+
);
495500
if (result === undefined) {
496501
result = await runCliJson<VersionInfo>(
497502
cmd,
@@ -500,7 +505,7 @@ async function getCodeQLForCmd(
500505
noStreamStdout: true,
501506
},
502507
);
503-
outputCache.cacheCodeQlVersion(getEnv(), cmd, result);
508+
outputCache.cacheCodeQlVersion(cacheFilePath, cmd, result);
504509
}
505510
return result;
506511
},

src/status-report.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
isSelfHostedRunner,
1515
} from "./actions-util";
1616
import { getAnalysisKey, getApiClient } from "./api-client";
17-
import { getCachedCodeQlVersion } from "./cli/output-cache";
17+
import {
18+
getCachedCodeQlVersion,
19+
getCommandCacheFilePath,
20+
} from "./cli/output-cache";
1821
import type { Config } from "./config/action-config";
1922
import type { ComputedInput, InputName } from "./config/inputs";
2023
import { parseRegistriesWithoutCredentials } from "./config/pack-registries";
@@ -376,7 +379,10 @@ export async function createStatusReportBase(
376379
core.exportVariable(EnvVar.WORKFLOW_STARTED_AT, workflowStartedAt);
377380
}
378381
const runnerOs = getRequiredEnvParam("RUNNER_OS");
379-
const codeQlCliVersion = getCachedCodeQlVersion(logger, getEnv());
382+
const codeQlCliVersion = getCachedCodeQlVersion(
383+
logger,
384+
getCommandCacheFilePath(getEnv()),
385+
);
380386
const actionRef = process.env["GITHUB_ACTION_REF"] || "";
381387
const testingEnvironment = getTestingEnvironment();
382388
// re-export the testing environment variable so that it is available to subsequent steps,

0 commit comments

Comments
 (0)