UNPKG

@ply-ct/ply

Version:

REST API Automated Testing

191 lines 7.26 kB
#!/usr/bin/env node "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 }); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const glob = __importStar(require("glob")); const tsNode = __importStar(require("ts-node")); const ply_1 = require("./ply"); const options_1 = require("./options"); const location_1 = require("./location"); const storage_1 = require("./storage"); const retrieval_1 = require("./retrieval"); const import_1 = require("./import/import"); const plyex_1 = require("./plyex/plyex"); const yaml = __importStar(require("./yaml")); const report_1 = require("./report/report"); const start = Date.now(); tsNode.register({ transpileOnly: true }); const opts = new options_1.Config(new options_1.Defaults(), true).options; const { runOptions, ...options } = opts; const plier = new ply_1.Plier(options); if (runOptions === null || runOptions === void 0 ? void 0 : runOptions.import) { plier.logger.debug('Options', options); const importer = new import_1.Import(runOptions.import, plier.logger); try { let valuesLoc = `${options.testsLocation}/values`; if (options.valuesFiles) { const firstEnabledValFile = Object.keys(options.valuesFiles).find((vf) => { return options.valuesFiles[vf]; }); if (firstEnabledValFile) valuesLoc = path.dirname(firstEnabledValFile); } const opts = { testsLocation: options.testsLocation, valuesLocation: valuesLoc, indent: options.prettyIndent, importToSuite: runOptions.importToSuite }; for (const path of options.args) { plier.logger.info('Importing', path); importer.doImport(new retrieval_1.Retrieval(path), opts); } } catch (err) { plier.logger.error(`${err}`, err); process.exit(1); } } else if (runOptions === null || runOptions === void 0 ? void 0 : runOptions.report) { plier.logger.debug('Options', options); const format = runOptions.report; const factory = new report_1.ReporterFactory(format); factory .createReporter() .then((reporter) => { reporter.report({ format: factory.format, output: opts.outputFile || `${opts.logLocation}/ply-runs.${factory.format}`, runsLocation: `${opts.logLocation}/runs`, logger: plier.logger, indent: opts.prettyIndent }); }) .catch((err) => { plier.logger.error(err.message, err); process.exit(1); }); } else if (runOptions === null || runOptions === void 0 ? void 0 : runOptions.openapi) { plier.logger.debug('Options', options); const plyex = new plyex_1.Plyex(runOptions.openapi, plier.logger); for (const path of options.args) { const contents = fs.readFileSync(path, { encoding: 'utf8' }); plier.logger.info('Overwriting', path); const isYaml = !contents.startsWith('{'); const openApi = isYaml ? yaml.load(path, contents) : JSON.parse(contents); plyex .augment(openApi) .then((augmented) => { let updated; if (isYaml) updated = yaml.dump(augmented, options.prettyIndent); else updated = JSON.stringify(augmented, null, options.prettyIndent); fs.writeFileSync(path, updated, { encoding: 'utf8' }); }) .catch(console.error); } } else { let paths = []; let args = options.args; const globOptions = { cwd: options.testsLocation, ignore: options.ignore }; if (args && args.length > 0) { // ignore skip unless passed on command-line since tests specified if (!options.skip) plier.options.skip = ''; // make arg paths relative to tests loc if (options.testsLocation !== '.') { args = args.map((arg) => { const argLoc = new location_1.Location(arg); if (argLoc.isChildOf(options.testsLocation)) { return argLoc.relativeTo(options.testsLocation); } else { plier.logger.error(`WARNING: ${arg} is not under testsLocation ${options.testsLocation}`); } return arg; }); } for (const arg of args) { const hash = arg.indexOf('#'); if (hash > 0) { paths.push(arg); } else { // treat as glob pattern for (const file of glob.sync(arg, globOptions)) { paths.push(file); } if (paths.length === 0) { throw new Error(`Test files(s) not found: ${args}`); } } } } else { paths = [ ...glob.sync(options.requestFiles, globOptions), ...glob.sync(options.caseFiles, globOptions), ...glob.sync(options.flowFiles, globOptions) ]; } paths = paths.map((p) => { return path.isAbsolute(p) ? p : options.testsLocation + path.sep + p; }); plier .find(paths) .then((plyees) => { plier.logger.debug('Plyees', plyees); plier .run(plyees, runOptions) .then((results) => { plier.logger.error('\nOverall Results: ' + JSON.stringify(results)); plier.logger.error(`Overall Time: ${Date.now() - start} ms`); // json reporter overrides overall outputFile if (plier.options.outputFile && plier.options.reporter !== 'json') { new storage_1.Storage(plier.options.outputFile).write(JSON.stringify(results, null, plier.options.prettyIndent)); } if (results.Failed || results.Errored) { process.exit(1); } }) .catch((err) => { plier.logger.error(err); process.exit(1); }); }) .catch((err) => { plier.logger.error(err); process.exit(1); }); } //# sourceMappingURL=cli.js.map