definition-tester
Version:
DefinitelyTyped repository testing infrastructure
190 lines • 8.46 kB
JavaScript
'use strict';
var path = require('path');
var fs = require('fs');
var Lazy = require('lazy.js');
var Promise = require('bluebird');
var Print_1 = require('../reporter/Print');
var DefaultReporter_1 = require('../reporter/DefaultReporter');
var File_1 = require('../file/File');
var FileIndex_1 = require('../file/FileIndex');
var Timer_1 = require('../util/Timer');
var GitChanges_1 = require('../util/GitChanges');
var EvalSuite_1 = require('../tsc/EvalSuite');
var SyntaxSuite_1 = require('../tsc/SyntaxSuite');
var TSLintSuite_1 = require('../lint/TSLintSuite');
var HeaderSuite_1 = require('../header/HeaderSuite');
function isEntryPointFile(file) {
var name = path.basename(file);
return name === 'index.d.ts' || name === path.basename(path.dirname(file)) + ".d.ts";
}
var TestRunner = (function () {
function TestRunner(options) {
this.options = options;
this.suites = [];
this.index = new FileIndex_1.default(options);
this.changes = new GitChanges_1.default(options.dtPath);
var tscVersion = 'unknown';
try {
var tscPackagePath = path.resolve(options.tscPath, '../../package.json');
var json = fs.readFileSync(tscPackagePath, { encoding: 'utf8' });
var data = JSON.parse(json);
tscVersion = data.version;
}
catch (e) {
}
this.print = new Print_1.default(tscVersion);
if (options.debug) {
console.dir(options);
}
}
TestRunner.prototype.addSuite = function (suite) {
this.suites.push(suite);
};
TestRunner.prototype.getTestsToRun = function () {
if (this.options.changes) {
return this.changes.readChangedFolders().then(function (changedFolders) {
return changedFolders.map(function (s) { return path.join(s, 'tsconfig.json'); });
});
}
else {
return Promise.resolve(this.index.findFilesByName(/^tsconfig\.json$/i));
}
};
TestRunner.prototype.getTsFiles = function () {
var _this = this;
if (this.options.changes) {
return this.changes.readChangedFolders().then(function (changedFolders) {
var a = changedFolders.map(function (s) { return _this.index.findFilesByName(/\w\.d\.ts$/); });
var b = Promise.all(a);
return b.then(function (results) { return results.reduce(function (memo, results) { return memo.concat(results); }, []); });
});
}
else {
return Promise.resolve(this.index.findFilesByName(/\w\.d\.ts$/));
}
};
TestRunner.prototype.run = function () {
var _this = this;
this.timer = new Timer_1.default();
this.timer.start();
return new Promise(function (resolve) {
return _this.getTestsToRun().done(function (testsToRun) {
if (_this.options.printRefMap) {
_this.print.printRefMap(_this.index, _this.index.refMap);
}
if (Lazy(_this.index.missing).some(function (arr) { return arr.length > 0; })) {
_this.print.printMissing(_this.index, _this.index.missing);
_this.print.printBoldDiv();
return Promise.resolve(false);
}
if (_this.options.printFiles) {
_this.print.printFiles(_this.index.files);
}
resolve(_this.runTests(testsToRun.map(function (test) { return File_1.default.fromFullPath(test); })).then(function () {
return !_this.suites.some(function (suite) {
return suite.ngTests.length !== 0;
});
}));
});
});
};
TestRunner.prototype.runTests = function (files) {
var _this = this;
var syntaxChecking = new SyntaxSuite_1.default(this.options);
var headers = new HeaderSuite_1.default(this.options);
var linter = new TSLintSuite_1.default(this.options);
return new Promise(function (resolve) {
_this.print.init(files.length, files.length);
_this.print.printHeader(_this.options);
if (_this.options.tests) {
_this.addSuite(syntaxChecking);
}
if (_this.options.lint) {
_this.addSuite(linter);
}
if (_this.options.headers) {
_this.addSuite(headers);
}
resolve(Promise.reduce(_this.suites, function (count, suite) {
suite.testReporter = suite.testReporter || new DefaultReporter_1.default(_this.print);
_this.print.printSuiteHeader(suite.testSuiteName);
if (suite.testSuiteName === 'Header format') {
return _this.getTsFiles().then(function (tsFiles) {
return suite.start((tsFiles.filter(isEntryPointFile)).map(function (test) { return File_1.default.fromFullPath(test); }), function (testResult) {
_this.print.printTestComplete(testResult);
}).then(function (suite) {
_this.print.printSuiteComplete(suite);
return count + 1;
});
});
}
else {
return suite.start(files, function (testResult) {
_this.print.printTestComplete(testResult);
}).then(function (suite) {
_this.print.printSuiteComplete(suite);
return count + 1;
});
}
}, 0));
}).then(function (count) {
_this.timer.end();
_this.finaliseTests(files);
});
};
TestRunner.prototype.finaliseTests = function (files) {
var _this = this;
var testEval = Lazy(this.suites).filter(function (suite) {
return (suite instanceof EvalSuite_1.default);
}).first();
var typings;
var withoutTestTypings;
if (testEval) {
var existsTestTypings_1 = Lazy(testEval.testResults).map(function (testResult) {
return testResult.targetFile.containingFolderPath;
}).reduce(function (a, b) {
return a.indexOf(b) < 0 ? a.concat([b]) : a;
}, []);
typings = Lazy(files).map(function (file) {
return file.containingFolderPath;
}).reduce(function (a, b) {
return a.indexOf(b) < 0 ? a.concat([b]) : a;
}, []);
withoutTestTypings = typings.filter(function (typing) {
return existsTestTypings_1.indexOf(typing) < 0;
});
this.print.printDiv();
this.print.printTypingsWithoutTest(withoutTestTypings);
}
this.print.printDiv();
this.print.printTotalMessage();
this.print.printDiv();
this.print.printElapsedTime(this.timer.asString, this.timer.time);
Lazy(this.suites).filter(function (suite) {
return suite.printErrorCount;
}).each(function (suite) {
_this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length);
});
if (testEval && withoutTestTypings) {
this.print.printSuiteErrorCount('Without tests', withoutTestTypings.length, typings.length, true);
}
this.print.printDiv();
if (this.suites.some(function (suite) {
return suite.ngTests.length !== 0;
})) {
this.print.printErrorsHeader();
this.suites.filter(function (suite) {
return suite.ngTests.length !== 0;
}).forEach(function (suite) {
suite.ngTests.forEach(function (testResult) {
_this.print.printErrorsForFile(testResult);
});
_this.print.printBoldDiv();
});
}
};
return TestRunner;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TestRunner;
//# sourceMappingURL=TestRunner.js.map