UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

186 lines 7.64 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; 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); }) : (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 = {}; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlyStep = exports.getStepId = void 0; const util = __importStar(require("./util")); const factory_1 = require("./exec/factory"); const impl_1 = require("./exec/impl"); function getStepId(step) { return step.subflow ? `${step.subflow.id}-${step.step.id}` : step.step.id; } exports.getStepId = getStepId; class PlyStep { constructor(step, requestSuite, logger, flow, flowInstance, subflow) { this.step = step; this.requestSuite = requestSuite; this.logger = logger; this.flow = flow; this.flowInstance = flowInstance; this.subflow = subflow; this.type = 'flow'; this.name = getStepId(this); this.stepName = step.name.replace(/\r?\n/g, ' '); this.instance = { id: util.genId(), flowInstanceId: this.flowInstance.id, stepId: step.id, status: 'In Progress' }; } async run(runtime, values, runOptions, runNum, instNum = 0) { var _a; this.instance.start = new Date(); let result; let stepRes; const level = this.subflow ? 1 : 0; const createExpected = runOptions === null || runOptions === void 0 ? void 0 : runOptions.createExpected; let key = this.stepName; if (instNum) key += `_${instNum}`; const resOpts = { level: 0, withExpected: createExpected, subflow: (_a = this.subflow) === null || _a === void 0 ? void 0 : _a.name }; try { runtime.appendResult(`${key}:`, { ...resOpts, level, comment: util.timestamp(this.instance.start) }); runtime.appendResult(`id: ${this.step.id}`, { ...resOpts, level: level + 1 }); const context = new impl_1.ContextImpl({ name: this.name, runtime, flow: this.flow, flowInstance: this.flowInstance, subflow: this.subflow, step: this.step, stepInstance: this.instance, logger: this.logger, values, runOptions, requestSuite: this.requestSuite, runNum, instNum }); const exec = await factory_1.ExecFactory.create(context); if (this.step.path === 'start' && this.subflow && !(runOptions === null || runOptions === void 0 ? void 0 : runOptions.submit) && !createExpected) { await this.requestSuite.runtime.padActualStart(this.subflow.id, instNum); } if (!(runOptions === null || runOptions === void 0 ? void 0 : runOptions.trusted)) { let trustRequired = true; const trustFun = exec.isTrustRequired; if (typeof trustFun === 'function') { trustRequired = trustFun(context); } if (trustRequired) { throw new Error(`Trusted context required for ${this.step.id}: ${this.stepName}`); } } const execResult = await exec.run(context); if (this.step.path !== 'start' && this.step.path !== 'stop' && !this.step.path.endsWith('request')) { this.instance.status = this.mapToInstanceStatus(execResult); if (execResult.message) this.instance.message = execResult.message; } if (!execResult.message && this.instance.message) { execResult.message = this.instance.message; } this.instance.end = new Date(); if (!(runOptions === null || runOptions === void 0 ? void 0 : runOptions.submit) && !createExpected) { await this.requestSuite.runtime.padActualStart(this.name, instNum); } result = { name: this.stepName, status: execResult.status, message: execResult.message || '' }; if (execResult.data && !this.step.path.endsWith('request')) { result.data = execResult.data; this.instance.data = execResult.data; } if (execResult.diffs) result.diffs = execResult.diffs; } catch (err) { this.logger.error(err.message, err); this.instance.status = 'Errored'; this.instance.message = err.message; result = { name: this.stepName, status: 'Errored', message: this.instance.message || '' }; } if (this.instance.data && !this.step.path.endsWith('request')) { const dataStr = typeof this.instance.data === 'string' ? this.instance.data : JSON.stringify(this.instance.data, null, runtime.options.prettyIndent); runtime.updateResult(key, 'data: |', { ...resOpts, level: level + 1 }); for (const line of util.lines(dataStr)) { runtime.updateResult(key, line, { ...resOpts, level: level + 2 }); } } // append status, result and message to actual result if (this.instance.end) { const elapsed = this.instance.end.getTime() - this.instance.start.getTime(); runtime.updateResult(key, `status: ${this.instance.status}`, { ...resOpts, level: level + 1, comment: `${elapsed} ms` }); if (typeof stepRes === 'boolean' || typeof stepRes === 'number' || stepRes) { this.instance.result = '' + stepRes; runtime.updateResult(key, `result: ${this.instance.result}`, { ...resOpts, level: level + 1 }); } if (this.instance.message) { runtime.updateResult(key, `message: '${this.instance.message}'`, { ...resOpts, level: level + 1 }); } } return result; } mapToInstanceStatus(execResult) { if (execResult.status === 'Passed' || execResult.status === 'Submitted') { return 'Completed'; } else { return execResult.status; } } } exports.PlyStep = PlyStep; //# sourceMappingURL=step.js.map