Implement unit tests for reading the Lua tracer FF in analyze-action.
This commit is contained in:
parent
f422a50448
commit
ab7316e0c5
12 changed files with 231 additions and 19 deletions
8
lib/analyze.js
generated
8
lib/analyze.js
generated
|
|
@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.runCleanup = exports.runFinalize = exports.runQueries = exports.CodeQLAnalysisError = void 0;
|
||||
exports.runCleanup = exports.runFinalize = exports.runQueries = exports.createdDBForScannedLanguages = exports.CodeQLAnalysisError = void 0;
|
||||
const fs = __importStar(require("fs"));
|
||||
const path = __importStar(require("path"));
|
||||
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
|
||||
|
|
@ -68,11 +68,10 @@ async function setupPythonExtractor(logger) {
|
|||
logger.info(`Setting LGTM_PYTHON_SETUP_VERSION=${output}`);
|
||||
process.env["LGTM_PYTHON_SETUP_VERSION"] = output;
|
||||
}
|
||||
async function createdDBForScannedLanguages(config, logger, featureFlags) {
|
||||
async function createdDBForScannedLanguages(codeql, config, logger, featureFlags) {
|
||||
// Insert the LGTM_INDEX_X env vars at this point so they are set when
|
||||
// we extract any scanned languages.
|
||||
analysisPaths.includeAndExcludeAnalysisPaths(config);
|
||||
const codeql = await (0, codeql_1.getCodeQL)(config.codeQLCmd);
|
||||
for (const language of config.languages) {
|
||||
if ((0, languages_1.isScannedLanguage)(language) &&
|
||||
!dbIsFinalized(config, language, logger)) {
|
||||
|
|
@ -85,6 +84,7 @@ async function createdDBForScannedLanguages(config, logger, featureFlags) {
|
|||
}
|
||||
}
|
||||
}
|
||||
exports.createdDBForScannedLanguages = createdDBForScannedLanguages;
|
||||
function dbIsFinalized(config, language, logger) {
|
||||
const dbPath = util.getCodeQLDatabasePath(config, language);
|
||||
try {
|
||||
|
|
@ -97,8 +97,8 @@ function dbIsFinalized(config, language, logger) {
|
|||
}
|
||||
}
|
||||
async function finalizeDatabaseCreation(config, threadsFlag, memoryFlag, logger, featureFlags) {
|
||||
await createdDBForScannedLanguages(config, logger, featureFlags);
|
||||
const codeql = await (0, codeql_1.getCodeQL)(config.codeQLCmd);
|
||||
await createdDBForScannedLanguages(codeql, config, logger, featureFlags);
|
||||
for (const language of config.languages) {
|
||||
if (dbIsFinalized(config, language, logger)) {
|
||||
logger.info(`There is already a finalized database for ${language} at the location where the CodeQL Action places databases, so we did not create one.`);
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
69
lib/analyze.test.js
generated
69
lib/analyze.test.js
generated
|
|
@ -29,7 +29,9 @@ 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");
|
||||
const logging_1 = require("./logging");
|
||||
const testing_utils_1 = require("./testing-utils");
|
||||
|
|
@ -210,4 +212,71 @@ const util = __importStar(require("./util"));
|
|||
}
|
||||
}
|
||||
});
|
||||
const stubConfig = {
|
||||
languages: [languages_1.Language.cpp, languages_1.Language.go],
|
||||
queries: {},
|
||||
pathsIgnore: [],
|
||||
paths: [],
|
||||
originalUserInput: {},
|
||||
tempDir: "",
|
||||
toolCacheDir: "",
|
||||
codeQLCmd: "",
|
||||
gitHubVersion: {
|
||||
type: util.GitHubVariant.DOTCOM,
|
||||
},
|
||||
dbLocation: "",
|
||||
packs: {},
|
||||
debugMode: false,
|
||||
debugArtifactName: util.DEFAULT_DEBUG_ARTIFACT_NAME,
|
||||
debugDatabaseName: util.DEFAULT_DEBUG_DATABASE_NAME,
|
||||
injectedMlQueries: false,
|
||||
};
|
||||
(0, ava_1.default)("createdDBForScannedLanguages() Lua feature flag enabled, but old CLI", async (t) => {
|
||||
const runnerConstructorStub = (0, codeql_test_1.stubToolRunnerConstructor)();
|
||||
const codeqlObject = await (0, codeql_1.getCodeQLForTesting)("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.9.0");
|
||||
const promise = (0, analyze_1.createdDBForScannedLanguages)(codeqlObject, stubConfig, (0, logging_1.getRunnerLogger)(true), (0, feature_flags_1.createFeatureFlags)([feature_flags_1.FeatureFlag.LuaTracerConfigEnabled]));
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.false(runnerConstructorStub.secondCall.args[1].includes("--internal-use-lua-tracing"), "--internal-use-lua-tracing should be absent, but it is present");
|
||||
t.false(runnerConstructorStub.secondCall.args[1].includes("--no-internal-use-lua-tracing"), "--no-internal-use-lua-tracing should be absent, but it is present");
|
||||
});
|
||||
(0, ava_1.default)("createdDBForScannedLanguages() Lua feature flag disabled, with old CLI", async (t) => {
|
||||
const runnerConstructorStub = (0, codeql_test_1.stubToolRunnerConstructor)();
|
||||
const codeqlObject = await (0, codeql_1.getCodeQLForTesting)("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.9.0");
|
||||
const promise = (0, analyze_1.createdDBForScannedLanguages)(codeqlObject, stubConfig, (0, logging_1.getRunnerLogger)(true), (0, feature_flags_1.createFeatureFlags)([feature_flags_1.FeatureFlag.LuaTracerConfigEnabled]));
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.false(runnerConstructorStub.secondCall.args[1].includes("--internal-use-lua-tracing"), "--internal-use-lua-tracing should be absent, but it is present");
|
||||
t.false(runnerConstructorStub.secondCall.args[1].includes("--no-internal-use-lua-tracing"), "--no-internal-use-lua-tracing should be absent, but it is present");
|
||||
});
|
||||
(0, ava_1.default)("createdDBForScannedLanguages() Lua feature flag enabled, with new CLI", async (t) => {
|
||||
const runnerConstructorStub = (0, codeql_test_1.stubToolRunnerConstructor)();
|
||||
const codeqlObject = await (0, codeql_1.getCodeQLForTesting)("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.10.0");
|
||||
const promise = (0, analyze_1.createdDBForScannedLanguages)(codeqlObject, stubConfig, (0, logging_1.getRunnerLogger)(true), (0, feature_flags_1.createFeatureFlags)([feature_flags_1.FeatureFlag.LuaTracerConfigEnabled]));
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.true(runnerConstructorStub.secondCall.args[1].includes("--internal-use-lua-tracing"), "--internal-use-lua-tracing should be present, but is not");
|
||||
t.false(runnerConstructorStub.secondCall.args[1].includes("--no-internal-use-lua-tracing"), "--no-internal-use-lua-tracing should be absent, but it is present");
|
||||
});
|
||||
(0, ava_1.default)("createdDBForScannedLanguages() Lua feature flag disabled, with new CLI", async (t) => {
|
||||
const runnerConstructorStub = (0, codeql_test_1.stubToolRunnerConstructor)();
|
||||
const codeqlObject = await (0, codeql_1.getCodeQLForTesting)("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.10.0");
|
||||
const promise = (0, analyze_1.createdDBForScannedLanguages)(codeqlObject, stubConfig, (0, logging_1.getRunnerLogger)(true), (0, feature_flags_1.createFeatureFlags)([]));
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.false(runnerConstructorStub.secondCall.args[1].includes("--internal-use-lua-tracing"), "--internal-use-lua-tracing should be absent, but is present");
|
||||
t.true(runnerConstructorStub.secondCall.args[1].includes("--no-internal-use-lua-tracing"), "--no-internal-use-lua-tracing should be present, but is absent");
|
||||
});
|
||||
//# sourceMappingURL=analyze.test.js.map
|
||||
File diff suppressed because one or more lines are too long
4
lib/codeql.js
generated
4
lib/codeql.js
generated
|
|
@ -384,8 +384,8 @@ exports.getCachedCodeQL = getCachedCodeQL;
|
|||
* a non-existent placeholder codeql command, so tests that use this function
|
||||
* should also stub the toolrunner.ToolRunner constructor.
|
||||
*/
|
||||
async function getCodeQLForTesting() {
|
||||
return getCodeQLForCmd("codeql-for-testing", false);
|
||||
async function getCodeQLForTesting(cmd = "codeql-for-testing") {
|
||||
return getCodeQLForCmd(cmd, false);
|
||||
}
|
||||
exports.getCodeQLForTesting = getCodeQLForTesting;
|
||||
/**
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
2
lib/codeql.test.js
generated
2
lib/codeql.test.js
generated
|
|
@ -22,6 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.stubToolRunnerConstructor = void 0;
|
||||
const path = __importStar(require("path"));
|
||||
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
|
||||
const toolcache = __importStar(require("@actions/tool-cache"));
|
||||
|
|
@ -291,4 +292,5 @@ function stubToolRunnerConstructor() {
|
|||
runnerConstructorStub.returns(runnerObjectStub);
|
||||
return runnerConstructorStub;
|
||||
}
|
||||
exports.stubToolRunnerConstructor = stubToolRunnerConstructor;
|
||||
//# sourceMappingURL=codeql.test.js.map
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -5,10 +5,12 @@ import test from "ava";
|
|||
import * as yaml from "js-yaml";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import { runQueries } from "./analyze";
|
||||
import { setCodeQL } from "./codeql";
|
||||
import { runQueries, createdDBForScannedLanguages } 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";
|
||||
import { Language } from "./languages";
|
||||
import { getRunnerLogger } from "./logging";
|
||||
import { setupTests, setupActionsVars } from "./testing-utils";
|
||||
|
|
@ -249,3 +251,139 @@ test("status report fields and search path setting", async (t) => {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
const stubConfig: Config = {
|
||||
languages: [Language.cpp, Language.go],
|
||||
queries: {},
|
||||
pathsIgnore: [],
|
||||
paths: [],
|
||||
originalUserInput: {},
|
||||
tempDir: "",
|
||||
toolCacheDir: "",
|
||||
codeQLCmd: "",
|
||||
gitHubVersion: {
|
||||
type: util.GitHubVariant.DOTCOM,
|
||||
} as util.GitHubVersion,
|
||||
dbLocation: "",
|
||||
packs: {},
|
||||
debugMode: false,
|
||||
debugArtifactName: util.DEFAULT_DEBUG_ARTIFACT_NAME,
|
||||
debugDatabaseName: util.DEFAULT_DEBUG_DATABASE_NAME,
|
||||
injectedMlQueries: false,
|
||||
};
|
||||
|
||||
test("createdDBForScannedLanguages() Lua feature flag enabled, but old CLI", async (t) => {
|
||||
const runnerConstructorStub = stubToolRunnerConstructor();
|
||||
const codeqlObject = await getCodeQLForTesting("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.9.0");
|
||||
|
||||
const promise = createdDBForScannedLanguages(
|
||||
codeqlObject,
|
||||
stubConfig,
|
||||
getRunnerLogger(true),
|
||||
createFeatureFlags([FeatureFlag.LuaTracerConfigEnabled])
|
||||
);
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.false(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--internal-use-lua-tracing"
|
||||
),
|
||||
"--internal-use-lua-tracing should be absent, but it is present"
|
||||
);
|
||||
t.false(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--no-internal-use-lua-tracing"
|
||||
),
|
||||
"--no-internal-use-lua-tracing should be absent, but it is present"
|
||||
);
|
||||
});
|
||||
|
||||
test("createdDBForScannedLanguages() Lua feature flag disabled, with old CLI", async (t) => {
|
||||
const runnerConstructorStub = stubToolRunnerConstructor();
|
||||
const codeqlObject = await getCodeQLForTesting("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.9.0");
|
||||
|
||||
const promise = createdDBForScannedLanguages(
|
||||
codeqlObject,
|
||||
stubConfig,
|
||||
getRunnerLogger(true),
|
||||
createFeatureFlags([FeatureFlag.LuaTracerConfigEnabled])
|
||||
);
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.false(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--internal-use-lua-tracing"
|
||||
),
|
||||
"--internal-use-lua-tracing should be absent, but it is present"
|
||||
);
|
||||
t.false(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--no-internal-use-lua-tracing"
|
||||
),
|
||||
"--no-internal-use-lua-tracing should be absent, but it is present"
|
||||
);
|
||||
});
|
||||
|
||||
test("createdDBForScannedLanguages() Lua feature flag enabled, with new CLI", async (t) => {
|
||||
const runnerConstructorStub = stubToolRunnerConstructor();
|
||||
const codeqlObject = await getCodeQLForTesting("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.10.0");
|
||||
|
||||
const promise = createdDBForScannedLanguages(
|
||||
codeqlObject,
|
||||
stubConfig,
|
||||
getRunnerLogger(true),
|
||||
createFeatureFlags([FeatureFlag.LuaTracerConfigEnabled])
|
||||
);
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.true(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--internal-use-lua-tracing"
|
||||
),
|
||||
"--internal-use-lua-tracing should be present, but is not"
|
||||
);
|
||||
t.false(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--no-internal-use-lua-tracing"
|
||||
),
|
||||
"--no-internal-use-lua-tracing should be absent, but it is present"
|
||||
);
|
||||
});
|
||||
|
||||
test("createdDBForScannedLanguages() Lua feature flag disabled, with new CLI", async (t) => {
|
||||
const runnerConstructorStub = stubToolRunnerConstructor();
|
||||
const codeqlObject = await getCodeQLForTesting("codeql/for-testing");
|
||||
sinon.stub(codeqlObject, "getVersion").resolves("2.10.0");
|
||||
|
||||
const promise = createdDBForScannedLanguages(
|
||||
codeqlObject,
|
||||
stubConfig,
|
||||
getRunnerLogger(true),
|
||||
createFeatureFlags([])
|
||||
);
|
||||
// call listener on `codeql resolve extractor`
|
||||
const mockToolRunner = runnerConstructorStub.getCall(0);
|
||||
mockToolRunner.args[2].listeners.stdout('"/path/to/extractor"');
|
||||
await promise;
|
||||
t.false(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--internal-use-lua-tracing"
|
||||
),
|
||||
"--internal-use-lua-tracing should be absent, but is present"
|
||||
);
|
||||
t.true(
|
||||
runnerConstructorStub.secondCall.args[1].includes(
|
||||
"--no-internal-use-lua-tracing"
|
||||
),
|
||||
"--no-internal-use-lua-tracing should be present, but is absent"
|
||||
);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import * as yaml from "js-yaml";
|
|||
|
||||
import * as analysisPaths from "./analysis-paths";
|
||||
import {
|
||||
CodeQL,
|
||||
CODEQL_VERSION_COUNTS_LINES,
|
||||
CODEQL_VERSION_NEW_TRACING,
|
||||
getCodeQL,
|
||||
|
|
@ -115,7 +116,8 @@ async function setupPythonExtractor(logger: Logger) {
|
|||
process.env["LGTM_PYTHON_SETUP_VERSION"] = output;
|
||||
}
|
||||
|
||||
async function createdDBForScannedLanguages(
|
||||
export async function createdDBForScannedLanguages(
|
||||
codeql: CodeQL,
|
||||
config: configUtils.Config,
|
||||
logger: Logger,
|
||||
featureFlags: FeatureFlags
|
||||
|
|
@ -124,7 +126,6 @@ async function createdDBForScannedLanguages(
|
|||
// we extract any scanned languages.
|
||||
analysisPaths.includeAndExcludeAnalysisPaths(config);
|
||||
|
||||
const codeql = await getCodeQL(config.codeQLCmd);
|
||||
for (const language of config.languages) {
|
||||
if (
|
||||
isScannedLanguage(language) &&
|
||||
|
|
@ -172,9 +173,9 @@ async function finalizeDatabaseCreation(
|
|||
logger: Logger,
|
||||
featureFlags: FeatureFlags
|
||||
) {
|
||||
await createdDBForScannedLanguages(config, logger, featureFlags);
|
||||
|
||||
const codeql = await getCodeQL(config.codeQLCmd);
|
||||
await createdDBForScannedLanguages(codeql, config, logger, featureFlags);
|
||||
|
||||
for (const language of config.languages) {
|
||||
if (dbIsFinalized(config, language, logger)) {
|
||||
logger.info(
|
||||
|
|
|
|||
|
|
@ -540,7 +540,7 @@ test("databaseInitCluster() Lua feature flag disabled, compatible CLI", async (t
|
|||
);
|
||||
});
|
||||
|
||||
function stubToolRunnerConstructor(): sinon.SinonStub<
|
||||
export function stubToolRunnerConstructor(): sinon.SinonStub<
|
||||
any[],
|
||||
toolrunner.ToolRunner
|
||||
> {
|
||||
|
|
|
|||
|
|
@ -634,8 +634,10 @@ export function getCachedCodeQL(): CodeQL {
|
|||
* a non-existent placeholder codeql command, so tests that use this function
|
||||
* should also stub the toolrunner.ToolRunner constructor.
|
||||
*/
|
||||
export async function getCodeQLForTesting(): Promise<CodeQL> {
|
||||
return getCodeQLForCmd("codeql-for-testing", false);
|
||||
export async function getCodeQLForTesting(
|
||||
cmd = "codeql-for-testing"
|
||||
): Promise<CodeQL> {
|
||||
return getCodeQLForCmd(cmd, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue