Skip to content

Commit 924990a

Browse files
committed
Move getCodeQLForTesting to testing-utils.ts
1 parent 4a2ca5c commit 924990a

6 files changed

Lines changed: 32 additions & 22 deletions

File tree

‎src/codeql.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
makeVersionInfo,
3535
createTestConfig,
3636
makeMacro,
37+
getCodeQLForTesting,
3738
} from "./testing-utils";
3839
import { ToolsDownloadStatusReport } from "./tools-download";
3940
import * as util from "./util";
@@ -140,7 +141,7 @@ function mockApiDetails(apiDetails: GitHubApiDetails) {
140141
}
141142

142143
async function stubCodeql(): Promise<codeql.CodeQL> {
143-
const codeqlObject = await codeql.getCodeQLForTesting();
144+
const codeqlObject = await getCodeQLForTesting();
144145
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6"));
145146
sinon
146147
.stub(codeqlObject, "isTracedLanguage")
@@ -1039,7 +1040,7 @@ test.serial("runTool summarizes autobuilder errors", async (t) => {
10391040
[2019-09-18 12:00:01] [autobuild] A non-error message
10401041
`;
10411042
stubToolRunnerConstructor(1, stderr);
1042-
const codeqlObject = await codeql.getCodeQLForTesting();
1043+
const codeqlObject = await getCodeQLForTesting();
10431044
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6"));
10441045
sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor");
10451046
// io throws because of the test CodeQL object.
@@ -1095,7 +1096,7 @@ test.serial("runTool recognizes fatal internal errors", async (t) => {
10951096
com.semmle.util.exception.CatastrophicError: An error occurred while evaluating ControlFlowGraph::ControlFlow::Root.isRootOf/1#dispred#f610e6ed/2@86282cc8
10961097
Severe disk cache trouble (corruption or out of space) at /home/runner/work/_temp/codeql_databases/go/db-go/default/cache/pages/28/33.pack: Failed to write item to disk`;
10971098
stubToolRunnerConstructor(1, stderr);
1098-
const codeqlObject = await codeql.getCodeQLForTesting();
1099+
const codeqlObject = await getCodeQLForTesting();
10991100
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6"));
11001101
sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor");
11011102
// io throws because of the test CodeQL object.

‎src/codeql.ts‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from "./feature-flags";
2727
import { isAnalyzingDefaultBranch } from "./git-utils";
2828
import { Language } from "./languages";
29-
import { getRunnerLogger, Logger } from "./logging";
29+
import type { Logger } from "./logging";
3030
import { writeBaseDatabaseOidsFile, writeOverlayChangesFile } from "./overlay";
3131
import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
3232
import * as setupCodeql from "./setup-codeql";
@@ -486,18 +486,6 @@ export function createStubCodeQL(partialCodeql: Partial<CodeQL>): CodeQL {
486486
};
487487
}
488488

489-
/**
490-
* Get a real, newly created CodeQL instance for testing. The instance refers to
491-
* a non-existent placeholder codeql command, so tests that use this function
492-
* should also stub the toolrunner.ToolRunner constructor.
493-
*/
494-
export async function getCodeQLForTesting(
495-
cmd = "codeql-for-testing",
496-
logger: Logger = getRunnerLogger(true),
497-
): Promise<CodeQL> {
498-
return getCodeQLForCmd(logger, cmd, false);
499-
}
500-
501489
/**
502490
* Return a CodeQL object for CodeQL CLI access.
503491
*
@@ -506,7 +494,7 @@ export async function getCodeQLForTesting(
506494
* version requirement. Must be set to true outside tests.
507495
* @returns A new CodeQL object
508496
*/
509-
async function getCodeQLForCmd(
497+
export async function getCodeQLForCmd(
510498
logger: Logger,
511499
cmd: string,
512500
checkVersion: boolean,

‎src/init-action-post-helper.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
createFeatures,
2121
createTestConfig,
2222
DEFAULT_ACTIONS_VARS,
23+
getCodeQLForTesting,
2324
getTestEnv,
2425
makeMacro,
2526
makeVersionInfo,
@@ -737,7 +738,7 @@ async function testFailedSarifUpload(
737738
.withArgs("matrix")
738739
.returns(JSON.stringify(matrix));
739740

740-
const codeqlObject = await codeql.getCodeQLForTesting();
741+
const codeqlObject = await getCodeQLForTesting();
741742
sinon.stub(codeql, "getCodeQL").resolves(codeqlObject);
742743
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6"));
743744
const databaseExportDiagnosticsStub = sinon.stub(
@@ -832,7 +833,7 @@ async function mockRiskAssessmentEnv(matrix: string) {
832833
.stub(apiClient, "getGitHubVersion")
833834
.resolves({ type: util.GitHubVariant.GHES, version: "3.0.0" });
834835

835-
const codeqlObject = await codeql.getCodeQLForTesting();
836+
const codeqlObject = await getCodeQLForTesting();
836837
const databaseExportDiagnostics = sinon
837838
.stub(codeqlObject, "databaseExportDiagnostics")
838839
.resolves();

‎src/testing-utils.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ import {
3030
featureConfig,
3131
FeatureEnablement,
3232
} from "./feature-flags";
33-
import { joinMessageStrings, Loggable, LoggableError, Logger } from "./logging";
33+
import {
34+
getRunnerLogger,
35+
joinMessageStrings,
36+
Loggable,
37+
LoggableError,
38+
Logger,
39+
} from "./logging";
3440
import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
3541
import { getBundlePlatform } from "./platform";
3642
import { ActionName } from "./status-report";
@@ -1000,3 +1006,15 @@ export function makeTestToken(length: number = 36) {
10001006
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
10011007
return chars.repeat(Math.ceil(length / chars.length)).slice(0, length);
10021008
}
1009+
1010+
/**
1011+
* Get a real, newly created CodeQL instance for testing. The instance refers to
1012+
* a non-existent placeholder codeql command, so tests that use this function
1013+
* should also stub the toolrunner.ToolRunner constructor.
1014+
*/
1015+
export async function getCodeQLForTesting(
1016+
cmd = "codeql-for-testing",
1017+
logger: Logger = getRunnerLogger(true),
1018+
): Promise<codeql.CodeQL> {
1019+
return codeql.getCodeQLForCmd(logger, cmd, false);
1020+
}

‎src/tracer-config.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import * as path from "path";
44
import test from "ava";
55
import * as sinon from "sinon";
66

7-
import { CodeQL, getCodeQLForTesting } from "./codeql";
7+
import type { CodeQL } from "./codeql";
88
import * as configUtils from "./config-utils";
99
import { BuiltInLanguage } from "./languages";
1010
import {
1111
createTestConfig,
12+
getCodeQLForTesting,
1213
makeVersionInfo,
1314
RecordingLogger,
1415
setupTests,

‎src/workflow.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import * as yaml from "js-yaml";
33
import * as sinon from "sinon";
44

55
import * as actionsUtil from "./actions-util";
6-
import { createStubCodeQL, getCodeQLForTesting } from "./codeql";
6+
import { createStubCodeQL } from "./codeql";
77
import { EnvVar } from "./environment";
88
import {
99
checkExpectedLogMessages,
10+
getCodeQLForTesting,
1011
getRecordingLogger,
1112
LoggedMessage,
1213
setupTests,

0 commit comments

Comments
 (0)