Innovenergy_trunk/frontend/node_modules/testcafe/lib/test-run/execute-js-expression/index.js

86 lines
12 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeAsyncJsExpression = exports.executeJsExpression = void 0;
const vm_1 = require("vm");
const runtime_1 = require("../../errors/runtime");
const test_run_1 = require("../../errors/test-run");
const execution_context_1 = require("../../api/test-controller/execution-context");
const constants_1 = require("./constants");
const test_run_tracker_1 = __importDefault(require("../../api/test-run-tracker"));
// NOTE: do not beautify this code since offsets for error lines and columns are coded here
function wrapInAsync(expression, testRunId) {
return `(async function ${test_run_tracker_1.default.getMarkedFnName(testRunId)} () {\n` +
expression + ';\n' +
'});';
}
function getErrorLineColumn(err) {
if (err.isTestCafeError) {
if (!err.callsite)
return {};
if (err.callsite.id)
return { line: 0, column: 0 };
const stackFrames = err.callsite.stackFrames || [];
const frameIndex = err.callsite.callsiteFrameIdx;
const stackFrame = stackFrames[frameIndex];
return stackFrame ? {
line: stackFrame.getLineNumber(),
column: stackFrame.getColumnNumber(),
} : {};
}
const result = err.stack && err.stack.match(constants_1.ERROR_LINE_COLUMN_REGEXP);
if (!result)
return {};
const line = result[1] ? parseInt(result[1], 10) : void 0;
const column = result[2] ? parseInt(result[2], 10) : void 0;
return { line, column };
}
function createErrorFormattingOptions() {
return {
filename: constants_1.ERROR_FILENAME,
lineOffset: constants_1.ERROR_LINE_OFFSET,
};
}
function getExecutionContext(testController, options = execution_context_1.DEFAULT_CONTEXT_OPTIONS) {
const context = testController.getExecutionContext();
// TODO: Find a way to avoid this assignment
(0, execution_context_1.setContextOptions)(context, options);
return context;
}
function isRuntimeError(err) {
return err instanceof runtime_1.GeneralError ||
err instanceof runtime_1.TestCompilationError ||
err instanceof runtime_1.APIError ||
err instanceof runtime_1.CompositeError;
}
function executeJsExpression(expression, testRun, options) {
const context = getExecutionContext(testRun.controller, options);
const errorOptions = createErrorFormattingOptions();
return (0, vm_1.runInContext)(expression, context, errorOptions);
}
exports.executeJsExpression = executeJsExpression;
async function executeAsyncJsExpression(expression, testRun, callsite, onBeforeRaisingError) {
if (!expression || !expression.length)
return Promise.resolve();
const context = getExecutionContext(testRun.controller);
const errorOptions = createErrorFormattingOptions(expression);
const wrappedExpression = wrapInAsync(expression, testRun.id);
try {
return await (0, vm_1.runInContext)(wrappedExpression, context, errorOptions)();
}
catch (err) {
const { line, column } = getErrorLineColumn(err);
let resultError = null;
if (err.isTestCafeError || isRuntimeError(err))
resultError = new test_run_1.UncaughtTestCafeErrorInCustomScript(err, expression, line, column, callsite);
else
resultError = new test_run_1.UncaughtErrorInCustomScript(err, expression, line, column, callsite);
if (onBeforeRaisingError)
await onBeforeRaisingError(resultError);
throw resultError;
}
}
exports.executeAsyncJsExpression = executeAsyncJsExpression;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdGVzdC1ydW4vZXhlY3V0ZS1qcy1leHByZXNzaW9uL2luZGV4LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7OztBQUFBLDJCQUFrQztBQUVsQyxrREFLOEI7QUFFOUIsb0RBQXlHO0FBQ3pHLG1GQUF5RztBQUV6RywyQ0FJcUI7QUFFckIsa0ZBQXdEO0FBRXhELDJGQUEyRjtBQUMzRixTQUFTLFdBQVcsQ0FBRSxVQUFVLEVBQUUsU0FBUztJQUN2QyxPQUFPLG1CQUFtQiwwQkFBYyxDQUFDLGVBQWUsQ0FBQyxTQUFTLENBQUMsU0FBUztRQUNyRSxVQUFVLEdBQUcsS0FBSztRQUNsQixLQUFLLENBQUM7QUFDakIsQ0FBQztBQUVELFNBQVMsa0JBQWtCLENBQUUsR0FBRztJQUM1QixJQUFJLEdBQUcsQ0FBQyxlQUFlLEVBQUU7UUFDckIs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