codeql-action/lib/analyze.test.js

149 строки
6.3 KiB
JavaScript
Исходник Обычный вид История

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;
};
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"));
const path = __importStar(require("path"));
const ava_1 = __importDefault(require("ava"));
const sinon_1 = __importDefault(require("sinon"));
const analyze_1 = require("./analyze");
const codeql_1 = require("./codeql");
const count = __importStar(require("./count-loc"));
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");
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
// 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) => {
const mockLinesOfCode = Object.entries(languages_1.Language).reduce((obj, [lang], i) => {
// use a different line count for each languaged
obj[lang] = i + 1;
return obj;
}, {});
sinon_1.default.stub(count, "countLoc").resolves(mockLinesOfCode);
let searchPathsUsed = [];
2020-09-10 20:02:33 +03:00
return await util.withTmpDir(async (tmpDir) => {
testing_utils_1.setupActionsVars(tmpDir, tmpDir);
const memoryFlag = "";
const addSnippetsFlag = "";
const threadsFlag = "";
2020-09-10 20:02:33 +03:00
for (const language of Object.values(languages_1.Language)) {
codeql_1.setCodeQL({
databaseAnalyze: async (_, sarifFile, searchPath) => {
fs.writeFileSync(sarifFile, JSON.stringify({
runs: [
// variant 1 uses metricId
{
properties: {
metricResults: [
{
metricId: `${language}/summary/lines-of-code`,
value: 123,
},
],
},
},
// variant 2 uses metric.id
{
properties: {
metricResults: [
{
metric: {
id: `${language}/summary/lines-of-code`,
},
value: 123,
},
],
},
},
{},
],
}));
searchPathsUsed.push(searchPath);
},
});
searchPathsUsed = [];
2020-09-10 20:02:33 +03:00
const config = {
languages: [language],
queries: {},
pathsIgnore: [],
paths: [],
originalUserInput: {},
tempDir: tmpDir,
toolCacheDir: tmpDir,
codeQLCmd: "",
2021-02-15 12:29:10 +03:00
gitHubVersion: {
type: util.GitHubVariant.DOTCOM,
},
2020-09-10 20:02:33 +03:00
};
fs.mkdirSync(util.getCodeQLDatabasePath(config.tempDir, language), {
recursive: true,
});
2020-09-10 20:02:33 +03:00
config.queries[language] = {
builtin: ["foo.ql"],
2020-09-10 20:02:33 +03:00
custom: [],
};
const builtinStatusReport = await analyze_1.runQueries(tmpDir, memoryFlag, addSnippetsFlag, threadsFlag, config, logging_1.getRunnerLogger(true));
2020-09-10 20:02:33 +03:00
t.deepEqual(Object.keys(builtinStatusReport).length, 1);
t.true(`analyze_builtin_queries_${language}_duration_ms` in builtinStatusReport);
config.queries[language] = {
builtin: [],
custom: [
{
queries: ["foo.ql"],
searchPath: "/1",
},
{
queries: ["bar.ql"],
searchPath: "/2",
},
],
2020-09-10 20:02:33 +03:00
};
const customStatusReport = await analyze_1.runQueries(tmpDir, memoryFlag, addSnippetsFlag, threadsFlag, config, logging_1.getRunnerLogger(true));
2020-09-10 20:02:33 +03:00
t.deepEqual(Object.keys(customStatusReport).length, 1);
t.true(`analyze_custom_queries_${language}_duration_ms` in customStatusReport);
t.deepEqual(searchPathsUsed, [undefined, "/1", "/2"]);
2020-09-10 20:02:33 +03:00
}
verifyLineCounts(tmpDir);
2020-09-10 20:02:33 +03:00
});
function verifyLineCounts(tmpDir) {
// eslint-disable-next-line github/array-foreach
Object.keys(languages_1.Language).forEach((lang, i) => {
verifyLineCountForFile(lang, path.join(tmpDir, `${lang}-builtin.sarif`), i + 1);
verifyLineCountForFile(lang, path.join(tmpDir, `${lang}-custom.sarif`), i + 1);
});
}
function verifyLineCountForFile(lang, filePath, lineCount) {
const sarif = JSON.parse(fs.readFileSync(filePath, "utf8"));
t.deepEqual(sarif.runs[0].properties.metricResults, [
{
metricId: `${lang}/summary/lines-of-code`,
value: 123,
baseline: lineCount,
},
]);
t.deepEqual(sarif.runs[1].properties.metricResults, [
{
metric: {
id: `${lang}/summary/lines-of-code`,
},
value: 123,
baseline: lineCount,
},
]);
// when the metric doesn't exists, it should not be added
t.deepEqual(sarif.runs[2].properties.metricResults, []);
}
2020-09-10 20:02:33 +03:00
});
//# sourceMappingURL=analyze.test.js.map