UNPKG

@intuitionrobotics/testelot

Version:
210 lines 7.93 kB
"use strict"; /* * Testelot is a typescript scenario composing framework * * Copyright (C) 2020 Intuition Robotics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Action = exports.Status = void 0; /** * Created by IR on 3/18/17. */ const ts_common_1 = require("@intuitionrobotics/ts-common"); const TestException_1 = require("./TestException"); var Status; (function (Status) { Status["Ready"] = "Ready"; Status["Running"] = "Running"; Status["Skipped"] = "Skipped"; Status["Success"] = "Success"; Status["Error"] = "Error"; })(Status || (exports.Status = Status = {})); class Action extends ts_common_1.Logger { constructor(actionType, tag) { super(tag || "Testelot"); this.uuid = (0, ts_common_1.generateUUID)(); this.label = "Unnamed Action"; this.policy = 1 /* ErrorPolicy.SkipOnError */; this.postExecutionDelay = 0; this.status = Status.Ready; this.actionType = actionType.name; } static resolveTestsToRun() { const strings = process.argv.filter((arg) => arg.includes("--test=")); console.log(`raw: ${(0, ts_common_1.__stringify)(strings)}`); this.testsToRun = strings.map(arg => arg.replace("--test=", "")); console.log(`Tests to run: ${(0, ts_common_1.__stringify)(this.testsToRun)}`); } expectToFail(_exceptionType, assertFailCondition) { this.shouldFailCondition = (e) => { const err = (0, ts_common_1.isErrorOfType)(e, _exceptionType); if (!err) throw new TestException_1.TestException(`Test should have failed with an: ${_exceptionType.name}`); return !assertFailCondition ? true : assertFailCondition(err); }; return this; } processReturnValue(returnValueProcessor) { this.assertFailCondition = returnValueProcessor; return this; } isContainer() { return false; } hasParent() { return !!this.parent; } setErrorPolicy(policy) { this.policy = policy; return this; } setParent(parent) { this.parent = parent; } setReporter(reporter) { this.reporter = reporter; } setWriteKey(writeKey) { this.writeKey = writeKey; return this; } setReadKey(readKey) { this.readKey = readKey; return this; } setPostExecutionDelay(postExecutionDelay) { this.postExecutionDelay = postExecutionDelay; return this; } setLabel(label) { this.label = label; return this; } getStarted() { return this._started; } getEnded() { return this._ended; } _executeSubAction(action) { return __awaiter(this, void 0, void 0, function* () { action.setParent(this); action.setReporter(this.reporter); yield action._execute(); }); } resolveLabel(param) { if (!this.label) return; return typeof this.label === "string" ? this.label : this.label(param); } _execute() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; this._started = (0, ts_common_1.currentTimeMillies)(); let label; let err; let retValue = undefined; try { let param; if (this.readKey) param = this.get(this.readKey); label = this.resolveLabel(param); if (Action.testsToRun.length > 0 && !Action.testsToRun.find(testToRun => (label || "").includes(testToRun))) this.setStatus(Status.Skipped); else this.setStatus(Status.Ready); if (this.status === Status.Skipped) { if (this.isContainer()) // @ts-ignore yield this.execute(); label && this.reporter.logVerbose(`skipped: ${label}`); this.reporter.onActionEnded(this); this._ended = (0, ts_common_1.currentTimeMillies)(); return; } if (this.isContainer()) label && this.reporter.logVerbose(`+ ${label}`); else { label && this.reporter.logVerbose(`Running: ${label}`); if (this.readKey) this.reporter.logDebug(`Using context: ${this.readKey.key}`); } this.reporter.onActionStarted(this); this.setStatus(Status.Running); retValue = yield this.execute((param || ts_common_1.Void)); } catch (e) { err = ((_a = this.shouldFailCondition) === null || _a === void 0 ? void 0 : _a.call(this, e)) ? undefined : e; } finally { if (this.status !== Status.Skipped) { this.setStatus(err ? Status.Error : Status.Success); this.reporter.onActionEnded(this); if (err) { label && this.reporter.logError(`Error in Action: ${label}`); this.reporter.logError(err); } else { // only set the ret value if we expect a success... if (this.writeKey && !this.shouldFailCondition) { this.set(this.writeKey, retValue); } (_b = this.assertFailCondition) === null || _b === void 0 ? void 0 : _b.call(this, retValue); // label && this.reporter.logVerbose(`ended: ${label}`); if (this.isContainer()) label && this.reporter.logVerbose(`- ${label}`); } } } if (this.postExecutionDelay > 0) yield (0, ts_common_1.timeout)(this.postExecutionDelay); this._ended = (0, ts_common_1.currentTimeMillies)(); }); } setStatus(status) { this.status = status; } get(key) { if (!this.parent) return key.defaultValue; return this.parent.get(key); } remove(key) { if (!this.parent) return false; return this.parent.remove(key); } set(key, value) { if (!this.parent) return; return this.parent.set(key, value); } reset() { // } } exports.Action = Action; Action.testsToRun = []; //# sourceMappingURL=Action.js.map