2020-09-10 20:02:33 +03:00
|
|
|
"use strict";
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
2020-09-29 16:43:37 +03:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
2020-09-10 20:02:33 +03:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const fs = __importStar(require("fs"));
|
2021-04-17 00:52:39 +03:00
|
|
|
const path = __importStar(require("path"));
|
2020-09-29 16:43:37 +03:00
|
|
|
const ava_1 = __importDefault(require("ava"));
|
2021-06-03 19:32:44 +03:00
|
|
|
const yaml = __importStar(require("js-yaml"));
|
|
|
|
const semver_1 = require("semver");
|
2021-04-17 00:52:39 +03:00
|
|
|
const sinon_1 = __importDefault(require("sinon"));
|
2020-09-29 16:43:37 +03:00
|
|
|
const analyze_1 = require("./analyze");
|
|
|
|
const codeql_1 = require("./codeql");
|
2021-05-13 20:56:27 +03:00
|
|
|
const count_loc_1 = require("./count-loc");
|
2021-04-17 00:52:39 +03:00
|
|
|
const count = __importStar(require("./count-loc"));
|
2020-10-01 13:03:30 +03:00
|
|
|
const languages_1 = require("./languages");
|
|
|
|
const logging_1 = require("./logging");
|
2020-09-10 20:02:33 +03:00
|
|
|
const testing_utils_1 = require("./testing-utils");
|
2020-10-01 13:03:30 +03:00
|
|
|
const util = __importStar(require("./util"));
|
2020-09-10 20:02:33 +03:00
|
|
|
testing_utils_1.setupTests(ava_1.default);
|
|
|
|
// Checks that the duration fields are populated for the correct language
|
2021-04-01 14:38:13 +03:00
|
|
|
// and correct case of builtin or custom. Also checks the correct search
|
|
|
|
// paths are set in the database analyze invocation.
|
|
|
|
ava_1.default("status report fields and search path setting", async (t) => {
|
2021-05-13 20:56:27 +03:00
|
|
|
const mockLinesOfCode = Object.values(languages_1.Language).reduce((obj, lang, i) => {
|
2021-04-29 00:57:44 +03:00
|
|
|
// use a different line count for each language
|
2021-04-17 00:52:39 +03:00
|
|
|
obj[lang] = i + 1;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
sinon_1.default.stub(count, "countLoc").resolves(mockLinesOfCode);
|
2021-04-01 14:38:13 +03:00
|
|
|
let searchPathsUsed = [];
|
2020-09-10 20:02:33 +03:00
|
|
|
return await util.withTmpDir(async (tmpDir) => {
|
2021-04-22 17:04:59 +03:00
|
|
|
testing_utils_1.setupActionsVars(tmpDir, tmpDir);
|
2020-09-18 11:52:44 +03:00
|
|
|
const memoryFlag = "";
|
|
|
|
const addSnippetsFlag = "";
|
|
|
|
const threadsFlag = "";
|
2021-06-03 19:32:44 +03:00
|
|
|
const packs = {
|
|
|
|
[languages_1.Language.cpp]: [
|
|
|
|
{
|
|
|
|
packName: "a/b",
|
2021-06-04 23:34:55 +03:00
|
|
|
version: semver_1.clean("1.0.0"),
|
2021-06-03 19:32:44 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
[languages_1.Language.java]: [
|
|
|
|
{
|
|
|
|
packName: "c/d",
|
2021-06-04 23:34:55 +03:00
|
|
|
version: semver_1.clean("2.0.0"),
|
2021-06-03 19:32:44 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2020-09-10 20:02:33 +03:00
|
|
|
for (const language of Object.values(languages_1.Language)) {
|
2021-04-17 00:52:39 +03:00
|
|
|
codeql_1.setCodeQL({
|
2021-06-08 02:05:32 +03:00
|
|
|
packDownload: async () => ({ packs: [] }),
|
2021-06-04 16:28:55 +03:00
|
|
|
databaseRunQueries: async (_db, searchPath) => {
|
|
|
|
searchPathsUsed.push(searchPath);
|
|
|
|
},
|
|
|
|
databaseInterpretResults: async (_db, _queriesRun, sarifFile) => {
|
2021-04-17 00:52:39 +03:00
|
|
|
fs.writeFileSync(sarifFile, JSON.stringify({
|
|
|
|
runs: [
|
2021-05-04 19:38:38 +03:00
|
|
|
// variant 1 uses ruleId
|
2021-04-17 00:52:39 +03:00
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
metricResults: [
|
|
|
|
{
|
2021-05-13 20:56:27 +03:00
|
|
|
ruleId: `${count_loc_1.getIdPrefix(language)}/summary/lines-of-code`,
|
2021-04-17 00:52:39 +03:00
|
|
|
value: 123,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2021-05-04 19:38:38 +03:00
|
|
|
// variant 2 uses rule.id
|
2021-04-17 00:52:39 +03:00
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
metricResults: [
|
|
|
|
{
|
2021-05-04 19:38:38 +03:00
|
|
|
rule: {
|
2021-05-13 20:56:27 +03:00
|
|
|
id: `${count_loc_1.getIdPrefix(language)}/summary/lines-of-code`,
|
2021-04-17 00:52:39 +03:00
|
|
|
},
|
|
|
|
value: 123,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2021-07-21 12:27:42 +03:00
|
|
|
// variant 3 references a rule with the lines-of-code tag
|
|
|
|
{
|
|
|
|
tool: {
|
|
|
|
extensions: [
|
|
|
|
{
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
tags: ["lines-of-code"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
properties: {
|
|
|
|
metricResults: [
|
|
|
|
{
|
|
|
|
rule: {
|
|
|
|
index: 0,
|
|
|
|
toolComponent: {
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
value: 123,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2021-04-17 00:52:39 +03:00
|
|
|
{},
|
|
|
|
],
|
|
|
|
}));
|
2021-05-20 02:01:06 +03:00
|
|
|
return "";
|
2021-04-17 00:52:39 +03:00
|
|
|
},
|
|
|
|
});
|
2021-04-01 14:38:13 +03:00
|
|
|
searchPathsUsed = [];
|
2020-09-10 20:02:33 +03:00
|
|
|
const config = {
|
|
|
|
languages: [language],
|
|
|
|
queries: {},
|
|
|
|
pathsIgnore: [],
|
|
|
|
paths: [],
|
|
|
|
originalUserInput: {},
|
|
|
|
tempDir: tmpDir,
|
|
|
|
toolCacheDir: tmpDir,
|
2020-09-18 11:52:44 +03:00
|
|
|
codeQLCmd: "",
|
2021-02-15 12:29:10 +03:00
|
|
|
gitHubVersion: {
|
|
|
|
type: util.GitHubVariant.DOTCOM,
|
|
|
|
},
|
2021-05-17 12:35:09 +03:00
|
|
|
dbLocation: path.resolve(tmpDir, "codeql_databases"),
|
2021-06-03 19:32:44 +03:00
|
|
|
packs,
|
2020-09-10 20:02:33 +03:00
|
|
|
};
|
2021-05-17 12:35:09 +03:00
|
|
|
fs.mkdirSync(util.getCodeQLDatabasePath(config, language), {
|
2020-09-18 11:52:44 +03:00
|
|
|
recursive: true,
|
|
|
|
});
|
2020-09-10 20:02:33 +03:00
|
|
|
config.queries[language] = {
|
2020-09-18 11:52:44 +03:00
|
|
|
builtin: ["foo.ql"],
|
2020-09-10 20:02:33 +03:00
|
|
|
custom: [],
|
|
|
|
};
|
2021-05-03 20:41:53 +03:00
|
|
|
const builtinStatusReport = await analyze_1.runQueries(tmpDir, memoryFlag, addSnippetsFlag, threadsFlag, undefined, config, logging_1.getRunnerLogger(true));
|
2021-06-03 19:32:44 +03:00
|
|
|
const hasPacks = language in packs;
|
2021-06-09 23:19:49 +03:00
|
|
|
const statusReportKeys = Object.keys(builtinStatusReport).sort();
|
|
|
|
if (hasPacks) {
|
|
|
|
t.deepEqual(statusReportKeys.length, 3, statusReportKeys.toString());
|
|
|
|
t.deepEqual(statusReportKeys[0], `analyze_builtin_queries_${language}_duration_ms`);
|
|
|
|
t.deepEqual(statusReportKeys[1], `analyze_custom_queries_${language}_duration_ms`);
|
|
|
|
t.deepEqual(statusReportKeys[2], `interpret_results_${language}_duration_ms`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t.deepEqual(statusReportKeys[0], `analyze_builtin_queries_${language}_duration_ms`);
|
|
|
|
t.deepEqual(statusReportKeys[1], `interpret_results_${language}_duration_ms`);
|
|
|
|
}
|
2020-09-10 20:02:33 +03:00
|
|
|
config.queries[language] = {
|
|
|
|
builtin: [],
|
2021-04-01 14:38:13 +03:00
|
|
|
custom: [
|
|
|
|
{
|
|
|
|
queries: ["foo.ql"],
|
|
|
|
searchPath: "/1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
queries: ["bar.ql"],
|
|
|
|
searchPath: "/2",
|
|
|
|
},
|
|
|
|
],
|
2020-09-10 20:02:33 +03:00
|
|
|
};
|
2021-05-03 20:41:53 +03:00
|
|
|
const customStatusReport = await analyze_1.runQueries(tmpDir, memoryFlag, addSnippetsFlag, threadsFlag, undefined, config, logging_1.getRunnerLogger(true));
|
2021-06-04 16:28:55 +03:00
|
|
|
t.deepEqual(Object.keys(customStatusReport).length, 2);
|
2020-09-10 20:02:33 +03:00
|
|
|
t.true(`analyze_custom_queries_${language}_duration_ms` in customStatusReport);
|
2021-06-03 19:32:44 +03:00
|
|
|
const expectedSearchPathsUsed = hasPacks
|
|
|
|
? [undefined, undefined, "/1", "/2", undefined]
|
|
|
|
: [undefined, "/1", "/2"];
|
|
|
|
t.deepEqual(searchPathsUsed, expectedSearchPathsUsed);
|
2021-06-04 16:28:55 +03:00
|
|
|
t.true(`interpret_results_${language}_duration_ms` in customStatusReport);
|
2020-09-10 20:02:33 +03:00
|
|
|
}
|
2021-04-17 00:52:39 +03:00
|
|
|
verifyLineCounts(tmpDir);
|
2021-06-03 19:32:44 +03:00
|
|
|
verifyQuerySuites(tmpDir);
|
2020-09-10 20:02:33 +03:00
|
|
|
});
|
2021-04-17 00:52:39 +03:00
|
|
|
function verifyLineCounts(tmpDir) {
|
|
|
|
// eslint-disable-next-line github/array-foreach
|
|
|
|
Object.keys(languages_1.Language).forEach((lang, i) => {
|
2021-06-04 16:28:55 +03:00
|
|
|
verifyLineCountForFile(lang, path.join(tmpDir, `${lang}.sarif`), i + 1);
|
2021-04-17 00:52:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function verifyLineCountForFile(lang, filePath, lineCount) {
|
2021-05-13 20:56:27 +03:00
|
|
|
const idPrefix = count_loc_1.getIdPrefix(lang);
|
2021-04-17 00:52:39 +03:00
|
|
|
const sarif = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
|
|
t.deepEqual(sarif.runs[0].properties.metricResults, [
|
|
|
|
{
|
2021-05-13 20:56:27 +03:00
|
|
|
ruleId: `${idPrefix}/summary/lines-of-code`,
|
2021-04-17 00:52:39 +03:00
|
|
|
value: 123,
|
|
|
|
baseline: lineCount,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
t.deepEqual(sarif.runs[1].properties.metricResults, [
|
|
|
|
{
|
2021-05-04 19:38:38 +03:00
|
|
|
rule: {
|
2021-05-13 20:56:27 +03:00
|
|
|
id: `${idPrefix}/summary/lines-of-code`,
|
2021-04-17 00:52:39 +03:00
|
|
|
},
|
|
|
|
value: 123,
|
|
|
|
baseline: lineCount,
|
|
|
|
},
|
|
|
|
]);
|
2021-07-21 12:27:42 +03:00
|
|
|
t.deepEqual(sarif.runs[2].properties.metricResults, [
|
|
|
|
{
|
|
|
|
rule: {
|
|
|
|
index: 0,
|
|
|
|
toolComponent: {
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
value: 123,
|
|
|
|
baseline: lineCount,
|
|
|
|
},
|
|
|
|
]);
|
2021-06-03 19:32:44 +03:00
|
|
|
// when the rule doesn't exist, it should not be added
|
2021-07-21 12:27:42 +03:00
|
|
|
t.deepEqual(sarif.runs[3].properties.metricResults, []);
|
2021-04-17 00:52:39 +03:00
|
|
|
}
|
2021-06-03 19:32:44 +03:00
|
|
|
function verifyQuerySuites(tmpDir) {
|
|
|
|
const qlsContent = [
|
|
|
|
{
|
|
|
|
query: "foo.ql",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const qlsContent2 = [
|
|
|
|
{
|
|
|
|
query: "bar.ql",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const qlsPackContentCpp = [
|
|
|
|
{
|
|
|
|
qlpack: "a/b",
|
|
|
|
version: "1.0.0",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const qlsPackContentJava = [
|
|
|
|
{
|
|
|
|
qlpack: "c/d",
|
|
|
|
version: "2.0.0",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
for (const lang of Object.values(languages_1.Language)) {
|
|
|
|
t.deepEqual(readContents(`${lang}-queries-builtin.qls`), qlsContent);
|
|
|
|
t.deepEqual(readContents(`${lang}-queries-custom-0.qls`), qlsContent);
|
|
|
|
t.deepEqual(readContents(`${lang}-queries-custom-1.qls`), qlsContent2);
|
|
|
|
const packSuiteName = `${lang}-queries-packs.qls`;
|
|
|
|
if (lang === languages_1.Language.cpp) {
|
|
|
|
t.deepEqual(readContents(packSuiteName), qlsPackContentCpp);
|
|
|
|
}
|
|
|
|
else if (lang === languages_1.Language.java) {
|
|
|
|
t.deepEqual(readContents(packSuiteName), qlsPackContentJava);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t.false(fs.existsSync(path.join(tmpDir, "codeql_databases", packSuiteName)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function readContents(name) {
|
|
|
|
const x = fs.readFileSync(path.join(tmpDir, "codeql_databases", name), "utf8");
|
|
|
|
console.log(x);
|
|
|
|
return yaml.safeLoad(fs.readFileSync(path.join(tmpDir, "codeql_databases", name), "utf8"));
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 20:02:33 +03:00
|
|
|
});
|
|
|
|
//# sourceMappingURL=analyze.test.js.map
|