flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
185 lines • 7.02 kB
JavaScript
"use strict";
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.SuiteExecution = exports.SuiteExecutionResult = exports.SuiteExecutionExitCode = void 0;
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const flagpole_execution_1 = require("../flagpole-execution");
var SuiteExecutionExitCode;
(function (SuiteExecutionExitCode) {
SuiteExecutionExitCode[SuiteExecutionExitCode["success"] = 0] = "success";
SuiteExecutionExitCode[SuiteExecutionExitCode["failure"] = 1] = "failure";
})(SuiteExecutionExitCode = exports.SuiteExecutionExitCode || (exports.SuiteExecutionExitCode = {}));
class SuiteExecutionResult {
constructor(output, exitCode) {
this._output = output;
this._exitCode = exitCode;
}
get output() {
return this._output;
}
get exitCode() {
return this._exitCode;
}
toString() {
return this._output.join("\n");
}
}
exports.SuiteExecutionResult = SuiteExecutionResult;
class SuiteExecution {
constructor() {
this._result = null;
this._started = null;
this._finished = null;
this._subscribers = [];
this._finally = [];
this._output = [];
this._finishedResolver = () => { };
this._finishedPromise = new Promise((resolve) => {
this._finishedResolver = resolve;
});
}
get result() {
return this._finishedPromise;
}
get exitCode() {
return this._result === null ? null : this._result.exitCode;
}
get output() {
return this._output;
}
static executePath(filePath) {
const execution = new SuiteExecution();
execution.executePath(filePath);
return execution;
}
static executeSuite(config) {
const execution = new SuiteExecution();
execution.executeSuite(config);
return execution;
}
subscribe(callback) {
this._subscribers.push(callback);
}
finally(callback) {
this._finally.push(callback);
}
toString() {
return this._output.join("\n");
}
executePath(filePath) {
return __awaiter(this, void 0, void 0, function* () {
if (this._result !== null || this._started !== null) {
throw new Error(`This execution has already run.`);
}
this._started = Date.now();
if (fs_1.existsSync(filePath)) {
this._result = yield this._execute(filePath);
}
else {
this._result = new SuiteExecutionResult(["Suite was not found in the output folder. Did you forget to build?"], 1);
}
this._finished = Date.now();
this._finally.forEach((callback) => {
callback.apply(this, [this]);
});
this._finishedResolver(this._result);
return this._result;
});
}
executeSuite(suite) {
return __awaiter(this, void 0, void 0, function* () {
return this.executePath(suite.getTestPath());
});
}
_execute(filePath) {
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
const opts = flagpole_execution_1.FlagpoleExecution.global.clone({
exitOnDone: true,
isChildProcess: true,
automaticallyPrintToConsole: true,
});
this._executeWithSpawn(filePath, opts, resolve);
}));
}
_executeWithExec(filePath, opts, resolve) {
const command = `node ${filePath} ${opts.toString()}`;
child_process_1.exec(command, (err, stdout, stderr) => {
const exitCode = err && err.code ? err.code : 0;
if (err) {
this._logLine("FAILED TEST SUITE:");
this._logLine(filePath + " exited with error code " + exitCode);
this._logLine("\n");
if (stderr) {
this._logLine(stderr);
}
}
else {
this._logLine(stdout);
}
resolve(new SuiteExecutionResult(this._output, exitCode));
});
}
_executeWithSpawn(filePath, opts, resolve) {
const command = [filePath].concat(opts.getOptionsArray());
const proc = child_process_1.spawn("node", command);
proc.stdout.on("data", (data) => {
this._logLine(data);
});
proc.stderr.on("data", (data) => {
this._logLine(data);
});
proc.on("error", (err) => {
this._logLine(err.message);
});
proc.on("close", (exitCode) => {
if (exitCode > 0 &&
opts.shouldOutputToConsole &&
!flagpole_execution_1.FlagpoleExecution.global.isCiOutput) {
this._logLine("FAILED TEST SUITE:");
this._logLine(filePath + " exited with error code " + exitCode);
this._logLine("\n");
}
resolve(new SuiteExecutionResult(this._output, exitCode));
});
}
_executeWithFork(filePath, opts, resolve) {
const options = {
stdio: ["pipe", "pipe", "pipe", "ipc"],
};
const proc = child_process_1.fork(filePath, opts.getOptionsArray(), options);
proc.on("exit", (exitCode) => {
if ((exitCode == null || exitCode !== 0) && opts.shouldOutputToConsole) {
this._logLine("FAILED TEST SUITE:");
this._logLine(filePath + " exited with error code " + exitCode);
this._logLine("\n");
}
resolve(new SuiteExecutionResult(this._output, exitCode === null ? -1 : exitCode));
});
proc.on("message", (msg) => {
console.log("child message received!");
this._logLine(msg);
});
}
_logLine(data) {
if (data) {
const lines = String(data).trim().split("\n");
lines.forEach((line) => {
this._output.push(line);
this._subscribers.forEach((callback) => {
callback.apply(this, [line, this]);
});
});
}
}
}
exports.SuiteExecution = SuiteExecution;
//# sourceMappingURL=suite-execution.js.map