2020-05-04 20:28:01 +03:00
|
|
|
"use strict";
|
2021-07-27 19:59:59 +03:00
|
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
|
|
}) : (function(o, m, k, k2) {
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
o[k2] = m[k];
|
|
|
|
}));
|
|
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
|
|
}) : function(o, v) {
|
|
|
|
o["default"] = v;
|
|
|
|
});
|
2020-05-04 20:28:01 +03:00
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
2021-07-27 19:59:59 +03:00
|
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
|
|
__setModuleDefault(result, mod);
|
2020-05-04 20:28:01 +03:00
|
|
|
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-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"));
|
2021-02-13 01:31:38 +03:00
|
|
|
const stream = __importStar(require("stream"));
|
2020-11-30 19:33:38 +03:00
|
|
|
const github = __importStar(require("@actions/github"));
|
2020-09-29 16:43:37 +03:00
|
|
|
const ava_1 = __importDefault(require("ava"));
|
2021-08-11 15:14:56 +03:00
|
|
|
const sinon = __importStar(require("sinon"));
|
2020-11-30 19:33:38 +03:00
|
|
|
const api = __importStar(require("./api-client"));
|
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"));
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, testing_utils_1.setupTests)(ava_1.default);
|
|
|
|
(0, ava_1.default)("getToolNames", (t) => {
|
2020-09-14 12:44:43 +03:00
|
|
|
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"]);
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, 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));
|
2021-02-17 02:04:38 +03:00
|
|
|
const expectedThreshold = process.platform === "win32" ? 1536 : 1024;
|
2020-09-15 20:42:23 +03:00
|
|
|
const tests = [
|
2021-02-17 02:04:38 +03:00
|
|
|
[undefined, `--ram=${totalMem - expectedThreshold}`],
|
|
|
|
["", `--ram=${totalMem - expectedThreshold}`],
|
2020-09-15 20:42:23 +03:00
|
|
|
["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);
|
|
|
|
}
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, 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
|
|
|
}
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("getAddSnippetsFlag() should return the correct flag", (t) => {
|
2020-09-10 19:18:02 +03:00
|
|
|
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");
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("getThreadsFlag() should return the correct --threads flag", (t) => {
|
2020-06-22 18:17:25 +03:00
|
|
|
const numCpus = os.cpus().length;
|
2020-09-15 20:42:23 +03:00
|
|
|
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) {
|
2021-09-10 23:53:13 +03:00
|
|
|
const flag = util.getThreadsFlag(input, (0, logging_1.getRunnerLogger)(true));
|
2020-06-22 18:17:25 +03:00
|
|
|
t.deepEqual(flag, expectedFlag);
|
|
|
|
}
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("getThreadsFlag() throws if the threads input is not an integer", (t) => {
|
|
|
|
t.throws(() => util.getThreadsFlag("hello!", (0, logging_1.getRunnerLogger)(true)));
|
2020-06-22 18:17:25 +03:00
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, 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;
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, 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;
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, 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;
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("parseGitHubUrl", (t) => {
|
2021-02-28 09:55:55 +03:00
|
|
|
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");
|
|
|
|
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(""), {
|
2020-09-28 20:28:46 +03:00
|
|
|
message: '"" is not a valid URL',
|
|
|
|
});
|
2021-02-28 09:55:55 +03:00
|
|
|
t.throws(() => util.parseGitHubUrl("ssh://github.com"), {
|
2020-09-28 20:28:46 +03:00
|
|
|
message: '"ssh://github.com" is not a http or https URL',
|
|
|
|
});
|
2021-02-28 09:55:55 +03:00
|
|
|
t.throws(() => util.parseGitHubUrl("http:///::::433"), {
|
2020-09-28 20:28:46 +03:00
|
|
|
message: '"http:///::::433" is not a valid URL',
|
|
|
|
});
|
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("allowed API versions", async (t) => {
|
2020-11-26 20:54:34 +03:00
|
|
|
t.is(util.apiVersionInRange("1.33.0", "1.33", "2.0"), undefined);
|
|
|
|
t.is(util.apiVersionInRange("1.33.1", "1.33", "2.0"), undefined);
|
|
|
|
t.is(util.apiVersionInRange("1.34.0", "1.33", "2.0"), undefined);
|
|
|
|
t.is(util.apiVersionInRange("2.0.0", "1.33", "2.0"), undefined);
|
|
|
|
t.is(util.apiVersionInRange("2.0.1", "1.33", "2.0"), undefined);
|
|
|
|
t.is(util.apiVersionInRange("1.32.0", "1.33", "2.0"), util.DisallowedAPIVersionReason.ACTION_TOO_NEW);
|
|
|
|
t.is(util.apiVersionInRange("2.1.0", "1.33", "2.0"), util.DisallowedAPIVersionReason.ACTION_TOO_OLD);
|
|
|
|
});
|
2020-11-30 19:33:38 +03:00
|
|
|
function mockGetMetaVersionHeader(versionHeader) {
|
|
|
|
// Passing an auth token is required, so we just use a dummy value
|
|
|
|
const client = github.getOctokit("123");
|
|
|
|
const response = {
|
|
|
|
headers: {
|
|
|
|
"x-github-enterprise-version": versionHeader,
|
|
|
|
},
|
|
|
|
};
|
2021-08-11 15:14:56 +03:00
|
|
|
const spyGetContents = sinon
|
2020-11-30 19:33:38 +03:00
|
|
|
.stub(client.meta, "get")
|
|
|
|
.resolves(response);
|
2021-08-11 15:14:56 +03:00
|
|
|
sinon.stub(api, "getApiClient").value(() => client);
|
2020-11-30 19:33:38 +03:00
|
|
|
return spyGetContents;
|
|
|
|
}
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("getGitHubVersion", async (t) => {
|
2020-11-30 19:33:38 +03:00
|
|
|
const v = await util.getGitHubVersion({
|
|
|
|
auth: "",
|
|
|
|
url: "https://github.com",
|
|
|
|
});
|
2021-02-15 12:29:10 +03:00
|
|
|
t.deepEqual(util.GitHubVariant.DOTCOM, v.type);
|
2020-11-30 19:33:38 +03:00
|
|
|
mockGetMetaVersionHeader("2.0");
|
|
|
|
const v2 = await util.getGitHubVersion({
|
|
|
|
auth: "",
|
|
|
|
url: "https://ghe.example.com",
|
|
|
|
});
|
2021-02-15 12:29:10 +03:00
|
|
|
t.deepEqual({ type: util.GitHubVariant.GHES, version: "2.0" }, v2);
|
2021-02-13 14:06:03 +03:00
|
|
|
mockGetMetaVersionHeader("GitHub AE");
|
|
|
|
const ghae = await util.getGitHubVersion({
|
|
|
|
auth: "",
|
|
|
|
url: "https://example.githubenterprise.com",
|
|
|
|
});
|
2021-02-15 12:29:10 +03:00
|
|
|
t.deepEqual({ type: util.GitHubVariant.GHAE }, ghae);
|
2020-11-30 19:33:38 +03:00
|
|
|
mockGetMetaVersionHeader(undefined);
|
|
|
|
const v3 = await util.getGitHubVersion({
|
|
|
|
auth: "",
|
|
|
|
url: "https://ghe.example.com",
|
|
|
|
});
|
2021-02-15 12:29:10 +03:00
|
|
|
t.deepEqual({ type: util.GitHubVariant.DOTCOM }, v3);
|
2020-11-30 19:33:38 +03:00
|
|
|
});
|
2021-09-10 23:53:13 +03:00
|
|
|
(0, ava_1.default)("getGitHubAuth", async (t) => {
|
2021-02-13 01:31:38 +03:00
|
|
|
const msgs = [];
|
|
|
|
const mockLogger = {
|
|
|
|
warning: (msg) => msgs.push(msg),
|
|
|
|
};
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
|
|
t.throwsAsync(async () => util.getGitHubAuth(mockLogger, "abc", true));
|
|
|
|
process.env.GITHUB_TOKEN = "123";
|
|
|
|
t.is("123", await util.getGitHubAuth(mockLogger, undefined, undefined));
|
|
|
|
t.is(msgs.length, 0);
|
|
|
|
t.is("abc", await util.getGitHubAuth(mockLogger, "abc", undefined));
|
|
|
|
t.is(msgs.length, 1); // warning expected
|
|
|
|
msgs.length = 0;
|
|
|
|
await mockStdInForAuth(t, mockLogger, "def", "def");
|
|
|
|
await mockStdInForAuth(t, mockLogger, "def", "", "def");
|
|
|
|
await mockStdInForAuth(t, mockLogger, "def", "def\n some extra garbage", "ghi");
|
|
|
|
await mockStdInForAuth(t, mockLogger, "defghi", "def", "ghi\n123");
|
|
|
|
await mockStdInForAuthExpectError(t, mockLogger, "");
|
|
|
|
await mockStdInForAuthExpectError(t, mockLogger, "", " ", "abc");
|
|
|
|
await mockStdInForAuthExpectError(t, mockLogger, " def\n some extra garbage", "ghi");
|
|
|
|
t.is(msgs.length, 0);
|
|
|
|
});
|
|
|
|
async function mockStdInForAuth(t, mockLogger, expected, ...text) {
|
|
|
|
const stdin = stream.Readable.from(text);
|
|
|
|
t.is(expected, await util.getGitHubAuth(mockLogger, undefined, true, stdin));
|
|
|
|
}
|
|
|
|
async function mockStdInForAuthExpectError(t, mockLogger, ...text) {
|
|
|
|
const stdin = stream.Readable.from(text);
|
|
|
|
await t.throwsAsync(async () => util.getGitHubAuth(mockLogger, undefined, true, stdin));
|
|
|
|
}
|
2020-05-13 18:31:24 +03:00
|
|
|
//# sourceMappingURL=util.test.js.map
|