Files
codeql-action/lib/testing-utils.js
T

92 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-06-22 12:12:14 +01:00
"use strict";
2021-07-27 17:59:59 +01: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;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
2021-07-27 17:59:59 +01: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);
return result;
};
2020-06-22 12:12:14 +01:00
Object.defineProperty(exports, "__esModule", { value: true });
2021-07-27 17:59:59 +01:00
exports.setupActionsVars = exports.setupTests = void 0;
2021-08-11 13:14:56 +01:00
const sinon = __importStar(require("sinon"));
const CodeQL = __importStar(require("./codeql"));
2020-06-23 14:33:20 +01:00
function wrapOutput(context) {
2020-06-23 17:17:11 +01:00
// Function signature taken from Socket.write.
// Note there are two overloads:
// write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
// write(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
return (chunk, encoding, cb) => {
// Work out which method overload we are in
2020-09-14 10:44:43 +01:00
if (cb === undefined && typeof encoding === "function") {
2020-06-23 17:17:11 +01:00
cb = encoding;
encoding = undefined;
}
// Record the output
2020-09-14 10:44:43 +01:00
if (typeof chunk === "string") {
2020-06-23 17:17:11 +01:00
context.testOutput += chunk;
}
else {
2020-09-14 10:44:43 +01:00
context.testOutput += new TextDecoder(encoding || "utf-8").decode(chunk);
2020-06-23 17:17:11 +01:00
}
// Satisfy contract by calling callback when done
2020-09-14 10:44:43 +01:00
if (cb !== undefined && typeof cb === "function") {
2020-06-23 17:17:11 +01:00
cb();
2020-06-23 14:33:20 +01:00
}
return true;
};
}
2020-07-06 16:04:02 +01:00
function setupTests(test) {
2020-06-22 12:12:14 +01:00
const typedTest = test;
2020-09-14 10:44:43 +01:00
typedTest.beforeEach((t) => {
// Set an empty CodeQL object so that all method calls will fail
// unless the test explicitly sets one up.
CodeQL.setCodeQL({});
// Replace stdout and stderr so we can record output during tests
2020-06-23 14:33:20 +01:00
t.context.testOutput = "";
2020-06-22 12:12:14 +01:00
const processStdoutWrite = process.stdout.write.bind(process.stdout);
2020-06-23 14:33:20 +01:00
t.context.stdoutWrite = processStdoutWrite;
process.stdout.write = wrapOutput(t.context);
const processStderrWrite = process.stderr.write.bind(process.stderr);
t.context.stderrWrite = processStderrWrite;
process.stderr.write = wrapOutput(t.context);
2020-07-21 11:24:37 +01:00
// Many tests modify environment variables. Take a copy now so that
2020-07-21 12:19:37 +01:00
// we reset them after the test to keep tests independent of each other.
2020-07-21 11:24:37 +01:00
// process.env only has strings fields, so a shallow copy is fine.
t.context.env = {};
2020-07-21 12:19:37 +01:00
Object.assign(t.context.env, process.env);
2020-06-22 12:12:14 +01:00
});
2020-09-14 10:44:43 +01:00
typedTest.afterEach.always((t) => {
// Restore stdout and stderr
// The captured output is only replayed if the test failed
2020-06-23 14:33:20 +01:00
process.stdout.write = t.context.stdoutWrite;
process.stderr.write = t.context.stderrWrite;
if (!t.passed) {
process.stdout.write(t.context.testOutput);
}
// Undo any modifications made by sinon
2021-08-11 13:14:56 +01:00
sinon.restore();
2020-07-21 11:24:37 +01:00
// Undo any modifications to the env
process.env = t.context.env;
2020-07-06 16:04:02 +01:00
});
2020-06-22 12:12:14 +01:00
}
2020-07-06 16:04:02 +01:00
exports.setupTests = setupTests;
// Sets environment variables that make using some libraries designed for
// use only on actions safe to use outside of actions.
function setupActionsVars(tempDir, toolsDir) {
process.env["RUNNER_TEMP"] = tempDir;
process.env["RUNNER_TOOL_CACHE"] = toolsDir;
}
exports.setupActionsVars = setupActionsVars;
2020-06-22 12:12:14 +01:00
//# sourceMappingURL=testing-utils.js.map