UNPKG

8.55 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};
11Object.defineProperty(exports, "__esModule", { value: true });
12const flagpoleexecutionoptions_1 = require("../flagpoleexecutionoptions");
13const flagpolereport_1 = require("../logging/flagpolereport");
14const util_1 = require("../util");
15const flagpole_1 = require("../flagpole");
16const child_process_1 = require("child_process");
17var SuiteExecutionExitCode;
18(function (SuiteExecutionExitCode) {
19 SuiteExecutionExitCode[SuiteExecutionExitCode["success"] = 0] = "success";
20 SuiteExecutionExitCode[SuiteExecutionExitCode["failure"] = 1] = "failure";
21})(SuiteExecutionExitCode = exports.SuiteExecutionExitCode || (exports.SuiteExecutionExitCode = {}));
22class SuiteExecutionResult {
23 constructor(output, exitCode) {
24 this._output = output;
25 this._exitCode = exitCode;
26 }
27 get output() {
28 return this._output;
29 }
30 get exitCode() {
31 return this._exitCode;
32 }
33 toString() {
34 return this._output.join("\n");
35 }
36}
37exports.SuiteExecutionResult = SuiteExecutionResult;
38class SuiteExecution {
39 constructor() {
40 this._result = null;
41 this._started = null;
42 this._finished = null;
43 this._subscribers = [];
44 this._finally = [];
45 this._output = [];
46 this._finishedResolver = () => { };
47 this._finishedPromise = new Promise(resolve => {
48 this._finishedResolver = resolve;
49 });
50 }
51 get result() {
52 return this._finishedPromise;
53 }
54 get exitCode() {
55 return this._result === null ? null : this._result.exitCode;
56 }
57 get output() {
58 return this._output;
59 }
60 static executePath(filePath, opts) {
61 const execution = new SuiteExecution();
62 execution.executePath(filePath, opts);
63 return execution;
64 }
65 static executeSuite(config) {
66 const execution = new SuiteExecution();
67 execution.executeSuite(config);
68 return execution;
69 }
70 subscribe(callback) {
71 this._subscribers.push(callback);
72 }
73 finally(callback) {
74 this._finally.push(callback);
75 }
76 toString() {
77 return this._output.join("\n");
78 }
79 executePath(filePath, opts) {
80 return __awaiter(this, void 0, void 0, function* () {
81 if (this._result !== null || this._started !== null) {
82 throw new Error(`This execution has already run.`);
83 }
84 this._started = Date.now();
85 this._result = yield this._execute(filePath, opts);
86 this._finished = Date.now();
87 this._finally.forEach((callback) => {
88 callback.apply(this, [this]);
89 });
90 this._finishedResolver(this._result);
91 return this._result;
92 });
93 }
94 executeSuite(config) {
95 return __awaiter(this, void 0, void 0, function* () {
96 return this.executePath(config.getTestPath(), flagpoleexecutionoptions_1.FlagpoleExecution.opts);
97 });
98 }
99 _execute(filePath, opts) {
100 return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
101 opts.exitOnDone = true;
102 opts.isChildProcess = true;
103 this._executeWithSpawn(filePath, opts, resolve);
104 }));
105 }
106 _executeWithExec(filePath, opts, resolve) {
107 const command = `node ${filePath} ${opts.toString()}`;
108 child_process_1.exec(command, (err, stdout, stderr) => {
109 const exitCode = err && err.code ? err.code : 0;
110 if (err) {
111 this._logLine("FAILED TEST SUITE:");
112 this._logLine(filePath + " exited with error code " + exitCode);
113 this._logLine("\n");
114 if (stderr) {
115 this._logLine(stderr);
116 }
117 }
118 else {
119 this._logLine(stdout);
120 }
121 resolve(new SuiteExecutionResult(this._output, exitCode));
122 });
123 }
124 _executeWithSpawn(filePath, opts, resolve) {
125 const command = [filePath].concat(opts.toArgs());
126 const proc = child_process_1.spawn("node", command);
127 proc.stdout.on("data", data => {
128 this._logLine(data);
129 });
130 proc.on("close", exitCode => {
131 if (exitCode > 0 && opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.console) {
132 this._logLine("FAILED TEST SUITE:");
133 this._logLine(filePath + " exited with error code " + exitCode);
134 this._logLine("\n");
135 }
136 resolve(new SuiteExecutionResult(this._output, exitCode));
137 });
138 }
139 _executeWithFork(filePath, opts, resolve) {
140 const options = {
141 stdio: ["pipe", "pipe", "pipe", "ipc"]
142 };
143 const proc = child_process_1.fork(filePath, opts.toArgs(), options);
144 proc.on("exit", exitCode => {
145 if ((exitCode == null || exitCode !== 0) &&
146 opts.output == flagpoleexecutionoptions_1.FlagpoleOutput.console) {
147 this._logLine("FAILED TEST SUITE:");
148 this._logLine(filePath + " exited with error code " + exitCode);
149 this._logLine("\n");
150 }
151 resolve(new SuiteExecutionResult(this._output, exitCode === null ? -1 : exitCode));
152 });
153 proc.on("message", msg => {
154 console.log("child message received!");
155 this._logLine(msg);
156 });
157 }
158 _logLine(data) {
159 if (data) {
160 const lines = String(data)
161 .trim()
162 .split("\n");
163 lines.forEach(line => {
164 this._output.push(line);
165 this._subscribers.forEach((callback) => {
166 callback.apply(this, [line, this]);
167 });
168 });
169 }
170 }
171}
172exports.SuiteExecution = SuiteExecution;
173class SuiteExecutionInline extends SuiteExecution {
174 static executePath(filePath, opts) {
175 const execution = new SuiteExecutionInline();
176 execution.executePath(filePath, opts);
177 return execution;
178 }
179 static executeSuite(config) {
180 const execution = new SuiteExecutionInline();
181 execution.executeSuite(config);
182 return execution;
183 }
184 _execute(filePath, opts) {
185 return __awaiter(this, void 0, void 0, function* () {
186 let exitCode = SuiteExecutionExitCode.success;
187 opts = Object.assign({}, opts);
188 opts.automaticallyPrintToConsole = false;
189 const globalOpts = Object.assign({}, flagpoleexecutionoptions_1.FlagpoleExecution.opts);
190 flagpoleexecutionoptions_1.FlagpoleExecution.opts = opts;
191 const preSuiteCount = flagpole_1.Flagpole.suites.length;
192 yield require(`${filePath}`);
193 const postSuiteCount = flagpole_1.Flagpole.suites.length;
194 if (postSuiteCount > preSuiteCount) {
195 const createdSuites = flagpole_1.Flagpole.suites.slice(preSuiteCount);
196 let promises = [];
197 createdSuites.forEach((suite) => {
198 promises.push(suite.finished);
199 });
200 yield Promise.all(promises);
201 yield util_1.asyncForEach(createdSuites, (suite) => __awaiter(this, void 0, void 0, function* () {
202 if (suite.hasFailed) {
203 exitCode = SuiteExecutionExitCode.failure;
204 }
205 const report = new flagpolereport_1.FlagpoleReport(suite, opts);
206 this._logLine(yield report.toString());
207 }));
208 }
209 flagpoleexecutionoptions_1.FlagpoleExecution.opts = globalOpts;
210 return new SuiteExecutionResult(this._output, exitCode);
211 });
212 }
213}
214exports.SuiteExecutionInline = SuiteExecutionInline;
215//# sourceMappingURL=suiteexecution.js.map
\No newline at end of file