UNPKG

8.24 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __rest = (this && this.__rest) || function (s, e) {
12 var t = {};
13 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14 t[p] = s[p];
15 if (s != null && typeof Object.getOwnPropertySymbols === "function")
16 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18 t[p[i]] = s[p[i]];
19 }
20 return t;
21};
22var __importDefault = (this && this.__importDefault) || function (mod) {
23 return (mod && mod.__esModule) ? mod : { "default": mod };
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.AllureCommandStepExecutable = void 0;
27const strip_ansi_1 = __importDefault(require("strip-ansi"));
28const model_js_1 = require("./model.js");
29class AllureCommandStepExecutable {
30 constructor(name) {
31 this.name = "";
32 this.attachments = [];
33 this.metadata = {};
34 this.name = name;
35 }
36 static toExecutableItem(runtime, stepMetadata) {
37 var _a, _b;
38 const executable = Object.assign(Object.assign({}, stepMetadata), { attachments: [], steps: [] });
39 if (((_a = stepMetadata.attachments) === null || _a === void 0 ? void 0 : _a.length) > 0) {
40 stepMetadata.attachments.forEach((attachment) => {
41 const attachmentContent = Buffer.from(attachment.content, attachment.encoding);
42 const attachmentFilename = runtime.writeAttachment(attachmentContent, attachment.type, attachment.encoding);
43 executable.attachments.push({
44 name: attachment.name,
45 type: attachment.type,
46 source: attachmentFilename,
47 });
48 });
49 }
50 if (((_b = stepMetadata.steps) === null || _b === void 0 ? void 0 : _b.length) > 0) {
51 executable.steps = stepMetadata.steps.map((nestedStep) => AllureCommandStepExecutable.toExecutableItem(runtime, nestedStep));
52 }
53 return executable;
54 }
55 label(label, value) {
56 if (!this.metadata.labels) {
57 this.metadata.labels = [];
58 }
59 this.metadata.labels.push({
60 name: label,
61 value,
62 });
63 }
64 link(url, name, type) {
65 if (!this.metadata.links) {
66 this.metadata.links = [];
67 }
68 this.metadata.links.push({
69 name,
70 url,
71 type,
72 });
73 }
74 parameter(name, value, options) {
75 if (!this.metadata.parameter) {
76 this.metadata.parameter = [];
77 }
78 this.metadata.parameter.push({
79 name,
80 value: JSON.stringify(value),
81 excluded: (options === null || options === void 0 ? void 0 : options.excluded) || false,
82 mode: options === null || options === void 0 ? void 0 : options.mode,
83 });
84 }
85 epic(epic) {
86 this.label(model_js_1.LabelName.EPIC, epic);
87 }
88 feature(feature) {
89 this.label(model_js_1.LabelName.FEATURE, feature);
90 }
91 story(story) {
92 this.label(model_js_1.LabelName.STORY, story);
93 }
94 suite(name) {
95 this.label(model_js_1.LabelName.SUITE, name);
96 }
97 parentSuite(name) {
98 this.label(model_js_1.LabelName.PARENT_SUITE, name);
99 }
100 subSuite(name) {
101 this.label(model_js_1.LabelName.SUB_SUITE, name);
102 }
103 owner(owner) {
104 this.label(model_js_1.LabelName.OWNER, owner);
105 }
106 severity(severity) {
107 this.label(model_js_1.LabelName.SEVERITY, severity);
108 }
109 tag(tag) {
110 this.label(model_js_1.LabelName.TAG, tag);
111 }
112 issue(name, url) {
113 this.link(url, name, model_js_1.LinkType.ISSUE);
114 }
115 tms(name, url) {
116 this.link(url, name, model_js_1.LinkType.TMS);
117 }
118 attach(content, type) {
119 const isBuffer = Buffer.isBuffer(content);
120 this.attachments.push({
121 name: "attachment",
122 content: isBuffer ? content.toString("base64") : content,
123 encoding: isBuffer ? "base64" : "utf8",
124 type,
125 });
126 }
127 description(content) {
128 this.metadata.description = content;
129 }
130 step(name, body) {
131 return __awaiter(this, void 0, void 0, function* () {
132 if (!this.metadata.steps) {
133 this.metadata.steps = [];
134 }
135 const nestedStep = new AllureCommandStepExecutable(name);
136 yield nestedStep.run(body, ({ labels = [], links = [], parameter = [], steps = [] }) => __awaiter(this, void 0, void 0, function* () {
137 this.metadata.labels = (this.metadata.labels || []).concat(labels);
138 this.metadata.links = (this.metadata.links || []).concat(links);
139 this.metadata.parameter = (this.metadata.parameter || []).concat(parameter);
140 this.metadata.steps = (this.metadata.steps || []).concat(steps);
141 }));
142 });
143 }
144 start(body) {
145 return __awaiter(this, void 0, void 0, function* () {
146 return yield new Promise((resolve) => this.run(body, (result) => __awaiter(this, void 0, void 0, function* () { return resolve(result); })));
147 });
148 }
149 run(body, messageEmitter) {
150 return __awaiter(this, void 0, void 0, function* () {
151 const startDate = new Date().getTime();
152 try {
153 yield body.call(this, this);
154 const _a = this.metadata, { steps = [], description = "", descriptionHtml = "" } = _a, metadata = __rest(_a, ["steps", "description", "descriptionHtml"]);
155 yield messageEmitter(Object.assign(Object.assign({}, metadata), { steps: [
156 {
157 name: this.name,
158 start: startDate,
159 stop: new Date().getTime(),
160 stage: model_js_1.Stage.FINISHED,
161 status: model_js_1.Status.PASSED,
162 statusDetails: {},
163 attachments: this.attachments,
164 parameters: [],
165 steps,
166 description,
167 },
168 ] }));
169 }
170 catch (e) {
171 const _b = this.metadata, { steps = [], description = "", descriptionHtml = "" } = _b, metadata = __rest(_b, ["steps", "description", "descriptionHtml"]);
172 yield messageEmitter(Object.assign(Object.assign({}, metadata), { steps: [
173 {
174 name: this.name,
175 start: startDate,
176 stop: new Date().getTime(),
177 stage: model_js_1.Stage.FINISHED,
178 status: model_js_1.Status.BROKEN,
179 statusDetails: {
180 message: (0, strip_ansi_1.default)((e.message || "")),
181 trace: (0, strip_ansi_1.default)((e.stack || "")),
182 },
183 attachments: this.attachments,
184 parameters: [],
185 steps,
186 description,
187 },
188 ] }));
189 throw e;
190 }
191 });
192 }
193}
194exports.AllureCommandStepExecutable = AllureCommandStepExecutable;
195//# sourceMappingURL=AllureCommandStep.js.map
\No newline at end of file