codeql-action/lib/util.test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

308 строки
13 KiB
JavaScript
Исходник Постоянная ссылка Обычный вид История

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;
2023-01-18 23:00:33 +03:00
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
2021-07-27 19:59:59 +03:00
}) : (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;
};
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"));
2022-08-10 15:57:57 +03:00
const path_1 = __importDefault(require("path"));
const ava_1 = __importDefault(require("ava"));
const environment_1 = require("./environment");
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"));
(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");
const toolNames = util.getToolNames(JSON.parse(input));
2020-05-04 20:28:01 +03:00
t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]);
});
const GET_MEMORY_FLAG_TESTS = [
{
input: undefined,
totalMemoryMb: 8 * 1024,
platform: "linux",
expectedMemoryValue: 7 * 1024,
},
{
input: undefined,
totalMemoryMb: 8 * 1024,
platform: "win32",
expectedMemoryValue: 6.5 * 1024,
},
{
input: "",
totalMemoryMb: 8 * 1024,
platform: "linux",
expectedMemoryValue: 7 * 1024,
},
{
input: "512",
totalMemoryMb: 8 * 1024,
platform: "linux",
expectedMemoryValue: 512,
},
{
input: undefined,
totalMemoryMb: 64 * 1024,
platform: "linux",
expectedMemoryValue: 61644, // Math.floor(1024 * (64 - 1 - 0.05 * (64 - 8)))
},
{
input: undefined,
totalMemoryMb: 64 * 1024,
platform: "win32",
expectedMemoryValue: 61132, // Math.floor(1024 * (64 - 1.5 - 0.05 * (64 - 8)))
},
{
input: undefined,
totalMemoryMb: 64 * 1024,
platform: "linux",
Bump the npm group with 12 updates (#2003) * Bump the npm group with 12 updates Bumps the npm group with 12 updates: | Package | From | To | | --- | --- | --- | | [@octokit/types](https://github.com/octokit/types.ts) | `12.1.1` | `12.3.0` | | [@types/uuid](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/uuid) | `9.0.6` | `9.0.7` | | [@types/adm-zip](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/adm-zip) | `0.5.3` | `0.5.4` | | [@types/js-yaml](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/js-yaml) | `4.0.8` | `4.0.9` | | [@types/semver](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/semver) | `7.5.4` | `7.5.5` | | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) | `6.9.1` | `6.12.0` | | [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) | `6.9.1` | `6.12.0` | | [eslint](https://github.com/eslint/eslint) | `8.52.0` | `8.54.0` | | [nock](https://github.com/nock/nock) | `13.3.7` | `13.3.8` | | [sinon](https://github.com/sinonjs/sinon) | `17.0.0` | `17.0.1` | | [@types/sinon](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/sinon) | `10.0.20` | `17.0.1` | | [typescript](https://github.com/Microsoft/TypeScript) | `5.2.2` | `5.3.2` | Updates `@octokit/types` from 12.1.1 to 12.3.0 - [Release notes](https://github.com/octokit/types.ts/releases) - [Commits](https://github.com/octokit/types.ts/compare/v12.1.1...v12.3.0) Updates `@types/uuid` from 9.0.6 to 9.0.7 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/uuid) Updates `@types/adm-zip` from 0.5.3 to 0.5.4 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/adm-zip) Updates `@types/js-yaml` from 4.0.8 to 4.0.9 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/js-yaml) Updates `@types/semver` from 7.5.4 to 7.5.5 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/semver) Updates `@typescript-eslint/eslint-plugin` from 6.9.1 to 6.12.0 - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v6.12.0/packages/eslint-plugin) Updates `@typescript-eslint/parser` from 6.9.1 to 6.12.0 - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v6.12.0/packages/parser) Updates `eslint` from 8.52.0 to 8.54.0 - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md) - [Commits](https://github.com/eslint/eslint/compare/v8.52.0...v8.54.0) Updates `nock` from 13.3.7 to 13.3.8 - [Release notes](https://github.com/nock/nock/releases) - [Changelog](https://github.com/nock/nock/blob/main/CHANGELOG.md) - [Commits](https://github.com/nock/nock/compare/v13.3.7...v13.3.8) Updates `sinon` from 17.0.0 to 17.0.1 - [Release notes](https://github.com/sinonjs/sinon/releases) - [Changelog](https://github.com/sinonjs/sinon/blob/main/docs/changelog.md) - [Commits](https://github.com/sinonjs/sinon/compare/v17.0.0...v17.0.1) Updates `@types/sinon` from 10.0.20 to 17.0.1 - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/sinon) Updates `typescript` from 5.2.2 to 5.3.2 - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](https://github.com/Microsoft/TypeScript/commits) --- updated-dependencies: - dependency-name: "@octokit/types" dependency-type: direct:production update-type: version-update:semver-minor dependency-group: npm - dependency-name: "@types/uuid" dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/adm-zip" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/js-yaml" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/semver" dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@typescript-eslint/eslint-plugin" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: "@typescript-eslint/parser" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: eslint dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: nock dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: sinon dependency-type: direct:development update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@types/sinon" dependency-type: direct:development update-type: version-update:semver-major dependency-group: npm - dependency-name: typescript dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm ... Signed-off-by: dependabot[bot] <support@github.com> * Update checked-in dependencies * Rebuild --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2023-11-21 01:35:28 +03:00
expectedMemoryValue: 58777, // Math.floor(1024 * (64 - 1 - 0.1 * (64 - 8)))
reservedPercentageValue: "10",
},
];
for (const { input, totalMemoryMb, platform, expectedMemoryValue, reservedPercentageValue, } of GET_MEMORY_FLAG_TESTS) {
(0, ava_1.default)(`Memory flag value is ${expectedMemoryValue} for ${input ?? "no user input"} on ${platform} with ${totalMemoryMb} MB total system RAM${reservedPercentageValue
? ` and reserved percentage env var set to ${reservedPercentageValue}`
: ""}`, async (t) => {
process.env[environment_1.EnvVar.SCALING_RESERVED_RAM_PERCENTAGE] =
reservedPercentageValue || undefined;
const flag = util.getMemoryFlagValueForPlatform(input, totalMemoryMb * 1024 * 1024, platform);
t.deepEqual(flag, expectedMemoryValue);
});
}
(0, ava_1.default)("getMemoryFlag() throws if the ram input is < 0 or NaN", async (t) => {
2020-06-22 18:17:25 +03:00
for (const input of ["-1", "hello!"]) {
2023-09-18 14:43:52 +03:00
t.throws(() => util.getMemoryFlag(input, (0, logging_1.getRunnerLogger)(true)));
2020-06-22 18:17:25 +03:00
}
});
(0, 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");
});
(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;
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) {
const flag = util.getThreadsFlag(input, (0, logging_1.getRunnerLogger)(true));
2020-06-22 18:17:25 +03:00
t.deepEqual(flag, expectedFlag);
}
});
(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
});
(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;
});
(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;
});
(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;
});
(0, 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");
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',
});
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',
});
t.throws(() => util.parseGitHubUrl("http:///::::433"), {
2020-09-28 20:28:46 +03:00
message: '"http:///::::433" is not a valid URL',
});
});
(0, ava_1.default)("allowed API versions", async (t) => {
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);
});
2022-08-11 14:45:26 +03:00
(0, ava_1.default)("doesDirectoryExist", async (t) => {
2022-08-11 17:08:06 +03:00
// Returns false if no file/dir of this name exists
2022-08-11 16:09:44 +03:00
t.false(util.doesDirectoryExist("non-existent-file.txt"));
2022-08-11 14:45:26 +03:00
await util.withTmpDir(async (tmpDir) => {
2022-08-11 17:08:06 +03:00
// Returns false if file
const testFile = `${tmpDir}/test-file.txt`;
fs.writeFileSync(testFile, "");
t.false(util.doesDirectoryExist(testFile));
// Returns true if directory
2022-08-11 14:45:26 +03:00
fs.writeFileSync(`${tmpDir}/nested-test-file.txt`, "");
t.true(util.doesDirectoryExist(tmpDir));
});
2022-08-10 15:57:57 +03:00
});
2022-08-11 14:45:26 +03:00
(0, ava_1.default)("listFolder", async (t) => {
2022-08-11 16:09:44 +03:00
// Returns empty if not a directory
t.deepEqual(util.listFolder("not-a-directory"), []);
// Returns empty if directory is empty
await util.withTmpDir(async (emptyTmpDir) => {
2022-08-11 14:45:26 +03:00
t.deepEqual(util.listFolder(emptyTmpDir), []);
2022-08-11 16:09:44 +03:00
});
// Returns all file names in directory
await util.withTmpDir(async (tmpDir) => {
const nestedDir = fs.mkdtempSync(path_1.default.join(tmpDir, "nested-"));
2022-08-11 17:46:56 +03:00
fs.writeFileSync(path_1.default.resolve(nestedDir, "nested-test-file.txt"), "");
fs.writeFileSync(path_1.default.resolve(tmpDir, "test-file-1.txt"), "");
fs.writeFileSync(path_1.default.resolve(tmpDir, "test-file-2.txt"), "");
fs.writeFileSync(path_1.default.resolve(tmpDir, "test-file-3.txt"), "");
2022-08-11 14:45:26 +03:00
t.deepEqual(util.listFolder(tmpDir), [
2022-08-11 17:46:56 +03:00
path_1.default.resolve(nestedDir, "nested-test-file.txt"),
path_1.default.resolve(tmpDir, "test-file-1.txt"),
path_1.default.resolve(tmpDir, "test-file-2.txt"),
path_1.default.resolve(tmpDir, "test-file-3.txt"),
2022-08-11 14:45:26 +03:00
]);
});
2022-08-10 15:57:57 +03:00
});
const longTime = 999999;
const shortTime = 10;
(0, ava_1.default)("withTimeout on long task", async (t) => {
let longTaskTimedOut = false;
const longTask = new Promise((resolve) => {
setTimeout(() => {
resolve(42);
}, longTime);
});
const result = await util.withTimeout(shortTime, longTask, () => {
longTaskTimedOut = true;
});
t.deepEqual(longTaskTimedOut, true);
t.deepEqual(result, undefined);
});
(0, ava_1.default)("withTimeout on short task", async (t) => {
let shortTaskTimedOut = false;
const shortTask = new Promise((resolve) => {
setTimeout(() => {
resolve(99);
}, shortTime);
});
const result = await util.withTimeout(longTime, shortTask, () => {
shortTaskTimedOut = true;
});
t.deepEqual(shortTaskTimedOut, false);
t.deepEqual(result, 99);
});
2022-10-11 12:04:21 +03:00
(0, ava_1.default)("withTimeout doesn't call callback if promise resolves", async (t) => {
let shortTaskTimedOut = false;
const shortTask = new Promise((resolve) => {
setTimeout(() => {
resolve(99);
}, shortTime);
});
const result = await util.withTimeout(100, shortTask, () => {
shortTaskTimedOut = true;
});
await new Promise((r) => setTimeout(r, 200));
t.deepEqual(shortTaskTimedOut, false);
t.deepEqual(result, 99);
});
function createMockSarifWithNotification(locations) {
return {
runs: [
{
tool: {
driver: {
name: "CodeQL",
},
},
invocations: [
{
toolExecutionNotifications: [
{
locations,
},
],
},
],
},
],
};
}
const stubLocation = {
physicalLocation: {
artifactLocation: {
uri: "file1",
},
},
};
(0, ava_1.default)("fixInvalidNotifications leaves notifications with unique locations alone", (t) => {
const messages = [];
const result = util.fixInvalidNotifications(createMockSarifWithNotification([stubLocation]), (0, testing_utils_1.getRecordingLogger)(messages));
t.deepEqual(result, createMockSarifWithNotification([stubLocation]));
t.is(messages.length, 1);
t.deepEqual(messages[0], {
type: "debug",
message: "No duplicate locations found in SARIF notification objects.",
});
});
(0, ava_1.default)("fixInvalidNotifications removes duplicate locations", (t) => {
const messages = [];
const result = util.fixInvalidNotifications(createMockSarifWithNotification([stubLocation, stubLocation]), (0, testing_utils_1.getRecordingLogger)(messages));
t.deepEqual(result, createMockSarifWithNotification([stubLocation]));
t.is(messages.length, 1);
t.deepEqual(messages[0], {
type: "info",
message: "Removed 1 duplicate locations from SARIF notification objects.",
});
});
2020-05-13 18:31:24 +03:00
//# sourceMappingURL=util.test.js.map