stryker
Version:
The extendable JavaScript mutation testing framework
128 lines • 5.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var os = require("os");
var test_runner_1 = require("stryker-api/test_runner");
var child_process_1 = require("child_process");
var objectUtils_1 = require("../utils/objectUtils");
var Timer_1 = require("../utils/Timer");
var util_1 = require("@stryker-mutator/util");
/**
* A test runner that uses a (bash or cmd) command to execute the tests.
* Does not know hom many tests are executed or any code coverage results,
* instead, it mimics a simple test result based on the exit code.
* The command can be configured, but defaults to `npm test`.
*/
var CommandTestRunner = /** @class */ (function () {
function CommandTestRunner(workingDir, options) {
this.workingDir = workingDir;
this.settings = Object.assign({
command: 'npm test'
}, options.commandRunner);
}
/**
* Determines whether a given name is "command" (ignore case)
* @param name Maybe "command", maybe not
*/
CommandTestRunner.is = function (name) {
return this.runnerName === name.toLowerCase();
};
CommandTestRunner.prototype.run = function () {
var _this = this;
return new Promise(function (res, rej) {
var timer = new Timer_1.default();
var output = [];
var childProcess = child_process_1.exec(_this.settings.command, { cwd: _this.workingDir });
childProcess.on('error', function (error) {
objectUtils_1.kill(childProcess.pid)
.then(function () { return handleResolve(errorResult(error)); })
.catch(rej);
});
childProcess.on('exit', function (code) {
var result = completeResult(code, timer);
handleResolve(result);
});
childProcess.stdout.on('data', function (chunk) {
output.push(chunk);
});
childProcess.stderr.on('data', function (chunk) {
output.push(chunk);
});
_this.timeoutHandler = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
handleResolve({ status: test_runner_1.RunStatus.Timeout, tests: [] });
return [4 /*yield*/, objectUtils_1.kill(childProcess.pid)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); };
var handleResolve = function (runResult) {
removeAllListeners();
_this.timeoutHandler = undefined;
res(runResult);
};
function removeAllListeners() {
childProcess.stderr.removeAllListeners();
childProcess.stdout.removeAllListeners();
childProcess.removeAllListeners();
}
function errorResult(error) {
return {
errorMessages: [util_1.errorToString(error)],
status: test_runner_1.RunStatus.Error,
tests: []
};
}
function completeResult(exitCode, timer) {
var duration = timer.elapsedMs();
if (exitCode === 0) {
return {
status: test_runner_1.RunStatus.Complete,
tests: [{
name: 'All tests',
status: test_runner_1.TestStatus.Success,
timeSpentMs: duration
}]
};
}
else {
return {
status: test_runner_1.RunStatus.Complete,
tests: [{
failureMessages: [output.map(function (buf) { return buf.toString(); }).join(os.EOL)],
name: 'All tests',
status: test_runner_1.TestStatus.Failed,
timeSpentMs: duration
}]
};
}
}
});
};
CommandTestRunner.prototype.dispose = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.timeoutHandler) return [3 /*break*/, 2];
return [4 /*yield*/, this.timeoutHandler()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
/**
* "command"
*/
CommandTestRunner.runnerName = CommandTestRunner.name.replace('TestRunner', '').toLowerCase();
return CommandTestRunner;
}());
exports.default = CommandTestRunner;
//# sourceMappingURL=CommandTestRunner.js.map
;