Make createdDBForScannedLanguages test robust to new async calls

Previously the test depended on `createdDBForScannedLanguages` making no
async calls prior to `codeql resolve extractor`.
This commit is contained in:
Henry Mercer 2022-09-01 14:59:39 +01:00
parent cf5d465980
commit 6d34731d93
3 changed files with 33 additions and 15 deletions

21
lib/analyze.test.js generated
View file

@ -24,12 +24,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
const ava_1 = __importDefault(require("ava"));
const yaml = __importStar(require("js-yaml"));
const sinon = __importStar(require("sinon"));
const analyze_1 = require("./analyze");
const codeql_1 = require("./codeql");
const codeql_test_1 = require("./codeql.test");
const count = __importStar(require("./count-loc"));
const feature_flags_1 = require("./feature-flags");
const languages_1 = require("./languages");
@ -409,15 +409,22 @@ for (const options of [
},
]) {
(0, ava_1.default)(`createdDBForScannedLanguages() ${options.name}`, async (t) => {
const runnerConstructorStub = (0, codeql_test_1.stubToolRunnerConstructor)();
const runnerObjectStub = sinon.createStubInstance(toolrunner.ToolRunner);
runnerObjectStub.exec.resolves(0);
const runnerConstructorStub = sinon.stub(toolrunner, "ToolRunner");
runnerConstructorStub.callsFake((_toolPath, args, execOptions) => {
// Call listener on `codeql resolve extractor`
if (args[0] === "resolve" && args[1] === "extractor") {
const func = execOptions.listeners.stdout;
t.truthy(func, "stdout listener is defined");
func(Buffer.from('"/path/to/extractor"'));
}
return runnerObjectStub;
});
const codeqlObject = await (0, codeql_1.getCodeQLForTesting)("codeql/for-testing");
sinon.stub(codeqlObject, "getVersion").resolves(options.version);
const promise = (0, analyze_1.createdDBForScannedLanguages)(codeqlObject, stubConfig, false, // Disable Go extraction reconciliation
await (0, analyze_1.createdDBForScannedLanguages)(codeqlObject, stubConfig, false, // Disable Go extraction reconciliation
(0, logging_1.getRunnerLogger)(true), (0, feature_flags_1.createFeatureFlags)(options.featureFlags));
// call listener on `codeql resolve extractor`
const mockToolRunner = runnerConstructorStub.getCall(0);
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
await promise;
if (options.yesFlagSet)
t.true(runnerConstructorStub.secondCall.args[1].includes("--internal-use-lua-tracing"), "--internal-use-lua-tracing should be present, but it is absent");
else

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,8 @@
import * as fs from "fs";
import * as path from "path";
import { ExecOptions } from "@actions/exec";
import * as toolrunner from "@actions/exec/lib/toolrunner";
import test, { ExecutionContext } from "ava";
import * as yaml from "js-yaml";
import * as sinon from "sinon";
@ -13,7 +15,6 @@ import {
validateQueryFilters,
} from "./analyze";
import { setCodeQL, getCodeQLForTesting } from "./codeql";
import { stubToolRunnerConstructor } from "./codeql.test";
import { Config } from "./config-utils";
import * as count from "./count-loc";
import { createFeatureFlags, FeatureFlag } from "./feature-flags";
@ -487,21 +488,31 @@ for (const options of [
},
]) {
test(`createdDBForScannedLanguages() ${options.name}`, async (t) => {
const runnerConstructorStub = stubToolRunnerConstructor();
const runnerObjectStub = sinon.createStubInstance(toolrunner.ToolRunner);
runnerObjectStub.exec.resolves(0);
const runnerConstructorStub = sinon.stub(toolrunner, "ToolRunner");
runnerConstructorStub.callsFake(
(_toolPath, args, execOptions: ExecOptions) => {
// Call listener on `codeql resolve extractor`
if (args[0] === "resolve" && args[1] === "extractor") {
const func = execOptions.listeners!.stdout as (data: Buffer) => void;
t.truthy(func, "stdout listener is defined");
func(Buffer.from('"/path/to/extractor"'));
}
return runnerObjectStub;
}
);
const codeqlObject = await getCodeQLForTesting("codeql/for-testing");
sinon.stub(codeqlObject, "getVersion").resolves(options.version);
const promise = createdDBForScannedLanguages(
await createdDBForScannedLanguages(
codeqlObject,
stubConfig,
false, // Disable Go extraction reconciliation
getRunnerLogger(true),
createFeatureFlags(options.featureFlags)
);
// call listener on `codeql resolve extractor`
const mockToolRunner = runnerConstructorStub.getCall(0);
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
await promise;
if (options.yesFlagSet)
t.true(
runnerConstructorStub.secondCall.args[1].includes(