UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

111 lines 4.74 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.RequestLoader = void 0; const suite_1 = require("./suite"); const retrieval_1 = require("./retrieval"); const request_1 = require("./request"); const runtime_1 = require("./runtime"); const result_1 = require("./result"); const log_1 = require("./log"); const logger_1 = require("./logger"); const skip_1 = require("./skip"); const yaml = __importStar(require("./yaml")); const util = __importStar(require("./util")); class RequestLoader { constructor(locations, options, logger) { this.locations = locations; this.options = options; this.logger = logger; if (options.skip) { this.skip = new skip_1.Skip(options.skip); } } async load() { const retrievals = this.locations.map((loc) => new retrieval_1.Retrieval(loc)); // load request files in parallel const promises = retrievals.map((retr) => this.loadSuite(retr)); const suites = await Promise.all(promises); suites.sort((s1, s2) => s1.name.localeCompare(s2.name)); return suites; } sync() { const suites = []; for (const location of this.locations) { suites.push(this.syncSuite(new retrieval_1.Retrieval(location))); } suites.sort((s1, s2) => s1.name.localeCompare(s2.name)); return suites; } async loadSuite(retrieval) { const contents = await retrieval.read(); if (typeof contents === 'undefined') { throw new Error('Cannot retrieve: ' + retrieval.location.absolute); } const resultPaths = await result_1.ResultPaths.create(this.options, retrieval); return this.buildSuite(retrieval, contents, resultPaths); } syncSuite(retrieval) { const contents = retrieval.sync(); if (typeof contents === 'undefined') { throw new Error('Cannot retrieve: ' + retrieval.location.absolute); } const resultPaths = result_1.ResultPaths.createSync(this.options, retrieval); return this.buildSuite(retrieval, contents, resultPaths); } buildSuite(retrieval, contents, resultPaths) { var _a; const runtime = new runtime_1.Runtime(this.options, retrieval, resultPaths); const logger = this.logger || new logger_1.Logger({ level: this.options.verbose ? log_1.LogLevel.debug : this.options.quiet ? log_1.LogLevel.error : log_1.LogLevel.info, prettyIndent: this.options.prettyIndent }, runtime.results.log); const suite = new suite_1.Suite(retrieval.location.base, 'request', retrieval.location.relativeTo(this.options.testsLocation), runtime, logger, 0, util.lines(contents).length - 1); const obj = yaml.load(retrieval.location.path, contents, true); if (obj) { for (const key of Object.keys(obj)) { const val = obj[key]; if (typeof val === 'object') { const startEnd = { start: val.__start, end: val.__end }; const { __start, __end, ...cleanObj } = val; const request = new request_1.PlyRequest(key, { ...startEnd, ...cleanObj }, logger, retrieval); suite.add(request); } } } // mark if skipped if ((_a = this.skip) === null || _a === void 0 ? void 0 : _a.isSkipped(suite.path)) { suite.skip = true; } return suite; } } exports.RequestLoader = RequestLoader; //# sourceMappingURL=requests.js.map