codeql-action/lib/util.test.js

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

2020-05-04 20:28:01 +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-05-04 20:28:01 +03:00
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
2020-06-22 18:17:25 +03:00
const os = __importStar(require("os"));
const ava_1 = __importDefault(require("ava"));
2020-09-01 16:13:10 +03:00
const logging_1 = require("./logging");
2020-06-23 15:24:41 +03:00
const testing_utils_1 = require("./testing-utils");
2020-05-04 20:28:01 +03:00
const util = __importStar(require("./util"));
2020-07-06 18:04:02 +03:00
testing_utils_1.setupTests(ava_1.default);
2020-09-14 12:44:43 +03:00
ava_1.default("getToolNames", (t) => {
const input = fs.readFileSync(`${__dirname}/../src/testdata/tool-names.sarif`, "utf8");
2020-05-04 20:28:01 +03:00
const toolNames = util.getToolNames(input);
t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]);
});
2020-09-14 12:44:43 +03:00
ava_1.default("getMemoryFlag() should return the correct --ram flag", (t) => {
2020-06-22 18:17:25 +03:00
const totalMem = Math.floor(os.totalmem() / (1024 * 1024));
const tests = [
[undefined, `--ram=${totalMem - 256}`],
["", `--ram=${totalMem - 256}`],
["512", "--ram=512"],
];
for (const [input, expectedFlag] of tests) {
2020-09-01 16:13:10 +03:00
const flag = util.getMemoryFlag(input);
2020-06-22 18:17:25 +03:00
t.deepEqual(flag, expectedFlag);
}
});
2020-09-14 12:44:43 +03:00
ava_1.default("getMemoryFlag() throws if the ram input is < 0 or NaN", (t) => {
2020-06-22 18:17:25 +03:00
for (const input of ["-1", "hello!"]) {
2020-09-01 16:13:10 +03:00
t.throws(() => util.getMemoryFlag(input));
2020-06-22 18:17:25 +03:00
}
});
2020-09-14 12:44:43 +03:00
ava_1.default("getAddSnippetsFlag() should return the correct flag", (t) => {
t.deepEqual(util.getAddSnippetsFlag(true), "--sarif-add-snippets");
t.deepEqual(util.getAddSnippetsFlag("true"), "--sarif-add-snippets");
t.deepEqual(util.getAddSnippetsFlag(false), "--no-sarif-add-snippets");
t.deepEqual(util.getAddSnippetsFlag(undefined), "--no-sarif-add-snippets");
t.deepEqual(util.getAddSnippetsFlag("false"), "--no-sarif-add-snippets");
t.deepEqual(util.getAddSnippetsFlag("foo bar"), "--no-sarif-add-snippets");
});
2020-09-14 12:44:43 +03:00
ava_1.default("getThreadsFlag() should return the correct --threads flag", (t) => {
2020-06-22 18:17:25 +03:00
const numCpus = os.cpus().length;
const tests = [
["0", "--threads=0"],
["1", "--threads=1"],
[undefined, `--threads=${numCpus}`],
["", `--threads=${numCpus}`],
[`${numCpus + 1}`, `--threads=${numCpus}`],
[`${-numCpus - 1}`, `--threads=${-numCpus}`],
];
for (const [input, expectedFlag] of tests) {
2020-09-01 16:13:10 +03:00
const flag = util.getThreadsFlag(input, logging_1.getRunnerLogger(true));
2020-06-22 18:17:25 +03:00
t.deepEqual(flag, expectedFlag);
}
});
2020-09-14 12:44:43 +03:00
ava_1.default("getThreadsFlag() throws if the threads input is not an integer", (t) => {
2020-09-01 16:13:10 +03:00
t.throws(() => util.getThreadsFlag("hello!", logging_1.getRunnerLogger(true)));
2020-06-22 18:17:25 +03:00
});
2020-09-14 12:44:43 +03:00
ava_1.default("isLocalRun() runs correctly", (t) => {
process.env.CODEQL_LOCAL_RUN = "";
t.assert(!util.isLocalRun());
2020-09-14 12:44:43 +03:00
process.env.CODEQL_LOCAL_RUN = "false";
t.assert(!util.isLocalRun());
2020-09-14 12:44:43 +03:00
process.env.CODEQL_LOCAL_RUN = "0";
t.assert(!util.isLocalRun());
2020-09-14 12:44:43 +03:00
process.env.CODEQL_LOCAL_RUN = "true";
t.assert(util.isLocalRun());
2020-09-14 12:44:43 +03:00
process.env.CODEQL_LOCAL_RUN = "hucairz";
t.assert(util.isLocalRun());
});
2020-09-14 12:44:43 +03:00
ava_1.default("getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)", (t) => {
2020-08-10 10:25:14 +03:00
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
const options = { foo: 42 };
process.env.CODEQL_ACTION_EXTRA_OPTIONS = JSON.stringify(options);
t.deepEqual(util.getExtraOptionsEnvParam(), options);
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});
2020-09-14 12:44:43 +03:00
ava_1.default("getExtraOptionsEnvParam() succeeds on valid options", (t) => {
2020-08-10 10:25:14 +03:00
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
const options = { database: { init: ["--debug"] } };
2020-09-14 12:44:43 +03:00
process.env.CODEQL_ACTION_EXTRA_OPTIONS = JSON.stringify(options);
2020-08-10 10:25:14 +03:00
t.deepEqual(util.getExtraOptionsEnvParam(), options);
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});
2020-09-14 12:44:43 +03:00
ava_1.default("getExtraOptionsEnvParam() fails on invalid JSON", (t) => {
2020-08-10 10:25:14 +03:00
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
process.env.CODEQL_ACTION_EXTRA_OPTIONS = "{{invalid-json}}";
t.throws(util.getExtraOptionsEnvParam);
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});
2020-09-28 20:28:46 +03:00
ava_1.default("parseGithubUrl", (t) => {
t.deepEqual(util.parseGithubUrl("github.com"), "https://github.com");
t.deepEqual(util.parseGithubUrl("https://github.com"), "https://github.com");
t.deepEqual(util.parseGithubUrl("https://api.github.com"), "https://github.com");
t.deepEqual(util.parseGithubUrl("https://github.com/foo/bar"), "https://github.com");
2020-09-28 20:28:46 +03:00
t.deepEqual(util.parseGithubUrl("github.example.com"), "https://github.example.com/");
t.deepEqual(util.parseGithubUrl("https://github.example.com"), "https://github.example.com/");
t.deepEqual(util.parseGithubUrl("https://api.github.example.com"), "https://github.example.com/");
t.deepEqual(util.parseGithubUrl("https://github.example.com/api/v3"), "https://github.example.com/");
t.deepEqual(util.parseGithubUrl("https://github.example.com:1234"), "https://github.example.com:1234/");
t.deepEqual(util.parseGithubUrl("https://api.github.example.com:1234"), "https://github.example.com:1234/");
t.deepEqual(util.parseGithubUrl("https://github.example.com:1234/api/v3"), "https://github.example.com:1234/");
t.deepEqual(util.parseGithubUrl("https://github.example.com/base/path"), "https://github.example.com/base/path/");
t.deepEqual(util.parseGithubUrl("https://github.example.com/base/path/api/v3"), "https://github.example.com/base/path/");
t.throws(() => util.parseGithubUrl(""), {
message: '"" is not a valid URL',
});
t.throws(() => util.parseGithubUrl("ssh://github.com"), {
message: '"ssh://github.com" is not a http or https URL',
});
t.throws(() => util.parseGithubUrl("http:///::::433"), {
message: '"http:///::::433" is not a valid URL',
});
});
2020-05-13 18:31:24 +03:00
//# sourceMappingURL=util.test.js.map