UNPKG

@magic-num/fileclient

Version:
158 lines (130 loc) 4.08 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _core = require("@magic-num/core"); var _fs = _interopRequireDefault(require("fs")); var _process = _interopRequireDefault(require("process")); var _path = _interopRequireDefault(require("path")); var _jsonfile = _interopRequireDefault(require("jsonfile")); /** * 请求处理 */ var Handler = /*#__PURE__*/ function () { function Handler(uri) { (0, _classCallCheck2.default)(this, Handler); this.id = uri; this.uri = new RegExp("^".concat(uri, "$")); this.tcs = []; } /** * 增加一个TestCase * @param {*} tc */ (0, _createClass2.default)(Handler, [{ key: "addTestCase", value: function addTestCase(tc) { if (tc instanceof _core.TestCase) { this.tcs.push(tc); } else { throw new Error('Only Testcase instance is allowed'); } } }, { key: "filterTestcase", value: function filterTestcase() { var _this = this; var filter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () { return true; }; var updateCounter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var tcs = this.tcs.filter(filter); if (updateCounter) { tcs.forEach(function (tc) { return _this.updateCounter(tc); }); } return tcs; } }, { key: "filter", value: function filter(params, conf) { var _params$__tcid__ = params.__tcid__, __tcid__ = _params$__tcid__ === void 0 ? null : _params$__tcid__; var _conf$accept = conf.accept, accept = _conf$accept === void 0 ? null : _conf$accept; var flt = null; if (accept) { flt = function flt(tc) { return accept.call(tc, params, conf); }; } else { if (__tcid__) { flt = function flt(tc) { return tc.id === __tcid__; }; } else { flt = function flt(tc) { return tc.accept(params, conf); }; } } var tcs = this.tcs.filter(flt); if (tcs.length === 1) return tcs[0]; if (tcs.length > 1) { console.warn('超过1个匹配结果'); return tcs[0]; } throw new Error('找不到对应的testcase'); } }, { key: "updateCounter", value: function updateCounter(tc) { try { var fpath = _path.default.join(_process.default.cwd(), 'testcase-stat.json'); var stat = _fs.default.readFileSync(fpath); var json = JSON.parse(stat); if (json[this.id] && json[this.id][tc.id]) { json[this.id][tc.id].counter++; _jsonfile.default.writeFileSync(fpath, json, { spaces: 2, EOL: '\r\n' }); console.info("update stat info ".concat(tc.id, ": ").concat(json[this.id][tc.id].counter)); } } catch (e) { console.error(e); } } }, { key: "execute", value: function execute() { var conf = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var _conf$params = conf.params, params = _conf$params === void 0 ? {} : _conf$params, _conf$__coverage__ = conf.__coverage__, __coverage__ = _conf$__coverage__ === void 0 ? false : _conf$__coverage__; try { var tc = this.filter(params, conf); tc.counter++; var data = tc.produce(params, conf); if (__coverage__) this.updateCounter(tc); return data; } catch (e) { return { success: false, error: e }; } } }]); return Handler; }(); var _default = Handler; exports.default = _default;