From 513db1d6adcadd6fdd46300f2550c7a9f90331e0 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 16:04:58 +0100 Subject: [PATCH 01/13] split runner code over files main file got cumbersome - extracted parts to own files - still one --out file - existing code into DT module cleaned some syntax and lint expanded arrow-functions to full form builds using 0.9.7 --- _infrastructure/tests/runner.js | 1244 +++++++++-------- _infrastructure/tests/runner.ts | 547 ++------ _infrastructure/tests/src/exec.js | 79 -- _infrastructure/tests/src/file.ts | 26 + _infrastructure/tests/src/{ => host}/exec.ts | 22 +- _infrastructure/tests/src/{ => host}/io.ts | 149 +- _infrastructure/tests/src/indexer.ts | 0 _infrastructure/tests/src/io.js | 475 ------- _infrastructure/tests/src/printer.ts | 111 ++ .../tests/src/reporter/reporter.ts | 36 + _infrastructure/tests/src/suite/suite.ts | 83 ++ _infrastructure/tests/src/suite/syntax.ts | 20 + _infrastructure/tests/src/suite/testEval.ts | 21 + _infrastructure/tests/src/suite/tscParams.ts | 49 + _infrastructure/tests/src/timer.ts | 46 + _infrastructure/tests/src/tsc.ts | 43 + _infrastructure/tests/src/util.ts | 5 + 17 files changed, 1288 insertions(+), 1668 deletions(-) delete mode 100644 _infrastructure/tests/src/exec.js create mode 100644 _infrastructure/tests/src/file.ts rename _infrastructure/tests/src/{ => host}/exec.ts (83%) rename _infrastructure/tests/src/{ => host}/io.ts (81%) create mode 100644 _infrastructure/tests/src/indexer.ts delete mode 100644 _infrastructure/tests/src/io.js create mode 100644 _infrastructure/tests/src/printer.ts create mode 100644 _infrastructure/tests/src/reporter/reporter.ts create mode 100644 _infrastructure/tests/src/suite/suite.ts create mode 100644 _infrastructure/tests/src/suite/syntax.ts create mode 100644 _infrastructure/tests/src/suite/testEval.ts create mode 100644 _infrastructure/tests/src/suite/tscParams.ts create mode 100644 _infrastructure/tests/src/timer.ts create mode 100644 _infrastructure/tests/src/tsc.ts create mode 100644 _infrastructure/tests/src/util.ts diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index 2721d83ab3..37aefb7e52 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // + var ExecResult = (function () { function ExecResult() { this.stdout = ""; @@ -69,14 +70,14 @@ var NodeExec = (function () { return NodeExec; })(); -var Exec = (function () { +var Exec = function () { var global = Function("return this;").call(null); if (typeof global.ActiveXObject !== "undefined") { return new WindowsScriptHostExec(); } else { return new NodeExec(); } -})(); +}(); // // Copyright (c) Microsoft Corporation. All rights reserved. // @@ -91,6 +92,7 @@ var Exec = (function () { // See the License for the specific language governing permissions and // limitations under the License. // + var IOUtils; (function (IOUtils) { // Creates the directory including its parent if not already present @@ -125,6 +127,8 @@ var IOUtils; IOUtils.throwIOError = throwIOError; })(IOUtils || (IOUtils = {})); + + var IO = (function () { // Create an IO object for use inside WindowsScriptHost hosts // Depends on WSCript and FileSystemObject @@ -154,11 +158,11 @@ var IO = (function () { try { var streamObj = getStreamObject(); streamObj.Open(); - streamObj.Type = 2; - streamObj.Charset = 'x-ansi'; + streamObj.Type = 2; // Text data + streamObj.Charset = 'x-ansi'; // Assume we are reading ansi text streamObj.LoadFromFile(path); var bomChar = streamObj.ReadText(2); - streamObj.Position = 0; + streamObj.Position = 0; // Position has to be at 0 before changing the encoding if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF) || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) { streamObj.Charset = 'unicode'; } else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) { @@ -213,7 +217,7 @@ var IO = (function () { deleteFile: function (path) { try { if (fso.FileExists(path)) { - fso.DeleteFile(path, true); + fso.DeleteFile(path, true); // true: delete read-only files } } catch (e) { IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); @@ -386,7 +390,7 @@ var IO = (function () { return; } else { mkdirRecursiveSync(_path.dirname(path)); - _fs.mkdirSync(path, 0775); + _fs.mkdirSync(path, parseInt('0775', 8)); } } @@ -465,6 +469,7 @@ var IO = (function () { } else { var parentPath = _path.resolve(rootPath, ".."); + // Node will just continue to repeat the root path, rather than return null if (rootPath === parentPath) { return null; } else { @@ -547,622 +552,681 @@ var IO = (function () { if (typeof ActiveXObject === "function") return getWindowsScriptHostIO(); -else if (typeof require === "function") + else if (typeof require === "function") return getNodeIO(); -else + else return null; })(); +var DT; +(function (DT) { + var path = require('path'); + + ///////////////////////////////// + // Given a document root + ts file pattern this class returns: + // all the TS files OR just tests OR just definition files + ///////////////////////////////// + var File = (function () { + function File(baseDir, filePathWithName) { + this.baseDir = baseDir; + this.filePathWithName = filePathWithName; + var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); + this.dir = dirName.split('/')[0]; + this.file = path.basename(this.filePathWithName, '.ts'); + this.ext = path.extname(this.filePathWithName); + } + Object.defineProperty(File.prototype, "formatName", { + // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' + get: function () { + var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); + return this.dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + this.file + this.ext; + }, + enumerable: true, + configurable: true + }); + return File; + })(); + DT.File = File; +})(DT || (DT = {})); +/// +/// +/// +var DT; +(function (DT) { + var Tsc = (function () { + function Tsc() { + } + Tsc.run = function (tsfile, options, callback) { + options = options || {}; + options.tscVersion = options.tscVersion || DT.DEFAULT_TSC_VERSION; + if (typeof options.checkNoImplicitAny === "undefined") { + options.checkNoImplicitAny = true; + } + if (typeof options.useTscParams === "undefined") { + options.useTscParams = true; + } + + if (!IO.fileExists(tsfile)) { + throw new Error(tsfile + " not exists"); + } + + var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + if (!IO.fileExists(tscPath)) { + throw new Error(tscPath + ' is not exists'); + } + var command = 'node ' + tscPath + ' --module commonjs '; + if (options.useTscParams && IO.fileExists(tsfile + '.tscparams')) { + command += '@' + tsfile + '.tscparams'; + } else if (options.checkNoImplicitAny) { + command += '--noImplicitAny'; + } + Exec.exec(command, [tsfile], function (execResult) { + callback(execResult); + }); + }; + return Tsc; + })(); + DT.Tsc = Tsc; +})(DT || (DT = {})); +/// +/// +var DT; +(function (DT) { + ///////////////////////////////// + // Timer.start starts a timer + // Timer.end stops the timer and sets asString to the pretty print value + ///////////////////////////////// + var Timer = (function () { + function Timer() { + this.time = 0; + } + Timer.prototype.start = function () { + this.time = 0; + this.startTime = this.now(); + }; + + Timer.prototype.now = function () { + return Date.now(); + }; + + Timer.prototype.end = function () { + this.time = (this.now() - this.startTime) / 1000; + this.asString = Timer.prettyDate(this.startTime, this.now()); + }; + + Timer.prettyDate = function (date1, date2) { + var diff = ((date2 - date1) / 1000), day_diff = Math.floor(diff / 86400); + + if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) + return; + + return (day_diff == 0 && (diff < 60 && (diff + " seconds") || diff < 120 && "1 minute" || diff < 3600 && Math.floor(diff / 60) + " minutes" || diff < 7200 && "1 hour" || diff < 86400 && Math.floor(diff / 3600) + " hours") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days" || day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); + }; + return Timer; + })(); + DT.Timer = Timer; +})(DT || (DT = {})); +var DT; +(function (DT) { + function endsWith(str, suffix) { + return str.indexOf(suffix, str.length - suffix.length) !== -1; + } + DT.endsWith = endsWith; +})(DT || (DT = {})); +/// +/// +var DT; +(function (DT) { + ///////////////////////////////// + // All the common things that we pring are functions of this class + ///////////////////////////////// + var Print = (function () { + function Print(version, typings, tests, tsFiles) { + this.version = version; + this.typings = typings; + this.tests = tests; + this.tsFiles = tsFiles; + this.WIDTH = 77; + } + Print.prototype.out = function (s) { + process.stdout.write(s); + return this; + }; + + Print.prototype.repeat = function (s, times) { + return new Array(times + 1).join(s); + }; + + Print.prototype.printHeader = function () { + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); + this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); + this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); + this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); + }; + + Print.prototype.printSuiteHeader = function (title) { + var left = Math.floor((this.WIDTH - title.length) / 2) - 1; + var right = Math.ceil((this.WIDTH - title.length) / 2) - 1; + this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); + this.out(title); + this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); + }; + + Print.prototype.printDiv = function () { + this.out('-----------------------------------------------------------------------------\n'); + }; + + Print.prototype.printBoldDiv = function () { + this.out('=============================================================================\n'); + }; + + Print.prototype.printErrorsHeader = function () { + this.out('=============================================================================\n'); + this.out(' \33[34m\33[1mErrors in files\33[0m \n'); + this.out('=============================================================================\n'); + }; + + Print.prototype.printErrorsForFile = function (testResult) { + this.out('----------------- For file:' + testResult.targetFile.formatName); + this.printBreak().out(testResult.stderr).printBreak(); + }; + + Print.prototype.printBreak = function () { + this.out('\n'); + return this; + }; + + Print.prototype.clearCurrentLine = function () { + this.out("\r\33[K"); + return this; + }; + + Print.prototype.printSuccessCount = function (current, total) { + this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + }; + + Print.prototype.printFailedCount = function (current, total) { + this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + }; + + Print.prototype.printTypingsWithoutTestsMessage = function () { + this.out(' \33[36m\33[1mTyping without tests\33[0m\n'); + }; + + Print.prototype.printTotalMessage = function () { + this.out(' \33[36m\33[1mTotal\33[0m\n'); + }; + + Print.prototype.printElapsedTime = function (time, s) { + this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); + }; + + Print.prototype.printSuiteErrorCount = function (errorHeadline, current, total, valuesColor) { + if (typeof valuesColor === "undefined") { valuesColor = '\33[31m\33[1m'; } + this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); + this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + }; + + Print.prototype.printTypingsWithoutTestName = function (file) { + this.out(' - \33[33m\33[1m' + file + '\33[0m\n'); + }; + + Print.prototype.printTypingsWithoutTest = function (withoutTestTypings) { + var _this = this; + if (withoutTestTypings.length > 0) { + this.printTypingsWithoutTestsMessage(); + + this.printDiv(); + withoutTestTypings.forEach(function (t) { + _this.printTypingsWithoutTestName(t); + }); + } + }; + return Print; + })(); + DT.Print = Print; +})(DT || (DT = {})); +var DT; +(function (DT) { + + + ///////////////////////////////// + // Default test reporter + ///////////////////////////////// + var DefaultTestReporter = (function () { + function DefaultTestReporter(print) { + this.print = print; + } + DefaultTestReporter.prototype.printPositiveCharacter = function (index, testResult) { + this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); + + this.printBreakIfNeeded(index); + }; + + DefaultTestReporter.prototype.printNegativeCharacter = function (index, testResult) { + this.print.out("x"); + + this.printBreakIfNeeded(index); + }; + + DefaultTestReporter.prototype.printBreakIfNeeded = function (index) { + if (index % this.print.WIDTH === 0) { + this.print.printBreak(); + } + }; + return DefaultTestReporter; + })(); + DT.DefaultTestReporter = DefaultTestReporter; +})(DT || (DT = {})); +/// +var DT; +(function (DT) { + + + ///////////////////////////////// + // Base class for test suite + ///////////////////////////////// + var TestSuiteBase = (function () { + function TestSuiteBase(options, testSuiteName, errorHeadline) { + this.options = options; + this.testSuiteName = testSuiteName; + this.errorHeadline = errorHeadline; + this.timer = new DT.Timer(); + this.testResults = []; + this.printErrorCount = true; + } + TestSuiteBase.prototype.filterTargetFiles = function (files) { + throw new Error("please implement this method"); + }; + + TestSuiteBase.prototype.start = function (targetFiles, testCallback, suiteCallback) { + var _this = this; + targetFiles = this.filterTargetFiles(targetFiles); + this.timer.start(); + var count = 0; + + // exec test is async process. serialize. + var executor = function () { + var targetFile = targetFiles[count]; + if (targetFile) { + _this.runTest(targetFile, function (result) { + testCallback(result, count + 1); + count++; + executor(); + }); + } else { + _this.timer.end(); + _this.finish(suiteCallback); + } + }; + executor(); + }; + + TestSuiteBase.prototype.runTest = function (targetFile, callback) { + var _this = this; + new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion }).run(function (result) { + _this.testResults.push(result); + callback(result); + }); + }; + + TestSuiteBase.prototype.finish = function (suiteCallback) { + suiteCallback(this); + }; + + Object.defineProperty(TestSuiteBase.prototype, "okTests", { + get: function () { + return this.testResults.filter(function (r) { + return r.success; + }); + }, + enumerable: true, + configurable: true + }); + + Object.defineProperty(TestSuiteBase.prototype, "ngTests", { + get: function () { + return this.testResults.filter(function (r) { + return !r.success; + }); + }, + enumerable: true, + configurable: true + }); + return TestSuiteBase; + })(); + DT.TestSuiteBase = TestSuiteBase; +})(DT || (DT = {})); +/// +/// var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; -var DefinitelyTyped; -(function (DefinitelyTyped) { - /// - /// - /// - (function (TestManager) { - var path = require('path'); - - TestManager.DEFAULT_TSC_VERSION = "0.9.1.1"; - - function endsWith(str, suffix) { - return str.indexOf(suffix, str.length - suffix.length) !== -1; +var DT; +(function (DT) { + ///////////////////////////////// + // .d.ts syntax inspection + ///////////////////////////////// + var SyntaxChecking = (function (_super) { + __extends(SyntaxChecking, _super); + function SyntaxChecking(options) { + _super.call(this, options, "Syntax checking", "Syntax error"); } - - var Tsc = (function () { - function Tsc() { - } - Tsc.run = function (tsfile, options, callback) { - options = options || {}; - options.tscVersion = options.tscVersion || TestManager.DEFAULT_TSC_VERSION; - if (typeof options.checkNoImplicitAny === "undefined") { - options.checkNoImplicitAny = true; - } - if (typeof options.useTscParams === "undefined") { - options.useTscParams = true; - } - - if (!IO.fileExists(tsfile)) { - throw new Error(tsfile + " not exists"); - } - - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!IO.fileExists(tscPath)) { - throw new Error(tscPath + ' is not exists'); - } - var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && IO.fileExists(tsfile + '.tscparams')) { - command += '@' + tsfile + '.tscparams'; - } else if (options.checkNoImplicitAny) { - command += '--noImplicitAny'; - } - Exec.exec(command, [tsfile], function (execResult) { - callback(execResult); - }); - }; - return Tsc; - })(); - - var Test = (function () { - function Test(suite, tsfile, options) { - this.suite = suite; - this.tsfile = tsfile; - this.options = options; - } - Test.prototype.run = function (callback) { - var _this = this; - Tsc.run(this.tsfile.filePathWithName, this.options, function (execResult) { - var testResult = new TestResult(); - testResult.hostedBy = _this.suite; - testResult.targetFile = _this.tsfile; - testResult.options = _this.options; - - testResult.stdout = execResult.stdout; - testResult.stderr = execResult.stderr; - testResult.exitCode = execResult.exitCode; - - callback(testResult); - }); - }; - return Test; - })(); - - ///////////////////////////////// - // Timer.start starts a timer - // Timer.end stops the timer and sets asString to the pretty print value - ///////////////////////////////// - var Timer = (function () { - function Timer() { - this.time = 0; - } - Timer.prototype.start = function () { - this.time = 0; - this.startTime = this.now(); - }; - - Timer.prototype.now = function () { - return Date.now(); - }; - - Timer.prototype.end = function () { - this.time = (this.now() - this.startTime) / 1000; - this.asString = Timer.prettyDate(this.startTime, this.now()); - }; - - Timer.prettyDate = function (date1, date2) { - var diff = ((date2 - date1) / 1000), day_diff = Math.floor(diff / 86400); - - if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) - return; - - return (day_diff == 0 && (diff < 60 && (diff + " seconds") || diff < 120 && "1 minute" || diff < 3600 && Math.floor(diff / 60) + " minutes" || diff < 7200 && "1 hour" || diff < 86400 && Math.floor(diff / 3600) + " hours") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days" || day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); - }; - return Timer; - })(); - TestManager.Timer = Timer; - - ///////////////////////////////// - // Given a document root + ts file pattern this class returns: - // all the TS files OR just tests OR just definition files - ///////////////////////////////// - var File = (function () { - function File(baseDir, filePathWithName) { - this.baseDir = baseDir; - this.filePathWithName = filePathWithName; - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - this.dir = dirName.split('/')[0]; - this.file = path.basename(this.filePathWithName, '.ts'); - this.ext = path.extname(this.filePathWithName); - } - Object.defineProperty(File.prototype, "formatName", { - get: // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' - function () { - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - return this.dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + this.file + this.ext; - }, - enumerable: true, - configurable: true + SyntaxChecking.prototype.filterTargetFiles = function (files) { + return files.filter(function (file) { + return DT.endsWith(file.formatName.toUpperCase(), '.D.TS'); }); - return File; - })(); - TestManager.File = File; - - ///////////////////////////////// - // Test results - ///////////////////////////////// - var TestResult = (function () { - function TestResult() { - } - Object.defineProperty(TestResult.prototype, "success", { - get: function () { - return this.exitCode === 0; - }, - enumerable: true, - configurable: true + }; + return SyntaxChecking; + })(DT.TestSuiteBase); + DT.SyntaxChecking = SyntaxChecking; +})(DT || (DT = {})); +/// +/// +var DT; +(function (DT) { + ///////////////////////////////// + // Compile with *-tests.ts + ///////////////////////////////// + var TestEval = (function (_super) { + __extends(TestEval, _super); + function TestEval(options) { + _super.call(this, options, "Typing tests", "Failed tests"); + } + TestEval.prototype.filterTargetFiles = function (files) { + return files.filter(function (file) { + return DT.endsWith(file.formatName.toUpperCase(), '-TESTS.TS'); }); - return TestResult; - })(); - TestManager.TestResult = TestResult; + }; + return TestEval; + })(DT.TestSuiteBase); + DT.TestEval = TestEval; +})(DT || (DT = {})); +/// +/// +var DT; +(function (DT) { + ///////////////////////////////// + // Try compile without .tscparams + // It may indicate that it is compatible with --noImplicitAny maybe... + ///////////////////////////////// + var FindNotRequiredTscparams = (function (_super) { + __extends(FindNotRequiredTscparams, _super); + function FindNotRequiredTscparams(options, print) { + var _this = this; + _super.call(this, options, "Find not required .tscparams files", "New arrival!"); + this.print = print; + this.printErrorCount = false; - ///////////////////////////////// - // Default test reporter - ///////////////////////////////// - var DefaultTestReporter = (function () { - function DefaultTestReporter(print) { - this.print = print; - } - DefaultTestReporter.prototype.printPositiveCharacter = function (index, testResult) { - this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); - - this.printBreakIfNeeded(index); - }; - - DefaultTestReporter.prototype.printNegativeCharacter = function (index, testResult) { - this.print.out("x"); - - this.printBreakIfNeeded(index); - }; - - DefaultTestReporter.prototype.printBreakIfNeeded = function (index) { - if (index % this.print.WIDTH === 0) { - this.print.printBreak(); + this.testReporter = { + printPositiveCharacter: function (index, testResult) { + _this.print.clearCurrentLine().printTypingsWithoutTestName(testResult.targetFile.formatName); + }, + printNegativeCharacter: function (index, testResult) { } }; - return DefaultTestReporter; - })(); + } + FindNotRequiredTscparams.prototype.filterTargetFiles = function (files) { + return files.filter(function (file) { + return IO.fileExists(file.filePathWithName + '.tscparams'); + }); + }; - ///////////////////////////////// - // All the common things that we pring are functions of this class - ///////////////////////////////// - var Print = (function () { - function Print(version, typings, tests, tsFiles) { - this.version = version; - this.typings = typings; - this.tests = tests; - this.tsFiles = tsFiles; - this.WIDTH = 77; + FindNotRequiredTscparams.prototype.runTest = function (targetFile, callback) { + var _this = this; + this.print.clearCurrentLine().out(targetFile.formatName); + new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true }).run(function (result) { + _this.testResults.push(result); + callback(result); + }); + }; + + FindNotRequiredTscparams.prototype.finish = function (suiteCallback) { + this.print.clearCurrentLine(); + suiteCallback(this); + }; + + Object.defineProperty(FindNotRequiredTscparams.prototype, "ngTests", { + get: function () { + // Do not show ng test results + return []; + }, + enumerable: true, + configurable: true + }); + return FindNotRequiredTscparams; + })(DT.TestSuiteBase); + DT.FindNotRequiredTscparams = FindNotRequiredTscparams; +})(DT || (DT = {})); +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +var DT; +(function (DT) { + var fs = require('fs'); + var path = require('path'); + + DT.DEFAULT_TSC_VERSION = "0.9.1.1"; + + var Test = (function () { + function Test(suite, tsfile, options) { + this.suite = suite; + this.tsfile = tsfile; + this.options = options; + } + Test.prototype.run = function (callback) { + var _this = this; + DT.Tsc.run(this.tsfile.filePathWithName, this.options, function (execResult) { + var testResult = new TestResult(); + testResult.hostedBy = _this.suite; + testResult.targetFile = _this.tsfile; + testResult.options = _this.options; + + testResult.stdout = execResult.stdout; + testResult.stderr = execResult.stderr; + testResult.exitCode = execResult.exitCode; + + callback(testResult); + }); + }; + return Test; + })(); + DT.Test = Test; + + ///////////////////////////////// + // Test results + ///////////////////////////////// + var TestResult = (function () { + function TestResult() { + } + Object.defineProperty(TestResult.prototype, "success", { + get: function () { + return this.exitCode === 0; + }, + enumerable: true, + configurable: true + }); + return TestResult; + })(); + DT.TestResult = TestResult; + + ///////////////////////////////// + // The main class to kick things off + ///////////////////////////////// + var TestRunner = (function () { + function TestRunner(dtPath, options) { + if (typeof options === "undefined") { options = { tscVersion: DT.DEFAULT_TSC_VERSION }; } + this.options = options; + this.suites = []; + this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; + + var filesName = IO.dir(dtPath, /.\.ts/g, { recursive: true }).sort(); + + // only includes .d.ts or -tests.ts or -test.ts or .ts + this.files = filesName.filter(function (fileName) { + return fileName.indexOf('../_infrastructure') < 0; + }).filter(function (fileName) { + return fileName.indexOf('../node_modules') < 0; + }).filter(function (fileName) { + return !DT.endsWith(fileName, ".tscparams"); + }).map(function (fileName) { + return new DT.File(dtPath, fileName); + }); + } + TestRunner.prototype.addSuite = function (suite) { + this.suites.push(suite); + }; + + TestRunner.prototype.run = function () { + var _this = this; + this.timer = new DT.Timer(); + this.timer.start(); + + var syntaxChecking = new DT.SyntaxChecking(this.options); + var testEval = new DT.TestEval(this.options); + if (!this.options.findNotRequiredTscparams) { + this.addSuite(syntaxChecking); + this.addSuite(testEval); } - Print.prototype.out = function (s) { - process.stdout.write(s); - return this; - }; - Print.prototype.repeat = function (s, times) { - return new Array(times + 1).join(s); - }; + var typings = syntaxChecking.filterTargetFiles(this.files).length; + var testFiles = testEval.filterTargetFiles(this.files).length; + this.print = new DT.Print(this.options.tscVersion, typings, testFiles, this.files.length); + this.print.printHeader(); - Print.prototype.printHeader = function () { - this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); - this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); - this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); - this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); - this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); - }; + if (this.options.findNotRequiredTscparams) { + this.addSuite(new DT.FindNotRequiredTscparams(this.options, this.print)); + } - Print.prototype.printSuiteHeader = function (title) { - var left = Math.floor((this.WIDTH - title.length) / 2) - 1; - var right = Math.ceil((this.WIDTH - title.length) / 2) - 1; - this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); - this.out(title); - this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); - }; + var count = 0; + var executor = function () { + var suite = _this.suites[count]; + if (suite) { + suite.testReporter = suite.testReporter || new DT.DefaultTestReporter(_this.print); - Print.prototype.printDiv = function () { - this.out('-----------------------------------------------------------------------------\n'); - }; - - Print.prototype.printBoldDiv = function () { - this.out('=============================================================================\n'); - }; - - Print.prototype.printErrorsHeader = function () { - this.out('=============================================================================\n'); - this.out(' \33[34m\33[1mErrors in files\33[0m \n'); - this.out('=============================================================================\n'); - }; - - Print.prototype.printErrorsForFile = function (testResult) { - this.out('----------------- For file:' + testResult.targetFile.formatName); - this.printBreak().out(testResult.stderr).printBreak(); - }; - - Print.prototype.printBreak = function () { - this.out('\n'); - return this; - }; - - Print.prototype.clearCurrentLine = function () { - this.out("\r\33[K"); - return this; - }; - - Print.prototype.printSuccessCount = function (current, total) { - this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - }; - - Print.prototype.printFailedCount = function (current, total) { - this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - }; - - Print.prototype.printTypingsWithoutTestsMessage = function () { - this.out(' \33[36m\33[1mTyping without tests\33[0m\n'); - }; - - Print.prototype.printTotalMessage = function () { - this.out(' \33[36m\33[1mTotal\33[0m\n'); - }; - - Print.prototype.printElapsedTime = function (time, s) { - this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); - }; - - Print.prototype.printSuiteErrorCount = function (errorHeadline, current, total, valuesColor) { - if (typeof valuesColor === "undefined") { valuesColor = '\33[31m\33[1m'; } - this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); - this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - }; - - Print.prototype.printTypingsWithoutTestName = function (file) { - this.out(' - \33[33m\33[1m' + file + '\33[0m\n'); - }; - - Print.prototype.printTypingsWithoutTest = function (withoutTestTypings) { - var _this = this; - if (withoutTestTypings.length > 0) { - this.printTypingsWithoutTestsMessage(); - - this.printDiv(); - withoutTestTypings.forEach(function (t) { - _this.printTypingsWithoutTestName(t); + _this.print.printSuiteHeader(suite.testSuiteName); + var targetFiles = suite.filterTargetFiles(_this.files); + suite.start(targetFiles, function (testResult, index) { + _this.testCompleteCallback(testResult, index); + }, function (suite) { + _this.suiteCompleteCallback(suite); + count++; + executor(); }); - } - }; - return Print; - })(); - - ///////////////////////////////// - // Base class for test suite - ///////////////////////////////// - var TestSuiteBase = (function () { - function TestSuiteBase(options, testSuiteName, errorHeadline) { - this.options = options; - this.testSuiteName = testSuiteName; - this.errorHeadline = errorHeadline; - this.timer = new Timer(); - this.testResults = []; - this.printErrorCount = true; - } - TestSuiteBase.prototype.filterTargetFiles = function (files) { - throw new Error("please implement this method"); - }; - - TestSuiteBase.prototype.start = function (targetFiles, testCallback, suiteCallback) { - var _this = this; - targetFiles = this.filterTargetFiles(targetFiles); - this.timer.start(); - var count = 0; - - // exec test is async process. serialize. - var executor = function () { - var targetFile = targetFiles[count]; - if (targetFile) { - _this.runTest(targetFile, function (result) { - testCallback(result, count + 1); - count++; - executor(); - }); - } else { - _this.timer.end(); - _this.finish(suiteCallback); - } - }; - executor(); - }; - - TestSuiteBase.prototype.runTest = function (targetFile, callback) { - var _this = this; - new Test(this, targetFile, { tscVersion: this.options.tscVersion }).run(function (result) { - _this.testResults.push(result); - callback(result); - }); - }; - - TestSuiteBase.prototype.finish = function (suiteCallback) { - suiteCallback(this); - }; - - Object.defineProperty(TestSuiteBase.prototype, "okTests", { - get: function () { - return this.testResults.filter(function (r) { - return r.success; - }); - }, - enumerable: true, - configurable: true - }); - - Object.defineProperty(TestSuiteBase.prototype, "ngTests", { - get: function () { - return this.testResults.filter(function (r) { - return !r.success; - }); - }, - enumerable: true, - configurable: true - }); - return TestSuiteBase; - })(); - - ///////////////////////////////// - // .d.ts syntax inspection - ///////////////////////////////// - var SyntaxChecking = (function (_super) { - __extends(SyntaxChecking, _super); - function SyntaxChecking(options) { - _super.call(this, options, "Syntax checking", "Syntax error"); - } - SyntaxChecking.prototype.filterTargetFiles = function (files) { - return files.filter(function (file) { - return endsWith(file.formatName.toUpperCase(), '.D.TS'); - }); - }; - return SyntaxChecking; - })(TestSuiteBase); - - ///////////////////////////////// - // Compile with *-tests.ts - ///////////////////////////////// - var TestEval = (function (_super) { - __extends(TestEval, _super); - function TestEval(options) { - _super.call(this, options, "Typing tests", "Failed tests"); - } - TestEval.prototype.filterTargetFiles = function (files) { - return files.filter(function (file) { - return endsWith(file.formatName.toUpperCase(), '-TESTS.TS'); - }); - }; - return TestEval; - })(TestSuiteBase); - - ///////////////////////////////// - // Try compile without .tscparams - // It may indicate that it is compatible with --noImplicitAny maybe... - ///////////////////////////////// - var FindNotRequiredTscparams = (function (_super) { - __extends(FindNotRequiredTscparams, _super); - function FindNotRequiredTscparams(options, print) { - var _this = this; - _super.call(this, options, "Find not required .tscparams files", "New arrival!"); - this.print = print; - this.printErrorCount = false; - - this.testReporter = { - printPositiveCharacter: function (index, testResult) { - _this.print.clearCurrentLine().printTypingsWithoutTestName(testResult.targetFile.formatName); - }, - printNegativeCharacter: function (index, testResult) { - } - }; - } - FindNotRequiredTscparams.prototype.filterTargetFiles = function (files) { - return files.filter(function (file) { - return IO.fileExists(file.filePathWithName + '.tscparams'); - }); - }; - - FindNotRequiredTscparams.prototype.runTest = function (targetFile, callback) { - var _this = this; - this.print.clearCurrentLine().out(targetFile.formatName); - new Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true }).run(function (result) { - _this.testResults.push(result); - callback(result); - }); - }; - - FindNotRequiredTscparams.prototype.finish = function (suiteCallback) { - this.print.clearCurrentLine(); - suiteCallback(this); - }; - - Object.defineProperty(FindNotRequiredTscparams.prototype, "ngTests", { - get: function () { - // Do not show ng test results - return []; - }, - enumerable: true, - configurable: true - }); - return FindNotRequiredTscparams; - })(TestSuiteBase); - - ///////////////////////////////// - // The main class to kick things off - ///////////////////////////////// - var TestRunner = (function () { - function TestRunner(dtPath, options) { - if (typeof options === "undefined") { options = { tscVersion: TestManager.DEFAULT_TSC_VERSION }; } - this.options = options; - this.suites = []; - this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - - var filesName = IO.dir(dtPath, /.\.ts/g, { recursive: true }).sort(); - - // only includes .d.ts or -tests.ts or -test.ts or .ts - filesName = filesName.filter(function (fileName) { - return fileName.indexOf('../_infrastructure') < 0; - }).filter(function (fileName) { - return !endsWith(fileName, ".tscparams"); - }); - this.files = filesName.map(function (fileName) { - return new File(dtPath, fileName); - }); - } - TestRunner.prototype.addSuite = function (suite) { - this.suites.push(suite); - }; - - TestRunner.prototype.run = function () { - var _this = this; - this.timer = new Timer(); - this.timer.start(); - - var syntaxChecking = new SyntaxChecking(this.options); - var testEval = new TestEval(this.options); - if (!this.options.findNotRequiredTscparams) { - this.addSuite(syntaxChecking); - this.addSuite(testEval); - } - - var typings = syntaxChecking.filterTargetFiles(this.files).length; - var testFiles = testEval.filterTargetFiles(this.files).length; - this.print = new Print(this.options.tscVersion, typings, testFiles, this.files.length); - this.print.printHeader(); - - if (this.options.findNotRequiredTscparams) { - this.addSuite(new FindNotRequiredTscparams(this.options, this.print)); - } - - var count = 0; - var executor = function () { - var suite = _this.suites[count]; - if (suite) { - suite.testReporter = suite.testReporter || new DefaultTestReporter(_this.print); - - _this.print.printSuiteHeader(suite.testSuiteName); - var targetFiles = suite.filterTargetFiles(_this.files); - suite.start(targetFiles, function (testResult, index) { - _this.testCompleteCallback(testResult, index); - }, function (suite) { - _this.suiteCompleteCallback(suite); - count++; - executor(); - }); - } else { - _this.timer.end(); - _this.allTestCompleteCallback(); - } - }; - executor(); - }; - - TestRunner.prototype.testCompleteCallback = function (testResult, index) { - var reporter = testResult.hostedBy.testReporter; - if (testResult.success) { - reporter.printPositiveCharacter(index, testResult); } else { - reporter.printNegativeCharacter(index, testResult); + _this.timer.end(); + _this.allTestCompleteCallback(); } }; + executor(); + }; - TestRunner.prototype.suiteCompleteCallback = function (suite) { - this.print.printBreak(); + TestRunner.prototype.testCompleteCallback = function (testResult, index) { + var reporter = testResult.hostedBy.testReporter; + if (testResult.success) { + reporter.printPositiveCharacter(index, testResult); + } else { + reporter.printNegativeCharacter(index, testResult); + } + }; - this.print.printDiv(); - this.print.printElapsedTime(suite.timer.asString, suite.timer.time); - this.print.printSuccessCount(suite.okTests.length, suite.testResults.length); - this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); - }; + TestRunner.prototype.suiteCompleteCallback = function (suite) { + this.print.printBreak(); - TestRunner.prototype.allTestCompleteCallback = function () { - var _this = this; - var testEval = this.suites.filter(function (suite) { - return suite instanceof TestEval; - })[0]; - if (testEval) { - var existsTestTypings = testEval.testResults.map(function (testResult) { - return testResult.targetFile.dir; - }).reduce(function (a, b) { - return a.indexOf(b) < 0 ? a.concat([b]) : a; - }, []); - var typings = this.files.map(function (file) { - return file.dir; - }).reduce(function (a, b) { - return a.indexOf(b) < 0 ? a.concat([b]) : a; - }, []); - var withoutTestTypings = typings.filter(function (typing) { - return existsTestTypings.indexOf(typing) < 0; - }); - this.print.printDiv(); - this.print.printTypingsWithoutTest(withoutTestTypings); - } + this.print.printDiv(); + this.print.printElapsedTime(suite.timer.asString, suite.timer.time); + this.print.printSuccessCount(suite.okTests.length, suite.testResults.length); + this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); + }; - this.print.printDiv(); - this.print.printTotalMessage(); - - this.print.printDiv(); - this.print.printElapsedTime(this.timer.asString, this.timer.time); - this.suites.filter(function (suite) { - return suite.printErrorCount; - }).forEach(function (suite) { - _this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); + TestRunner.prototype.allTestCompleteCallback = function () { + var _this = this; + var testEval = this.suites.filter(function (suite) { + return suite instanceof DT.TestEval; + })[0]; + if (testEval) { + var existsTestTypings = testEval.testResults.map(function (testResult) { + return testResult.targetFile.dir; + }).reduce(function (a, b) { + return a.indexOf(b) < 0 ? a.concat([b]) : a; + }, []); + var typings = this.files.map(function (file) { + return file.dir; + }).reduce(function (a, b) { + return a.indexOf(b) < 0 ? a.concat([b]) : a; + }, []); + var withoutTestTypings = typings.filter(function (typing) { + return existsTestTypings.indexOf(typing) < 0; }); - if (testEval) { - this.print.printSuiteErrorCount("Without tests", withoutTestTypings.length, typings.length, '\33[33m\33[1m'); - } - this.print.printDiv(); - if (this.suites.some(function (suite) { + this.print.printTypingsWithoutTest(withoutTestTypings); + } + + this.print.printDiv(); + this.print.printTotalMessage(); + + this.print.printDiv(); + this.print.printElapsedTime(this.timer.asString, this.timer.time); + this.suites.filter(function (suite) { + return suite.printErrorCount; + }).forEach(function (suite) { + _this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); + }); + if (testEval) { + this.print.printSuiteErrorCount("Without tests", withoutTestTypings.length, typings.length, '\33[33m\33[1m'); + } + + 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; - })) { - 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(); + }).forEach(function (suite) { + suite.ngTests.forEach(function (testResult) { + _this.print.printErrorsForFile(testResult); }); + _this.print.printBoldDiv(); + }); - process.exit(1); - } - }; - return TestRunner; - })(); - TestManager.TestRunner = TestRunner; - })(DefinitelyTyped.TestManager || (DefinitelyTyped.TestManager = {})); - var TestManager = DefinitelyTyped.TestManager; -})(DefinitelyTyped || (DefinitelyTyped = {})); + process.exit(1); + } + }; + return TestRunner; + })(); + DT.TestRunner = TestRunner; -var dtPath = __dirname + '/../..'; -var findNotRequiredTscparams = process.argv.some(function (arg) { - return arg == "--try-without-tscparams"; -}); -var tscVersionIndex = process.argv.indexOf("--tsc-version"); -var tscVersion = DefinitelyTyped.TestManager.DEFAULT_TSC_VERSION; -if (-1 < tscVersionIndex) { - tscVersion = process.argv[tscVersionIndex + 1]; -} + var dtPath = __dirname + '/../..'; + var findNotRequiredTscparams = process.argv.some(function (arg) { + return arg == "--try-without-tscparams"; + }); + var tscVersionIndex = process.argv.indexOf("--tsc-version"); + var tscVersion = DT.DEFAULT_TSC_VERSION; + if (-1 < tscVersionIndex) { + tscVersion = process.argv[tscVersionIndex + 1]; + } -var runner = new DefinitelyTyped.TestManager.TestRunner(dtPath, { - tscVersion: tscVersion, - findNotRequiredTscparams: findNotRequiredTscparams -}); -runner.run(); + var runner = new TestRunner(dtPath, { + tscVersion: tscVersion, + findNotRequiredTscparams: findNotRequiredTscparams + }); + runner.run(); +})(DT || (DT = {})); +//# sourceMappingURL=runner.js.map diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index f8746ab518..86a4c2caa3 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -1,60 +1,33 @@ /// -/// -/// +/// +/// -module DefinitelyTyped.TestManager { +/// +/// +/// +/// + +/// +/// + +/// +/// +/// +/// + +module DT { + var fs = require('fs'); var path = require('path'); export var DEFAULT_TSC_VERSION = "0.9.1.1"; - function endsWith(str:string, suffix:string) { - return str.indexOf(suffix, str.length - suffix.length) !== -1; - } - - export interface TscExecOptions { - tscVersion?:string; - useTscParams?:boolean; - checkNoImplicitAny?:boolean; - } - - class Tsc { - public static run(tsfile:string, options:TscExecOptions, callback:(result:ExecResult)=>void) { - options = options || {}; - options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; - if (typeof options.checkNoImplicitAny === "undefined") { - options.checkNoImplicitAny = true; - } - if (typeof options.useTscParams === "undefined") { - options.useTscParams = true; - } - - if (!IO.fileExists(tsfile)) { - throw new Error(tsfile + " not exists"); - } - - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!IO.fileExists(tscPath)) { - throw new Error(tscPath + ' is not exists'); - } - var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && IO.fileExists(tsfile + '.tscparams')) { - command += '@' + tsfile + '.tscparams'; - } else if (options.checkNoImplicitAny) { - command += '--noImplicitAny'; - } - Exec.exec(command, [tsfile], (execResult) => { - callback(execResult); - }); - } - } - - class Test { - constructor(public suite:ITestSuite, public tsfile:File, public options?:TscExecOptions) { + export class Test { + constructor(public suite: ITestSuite, public tsfile: File, public options?: TscExecOptions) { } - public run(callback:(result:TestResult)=>void) { - Tsc.run(this.tsfile.filePathWithName, this.options, (execResult)=> { + public run(callback: (result: TestResult) => void) { + Tsc.run(this.tsfile.filePathWithName, this.options, (execResult) => { var testResult = new TestResult(); testResult.hostedBy = this.suite; testResult.targetFile = this.tsfile; @@ -68,380 +41,22 @@ module DefinitelyTyped.TestManager { }); } } - - ///////////////////////////////// - // Timer.start starts a timer - // Timer.end stops the timer and sets asString to the pretty print value - ///////////////////////////////// - export class Timer { - startTime:number; - time = 0; - asString:string; - - public start() { - this.time = 0; - this.startTime = this.now(); - } - - public now():number { - return Date.now(); - } - - public end() { - this.time = (this.now() - this.startTime) / 1000; - this.asString = Timer.prettyDate(this.startTime, this.now()); - } - - public static prettyDate(date1:number, date2:number):string { - var diff = ((date2 - date1) / 1000), - day_diff = Math.floor(diff / 86400); - - if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) - return; - - return (day_diff == 0 && ( - diff < 60 && (diff + " seconds") || - diff < 120 && "1 minute" || - diff < 3600 && Math.floor(diff / 60) + " minutes" || - diff < 7200 && "1 hour" || - diff < 86400 && Math.floor(diff / 3600) + " hours") || - day_diff == 1 && "Yesterday" || - day_diff < 7 && day_diff + " days" || - day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); - } - } - - ///////////////////////////////// - // Given a document root + ts file pattern this class returns: - // all the TS files OR just tests OR just definition files - ///////////////////////////////// - export class File { - dir:string; - file:string; - ext:string; - - constructor(public baseDir:string, public filePathWithName:string) { - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - this.dir = dirName.split('/')[0]; - this.file = path.basename(this.filePathWithName, '.ts'); - this.ext = path.extname(this.filePathWithName); - } - - // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' - public get formatName():string { - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - return this.dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + this.file + this.ext; - } - } - ///////////////////////////////// // Test results ///////////////////////////////// export class TestResult { - hostedBy:ITestSuite; - targetFile:File; - options:TscExecOptions; + hostedBy: ITestSuite; + targetFile: File; + options: TscExecOptions; - stdout:string; - stderr:string; - exitCode:number; + stdout: string; + stderr: string; + exitCode: number; - public get success():boolean { + public get success(): boolean { return this.exitCode === 0; } } - - ///////////////////////////////// - // The interface for test suite - ///////////////////////////////// - export interface ITestSuite { - testSuiteName:string; - errorHeadline:string; - filterTargetFiles(files:File[]):File[]; - - start(targetFiles:File[], testCallback:(result:TestResult, index:number)=>void, suiteCallback:(suite:ITestSuite)=>void):void; - - testResults:TestResult[]; - okTests:TestResult[]; - ngTests:TestResult[]; - timer:Timer; - - testReporter:ITestReporter; - printErrorCount:boolean; - } - - ///////////////////////////////// - // Test reporter interface - // for example, . and x - ///////////////////////////////// - export interface ITestReporter { - printPositiveCharacter(index:number, testResult:TestResult):void; - printNegativeCharacter(index:number, testResult:TestResult):void; - } - - ///////////////////////////////// - // Default test reporter - ///////////////////////////////// - class DefaultTestReporter implements ITestReporter { - constructor(public print:Print) { - } - - public printPositiveCharacter(index:number, testResult:TestResult) { - this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); - - this.printBreakIfNeeded(index); - } - - public printNegativeCharacter(index:number, testResult:TestResult) { - this.print.out("x"); - - this.printBreakIfNeeded(index); - } - - private printBreakIfNeeded(index:number) { - if (index % this.print.WIDTH === 0) { - this.print.printBreak(); - } - } - } - - ///////////////////////////////// - // All the common things that we pring are functions of this class - ///////////////////////////////// - class Print { - - WIDTH = 77; - - constructor(public version:string, public typings:number, public tests:number, public tsFiles:number) { - } - - public out(s:any):Print { - process.stdout.write(s); - return this; - } - - public repeat(s:string, times:number):string { - return new Array(times + 1).join(s); - } - - public printHeader() { - this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); - this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); - this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); - this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); - this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); - } - - public printSuiteHeader(title:string) { - var left = Math.floor((this.WIDTH - title.length ) / 2) - 1; - var right = Math.ceil((this.WIDTH - title.length ) / 2) - 1; - this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); - this.out(title); - this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); - } - - public printDiv() { - this.out('-----------------------------------------------------------------------------\n'); - } - - public printBoldDiv() { - this.out('=============================================================================\n'); - } - - public printErrorsHeader() { - this.out('=============================================================================\n'); - this.out(' \33[34m\33[1mErrors in files\33[0m \n'); - this.out('=============================================================================\n'); - } - - public printErrorsForFile(testResult:TestResult) { - this.out('----------------- For file:' + testResult.targetFile.formatName); - this.printBreak().out(testResult.stderr).printBreak(); - } - - public printBreak():Print { - this.out('\n'); - return this; - } - - public clearCurrentLine():Print { - this.out("\r\33[K"); - return this; - } - - public printSuccessCount(current:number, total:number) { - this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - } - - public printFailedCount(current:number, total:number) { - this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - } - - public printTypingsWithoutTestsMessage() { - this.out(' \33[36m\33[1mTyping without tests\33[0m\n'); - } - - public printTotalMessage() { - this.out(' \33[36m\33[1mTotal\33[0m\n'); - } - - public printElapsedTime(time:string, s:number) { - this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); - } - - public printSuiteErrorCount(errorHeadline:string, current:number, total:number, valuesColor = '\33[31m\33[1m') { - this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); - this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - } - - public printTypingsWithoutTestName(file:string) { - this.out(' - \33[33m\33[1m' + file + '\33[0m\n'); - } - - public printTypingsWithoutTest(withoutTestTypings:string[]) { - if (withoutTestTypings.length > 0) { - this.printTypingsWithoutTestsMessage(); - - this.printDiv(); - withoutTestTypings.forEach(t=> { - this.printTypingsWithoutTestName(t); - }); - } - } - } - - ///////////////////////////////// - // Base class for test suite - ///////////////////////////////// - class TestSuiteBase implements ITestSuite { - timer:Timer = new Timer(); - testResults:TestResult[] = []; - testReporter:ITestReporter; - printErrorCount = true; - - constructor(public options:ITestRunnerOptions, public testSuiteName:string, public errorHeadline:string) { - } - - public filterTargetFiles(files:File[]):File[] { - throw new Error("please implement this method"); - } - - public start(targetFiles:File[], testCallback:(result:TestResult, index:number)=>void, suiteCallback:(suite:ITestSuite)=>void):void { - targetFiles = this.filterTargetFiles(targetFiles); - this.timer.start(); - var count = 0; - // exec test is async process. serialize. - var executor = () => { - var targetFile = targetFiles[count]; - if (targetFile) { - this.runTest(targetFile, (result)=> { - testCallback(result, count + 1); - count++; - executor(); - }); - } else { - this.timer.end(); - this.finish(suiteCallback); - } - }; - executor(); - } - - public runTest(targetFile:File, callback:(result:TestResult)=>void):void { - new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run(result=> { - this.testResults.push(result); - callback(result); - }); - } - - public finish(suiteCallback:(suite:ITestSuite)=>void) { - suiteCallback(this); - } - - public get okTests():TestResult[] { - return this.testResults.filter(r=>r.success); - } - - public get ngTests():TestResult[] { - return this.testResults.filter(r=>!r.success); - } - } - - ///////////////////////////////// - // .d.ts syntax inspection - ///////////////////////////////// - class SyntaxChecking extends TestSuiteBase { - - constructor(options:ITestRunnerOptions) { - super(options, "Syntax checking", "Syntax error"); - } - - public filterTargetFiles(files:File[]):File[] { - return files.filter(file => endsWith(file.formatName.toUpperCase(), '.D.TS')); - } - } - - ///////////////////////////////// - // Compile with *-tests.ts - ///////////////////////////////// - class TestEval extends TestSuiteBase { - - constructor(options) { - super(options, "Typing tests", "Failed tests"); - } - - public filterTargetFiles(files:File[]):File[] { - return files.filter(file => endsWith(file.formatName.toUpperCase(), '-TESTS.TS')); - } - } - - ///////////////////////////////// - // Try compile without .tscparams - // It may indicate that it is compatible with --noImplicitAny maybe... - ///////////////////////////////// - class FindNotRequiredTscparams extends TestSuiteBase { - testReporter:ITestReporter; - printErrorCount = false; - - constructor(options:ITestRunnerOptions, private print:Print) { - super(options, "Find not required .tscparams files", "New arrival!"); - - this.testReporter = { - printPositiveCharacter: (index:number, testResult:TestResult)=> { - this.print - .clearCurrentLine() - .printTypingsWithoutTestName(testResult.targetFile.formatName); - }, - printNegativeCharacter: (index:number, testResult:TestResult)=> { - } - } - } - - public filterTargetFiles(files:File[]):File[] { - return files.filter(file=> IO.fileExists(file.filePathWithName + '.tscparams')); - } - - public runTest(targetFile:File, callback:(result:TestResult)=>void):void { - this.print.clearCurrentLine().out(targetFile.formatName); - new Test(this, targetFile, {tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true}).run(result=> { - this.testResults.push(result); - callback(result); - }); - } - - public finish(suiteCallback:(suite:ITestSuite)=>void) { - this.print.clearCurrentLine(); - suiteCallback(this); - } - - public get ngTests():TestResult[] { - // Do not show ng test results - return []; - } - } - export interface ITestRunnerOptions { tscVersion:string; findNotRequiredTscparams?:boolean; @@ -451,23 +66,31 @@ module DefinitelyTyped.TestManager { // The main class to kick things off ///////////////////////////////// export class TestRunner { - files:File[]; - timer:Timer; - suites:ITestSuite[] = []; - private print:Print; + files: File[]; + timer: Timer; + suites: ITestSuite[] = []; + private print: Print; - constructor(dtPath:string, public options:ITestRunnerOptions = {tscVersion: DEFAULT_TSC_VERSION}) { + constructor(dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; var filesName = IO.dir(dtPath, /.\.ts/g, { recursive: true }).sort(); // only includes .d.ts or -tests.ts or -test.ts or .ts - filesName = filesName - .filter(fileName => fileName.indexOf('../_infrastructure') < 0) - .filter(fileName => !endsWith(fileName, ".tscparams")); - this.files = filesName.map(fileName => new File(dtPath, fileName)); + this.files = filesName + .filter((fileName) => { + return fileName.indexOf('../_infrastructure') < 0; + }) + .filter((fileName) => { + return fileName.indexOf('../node_modules') < 0; + }) + .filter((fileName) => { + return !DT.endsWith(fileName, ".tscparams"); + }).map((fileName) => { + return new File(dtPath, fileName); + }); } - public addSuite(suite:ITestSuite) { + public addSuite(suite: ITestSuite) { this.suites.push(suite); } @@ -504,7 +127,7 @@ module DefinitelyTyped.TestManager { (testResult, index) => { this.testCompleteCallback(testResult, index); }, - suite=> { + (suite) => { this.suiteCompleteCallback(suite); count++; executor(); @@ -517,7 +140,7 @@ module DefinitelyTyped.TestManager { executor(); } - private testCompleteCallback(testResult:TestResult, index:number) { + private testCompleteCallback(testResult: TestResult, index: number) { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { reporter.printPositiveCharacter(index, testResult); @@ -526,7 +149,7 @@ module DefinitelyTyped.TestManager { } } - private suiteCompleteCallback(suite:ITestSuite) { + private suiteCompleteCallback(suite: ITestSuite) { this.print.printBreak(); this.print.printDiv(); @@ -536,16 +159,26 @@ module DefinitelyTyped.TestManager { } private allTestCompleteCallback() { - var testEval = this.suites.filter(suite=>suite instanceof TestEval)[0]; + var testEval = this.suites.filter(suite => suite instanceof TestEval)[0]; if (testEval) { - var existsTestTypings:string[] = testEval.testResults - .map(testResult=>testResult.targetFile.dir) - .reduce((a, b)=> a.indexOf(b) < 0 ? a.concat([b]) : a, []); - var typings:string[] = this.files - .map(file=>file.dir) - .reduce((a, b)=> a.indexOf(b) < 0 ? a.concat([b]) : a, []); - var withoutTestTypings:string[] = typings - .filter(typing=>existsTestTypings.indexOf(typing) < 0); + var existsTestTypings: string[] = testEval.testResults + .map((testResult) => { + return testResult.targetFile.dir; + }) + .reduce((a: string[], b: string) => { + return a.indexOf(b) < 0 ? a.concat([b]) : a; + }, []); + var typings: string[] = this.files + .map((file) => { + return file.dir; + }) + .reduce((a: string[], b: string) => { + return a.indexOf(b) < 0 ? a.concat([b]) : a; + }, []); + var withoutTestTypings: string[] = typings + .filter((typing) => { + return existsTestTypings.indexOf(typing) < 0; + }); this.print.printDiv(); this.print.printTypingsWithoutTest(withoutTestTypings); } @@ -556,8 +189,10 @@ module DefinitelyTyped.TestManager { this.print.printDiv(); this.print.printElapsedTime(this.timer.asString, this.timer.time); this.suites - .filter(suite=>suite.printErrorCount) - .forEach(suite=> { + .filter((suite: ITestSuite) => { + return suite.printErrorCount; + }) + .forEach((suite: ITestSuite) => { this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); }); if (testEval) { @@ -565,13 +200,17 @@ module DefinitelyTyped.TestManager { } this.print.printDiv(); - if (this.suites.some(suite=>suite.ngTests.length !== 0)) { + if (this.suites.some((suite) => { + return suite.ngTests.length !== 0 + })) { this.print.printErrorsHeader(); this.suites - .filter(suite=>suite.ngTests.length !== 0) - .forEach(suite=> { - suite.ngTests.forEach(testResult => { + .filter((suite) => { + return suite.ngTests.length !== 0; + }) + .forEach((suite) => { + suite.ngTests.forEach((testResult) => { this.print.printErrorsForFile(testResult); }); this.print.printBoldDiv(); @@ -581,18 +220,18 @@ module DefinitelyTyped.TestManager { } } } -} -var dtPath = __dirname + '/../..'; -var findNotRequiredTscparams = process.argv.some(arg=>arg == "--try-without-tscparams"); -var tscVersionIndex = process.argv.indexOf("--tsc-version"); -var tscVersion = DefinitelyTyped.TestManager.DEFAULT_TSC_VERSION; -if (-1 < tscVersionIndex) { - tscVersion = process.argv[tscVersionIndex + 1]; -} + var dtPath = __dirname + '/../..'; + var findNotRequiredTscparams = process.argv.some(arg => arg == "--try-without-tscparams"); + var tscVersionIndex = process.argv.indexOf("--tsc-version"); + var tscVersion = DEFAULT_TSC_VERSION; + if (-1 < tscVersionIndex) { + tscVersion = process.argv[tscVersionIndex + 1]; + } -var runner = new DefinitelyTyped.TestManager.TestRunner(dtPath, { - tscVersion: tscVersion, - findNotRequiredTscparams: findNotRequiredTscparams -}); -runner.run(); + var runner = new TestRunner(dtPath, { + tscVersion: tscVersion, + findNotRequiredTscparams: findNotRequiredTscparams + }); + runner.run(); +} diff --git a/_infrastructure/tests/src/exec.js b/_infrastructure/tests/src/exec.js deleted file mode 100644 index 2b8ea5c597..0000000000 --- a/_infrastructure/tests/src/exec.js +++ /dev/null @@ -1,79 +0,0 @@ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -var ExecResult = (function () { - function ExecResult() { - this.stdout = ""; - this.stderr = ""; - } - return ExecResult; -})(); - -var WindowsScriptHostExec = (function () { - function WindowsScriptHostExec() { - } - WindowsScriptHostExec.prototype.exec = function (filename, cmdLineArgs, handleResult) { - var result = new ExecResult(); - var shell = new ActiveXObject('WScript.Shell'); - try { - var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' ')); - } catch (e) { - result.stderr = e.message; - result.exitCode = 1; - handleResult(result); - return; - } - - while (process.Status != 0) { - } - - result.exitCode = process.ExitCode; - if (!process.StdOut.AtEndOfStream) - result.stdout = process.StdOut.ReadAll(); - if (!process.StdErr.AtEndOfStream) - result.stderr = process.StdErr.ReadAll(); - - handleResult(result); - }; - return WindowsScriptHostExec; -})(); - -var NodeExec = (function () { - function NodeExec() { - } - NodeExec.prototype.exec = function (filename, cmdLineArgs, handleResult) { - var nodeExec = require('child_process').exec; - - var result = new ExecResult(); - result.exitCode = null; - var cmdLine = filename + ' ' + cmdLineArgs.join(' '); - - var process = nodeExec(cmdLine, function (error, stdout, stderr) { - result.stdout = stdout; - result.stderr = stderr; - result.exitCode = error ? error.code : 0; - handleResult(result); - }); - }; - return NodeExec; -})(); - -var Exec = (function () { - var global = Function("return this;").call(null); - if (typeof global.ActiveXObject !== "undefined") { - return new WindowsScriptHostExec(); - } else { - return new NodeExec(); - } -})(); diff --git a/_infrastructure/tests/src/file.ts b/_infrastructure/tests/src/file.ts new file mode 100644 index 0000000000..f349a09da6 --- /dev/null +++ b/_infrastructure/tests/src/file.ts @@ -0,0 +1,26 @@ +module DT { + var path = require('path'); + + ///////////////////////////////// + // Given a document root + ts file pattern this class returns: + // all the TS files OR just tests OR just definition files + ///////////////////////////////// + export class File { + dir: string; + file: string; + ext: string; + + constructor(public baseDir: string, public filePathWithName: string) { + var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); + this.dir = dirName.split('/')[0]; + this.file = path.basename(this.filePathWithName, '.ts'); + this.ext = path.extname(this.filePathWithName); + } + + // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' + public get formatName(): string { + var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); + return this.dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + this.file + this.ext; + } + } +} diff --git a/_infrastructure/tests/src/exec.ts b/_infrastructure/tests/src/host/exec.ts similarity index 83% rename from _infrastructure/tests/src/exec.ts rename to _infrastructure/tests/src/host/exec.ts index 5123d4cfc6..2bddb0043c 100644 --- a/_infrastructure/tests/src/exec.ts +++ b/_infrastructure/tests/src/host/exec.ts @@ -18,8 +18,6 @@ interface IExec { exec: (filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) => void; } -declare var require; - class ExecResult { public stdout = ""; public stderr = ""; @@ -27,31 +25,31 @@ class ExecResult { } class WindowsScriptHostExec implements IExec { - public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) : void { + public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { var result = new ExecResult(); var shell = new ActiveXObject('WScript.Shell'); try { var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' ')); - } catch(e) { + } catch (e) { result.stderr = e.message; result.exitCode = 1 handleResult(result); return; } // Wait for it to finish running - while (process.Status != 0) { /* todo: sleep? */ } + while (process.Status != 0) { /* todo: sleep? */ + } - result.exitCode = process.ExitCode; - if(!process.StdOut.AtEndOfStream) result.stdout = process.StdOut.ReadAll(); - if(!process.StdErr.AtEndOfStream) result.stderr = process.StdErr.ReadAll(); + if (!process.StdOut.AtEndOfStream) result.stdout = process.StdOut.ReadAll(); + if (!process.StdErr.AtEndOfStream) result.stderr = process.StdErr.ReadAll(); handleResult(result); } } class NodeExec implements IExec { - public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) : void { + public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { var nodeExec = require('child_process').exec; var result = new ExecResult(); @@ -67,11 +65,11 @@ class NodeExec implements IExec { } } -var Exec: IExec = function() : IExec { +var Exec: IExec = function (): IExec { var global = Function("return this;").call(null); - if(typeof global.ActiveXObject !== "undefined") { + if (typeof global.ActiveXObject !== "undefined") { return new WindowsScriptHostExec(); } else { return new NodeExec(); } -}(); \ No newline at end of file +}(); diff --git a/_infrastructure/tests/src/io.ts b/_infrastructure/tests/src/host/io.ts similarity index 81% rename from _infrastructure/tests/src/io.ts rename to _infrastructure/tests/src/host/io.ts index 2c7a81f4f3..b308ef13f6 100644 --- a/_infrastructure/tests/src/io.ts +++ b/_infrastructure/tests/src/host/io.ts @@ -27,7 +27,8 @@ interface IIO { writeFile(path: string, contents: string): void; createFile(path: string, useUTF8?: boolean): ITextWriter; deleteFile(path: string): void; - dir(path: string, re?: RegExp, options?: { recursive?: boolean; deep?: number; }): string[]; + dir(path: string, re?: RegExp, options?: { recursive?: boolean; deep?: number; + }): string[]; fileExists(path: string): boolean; directoryExists(path: string): boolean; createDirectory(path: string): void; @@ -39,7 +40,7 @@ interface IIO { arguments: string[]; stderr: ITextWriter; stdout: ITextWriter; - watchFile(filename: string, callback: (string) => void ): IFileWatcher; + watchFile(filename: string, callback: (string) => void): IFileWatcher; run(source: string, filename: string): void; getExecutingFilePath(): string; quit(exitCode?: number); @@ -79,9 +80,12 @@ module IOUtils { // Declare dependencies needed for all supported hosts declare class Enumerator { public atEnd(): boolean; + public moveNext(); + public item(): any; - constructor (o: any); + + constructor(o: any); } // Missing in node definitions, but called in code below. @@ -91,7 +95,7 @@ interface NodeProcess { } } -var IO = (function() { +var IO = (function () { // Create an IO object for use inside WindowsScriptHost hosts // Depends on WSCript and FileSystemObject @@ -99,15 +103,15 @@ var IO = (function() { var fso = new ActiveXObject("Scripting.FileSystemObject"); var streamObjectPool = []; - function getStreamObject(): any { + function getStreamObject(): any { if (streamObjectPool.length > 0) { return streamObjectPool.pop(); - } else { + } else { return new ActiveXObject("ADODB.Stream"); } } - function releaseStreamObject(obj: any) { + function releaseStreamObject(obj: any) { streamObjectPool.push(obj); } @@ -117,7 +121,7 @@ var IO = (function() { } return { - readFile: function(path) { + readFile: function (path) { try { var streamObj = getStreamObject(); streamObj.Open(); @@ -130,7 +134,7 @@ var IO = (function() { || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) { streamObj.Charset = 'unicode'; } else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) { - streamObj.Charset = 'utf-8'; + streamObj.Charset = 'utf-8'; } // Read the whole file @@ -144,25 +148,25 @@ var IO = (function() { } }, - writeFile: function(path, contents) { + writeFile: function (path, contents) { var file = this.createFile(path); file.Write(contents); file.Close(); }, - fileExists: function(path: string): boolean { + fileExists: function (path: string): boolean { return fso.FileExists(path); }, - resolvePath: function(path: string): string { + resolvePath: function (path: string): string { return fso.GetAbsolutePathName(path); }, - dirName: function(path: string): string { + dirName: function (path: string): string { return fso.GetParentFolderName(path); }, - findFile: function(rootPath: string, partialFilePath: string): IResolvedFile { + findFile: function (rootPath: string, partialFilePath: string): IResolvedFile { var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath; while (true) { @@ -188,7 +192,7 @@ var IO = (function() { } }, - deleteFile: function(path: string): void { + deleteFile: function (path: string): void { try { if (fso.FileExists(path)) { fso.DeleteFile(path, true); // true: delete read-only files @@ -204,9 +208,13 @@ var IO = (function() { streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi'; streamObj.Open(); return { - Write: function (str) { streamObj.WriteText(str, 0); }, - WriteLine: function (str) { streamObj.WriteText(str, 1); }, - Close: function() { + Write: function (str) { + streamObj.WriteText(str, 0); + }, + WriteLine: function (str) { + streamObj.WriteText(str, 1); + }, + Close: function () { try { streamObj.SaveToFile(path, 2); } catch (saveError) { @@ -225,11 +233,11 @@ var IO = (function() { } }, - directoryExists: function(path) { + directoryExists: function (path) { return fso.FolderExists(path); }, - createDirectory: function(path) { + createDirectory: function (path) { try { if (!this.directoryExists(path)) { fso.CreateFolder(path); @@ -239,23 +247,24 @@ var IO = (function() { } }, - dir: function(path, spec?, options?) { - options = options || <{ recursive?: boolean; deep?: number; }>{}; - function filesInFolder(folder, root): string[]{ + dir: function (path, spec?, options?) { + options = options || <{ recursive?: boolean; deep?: number; + }>{}; + function filesInFolder(folder, root): string[] { var paths = []; var fc: Enumerator; if (options.recursive) { fc = new Enumerator(folder.subfolders); - for (; !fc.atEnd() ; fc.moveNext()) { + for (; !fc.atEnd(); fc.moveNext()) { paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name)); } } fc = new Enumerator(folder.files); - for (; !fc.atEnd() ; fc.moveNext()) { + for (; !fc.atEnd(); fc.moveNext()) { if (!spec || fc.item().Name.match(spec)) { paths.push(root + "/" + fc.item().Name); } @@ -270,11 +279,11 @@ var IO = (function() { return filesInFolder(folder, path); }, - print: function(str) { + print: function (str) { WScript.StdOut.Write(str); }, - printLine: function(str) { + printLine: function (str) { WScript.Echo(str); }, @@ -282,7 +291,7 @@ var IO = (function() { stderr: WScript.StdErr, stdout: WScript.StdOut, watchFile: null, - run: function(source, filename) { + run: function (source, filename) { try { eval(source); } catch (e) { @@ -292,7 +301,7 @@ var IO = (function() { getExecutingFilePath: function () { return WScript.ScriptFullName; }, - quit: function (exitCode : number = 0) { + quit: function (exitCode: number = 0) { try { WScript.Quit(exitCode); } catch (e) { @@ -311,7 +320,7 @@ var IO = (function() { var _module = require('module'); return { - readFile: function(file) { + readFile: function (file) { try { var buffer = _fs.readFileSync(file); switch (buffer[0]) { @@ -348,17 +357,17 @@ var IO = (function() { } }, writeFile: <(path: string, contents: string) => void >_fs.writeFileSync, - deleteFile: function(path) { + deleteFile: function (path) { try { _fs.unlinkSync(path); } catch (e) { IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); } }, - fileExists: function(path): boolean { + fileExists: function (path): boolean { return _fs.existsSync(path); }, - createFile: function(path, useUTF8?) { + createFile: function (path, useUTF8?) { function mkdirRecursiveSync(path) { var stats = _fs.statSync(path); if (stats.isFile()) { @@ -367,7 +376,7 @@ var IO = (function() { return; } else { mkdirRecursiveSync(_path.dirname(path)); - _fs.mkdirSync(path, 0775); + _fs.mkdirSync(path, parseInt('0775', 8)); } } @@ -379,15 +388,23 @@ var IO = (function() { IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e); } return { - Write: function(str) { _fs.writeSync(fd, str); }, - WriteLine: function(str) { _fs.writeSync(fd, str + '\r\n'); }, - Close: function() { _fs.closeSync(fd); fd = null; } + Write: function (str) { + _fs.writeSync(fd, str); + }, + WriteLine: function (str) { + _fs.writeSync(fd, str + '\r\n'); + }, + Close: function () { + _fs.closeSync(fd); + fd = null; + } }; }, dir: function dir(path, spec?, options?) { - options = options || <{ recursive?: boolean; deep?: number; }>{}; + options = options || <{ recursive?: boolean; deep?: number; + }>{}; - function filesInFolder(folder: string, deep?: number): string[]{ + function filesInFolder(folder: string, deep?: number): string[] { var paths = []; var files = _fs.readdirSync(folder); @@ -407,7 +424,7 @@ var IO = (function() { return filesInFolder(path, 0); }, - createDirectory: function(path: string): void { + createDirectory: function (path: string): void { try { if (!this.directoryExists(path)) { _fs.mkdirSync(path); @@ -417,16 +434,16 @@ var IO = (function() { } }, - directoryExists: function(path: string): boolean { + directoryExists: function (path: string): boolean { return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory(); }, - resolvePath: function(path: string): string { + resolvePath: function (path: string): string { return _path.resolve(path); }, - dirName: function(path: string): string { + dirName: function (path: string): string { return _path.dirname(path); }, - findFile: function(rootPath: string, partialFilePath): IResolvedFile { + findFile: function (rootPath: string, partialFilePath): IResolvedFile { var path = rootPath + "/" + partialFilePath; while (true) { @@ -452,24 +469,38 @@ var IO = (function() { } } }, - print: function(str) { process.stdout.write(str) }, - printLine: function(str) { process.stdout.write(str + '\n') }, + print: function (str) { + process.stdout.write(str) + }, + printLine: function (str) { + process.stdout.write(str + '\n') + }, arguments: process.argv.slice(2), stderr: { - Write: function(str) { process.stderr.write(str); }, - WriteLine: function(str) { process.stderr.write(str + '\n'); }, - Close: function() { } + Write: function (str) { + process.stderr.write(str); + }, + WriteLine: function (str) { + process.stderr.write(str + '\n'); + }, + Close: function () { + } }, stdout: { - Write: function(str) { process.stdout.write(str); }, - WriteLine: function(str) { process.stdout.write(str + '\n'); }, - Close: function() { } + Write: function (str) { + process.stdout.write(str); + }, + WriteLine: function (str) { + process.stdout.write(str + '\n'); + }, + Close: function () { + } }, - watchFile: function(filename: string, callback: (string) => void ): IFileWatcher { + watchFile: function (filename: string, callback: (string) => void): IFileWatcher { var firstRun = true; var processingChange = false; - var fileChanged: any = function(curr, prev) { + var fileChanged: any = function (curr, prev) { if (!firstRun) { if (curr.mtime < prev.mtime) { return; @@ -479,7 +510,9 @@ var IO = (function() { if (!processingChange) { processingChange = true; callback(filename); - setTimeout(function() { processingChange = false; }, 100); + setTimeout(function () { + processingChange = false; + }, 100); } } firstRun = false; @@ -489,16 +522,16 @@ var IO = (function() { fileChanged(); return { filename: filename, - close: function() { + close: function () { _fs.unwatchFile(filename, fileChanged); } }; }, - run: function(source, filename) { + run: function (source, filename) { require.main.filename = filename; require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename))); require.main._compile(source, filename); - }, + }, getExecutingFilePath: function () { return process.mainModule.filename; }, diff --git a/_infrastructure/tests/src/indexer.ts b/_infrastructure/tests/src/indexer.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/_infrastructure/tests/src/io.js b/_infrastructure/tests/src/io.js deleted file mode 100644 index 0f8b9516c8..0000000000 --- a/_infrastructure/tests/src/io.js +++ /dev/null @@ -1,475 +0,0 @@ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -var IOUtils; -(function (IOUtils) { - // Creates the directory including its parent if not already present - function createDirectoryStructure(ioHost, dirName) { - if (ioHost.directoryExists(dirName)) { - return; - } - - var parentDirectory = ioHost.dirName(dirName); - if (parentDirectory != "") { - createDirectoryStructure(ioHost, parentDirectory); - } - ioHost.createDirectory(dirName); - } - - // Creates a file including its directory structure if not already present - function createFileAndFolderStructure(ioHost, fileName, useUTF8) { - var path = ioHost.resolvePath(fileName); - var dirName = ioHost.dirName(path); - createDirectoryStructure(ioHost, dirName); - return ioHost.createFile(path, useUTF8); - } - IOUtils.createFileAndFolderStructure = createFileAndFolderStructure; - - function throwIOError(message, error) { - var errorMessage = message; - if (error && error.message) { - errorMessage += (" " + error.message); - } - throw new Error(errorMessage); - } - IOUtils.throwIOError = throwIOError; -})(IOUtils || (IOUtils = {})); - -var IO = (function () { - // Create an IO object for use inside WindowsScriptHost hosts - // Depends on WSCript and FileSystemObject - function getWindowsScriptHostIO() { - var fso = new ActiveXObject("Scripting.FileSystemObject"); - var streamObjectPool = []; - - function getStreamObject() { - if (streamObjectPool.length > 0) { - return streamObjectPool.pop(); - } else { - return new ActiveXObject("ADODB.Stream"); - } - } - - function releaseStreamObject(obj) { - streamObjectPool.push(obj); - } - - var args = []; - for (var i = 0; i < WScript.Arguments.length; i++) { - args[i] = WScript.Arguments.Item(i); - } - - return { - readFile: function (path) { - try { - var streamObj = getStreamObject(); - streamObj.Open(); - streamObj.Type = 2; - streamObj.Charset = 'x-ansi'; - streamObj.LoadFromFile(path); - var bomChar = streamObj.ReadText(2); - streamObj.Position = 0; - if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF) || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) { - streamObj.Charset = 'unicode'; - } else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) { - streamObj.Charset = 'utf-8'; - } - - // Read the whole file - var str = streamObj.ReadText(-1); - streamObj.Close(); - releaseStreamObject(streamObj); - return str; - } catch (err) { - IOUtils.throwIOError("Error reading file \"" + path + "\".", err); - } - }, - writeFile: function (path, contents) { - var file = this.createFile(path); - file.Write(contents); - file.Close(); - }, - fileExists: function (path) { - return fso.FileExists(path); - }, - resolvePath: function (path) { - return fso.GetAbsolutePathName(path); - }, - dirName: function (path) { - return fso.GetParentFolderName(path); - }, - findFile: function (rootPath, partialFilePath) { - var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath; - - while (true) { - if (fso.FileExists(path)) { - try { - var content = this.readFile(path); - return { content: content, path: path }; - } catch (err) { - //Tools.CompilerDiagnostics.debugPrint("Could not find " + path + ", trying parent"); - } - } else { - rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath)); - - if (rootPath == "") { - return null; - } else { - path = fso.BuildPath(rootPath, partialFilePath); - } - } - } - }, - deleteFile: function (path) { - try { - if (fso.FileExists(path)) { - fso.DeleteFile(path, true); - } - } catch (e) { - IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); - } - }, - createFile: function (path, useUTF8) { - try { - var streamObj = getStreamObject(); - streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi'; - streamObj.Open(); - return { - Write: function (str) { - streamObj.WriteText(str, 0); - }, - WriteLine: function (str) { - streamObj.WriteText(str, 1); - }, - Close: function () { - try { - streamObj.SaveToFile(path, 2); - } catch (saveError) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError); - } finally { - if (streamObj.State != 0) { - streamObj.Close(); - } - releaseStreamObject(streamObj); - } - } - }; - } catch (creationError) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError); - } - }, - directoryExists: function (path) { - return fso.FolderExists(path); - }, - createDirectory: function (path) { - try { - if (!this.directoryExists(path)) { - fso.CreateFolder(path); - } - } catch (e) { - IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e); - } - }, - dir: function (path, spec, options) { - options = options || {}; - function filesInFolder(folder, root) { - var paths = []; - var fc; - - if (options.recursive) { - fc = new Enumerator(folder.subfolders); - - for (; !fc.atEnd(); fc.moveNext()) { - paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name)); - } - } - - fc = new Enumerator(folder.files); - - for (; !fc.atEnd(); fc.moveNext()) { - if (!spec || fc.item().Name.match(spec)) { - paths.push(root + "/" + fc.item().Name); - } - } - - return paths; - } - - var folder = fso.GetFolder(path); - var paths = []; - - return filesInFolder(folder, path); - }, - print: function (str) { - WScript.StdOut.Write(str); - }, - printLine: function (str) { - WScript.Echo(str); - }, - arguments: args, - stderr: WScript.StdErr, - stdout: WScript.StdOut, - watchFile: null, - run: function (source, filename) { - try { - eval(source); - } catch (e) { - IOUtils.throwIOError("Error while executing file '" + filename + "'.", e); - } - }, - getExecutingFilePath: function () { - return WScript.ScriptFullName; - }, - quit: function (exitCode) { - if (typeof exitCode === "undefined") { exitCode = 0; } - try { - WScript.Quit(exitCode); - } catch (e) { - } - } - }; - } - ; - - // Create an IO object for use inside Node.js hosts - // Depends on 'fs' and 'path' modules - function getNodeIO() { - var _fs = require('fs'); - var _path = require('path'); - var _module = require('module'); - - return { - readFile: function (file) { - try { - var buffer = _fs.readFileSync(file); - switch (buffer[0]) { - case 0xFE: - if (buffer[1] == 0xFF) { - // utf16-be. Reading the buffer as big endian is not supported, so convert it to - // Little Endian first - var i = 0; - while ((i + 1) < buffer.length) { - var temp = buffer[i]; - buffer[i] = buffer[i + 1]; - buffer[i + 1] = temp; - i += 2; - } - return buffer.toString("ucs2", 2); - } - break; - case 0xFF: - if (buffer[1] == 0xFE) { - // utf16-le - return buffer.toString("ucs2", 2); - } - break; - case 0xEF: - if (buffer[1] == 0xBB) { - // utf-8 - return buffer.toString("utf8", 3); - } - } - - // Default behaviour - return buffer.toString(); - } catch (e) { - IOUtils.throwIOError("Error reading file \"" + file + "\".", e); - } - }, - writeFile: _fs.writeFileSync, - deleteFile: function (path) { - try { - _fs.unlinkSync(path); - } catch (e) { - IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); - } - }, - fileExists: function (path) { - return _fs.existsSync(path); - }, - createFile: function (path, useUTF8) { - function mkdirRecursiveSync(path) { - var stats = _fs.statSync(path); - if (stats.isFile()) { - IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null); - } else if (stats.isDirectory()) { - return; - } else { - mkdirRecursiveSync(_path.dirname(path)); - _fs.mkdirSync(path, 0775); - } - } - - mkdirRecursiveSync(_path.dirname(path)); - - try { - var fd = _fs.openSync(path, 'w'); - } catch (e) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e); - } - return { - Write: function (str) { - _fs.writeSync(fd, str); - }, - WriteLine: function (str) { - _fs.writeSync(fd, str + '\r\n'); - }, - Close: function () { - _fs.closeSync(fd); - fd = null; - } - }; - }, - dir: function dir(path, spec, options) { - options = options || {}; - - function filesInFolder(folder, deep) { - var paths = []; - - var files = _fs.readdirSync(folder); - for (var i = 0; i < files.length; i++) { - var stat = _fs.statSync(folder + "/" + files[i]); - if (options.recursive && stat.isDirectory()) { - if (deep < (options.deep || 100)) { - paths = paths.concat(filesInFolder(folder + "/" + files[i], 1)); - } - } else if (stat.isFile() && (!spec || files[i].match(spec))) { - paths.push(folder + "/" + files[i]); - } - } - - return paths; - } - - return filesInFolder(path, 0); - }, - createDirectory: function (path) { - try { - if (!this.directoryExists(path)) { - _fs.mkdirSync(path); - } - } catch (e) { - IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e); - } - }, - directoryExists: function (path) { - return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory(); - }, - resolvePath: function (path) { - return _path.resolve(path); - }, - dirName: function (path) { - return _path.dirname(path); - }, - findFile: function (rootPath, partialFilePath) { - var path = rootPath + "/" + partialFilePath; - - while (true) { - if (_fs.existsSync(path)) { - try { - var content = this.readFile(path); - return { content: content, path: path }; - } catch (err) { - //Tools.CompilerDiagnostics.debugPrint(("Could not find " + path) + ", trying parent"); - } - } else { - var parentPath = _path.resolve(rootPath, ".."); - - if (rootPath === parentPath) { - return null; - } else { - rootPath = parentPath; - path = _path.resolve(rootPath, partialFilePath); - } - } - } - }, - print: function (str) { - process.stdout.write(str); - }, - printLine: function (str) { - process.stdout.write(str + '\n'); - }, - arguments: process.argv.slice(2), - stderr: { - Write: function (str) { - process.stderr.write(str); - }, - WriteLine: function (str) { - process.stderr.write(str + '\n'); - }, - Close: function () { - } - }, - stdout: { - Write: function (str) { - process.stdout.write(str); - }, - WriteLine: function (str) { - process.stdout.write(str + '\n'); - }, - Close: function () { - } - }, - watchFile: function (filename, callback) { - var firstRun = true; - var processingChange = false; - - var fileChanged = function (curr, prev) { - if (!firstRun) { - if (curr.mtime < prev.mtime) { - return; - } - - _fs.unwatchFile(filename, fileChanged); - if (!processingChange) { - processingChange = true; - callback(filename); - setTimeout(function () { - processingChange = false; - }, 100); - } - } - firstRun = false; - _fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged); - }; - - fileChanged(); - return { - filename: filename, - close: function () { - _fs.unwatchFile(filename, fileChanged); - } - }; - }, - run: function (source, filename) { - require.main.filename = filename; - require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename))); - require.main._compile(source, filename); - }, - getExecutingFilePath: function () { - return process.mainModule.filename; - }, - quit: process.exit - }; - } - ; - - if (typeof ActiveXObject === "function") - return getWindowsScriptHostIO(); -else if (typeof require === "function") - return getNodeIO(); -else - return null; -})(); diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts new file mode 100644 index 0000000000..d1f0f952e2 --- /dev/null +++ b/_infrastructure/tests/src/printer.ts @@ -0,0 +1,111 @@ +/// +/// + +module DT { + ///////////////////////////////// + // All the common things that we pring are functions of this class + ///////////////////////////////// + export class Print { + + WIDTH = 77; + + constructor(public version: string, public typings: number, public tests: number, public tsFiles: number) { + } + + public out(s: any): Print { + process.stdout.write(s); + return this; + } + + public repeat(s: string, times: number): string { + return new Array(times + 1).join(s); + } + + public printHeader() { + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); + this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); + this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); + this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); + } + + public printSuiteHeader(title: string) { + var left = Math.floor((this.WIDTH - title.length ) / 2) - 1; + var right = Math.ceil((this.WIDTH - title.length ) / 2) - 1; + this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); + this.out(title); + this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); + } + + public printDiv() { + this.out('-----------------------------------------------------------------------------\n'); + } + + public printBoldDiv() { + this.out('=============================================================================\n'); + } + + public printErrorsHeader() { + this.out('=============================================================================\n'); + this.out(' \33[34m\33[1mErrors in files\33[0m \n'); + this.out('=============================================================================\n'); + } + + public printErrorsForFile(testResult: TestResult) { + this.out('----------------- For file:' + testResult.targetFile.formatName); + this.printBreak().out(testResult.stderr).printBreak(); + } + + public printBreak(): Print { + this.out('\n'); + return this; + } + + public clearCurrentLine(): Print { + this.out("\r\33[K"); + return this; + } + + public printSuccessCount(current: number, total: number) { + this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } + + public printFailedCount(current: number, total: number) { + this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } + + public printTypingsWithoutTestsMessage() { + this.out(' \33[36m\33[1mTyping without tests\33[0m\n'); + } + + public printTotalMessage() { + this.out(' \33[36m\33[1mTotal\33[0m\n'); + } + + public printElapsedTime(time: string, s: number) { + this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); + } + + public printSuiteErrorCount(errorHeadline: string, current: number, total: number, valuesColor = '\33[31m\33[1m') { + this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); + this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } + + public printTypingsWithoutTestName(file: string) { + this.out(' - \33[33m\33[1m' + file + '\33[0m\n'); + } + + public printTypingsWithoutTest(withoutTestTypings: string[]) { + if (withoutTestTypings.length > 0) { + this.printTypingsWithoutTestsMessage(); + + this.printDiv(); + withoutTestTypings.forEach((t) => { + this.printTypingsWithoutTestName(t); + }); + } + } + } +} diff --git a/_infrastructure/tests/src/reporter/reporter.ts b/_infrastructure/tests/src/reporter/reporter.ts new file mode 100644 index 0000000000..d4556e0cc0 --- /dev/null +++ b/_infrastructure/tests/src/reporter/reporter.ts @@ -0,0 +1,36 @@ +module DT { + ///////////////////////////////// + // Test reporter interface + // for example, . and x + ///////////////////////////////// + export interface ITestReporter { + printPositiveCharacter(index: number, testResult: TestResult):void; + printNegativeCharacter(index: number, testResult: TestResult):void; + } + + ///////////////////////////////// + // Default test reporter + ///////////////////////////////// + export class DefaultTestReporter implements ITestReporter { + constructor(public print: Print) { + } + + public printPositiveCharacter(index: number, testResult: TestResult) { + this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); + + this.printBreakIfNeeded(index); + } + + public printNegativeCharacter(index: number, testResult: TestResult) { + this.print.out("x"); + + this.printBreakIfNeeded(index); + } + + private printBreakIfNeeded(index: number) { + if (index % this.print.WIDTH === 0) { + this.print.printBreak(); + } + } + } +} diff --git a/_infrastructure/tests/src/suite/suite.ts b/_infrastructure/tests/src/suite/suite.ts new file mode 100644 index 0000000000..32013cce24 --- /dev/null +++ b/_infrastructure/tests/src/suite/suite.ts @@ -0,0 +1,83 @@ +/// + +module DT { + ///////////////////////////////// + // The interface for test suite + ///////////////////////////////// + export interface ITestSuite { + testSuiteName:string; + errorHeadline:string; + filterTargetFiles(files: File[]):File[]; + + start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void):void; + + testResults:TestResult[]; + okTests:TestResult[]; + ngTests:TestResult[]; + timer:Timer; + + testReporter:ITestReporter; + printErrorCount:boolean; + } + + ///////////////////////////////// + // Base class for test suite + ///////////////////////////////// + export class TestSuiteBase implements ITestSuite { + timer: Timer = new Timer(); + testResults: TestResult[] = []; + testReporter: ITestReporter; + printErrorCount = true; + + constructor(public options: ITestRunnerOptions, public testSuiteName: string, public errorHeadline: string) { + } + + public filterTargetFiles(files: File[]): File[] { + throw new Error("please implement this method"); + } + + public start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void): void { + targetFiles = this.filterTargetFiles(targetFiles); + this.timer.start(); + var count = 0; + // exec test is async process. serialize. + var executor = () => { + var targetFile = targetFiles[count]; + if (targetFile) { + this.runTest(targetFile, (result) => { + testCallback(result, count + 1); + count++; + executor(); + }); + } else { + this.timer.end(); + this.finish(suiteCallback); + } + }; + executor(); + } + + public runTest(targetFile: File, callback: (result: TestResult) => void): void { + new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run((result) => { + this.testResults.push(result); + callback(result); + }); + } + + public finish(suiteCallback: (suite: ITestSuite) => void) { + suiteCallback(this); + } + + public get okTests(): TestResult[] { + return this.testResults.filter((r) => { + return r.success; + }); + } + + public get ngTests(): TestResult[] { + return this.testResults.filter((r) => { + return !r.success + }); + } + } +} diff --git a/_infrastructure/tests/src/suite/syntax.ts b/_infrastructure/tests/src/suite/syntax.ts new file mode 100644 index 0000000000..f36c26d99b --- /dev/null +++ b/_infrastructure/tests/src/suite/syntax.ts @@ -0,0 +1,20 @@ +/// +/// + +module DT { + ///////////////////////////////// + // .d.ts syntax inspection + ///////////////////////////////// + export class SyntaxChecking extends TestSuiteBase { + + constructor(options: ITestRunnerOptions) { + super(options, "Syntax checking", "Syntax error"); + } + + public filterTargetFiles(files: File[]): File[] { + return files.filter((file) => { + return DT.endsWith(file.formatName.toUpperCase(), '.D.TS'); + }); + } + } +} diff --git a/_infrastructure/tests/src/suite/testEval.ts b/_infrastructure/tests/src/suite/testEval.ts new file mode 100644 index 0000000000..860e179431 --- /dev/null +++ b/_infrastructure/tests/src/suite/testEval.ts @@ -0,0 +1,21 @@ +/// +/// + +module DT { + ///////////////////////////////// + // Compile with *-tests.ts + ///////////////////////////////// + export class TestEval extends TestSuiteBase { + + constructor(options) { + super(options, "Typing tests", "Failed tests"); + } + + public filterTargetFiles(files: File[]): File[] { + return files.filter((file) => { + return DT.endsWith(file.formatName.toUpperCase(), '-TESTS.TS') + }); + } + } + +} diff --git a/_infrastructure/tests/src/suite/tscParams.ts b/_infrastructure/tests/src/suite/tscParams.ts new file mode 100644 index 0000000000..65491d30d3 --- /dev/null +++ b/_infrastructure/tests/src/suite/tscParams.ts @@ -0,0 +1,49 @@ +/// +/// + +module DT { + ///////////////////////////////// + // Try compile without .tscparams + // It may indicate that it is compatible with --noImplicitAny maybe... + ///////////////////////////////// + export class FindNotRequiredTscparams extends TestSuiteBase { + testReporter: ITestReporter; + printErrorCount = false; + + constructor(options: ITestRunnerOptions, private print: Print) { + super(options, "Find not required .tscparams files", "New arrival!"); + + this.testReporter = { + printPositiveCharacter: (index: number, testResult: TestResult) => { + this.print + .clearCurrentLine() + .printTypingsWithoutTestName(testResult.targetFile.formatName); + }, + printNegativeCharacter: (index: number, testResult: TestResult) => { + } + } + } + + public filterTargetFiles(files: File[]): File[] { + return files.filter(file=> IO.fileExists(file.filePathWithName + '.tscparams')); + } + + public runTest(targetFile: File, callback: (result: TestResult) => void): void { + this.print.clearCurrentLine().out(targetFile.formatName); + new Test(this, targetFile, {tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true}).run(result=> { + this.testResults.push(result); + callback(result); + }); + } + + public finish(suiteCallback: (suite: ITestSuite)=>void) { + this.print.clearCurrentLine(); + suiteCallback(this); + } + + public get ngTests(): TestResult[] { + // Do not show ng test results + return []; + } + } +} diff --git a/_infrastructure/tests/src/timer.ts b/_infrastructure/tests/src/timer.ts new file mode 100644 index 0000000000..a92d08fafa --- /dev/null +++ b/_infrastructure/tests/src/timer.ts @@ -0,0 +1,46 @@ +/// +/// + +module DT { + ///////////////////////////////// + // Timer.start starts a timer + // Timer.end stops the timer and sets asString to the pretty print value + ///////////////////////////////// + export class Timer { + startTime: number; + time = 0; + asString: string; + + public start() { + this.time = 0; + this.startTime = this.now(); + } + + public now(): number { + return Date.now(); + } + + public end() { + this.time = (this.now() - this.startTime) / 1000; + this.asString = Timer.prettyDate(this.startTime, this.now()); + } + + public static prettyDate(date1: number, date2: number): string { + var diff = ((date2 - date1) / 1000), + day_diff = Math.floor(diff / 86400); + + if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) + return; + + return (day_diff == 0 && ( + diff < 60 && (diff + " seconds") || + diff < 120 && "1 minute" || + diff < 3600 && Math.floor(diff / 60) + " minutes" || + diff < 7200 && "1 hour" || + diff < 86400 && Math.floor(diff / 3600) + " hours") || + day_diff == 1 && "Yesterday" || + day_diff < 7 && day_diff + " days" || + day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); + } + } +} diff --git a/_infrastructure/tests/src/tsc.ts b/_infrastructure/tests/src/tsc.ts new file mode 100644 index 0000000000..708f42a911 --- /dev/null +++ b/_infrastructure/tests/src/tsc.ts @@ -0,0 +1,43 @@ +/// +/// + +/// + +module DT { + export interface TscExecOptions { + tscVersion?:string; + useTscParams?:boolean; + checkNoImplicitAny?:boolean; + } + + export class Tsc { + public static run(tsfile: string, options: TscExecOptions, callback: (result: ExecResult) => void) { + options = options || {}; + options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; + if (typeof options.checkNoImplicitAny === "undefined") { + options.checkNoImplicitAny = true; + } + if (typeof options.useTscParams === "undefined") { + options.useTscParams = true; + } + + if (!IO.fileExists(tsfile)) { + throw new Error(tsfile + " not exists"); + } + + var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + if (!IO.fileExists(tscPath)) { + throw new Error(tscPath + ' is not exists'); + } + var command = 'node ' + tscPath + ' --module commonjs '; + if (options.useTscParams && IO.fileExists(tsfile + '.tscparams')) { + command += '@' + tsfile + '.tscparams'; + } else if (options.checkNoImplicitAny) { + command += '--noImplicitAny'; + } + Exec.exec(command, [tsfile], (execResult) => { + callback(execResult); + }); + } + } +} diff --git a/_infrastructure/tests/src/util.ts b/_infrastructure/tests/src/util.ts new file mode 100644 index 0000000000..d907fe49d2 --- /dev/null +++ b/_infrastructure/tests/src/util.ts @@ -0,0 +1,5 @@ +module DT { + export function endsWith(str: string, suffix: string) { + return str.indexOf(suffix, str.length - suffix.length) !== -1; + } +} From 880e167158d26cff3984c43977b62de4ef74c743 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 19:25:46 +0100 Subject: [PATCH 02/13] added reference parser stores as File::references[]:File added source-map-support dependency added glob dependency ditched io env wrapper for regular node fs / glob ditched exec env windows wrapper added extract util cleaned File.ts directory handling added pre-stage to test runner temporary exclude most files (for testing) --- _infrastructure/tests/runner.js | 712 +++++------------- _infrastructure/tests/runner.ts | 57 +- .../tests/src/{indexer.ts => changes.ts} | 0 _infrastructure/tests/src/file.ts | 17 +- _infrastructure/tests/src/host/exec.ts | 31 +- _infrastructure/tests/src/host/io.ts | 548 -------------- _infrastructure/tests/src/references.ts | 80 ++ _infrastructure/tests/src/suite/tscParams.ts | 12 +- _infrastructure/tests/src/timer.ts | 13 +- _infrastructure/tests/src/tsc.ts | 8 +- _infrastructure/tests/src/util.ts | 20 + 11 files changed, 363 insertions(+), 1135 deletions(-) rename _infrastructure/tests/src/{indexer.ts => changes.ts} (100%) delete mode 100644 _infrastructure/tests/src/host/io.ts create mode 100644 _infrastructure/tests/src/references.ts diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index 37aefb7e52..f73c72a395 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -21,35 +21,6 @@ var ExecResult = (function () { return ExecResult; })(); -var WindowsScriptHostExec = (function () { - function WindowsScriptHostExec() { - } - WindowsScriptHostExec.prototype.exec = function (filename, cmdLineArgs, handleResult) { - var result = new ExecResult(); - var shell = new ActiveXObject('WScript.Shell'); - try { - var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' ')); - } catch (e) { - result.stderr = e.message; - result.exitCode = 1; - handleResult(result); - return; - } - - while (process.Status != 0) { - } - - result.exitCode = process.ExitCode; - if (!process.StdOut.AtEndOfStream) - result.stdout = process.StdOut.ReadAll(); - if (!process.StdErr.AtEndOfStream) - result.stderr = process.StdErr.ReadAll(); - - handleResult(result); - }; - return WindowsScriptHostExec; -})(); - var NodeExec = (function () { function NodeExec() { } @@ -71,492 +42,8 @@ var NodeExec = (function () { })(); var Exec = function () { - var global = Function("return this;").call(null); - if (typeof global.ActiveXObject !== "undefined") { - return new WindowsScriptHostExec(); - } else { - return new NodeExec(); - } + return new NodeExec(); }(); -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -var IOUtils; -(function (IOUtils) { - // Creates the directory including its parent if not already present - function createDirectoryStructure(ioHost, dirName) { - if (ioHost.directoryExists(dirName)) { - return; - } - - var parentDirectory = ioHost.dirName(dirName); - if (parentDirectory != "") { - createDirectoryStructure(ioHost, parentDirectory); - } - ioHost.createDirectory(dirName); - } - - // Creates a file including its directory structure if not already present - function createFileAndFolderStructure(ioHost, fileName, useUTF8) { - var path = ioHost.resolvePath(fileName); - var dirName = ioHost.dirName(path); - createDirectoryStructure(ioHost, dirName); - return ioHost.createFile(path, useUTF8); - } - IOUtils.createFileAndFolderStructure = createFileAndFolderStructure; - - function throwIOError(message, error) { - var errorMessage = message; - if (error && error.message) { - errorMessage += (" " + error.message); - } - throw new Error(errorMessage); - } - IOUtils.throwIOError = throwIOError; -})(IOUtils || (IOUtils = {})); - - - -var IO = (function () { - // Create an IO object for use inside WindowsScriptHost hosts - // Depends on WSCript and FileSystemObject - function getWindowsScriptHostIO() { - var fso = new ActiveXObject("Scripting.FileSystemObject"); - var streamObjectPool = []; - - function getStreamObject() { - if (streamObjectPool.length > 0) { - return streamObjectPool.pop(); - } else { - return new ActiveXObject("ADODB.Stream"); - } - } - - function releaseStreamObject(obj) { - streamObjectPool.push(obj); - } - - var args = []; - for (var i = 0; i < WScript.Arguments.length; i++) { - args[i] = WScript.Arguments.Item(i); - } - - return { - readFile: function (path) { - try { - var streamObj = getStreamObject(); - streamObj.Open(); - streamObj.Type = 2; // Text data - streamObj.Charset = 'x-ansi'; // Assume we are reading ansi text - streamObj.LoadFromFile(path); - var bomChar = streamObj.ReadText(2); - streamObj.Position = 0; // Position has to be at 0 before changing the encoding - if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF) || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) { - streamObj.Charset = 'unicode'; - } else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) { - streamObj.Charset = 'utf-8'; - } - - // Read the whole file - var str = streamObj.ReadText(-1); - streamObj.Close(); - releaseStreamObject(streamObj); - return str; - } catch (err) { - IOUtils.throwIOError("Error reading file \"" + path + "\".", err); - } - }, - writeFile: function (path, contents) { - var file = this.createFile(path); - file.Write(contents); - file.Close(); - }, - fileExists: function (path) { - return fso.FileExists(path); - }, - resolvePath: function (path) { - return fso.GetAbsolutePathName(path); - }, - dirName: function (path) { - return fso.GetParentFolderName(path); - }, - findFile: function (rootPath, partialFilePath) { - var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath; - - while (true) { - if (fso.FileExists(path)) { - try { - var content = this.readFile(path); - return { content: content, path: path }; - } catch (err) { - //Tools.CompilerDiagnostics.debugPrint("Could not find " + path + ", trying parent"); - } - } else { - rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath)); - - if (rootPath == "") { - return null; - } else { - path = fso.BuildPath(rootPath, partialFilePath); - } - } - } - }, - deleteFile: function (path) { - try { - if (fso.FileExists(path)) { - fso.DeleteFile(path, true); // true: delete read-only files - } - } catch (e) { - IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); - } - }, - createFile: function (path, useUTF8) { - try { - var streamObj = getStreamObject(); - streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi'; - streamObj.Open(); - return { - Write: function (str) { - streamObj.WriteText(str, 0); - }, - WriteLine: function (str) { - streamObj.WriteText(str, 1); - }, - Close: function () { - try { - streamObj.SaveToFile(path, 2); - } catch (saveError) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError); - } finally { - if (streamObj.State != 0) { - streamObj.Close(); - } - releaseStreamObject(streamObj); - } - } - }; - } catch (creationError) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError); - } - }, - directoryExists: function (path) { - return fso.FolderExists(path); - }, - createDirectory: function (path) { - try { - if (!this.directoryExists(path)) { - fso.CreateFolder(path); - } - } catch (e) { - IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e); - } - }, - dir: function (path, spec, options) { - options = options || {}; - function filesInFolder(folder, root) { - var paths = []; - var fc; - - if (options.recursive) { - fc = new Enumerator(folder.subfolders); - - for (; !fc.atEnd(); fc.moveNext()) { - paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name)); - } - } - - fc = new Enumerator(folder.files); - - for (; !fc.atEnd(); fc.moveNext()) { - if (!spec || fc.item().Name.match(spec)) { - paths.push(root + "/" + fc.item().Name); - } - } - - return paths; - } - - var folder = fso.GetFolder(path); - var paths = []; - - return filesInFolder(folder, path); - }, - print: function (str) { - WScript.StdOut.Write(str); - }, - printLine: function (str) { - WScript.Echo(str); - }, - arguments: args, - stderr: WScript.StdErr, - stdout: WScript.StdOut, - watchFile: null, - run: function (source, filename) { - try { - eval(source); - } catch (e) { - IOUtils.throwIOError("Error while executing file '" + filename + "'.", e); - } - }, - getExecutingFilePath: function () { - return WScript.ScriptFullName; - }, - quit: function (exitCode) { - if (typeof exitCode === "undefined") { exitCode = 0; } - try { - WScript.Quit(exitCode); - } catch (e) { - } - } - }; - } - ; - - // Create an IO object for use inside Node.js hosts - // Depends on 'fs' and 'path' modules - function getNodeIO() { - var _fs = require('fs'); - var _path = require('path'); - var _module = require('module'); - - return { - readFile: function (file) { - try { - var buffer = _fs.readFileSync(file); - switch (buffer[0]) { - case 0xFE: - if (buffer[1] == 0xFF) { - // utf16-be. Reading the buffer as big endian is not supported, so convert it to - // Little Endian first - var i = 0; - while ((i + 1) < buffer.length) { - var temp = buffer[i]; - buffer[i] = buffer[i + 1]; - buffer[i + 1] = temp; - i += 2; - } - return buffer.toString("ucs2", 2); - } - break; - case 0xFF: - if (buffer[1] == 0xFE) { - // utf16-le - return buffer.toString("ucs2", 2); - } - break; - case 0xEF: - if (buffer[1] == 0xBB) { - // utf-8 - return buffer.toString("utf8", 3); - } - } - - // Default behaviour - return buffer.toString(); - } catch (e) { - IOUtils.throwIOError("Error reading file \"" + file + "\".", e); - } - }, - writeFile: _fs.writeFileSync, - deleteFile: function (path) { - try { - _fs.unlinkSync(path); - } catch (e) { - IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); - } - }, - fileExists: function (path) { - return _fs.existsSync(path); - }, - createFile: function (path, useUTF8) { - function mkdirRecursiveSync(path) { - var stats = _fs.statSync(path); - if (stats.isFile()) { - IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null); - } else if (stats.isDirectory()) { - return; - } else { - mkdirRecursiveSync(_path.dirname(path)); - _fs.mkdirSync(path, parseInt('0775', 8)); - } - } - - mkdirRecursiveSync(_path.dirname(path)); - - try { - var fd = _fs.openSync(path, 'w'); - } catch (e) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e); - } - return { - Write: function (str) { - _fs.writeSync(fd, str); - }, - WriteLine: function (str) { - _fs.writeSync(fd, str + '\r\n'); - }, - Close: function () { - _fs.closeSync(fd); - fd = null; - } - }; - }, - dir: function dir(path, spec, options) { - options = options || {}; - - function filesInFolder(folder, deep) { - var paths = []; - - var files = _fs.readdirSync(folder); - for (var i = 0; i < files.length; i++) { - var stat = _fs.statSync(folder + "/" + files[i]); - if (options.recursive && stat.isDirectory()) { - if (deep < (options.deep || 100)) { - paths = paths.concat(filesInFolder(folder + "/" + files[i], 1)); - } - } else if (stat.isFile() && (!spec || files[i].match(spec))) { - paths.push(folder + "/" + files[i]); - } - } - - return paths; - } - - return filesInFolder(path, 0); - }, - createDirectory: function (path) { - try { - if (!this.directoryExists(path)) { - _fs.mkdirSync(path); - } - } catch (e) { - IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e); - } - }, - directoryExists: function (path) { - return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory(); - }, - resolvePath: function (path) { - return _path.resolve(path); - }, - dirName: function (path) { - return _path.dirname(path); - }, - findFile: function (rootPath, partialFilePath) { - var path = rootPath + "/" + partialFilePath; - - while (true) { - if (_fs.existsSync(path)) { - try { - var content = this.readFile(path); - return { content: content, path: path }; - } catch (err) { - //Tools.CompilerDiagnostics.debugPrint(("Could not find " + path) + ", trying parent"); - } - } else { - var parentPath = _path.resolve(rootPath, ".."); - - // Node will just continue to repeat the root path, rather than return null - if (rootPath === parentPath) { - return null; - } else { - rootPath = parentPath; - path = _path.resolve(rootPath, partialFilePath); - } - } - } - }, - print: function (str) { - process.stdout.write(str); - }, - printLine: function (str) { - process.stdout.write(str + '\n'); - }, - arguments: process.argv.slice(2), - stderr: { - Write: function (str) { - process.stderr.write(str); - }, - WriteLine: function (str) { - process.stderr.write(str + '\n'); - }, - Close: function () { - } - }, - stdout: { - Write: function (str) { - process.stdout.write(str); - }, - WriteLine: function (str) { - process.stdout.write(str + '\n'); - }, - Close: function () { - } - }, - watchFile: function (filename, callback) { - var firstRun = true; - var processingChange = false; - - var fileChanged = function (curr, prev) { - if (!firstRun) { - if (curr.mtime < prev.mtime) { - return; - } - - _fs.unwatchFile(filename, fileChanged); - if (!processingChange) { - processingChange = true; - callback(filename); - setTimeout(function () { - processingChange = false; - }, 100); - } - } - firstRun = false; - _fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged); - }; - - fileChanged(); - return { - filename: filename, - close: function () { - _fs.unwatchFile(filename, fileChanged); - } - }; - }, - run: function (source, filename) { - require.main.filename = filename; - require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename))); - require.main._compile(source, filename); - }, - getExecutingFilePath: function () { - return process.mainModule.filename; - }, - quit: process.exit - }; - } - ; - - if (typeof ActiveXObject === "function") - return getWindowsScriptHostIO(); - else if (typeof require === "function") - return getNodeIO(); - else - return null; -})(); var DT; (function (DT) { var path = require('path'); @@ -569,20 +56,31 @@ var DT; function File(baseDir, filePathWithName) { this.baseDir = baseDir; this.filePathWithName = filePathWithName; - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - this.dir = dirName.split('/')[0]; - this.file = path.basename(this.filePathWithName, '.ts'); + this.references = []; this.ext = path.extname(this.filePathWithName); + this.file = path.basename(this.filePathWithName, this.ext); + this.dir = path.dirname(this.filePathWithName); } Object.defineProperty(File.prototype, "formatName", { // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' get: function () { - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - return this.dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + this.file + this.ext; + return path.join(this.dir, this.file + this.ext); }, enumerable: true, configurable: true }); + + Object.defineProperty(File.prototype, "fullPath", { + get: function () { + return path.join(this.baseDir, this.dir, this.file + this.ext); + }, + enumerable: true, + configurable: true + }); + + File.prototype.toString = function () { + return '[File ' + this.filePathWithName + ']'; + }; return File; })(); DT.File = File; @@ -592,6 +90,8 @@ var DT; /// var DT; (function (DT) { + var fs = require('fs'); + var Tsc = (function () { function Tsc() { } @@ -605,16 +105,16 @@ var DT; options.useTscParams = true; } - if (!IO.fileExists(tsfile)) { + if (!fs.existsSync(tsfile)) { throw new Error(tsfile + " not exists"); } var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!IO.fileExists(tscPath)) { + if (!fs.existsSync(tscPath)) { throw new Error(tscPath + ' is not exists'); } var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && IO.fileExists(tsfile + '.tscparams')) { + if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { command += '@' + tsfile + '.tscparams'; } else if (options.checkNoImplicitAny) { command += '--noImplicitAny'; @@ -654,10 +154,12 @@ var DT; }; Timer.prettyDate = function (date1, date2) { - var diff = ((date2 - date1) / 1000), day_diff = Math.floor(diff / 86400); + var diff = ((date2 - date1) / 1000); + var day_diff = Math.floor(diff / 86400); - if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) - return; + if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) { + return null; + } return (day_diff == 0 && (diff < 60 && (diff + " seconds") || diff < 120 && "1 minute" || diff < 3600 && Math.floor(diff / 60) + " minutes" || diff < 7200 && "1 hour" || diff < 86400 && Math.floor(diff / 3600) + " hours") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days" || day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); }; @@ -667,10 +169,107 @@ var DT; })(DT || (DT = {})); var DT; (function (DT) { + var referenceTagExp = //g; + function endsWith(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; } DT.endsWith = endsWith; + + function extractReferenceTags(source) { + var ret = []; + var match; + + if (!referenceTagExp.global) { + throw new Error('referenceTagExp RegExp must have global flag'); + } + referenceTagExp.lastIndex = 0; + + while ((match = referenceTagExp.exec(source))) { + if (match.length > 0 && match[1].length > 0) { + ret.push(match[1]); + } + } + return ret; + } + DT.extractReferenceTags = extractReferenceTags; +})(DT || (DT = {})); +/// +/// +/// +var DT; +(function (DT) { + var fs = require('fs'); + var path = require('path'); + + var ReferenceIndex = (function () { + function ReferenceIndex(options) { + this.options = options; + } + ReferenceIndex.prototype.collectReferences = function (files, callback) { + var _this = this; + this.fileMap = Object.create(null); + files.forEach(function (file) { + _this.fileMap[file.fullPath] = file; + }); + this.loadReferences(files, function () { + callback(); + }); + }; + + ReferenceIndex.prototype.loadReferences = function (files, callback) { + var _this = this; + var queue = files.slice(0); + var active = []; + var max = 50; + var next = function () { + if (queue.length === 0 && active.length === 0) { + callback(); + return; + } + + while (queue.length > 0 && active.length < max) { + var file = queue.pop(); + active.push(file); + _this.parseFile(file, function (file) { + active.splice(active.indexOf(file), 1); + next(); + }); + } + }; + process.nextTick(next); + }; + + ReferenceIndex.prototype.parseFile = function (file, callback) { + var _this = this; + fs.readFile(file.filePathWithName, { + encoding: 'utf8', + flag: 'r' + }, function (err, content) { + if (err) { + throw err; + } + + // console.log('----'); + // console.log(file.filePathWithName); + file.references = DT.extractReferenceTags(content).map(function (ref) { + return path.resolve(path.dirname(file.fullPath), ref); + }).reduce(function (memo, ref) { + if (ref in _this.fileMap) { + memo.push(_this.fileMap[ref]); + } else { + console.log('not mapped? -> ' + ref); + } + return memo; + }, []); + + // console.log(file.references); + callback(file); + }); + }; + return ReferenceIndex; + })(); + DT.ReferenceIndex = ReferenceIndex; })(DT || (DT = {})); /// /// @@ -951,6 +550,8 @@ var DT; /// var DT; (function (DT) { + var fs = require('fs'); + ///////////////////////////////// // Try compile without .tscparams // It may indicate that it is compatible with --noImplicitAny maybe... @@ -973,14 +574,18 @@ var DT; } FindNotRequiredTscparams.prototype.filterTargetFiles = function (files) { return files.filter(function (file) { - return IO.fileExists(file.filePathWithName + '.tscparams'); + return fs.existsSync(file.filePathWithName + '.tscparams'); }); }; FindNotRequiredTscparams.prototype.runTest = function (targetFile, callback) { var _this = this; this.print.clearCurrentLine().out(targetFile.formatName); - new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true }).run(function (result) { + new DT.Test(this, targetFile, { + tscVersion: this.options.tscVersion, + useTscParams: false, + checkNoImplicitAny: true + }).run(function (result) { _this.testResults.push(result); callback(result); }); @@ -1005,11 +610,11 @@ var DT; })(DT || (DT = {})); /// /// -/// /// /// /// /// +/// /// /// /// @@ -1018,8 +623,11 @@ var DT; /// var DT; (function (DT) { + require('source-map-support'); + var fs = require('fs'); var path = require('path'); + var glob = require('glob'); DT.DEFAULT_TSC_VERSION = "0.9.1.1"; @@ -1075,16 +683,32 @@ var DT; this.suites = []; this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - var filesName = IO.dir(dtPath, /.\.ts/g, { recursive: true }).sort(); + // TOD0 remove this after dev! + var testNames = [ + 'async/', + 'jquery/jquery.d', + 'angularjs/angular.d', + 'pixi/' + ]; + if (process.env.TRAVIS) { + testNames = null; + } + // should be async // only includes .d.ts or -tests.ts or -test.ts or .ts + var filesName = glob.sync('**/*.ts', { cwd: dtPath }); this.files = filesName.filter(function (fileName) { - return fileName.indexOf('../_infrastructure') < 0; + return fileName.indexOf('_infrastructure') < 0; }).filter(function (fileName) { - return fileName.indexOf('../node_modules') < 0; + return fileName.indexOf('node_modules/') < 0; }).filter(function (fileName) { - return !DT.endsWith(fileName, ".tscparams"); - }).map(function (fileName) { + // TOD0 remove this after dev! + return !testNames || testNames.some(function (pattern) { + return fileName.indexOf(pattern) > -1; + }); + }).filter(function (fileName) { + return /^[a-z]/i.test(fileName); + }).sort().map(function (fileName) { return new DT.File(dtPath, fileName); }); } @@ -1097,6 +721,19 @@ var DT; this.timer = new DT.Timer(); this.timer.start(); + var index = new DT.ReferenceIndex(this.options); + index.collectReferences(this.files, function () { + _this.files.forEach(function (file) { + console.log(file.filePathWithName); + file.references.forEach(function (file) { + console.log(' - %s', file.filePathWithName); + }); + }); + _this.runTests(); + }); + }; + TestRunner.prototype.runTests = function () { + var _this = this; var syntaxChecking = new DT.SyntaxChecking(this.options); var testEval = new DT.TestEval(this.options); if (!this.options.findNotRequiredTscparams) { @@ -1213,7 +850,7 @@ var DT; })(); DT.TestRunner = TestRunner; - var dtPath = __dirname + '/../..'; + var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); var findNotRequiredTscparams = process.argv.some(function (arg) { return arg == "--try-without-tscparams"; }); @@ -1223,6 +860,13 @@ var DT; tscVersion = process.argv[tscVersionIndex + 1]; } + console.log('--'); + console.log(' dtPath %s', dtPath); + console.log(' tscVersion %s', tscVersion); + console.log(' findNotRequiredTscparams %s', findNotRequiredTscparams); + console.log('--'); + console.log(''); + var runner = new TestRunner(dtPath, { tscVersion: tscVersion, findNotRequiredTscparams: findNotRequiredTscparams diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index 86a4c2caa3..bf05025266 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -1,12 +1,12 @@ /// /// -/// /// /// /// /// +/// /// /// @@ -17,8 +17,11 @@ /// module DT { + require('source-map-support'); + var fs = require('fs'); var path = require('path'); + var glob = require('glob'); export var DEFAULT_TSC_VERSION = "0.9.1.1"; @@ -74,18 +77,38 @@ module DT { constructor(dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - var filesName = IO.dir(dtPath, /.\.ts/g, { recursive: true }).sort(); + // TOD0 remove this after dev! + var testNames = [ + 'async/', + 'jquery/jquery.d', + 'angularjs/angular.d', + 'pixi/' + ]; + if (process.env.TRAVIS) { + testNames = null; + } + + // should be async // only includes .d.ts or -tests.ts or -test.ts or .ts + var filesName = glob.sync('**/*.ts', { cwd: dtPath }); this.files = filesName .filter((fileName) => { - return fileName.indexOf('../_infrastructure') < 0; + return fileName.indexOf('_infrastructure') < 0; }) .filter((fileName) => { - return fileName.indexOf('../node_modules') < 0; + return fileName.indexOf('node_modules/') < 0; }) .filter((fileName) => { - return !DT.endsWith(fileName, ".tscparams"); - }).map((fileName) => { + // TOD0 remove this after dev! + return !testNames || testNames.some((pattern) => { + return fileName.indexOf(pattern) > -1; + }); + }) + .filter((fileName) => { + return /^[a-z]/i.test(fileName); + }) + .sort() + .map((fileName) => { return new File(dtPath, fileName); }); } @@ -98,6 +121,19 @@ module DT { this.timer = new Timer(); this.timer.start(); + var index = new ReferenceIndex(this.options); + index.collectReferences(this.files, () => { + this.files.forEach((file) => { + console.log(file.filePathWithName); + file.references.forEach((file) => { + console.log(' - %s', file.filePathWithName); + }); + }); + this.runTests(); + }); + } + public runTests() { + var syntaxChecking = new SyntaxChecking(this.options); var testEval = new TestEval(this.options); if (!this.options.findNotRequiredTscparams) { @@ -221,7 +257,7 @@ module DT { } } - var dtPath = __dirname + '/../..'; + var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); var findNotRequiredTscparams = process.argv.some(arg => arg == "--try-without-tscparams"); var tscVersionIndex = process.argv.indexOf("--tsc-version"); var tscVersion = DEFAULT_TSC_VERSION; @@ -229,6 +265,13 @@ module DT { tscVersion = process.argv[tscVersionIndex + 1]; } + console.log('--'); + console.log(' dtPath %s', dtPath); + console.log(' tscVersion %s', tscVersion); + console.log(' findNotRequiredTscparams %s', findNotRequiredTscparams); + console.log('--'); + console.log(''); + var runner = new TestRunner(dtPath, { tscVersion: tscVersion, findNotRequiredTscparams: findNotRequiredTscparams diff --git a/_infrastructure/tests/src/indexer.ts b/_infrastructure/tests/src/changes.ts similarity index 100% rename from _infrastructure/tests/src/indexer.ts rename to _infrastructure/tests/src/changes.ts diff --git a/_infrastructure/tests/src/file.ts b/_infrastructure/tests/src/file.ts index f349a09da6..c1a8d1f517 100644 --- a/_infrastructure/tests/src/file.ts +++ b/_infrastructure/tests/src/file.ts @@ -9,18 +9,25 @@ module DT { dir: string; file: string; ext: string; + references: File[] = []; constructor(public baseDir: string, public filePathWithName: string) { - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - this.dir = dirName.split('/')[0]; - this.file = path.basename(this.filePathWithName, '.ts'); this.ext = path.extname(this.filePathWithName); + this.file = path.basename(this.filePathWithName, this.ext); + this.dir = path.dirname(this.filePathWithName); } // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' public get formatName(): string { - var dirName = path.dirname(this.filePathWithName.substr(this.baseDir.length + 1)).replace('\\', '/'); - return this.dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + this.file + this.ext; + return path.join(this.dir, this.file + this.ext); + } + + public get fullPath(): string { + return path.join(this.baseDir, this.dir, this.file + this.ext); + } + + toString() { + return '[File ' + this.filePathWithName + ']'; } } } diff --git a/_infrastructure/tests/src/host/exec.ts b/_infrastructure/tests/src/host/exec.ts index 2bddb0043c..9ff896c3f8 100644 --- a/_infrastructure/tests/src/host/exec.ts +++ b/_infrastructure/tests/src/host/exec.ts @@ -24,30 +24,6 @@ class ExecResult { public exitCode: number; } -class WindowsScriptHostExec implements IExec { - public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { - var result = new ExecResult(); - var shell = new ActiveXObject('WScript.Shell'); - try { - var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' ')); - } catch (e) { - result.stderr = e.message; - result.exitCode = 1 - handleResult(result); - return; - } - // Wait for it to finish running - while (process.Status != 0) { /* todo: sleep? */ - } - - result.exitCode = process.ExitCode; - if (!process.StdOut.AtEndOfStream) result.stdout = process.StdOut.ReadAll(); - if (!process.StdErr.AtEndOfStream) result.stderr = process.StdErr.ReadAll(); - - handleResult(result); - } -} - class NodeExec implements IExec { public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { var nodeExec = require('child_process').exec; @@ -66,10 +42,5 @@ class NodeExec implements IExec { } var Exec: IExec = function (): IExec { - var global = Function("return this;").call(null); - if (typeof global.ActiveXObject !== "undefined") { - return new WindowsScriptHostExec(); - } else { - return new NodeExec(); - } + return new NodeExec(); }(); diff --git a/_infrastructure/tests/src/host/io.ts b/_infrastructure/tests/src/host/io.ts deleted file mode 100644 index b308ef13f6..0000000000 --- a/_infrastructure/tests/src/host/io.ts +++ /dev/null @@ -1,548 +0,0 @@ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -interface IResolvedFile { - content: string; - path: string; -} - -interface IFileWatcher { - close(): void; -} - -interface IIO { - readFile(path: string): string; - writeFile(path: string, contents: string): void; - createFile(path: string, useUTF8?: boolean): ITextWriter; - deleteFile(path: string): void; - dir(path: string, re?: RegExp, options?: { recursive?: boolean; deep?: number; - }): string[]; - fileExists(path: string): boolean; - directoryExists(path: string): boolean; - createDirectory(path: string): void; - resolvePath(path: string): string; - dirName(path: string): string; - findFile(rootPath: string, partialFilePath: string): IResolvedFile; - print(str: string): void; - printLine(str: string): void; - arguments: string[]; - stderr: ITextWriter; - stdout: ITextWriter; - watchFile(filename: string, callback: (string) => void): IFileWatcher; - run(source: string, filename: string): void; - getExecutingFilePath(): string; - quit(exitCode?: number); -} - -module IOUtils { - // Creates the directory including its parent if not already present - function createDirectoryStructure(ioHost: IIO, dirName: string) { - if (ioHost.directoryExists(dirName)) { - return; - } - - var parentDirectory = ioHost.dirName(dirName); - if (parentDirectory != "") { - createDirectoryStructure(ioHost, parentDirectory); - } - ioHost.createDirectory(dirName); - } - - // Creates a file including its directory structure if not already present - export function createFileAndFolderStructure(ioHost: IIO, fileName: string, useUTF8?: boolean) { - var path = ioHost.resolvePath(fileName); - var dirName = ioHost.dirName(path); - createDirectoryStructure(ioHost, dirName); - return ioHost.createFile(path, useUTF8); - } - - export function throwIOError(message: string, error: Error) { - var errorMessage = message; - if (error && error.message) { - errorMessage += (" " + error.message); - } - throw new Error(errorMessage); - } -} - -// Declare dependencies needed for all supported hosts -declare class Enumerator { - public atEnd(): boolean; - - public moveNext(); - - public item(): any; - - constructor(o: any); -} - -// Missing in node definitions, but called in code below. -interface NodeProcess { - mainModule: { - filename: string; - } -} - -var IO = (function () { - - // Create an IO object for use inside WindowsScriptHost hosts - // Depends on WSCript and FileSystemObject - function getWindowsScriptHostIO(): IIO { - var fso = new ActiveXObject("Scripting.FileSystemObject"); - var streamObjectPool = []; - - function getStreamObject(): any { - if (streamObjectPool.length > 0) { - return streamObjectPool.pop(); - } else { - return new ActiveXObject("ADODB.Stream"); - } - } - - function releaseStreamObject(obj: any) { - streamObjectPool.push(obj); - } - - var args = []; - for (var i = 0; i < WScript.Arguments.length; i++) { - args[i] = WScript.Arguments.Item(i); - } - - return { - readFile: function (path) { - try { - var streamObj = getStreamObject(); - streamObj.Open(); - streamObj.Type = 2; // Text data - streamObj.Charset = 'x-ansi'; // Assume we are reading ansi text - streamObj.LoadFromFile(path); - var bomChar = streamObj.ReadText(2); // Read the BOM char - streamObj.Position = 0; // Position has to be at 0 before changing the encoding - if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF) - || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) { - streamObj.Charset = 'unicode'; - } else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) { - streamObj.Charset = 'utf-8'; - } - - // Read the whole file - var str = streamObj.ReadText(-1 /* read from the current position to EOS */); - streamObj.Close(); - releaseStreamObject(streamObj); - return str; - } - catch (err) { - IOUtils.throwIOError("Error reading file \"" + path + "\".", err); - } - }, - - writeFile: function (path, contents) { - var file = this.createFile(path); - file.Write(contents); - file.Close(); - }, - - fileExists: function (path: string): boolean { - return fso.FileExists(path); - }, - - resolvePath: function (path: string): string { - return fso.GetAbsolutePathName(path); - }, - - dirName: function (path: string): string { - return fso.GetParentFolderName(path); - }, - - findFile: function (rootPath: string, partialFilePath: string): IResolvedFile { - var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath; - - while (true) { - if (fso.FileExists(path)) { - try { - var content = this.readFile(path); - return { content: content, path: path }; - } - catch (err) { - //Tools.CompilerDiagnostics.debugPrint("Could not find " + path + ", trying parent"); - } - } - else { - rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath)); - - if (rootPath == "") { - return null; - } - else { - path = fso.BuildPath(rootPath, partialFilePath); - } - } - } - }, - - deleteFile: function (path: string): void { - try { - if (fso.FileExists(path)) { - fso.DeleteFile(path, true); // true: delete read-only files - } - } catch (e) { - IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); - } - }, - - createFile: function (path, useUTF8?) { - try { - var streamObj = getStreamObject(); - streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi'; - streamObj.Open(); - return { - Write: function (str) { - streamObj.WriteText(str, 0); - }, - WriteLine: function (str) { - streamObj.WriteText(str, 1); - }, - Close: function () { - try { - streamObj.SaveToFile(path, 2); - } catch (saveError) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError); - } - finally { - if (streamObj.State != 0 /*adStateClosed*/) { - streamObj.Close(); - } - releaseStreamObject(streamObj); - } - } - }; - } catch (creationError) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError); - } - }, - - directoryExists: function (path) { - return fso.FolderExists(path); - }, - - createDirectory: function (path) { - try { - if (!this.directoryExists(path)) { - fso.CreateFolder(path); - } - } catch (e) { - IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e); - } - }, - - dir: function (path, spec?, options?) { - options = options || <{ recursive?: boolean; deep?: number; - }>{}; - function filesInFolder(folder, root): string[] { - var paths = []; - var fc: Enumerator; - - if (options.recursive) { - fc = new Enumerator(folder.subfolders); - - for (; !fc.atEnd(); fc.moveNext()) { - paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name)); - } - } - - fc = new Enumerator(folder.files); - - for (; !fc.atEnd(); fc.moveNext()) { - if (!spec || fc.item().Name.match(spec)) { - paths.push(root + "/" + fc.item().Name); - } - } - - return paths; - } - - var folder = fso.GetFolder(path); - var paths = []; - - return filesInFolder(folder, path); - }, - - print: function (str) { - WScript.StdOut.Write(str); - }, - - printLine: function (str) { - WScript.Echo(str); - }, - - arguments: args, - stderr: WScript.StdErr, - stdout: WScript.StdOut, - watchFile: null, - run: function (source, filename) { - try { - eval(source); - } catch (e) { - IOUtils.throwIOError("Error while executing file '" + filename + "'.", e); - } - }, - getExecutingFilePath: function () { - return WScript.ScriptFullName; - }, - quit: function (exitCode: number = 0) { - try { - WScript.Quit(exitCode); - } catch (e) { - } - } - } - - }; - - // Create an IO object for use inside Node.js hosts - // Depends on 'fs' and 'path' modules - function getNodeIO(): IIO { - - var _fs = require('fs'); - var _path = require('path'); - var _module = require('module'); - - return { - readFile: function (file) { - try { - var buffer = _fs.readFileSync(file); - switch (buffer[0]) { - case 0xFE: - if (buffer[1] == 0xFF) { - // utf16-be. Reading the buffer as big endian is not supported, so convert it to - // Little Endian first - var i = 0; - while ((i + 1) < buffer.length) { - var temp = buffer[i] - buffer[i] = buffer[i + 1]; - buffer[i + 1] = temp; - i += 2; - } - return buffer.toString("ucs2", 2); - } - break; - case 0xFF: - if (buffer[1] == 0xFE) { - // utf16-le - return buffer.toString("ucs2", 2); - } - break; - case 0xEF: - if (buffer[1] == 0xBB) { - // utf-8 - return buffer.toString("utf8", 3); - } - } - // Default behaviour - return buffer.toString(); - } catch (e) { - IOUtils.throwIOError("Error reading file \"" + file + "\".", e); - } - }, - writeFile: <(path: string, contents: string) => void >_fs.writeFileSync, - deleteFile: function (path) { - try { - _fs.unlinkSync(path); - } catch (e) { - IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); - } - }, - fileExists: function (path): boolean { - return _fs.existsSync(path); - }, - createFile: function (path, useUTF8?) { - function mkdirRecursiveSync(path) { - var stats = _fs.statSync(path); - if (stats.isFile()) { - IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null); - } else if (stats.isDirectory()) { - return; - } else { - mkdirRecursiveSync(_path.dirname(path)); - _fs.mkdirSync(path, parseInt('0775', 8)); - } - } - - mkdirRecursiveSync(_path.dirname(path)); - - try { - var fd = _fs.openSync(path, 'w'); - } catch (e) { - IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e); - } - return { - Write: function (str) { - _fs.writeSync(fd, str); - }, - WriteLine: function (str) { - _fs.writeSync(fd, str + '\r\n'); - }, - Close: function () { - _fs.closeSync(fd); - fd = null; - } - }; - }, - dir: function dir(path, spec?, options?) { - options = options || <{ recursive?: boolean; deep?: number; - }>{}; - - function filesInFolder(folder: string, deep?: number): string[] { - var paths = []; - - var files = _fs.readdirSync(folder); - for (var i = 0; i < files.length; i++) { - var stat = _fs.statSync(folder + "/" + files[i]); - if (options.recursive && stat.isDirectory()) { - if (deep < (options.deep || 100)) { - paths = paths.concat(filesInFolder(folder + "/" + files[i], 1)); - } - } else if (stat.isFile() && (!spec || files[i].match(spec))) { - paths.push(folder + "/" + files[i]); - } - } - - return paths; - } - - return filesInFolder(path, 0); - }, - createDirectory: function (path: string): void { - try { - if (!this.directoryExists(path)) { - _fs.mkdirSync(path); - } - } catch (e) { - IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e); - } - }, - - directoryExists: function (path: string): boolean { - return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory(); - }, - resolvePath: function (path: string): string { - return _path.resolve(path); - }, - dirName: function (path: string): string { - return _path.dirname(path); - }, - findFile: function (rootPath: string, partialFilePath): IResolvedFile { - var path = rootPath + "/" + partialFilePath; - - while (true) { - if (_fs.existsSync(path)) { - try { - var content = this.readFile(path); - return { content: content, path: path }; - } catch (err) { - //Tools.CompilerDiagnostics.debugPrint(("Could not find " + path) + ", trying parent"); - } - } - else { - var parentPath = _path.resolve(rootPath, ".."); - - // Node will just continue to repeat the root path, rather than return null - if (rootPath === parentPath) { - return null; - } - else { - rootPath = parentPath; - path = _path.resolve(rootPath, partialFilePath); - } - } - } - }, - print: function (str) { - process.stdout.write(str) - }, - printLine: function (str) { - process.stdout.write(str + '\n') - }, - arguments: process.argv.slice(2), - stderr: { - Write: function (str) { - process.stderr.write(str); - }, - WriteLine: function (str) { - process.stderr.write(str + '\n'); - }, - Close: function () { - } - }, - stdout: { - Write: function (str) { - process.stdout.write(str); - }, - WriteLine: function (str) { - process.stdout.write(str + '\n'); - }, - Close: function () { - } - }, - watchFile: function (filename: string, callback: (string) => void): IFileWatcher { - var firstRun = true; - var processingChange = false; - - var fileChanged: any = function (curr, prev) { - if (!firstRun) { - if (curr.mtime < prev.mtime) { - return; - } - - _fs.unwatchFile(filename, fileChanged); - if (!processingChange) { - processingChange = true; - callback(filename); - setTimeout(function () { - processingChange = false; - }, 100); - } - } - firstRun = false; - _fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged); - }; - - fileChanged(); - return { - filename: filename, - close: function () { - _fs.unwatchFile(filename, fileChanged); - } - }; - }, - run: function (source, filename) { - require.main.filename = filename; - require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename))); - require.main._compile(source, filename); - }, - getExecutingFilePath: function () { - return process.mainModule.filename; - }, - quit: process.exit - } - }; - - if (typeof ActiveXObject === "function") - return getWindowsScriptHostIO(); - else if (typeof require === "function") - return getNodeIO(); - else - return null; // Unsupported host -})(); diff --git a/_infrastructure/tests/src/references.ts b/_infrastructure/tests/src/references.ts new file mode 100644 index 0000000000..c98f947c38 --- /dev/null +++ b/_infrastructure/tests/src/references.ts @@ -0,0 +1,80 @@ +/// +/// + +/// + +module DT { + var fs = require('fs'); + var path = require('path'); + + export class ReferenceIndex { + + fileMap: {[path:string]:File}; + + constructor(public options: ITestRunnerOptions) { + + } + + collectReferences(files: File[], callback: () => void): void { + this.fileMap = Object.create(null); + files.forEach((file) => { + this.fileMap[file.fullPath] = file; + }); + this.loadReferences(files, () => { + callback(); + }); + } + + private loadReferences(files: File[], callback: () => void): void { + var queue = files.slice(0); + var active = []; + var max = 50; + var next = () => { + if (queue.length === 0 && active.length === 0) { + callback(); + return; + } + // queue paralel + while (queue.length > 0 && active.length < max) { + var file = queue.pop(); + active.push(file); + this.parseFile(file, (file) => { + active.splice(active.indexOf(file), 1); + next(); + }); + } + }; + process.nextTick(next); + } + + private parseFile(file: File, callback: (file: File) => void): void { + fs.readFile(file.filePathWithName, { + encoding: 'utf8', + flag: 'r' + }, (err, content) => { + if (err) { + // just blow up? + throw err; + } + // console.log('----'); + // console.log(file.filePathWithName); + + file.references = extractReferenceTags(content).map((ref: string) => { + return path.resolve(path.dirname(file.fullPath), ref); + }).reduce((memo: File[], ref: string) => { + if (ref in this.fileMap) { + memo.push(this.fileMap[ref]); + } + else { + console.log('not mapped? -> ' + ref); + } + return memo; + }, []); + + // console.log(file.references); + + callback(file); + }); + } + } +} diff --git a/_infrastructure/tests/src/suite/tscParams.ts b/_infrastructure/tests/src/suite/tscParams.ts index 65491d30d3..510e15ce77 100644 --- a/_infrastructure/tests/src/suite/tscParams.ts +++ b/_infrastructure/tests/src/suite/tscParams.ts @@ -2,6 +2,8 @@ /// module DT { + var fs = require('fs'); + ///////////////////////////////// // Try compile without .tscparams // It may indicate that it is compatible with --noImplicitAny maybe... @@ -25,12 +27,18 @@ module DT { } public filterTargetFiles(files: File[]): File[] { - return files.filter(file=> IO.fileExists(file.filePathWithName + '.tscparams')); + return files.filter((file) => { + return fs.existsSync(file.filePathWithName + '.tscparams'); + }); } public runTest(targetFile: File, callback: (result: TestResult) => void): void { this.print.clearCurrentLine().out(targetFile.formatName); - new Test(this, targetFile, {tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true}).run(result=> { + new Test(this, targetFile, { + tscVersion: this.options.tscVersion, + useTscParams: false, + checkNoImplicitAny: true + }).run(result=> { this.testResults.push(result); callback(result); }); diff --git a/_infrastructure/tests/src/timer.ts b/_infrastructure/tests/src/timer.ts index a92d08fafa..e03a941668 100644 --- a/_infrastructure/tests/src/timer.ts +++ b/_infrastructure/tests/src/timer.ts @@ -26,13 +26,14 @@ module DT { } public static prettyDate(date1: number, date2: number): string { - var diff = ((date2 - date1) / 1000), - day_diff = Math.floor(diff / 86400); + var diff = ((date2 - date1) / 1000); + var day_diff = Math.floor(diff / 86400); - if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) - return; + if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) { + return null; + } - return (day_diff == 0 && ( + return ( (day_diff == 0 && ( diff < 60 && (diff + " seconds") || diff < 120 && "1 minute" || diff < 3600 && Math.floor(diff / 60) + " minutes" || @@ -40,7 +41,7 @@ module DT { diff < 86400 && Math.floor(diff / 3600) + " hours") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days" || - day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); + day_diff < 31 && Math.ceil(day_diff / 7) + " weeks")); } } } diff --git a/_infrastructure/tests/src/tsc.ts b/_infrastructure/tests/src/tsc.ts index 708f42a911..5a2c803c80 100644 --- a/_infrastructure/tests/src/tsc.ts +++ b/_infrastructure/tests/src/tsc.ts @@ -4,6 +4,8 @@ /// module DT { + var fs = require('fs'); + export interface TscExecOptions { tscVersion?:string; useTscParams?:boolean; @@ -21,16 +23,16 @@ module DT { options.useTscParams = true; } - if (!IO.fileExists(tsfile)) { + if (!fs.existsSync(tsfile)) { throw new Error(tsfile + " not exists"); } var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!IO.fileExists(tscPath)) { + if (!fs.existsSync(tscPath)) { throw new Error(tscPath + ' is not exists'); } var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && IO.fileExists(tsfile + '.tscparams')) { + if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { command += '@' + tsfile + '.tscparams'; } else if (options.checkNoImplicitAny) { command += '--noImplicitAny'; diff --git a/_infrastructure/tests/src/util.ts b/_infrastructure/tests/src/util.ts index d907fe49d2..3b441991fd 100644 --- a/_infrastructure/tests/src/util.ts +++ b/_infrastructure/tests/src/util.ts @@ -1,5 +1,25 @@ module DT { + + var referenceTagExp = //g; + export function endsWith(str: string, suffix: string) { return str.indexOf(suffix, str.length - suffix.length) !== -1; } + + export function extractReferenceTags(source: string): string[] { + var ret: string[] = []; + var match: RegExpExecArray; + + if (!referenceTagExp.global) { + throw new Error('referenceTagExp RegExp must have global flag'); + } + referenceTagExp.lastIndex = 0; + + while ((match = referenceTagExp.exec(source))) { + if (match.length > 0 && match[1].length > 0) { + ret.push(match[1]); + } + } + return ret; + } } From 8b1a4c104075f238b3196343174f3f91487b6b6b Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 20:32:43 +0100 Subject: [PATCH 03/13] added git-change helper added dependencies to package.json renamed file index helper restructured references added _ref.d.ts changed references to double-quoted --- _infrastructure/tests/_ref.d.ts | 1 + _infrastructure/tests/runner.js | 177 +++++++++++++----- _infrastructure/tests/runner.ts | 115 +++++++----- _infrastructure/tests/src/changes.ts | 37 ++++ _infrastructure/tests/src/file.ts | 2 + .../tests/src/{references.ts => index.ts} | 11 +- _infrastructure/tests/src/printer.ts | 4 +- .../tests/src/reporter/reporter.ts | 3 + _infrastructure/tests/src/timer.ts | 4 +- _infrastructure/tests/src/tsc.ts | 7 +- _infrastructure/tests/src/util.ts | 2 + package.json | 17 +- 12 files changed, 265 insertions(+), 115 deletions(-) create mode 100644 _infrastructure/tests/_ref.d.ts rename _infrastructure/tests/src/{references.ts => index.ts} (90%) diff --git a/_infrastructure/tests/_ref.d.ts b/_infrastructure/tests/_ref.d.ts new file mode 100644 index 0000000000..1743cad05c --- /dev/null +++ b/_infrastructure/tests/_ref.d.ts @@ -0,0 +1 @@ +/// diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index f73c72a395..ee80513a21 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -44,6 +44,7 @@ var NodeExec = (function () { var Exec = function () { return new NodeExec(); }(); +/// var DT; (function (DT) { var path = require('path'); @@ -85,9 +86,9 @@ var DT; })(); DT.File = File; })(DT || (DT = {})); -/// -/// -/// +/// +/// +/// var DT; (function (DT) { var fs = require('fs'); @@ -127,8 +128,8 @@ var DT; })(); DT.Tsc = Tsc; })(DT || (DT = {})); -/// -/// +/// +/// var DT; (function (DT) { ///////////////////////////////// @@ -167,6 +168,7 @@ var DT; })(); DT.Timer = Timer; })(DT || (DT = {})); +/// var DT; (function (DT) { var referenceTagExp = //g; @@ -194,19 +196,19 @@ var DT; } DT.extractReferenceTags = extractReferenceTags; })(DT || (DT = {})); -/// -/// -/// +/// +/// +/// var DT; (function (DT) { var fs = require('fs'); var path = require('path'); - var ReferenceIndex = (function () { - function ReferenceIndex(options) { + var FileIndex = (function () { + function FileIndex(options) { this.options = options; } - ReferenceIndex.prototype.collectReferences = function (files, callback) { + FileIndex.prototype.parseFiles = function (files, callback) { var _this = this; this.fileMap = Object.create(null); files.forEach(function (file) { @@ -217,7 +219,7 @@ var DT; }); }; - ReferenceIndex.prototype.loadReferences = function (files, callback) { + FileIndex.prototype.loadReferences = function (files, callback) { var _this = this; var queue = files.slice(0); var active = []; @@ -240,7 +242,7 @@ var DT; process.nextTick(next); }; - ReferenceIndex.prototype.parseFile = function (file, callback) { + FileIndex.prototype.parseFile = function (file, callback) { var _this = this; fs.readFile(file.filePathWithName, { encoding: 'utf8', @@ -267,12 +269,49 @@ var DT; callback(file); }); }; - return ReferenceIndex; + return FileIndex; })(); - DT.ReferenceIndex = ReferenceIndex; + DT.FileIndex = FileIndex; })(DT || (DT = {})); -/// -/// +/// +var DT; +(function (DT) { + var fs = require('fs'); + var path = require('path'); + var Git = require('git-wrapper'); + + var GitChanges = (function () { + function GitChanges(baseDir) { + this.baseDir = baseDir; + this.options = []; + var dir = path.join(baseDir, '.git'); + if (!fs.existsSync(dir)) { + throw new Error('cannot locate git-dir: ' + dir); + } + this.options['git-dir'] = dir; + } + GitChanges.prototype.getChanges = function (callback) { + //git diff --name-only HEAD~1 + var git = new Git(this.options); + var opts = {}; + var args = ['--name-only HEAD~1']; + git.exec('diff', opts, args, function (err, msg) { + if (err) { + callback(err, null); + return; + } + var paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); + + // console.log(paths); + callback(null, paths); + }); + }; + return GitChanges; + })(); + DT.GitChanges = GitChanges; +})(DT || (DT = {})); +/// +/// var DT; (function (DT) { ///////////////////////////////// @@ -387,6 +426,8 @@ var DT; })(); DT.Print = Print; })(DT || (DT = {})); +/// +/// var DT; (function (DT) { @@ -608,27 +649,41 @@ var DT; })(DT.TestSuiteBase); DT.FindNotRequiredTscparams = FindNotRequiredTscparams; })(DT || (DT = {})); -/// -/// -/// -/// -/// +/// +/// +/// +/// +/// /// -/// -/// -/// -/// -/// -/// -/// +/// +/// +/// +/// +/// +/// +/// +/// var DT; (function (DT) { - require('source-map-support'); + require('source-map-support').install(); var fs = require('fs'); var path = require('path'); var glob = require('glob'); + var tsExp = /\.ts$/; + + // TOD0 remove this after dev! + var testNames = [ + 'async/', + 'jquery/jquery.d', + 'angularjs/angular.d', + 'pixi/' + ]; + + /* if (process.env.TRAVIS) { + testNames = null; + } */ DT.DEFAULT_TSC_VERSION = "0.9.1.1"; var Test = (function () { @@ -679,39 +734,36 @@ var DT; var TestRunner = (function () { function TestRunner(dtPath, options) { if (typeof options === "undefined") { options = { tscVersion: DT.DEFAULT_TSC_VERSION }; } + var _this = this; + this.dtPath = dtPath; this.options = options; this.suites = []; this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - // TOD0 remove this after dev! - var testNames = [ - 'async/', - 'jquery/jquery.d', - 'angularjs/angular.d', - 'pixi/' - ]; - if (process.env.TRAVIS) { - testNames = null; - } + this.index = new DT.FileIndex(this.options); // should be async // only includes .d.ts or -tests.ts or -test.ts or .ts var filesName = glob.sync('**/*.ts', { cwd: dtPath }); this.files = filesName.filter(function (fileName) { - return fileName.indexOf('_infrastructure') < 0; - }).filter(function (fileName) { - return fileName.indexOf('node_modules/') < 0; - }).filter(function (fileName) { - // TOD0 remove this after dev! - return !testNames || testNames.some(function (pattern) { - return fileName.indexOf(pattern) > -1; - }); - }).filter(function (fileName) { - return /^[a-z]/i.test(fileName); + return _this.checkAcceptFile(fileName); }).sort().map(function (fileName) { return new DT.File(dtPath, fileName); }); } + TestRunner.prototype.checkAcceptFile = function (fileName) { + var ok = tsExp.test(fileName); + ok = ok && fileName.indexOf('_infrastructure') < 0; + ok = ok && fileName.indexOf('node_modules/') < 0; + ok = ok && /^[a-z]/i.test(fileName); + + //TODO remove this dev code + ok = ok && (!testNames || testNames.some(function (pattern) { + return fileName.indexOf(pattern) > -1; + })); + return ok; + }; + TestRunner.prototype.addSuite = function (suite) { this.suites.push(suite); }; @@ -721,17 +773,38 @@ var DT; this.timer = new DT.Timer(); this.timer.start(); - var index = new DT.ReferenceIndex(this.options); - index.collectReferences(this.files, function () { + this.index.parseFiles(this.files, function () { + console.log('files:'); + console.log('---'); _this.files.forEach(function (file) { console.log(file.filePathWithName); file.references.forEach(function (file) { console.log(' - %s', file.filePathWithName); }); }); + console.log('---'); + _this.getChanges(); + }); + }; + + TestRunner.prototype.getChanges = function () { + var _this = this; + var changes = new DT.GitChanges(this.dtPath); + changes.getChanges(function (err, changes) { + if (err) { + throw err; + } + console.log('changes:'); + console.log('---'); + changes.forEach(function (file) { + console.log(file); + }); + console.log('---'); + _this.runTests(); }); }; + TestRunner.prototype.runTests = function () { var _this = this; var syntaxChecking = new DT.SyntaxChecking(this.options); diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index bf05025266..555ec54ab2 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -1,28 +1,43 @@ -/// +/// -/// +/// -/// -/// -/// +/// +/// +/// /// -/// -/// -/// +/// +/// -/// -/// -/// -/// +/// +/// + +/// +/// +/// +/// module DT { - require('source-map-support'); + require('source-map-support').install(); var fs = require('fs'); var path = require('path'); var glob = require('glob'); + var tsExp = /\.ts$/; + + // TOD0 remove this after dev! + var testNames = [ + 'async/', + 'jquery/jquery.d', + 'angularjs/angular.d', + 'pixi/' + ]; + /* if (process.env.TRAVIS) { + testNames = null; + } */ + export var DEFAULT_TSC_VERSION = "0.9.1.1"; export class Test { @@ -72,40 +87,21 @@ module DT { files: File[]; timer: Timer; suites: ITestSuite[] = []; + private index: FileIndex; private print: Print; - constructor(dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { + constructor(public dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - // TOD0 remove this after dev! - var testNames = [ - 'async/', - 'jquery/jquery.d', - 'angularjs/angular.d', - 'pixi/' - ]; - if (process.env.TRAVIS) { - testNames = null; - } + + this.index = new FileIndex(this.options); // should be async // only includes .d.ts or -tests.ts or -test.ts or .ts var filesName = glob.sync('**/*.ts', { cwd: dtPath }); this.files = filesName .filter((fileName) => { - return fileName.indexOf('_infrastructure') < 0; - }) - .filter((fileName) => { - return fileName.indexOf('node_modules/') < 0; - }) - .filter((fileName) => { - // TOD0 remove this after dev! - return !testNames || testNames.some((pattern) => { - return fileName.indexOf(pattern) > -1; - }); - }) - .filter((fileName) => { - return /^[a-z]/i.test(fileName); + return this.checkAcceptFile(fileName); }) .sort() .map((fileName) => { @@ -113,26 +109,59 @@ module DT { }); } - public addSuite(suite: ITestSuite) { + public checkAcceptFile(fileName: string): boolean { + var ok = tsExp.test(fileName); + ok = ok && fileName.indexOf('_infrastructure') < 0; + ok = ok && fileName.indexOf('node_modules/') < 0; + ok = ok && /^[a-z]/i.test(fileName); + + //TODO remove this dev code + ok = ok && (!testNames || testNames.some((pattern) => { + return fileName.indexOf(pattern) > -1; + })); + return ok; + } + + public addSuite(suite: ITestSuite):void { this.suites.push(suite); } - public run() { + public run():void { this.timer = new Timer(); this.timer.start(); - var index = new ReferenceIndex(this.options); - index.collectReferences(this.files, () => { + this.index.parseFiles(this.files, () => { + console.log('files:'); + console.log('---'); this.files.forEach((file) => { console.log(file.filePathWithName); file.references.forEach((file) => { - console.log(' - %s', file.filePathWithName); + console.log(' - %s', file.filePathWithName); }); }); + console.log('---'); + this.getChanges(); + }); + } + + public getChanges():void { + var changes = new GitChanges(this.dtPath); + changes.getChanges((err, changes: string[]) => { + if (err) { + throw err; + } + console.log('changes:'); + console.log('---'); + changes.forEach((file) => { + console.log(file); + }); + console.log('---'); + this.runTests(); }); } - public runTests() { + + public runTests():void { var syntaxChecking = new SyntaxChecking(this.options); var testEval = new TestEval(this.options); diff --git a/_infrastructure/tests/src/changes.ts b/_infrastructure/tests/src/changes.ts index e69de29bb2..5f28f24c5f 100644 --- a/_infrastructure/tests/src/changes.ts +++ b/_infrastructure/tests/src/changes.ts @@ -0,0 +1,37 @@ +/// + +module DT { + + var fs = require('fs'); + var path = require('path'); + var Git = require('git-wrapper'); + + export class GitChanges { + + options = []; + + constructor(public baseDir: string) { + var dir = path.join(baseDir, '.git'); + if (!fs.existsSync(dir)) { + throw new Error('cannot locate git-dir: ' + dir); + } + this.options['git-dir'] = dir; + } + + getChanges(callback: (err, paths: string[]) => void): void { + //git diff --name-only HEAD~1 + var git = new Git(this.options); + var opts = {}; + var args = ['--name-only HEAD~1']; + git.exec('diff', opts, args, (err, msg: string) => { + if (err) { + callback(err, null); + return; + } + var paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); + // console.log(paths); + callback(null, paths); + }); + } + } +} diff --git a/_infrastructure/tests/src/file.ts b/_infrastructure/tests/src/file.ts index c1a8d1f517..bd7f62dc86 100644 --- a/_infrastructure/tests/src/file.ts +++ b/_infrastructure/tests/src/file.ts @@ -1,3 +1,5 @@ +/// + module DT { var path = require('path'); diff --git a/_infrastructure/tests/src/references.ts b/_infrastructure/tests/src/index.ts similarity index 90% rename from _infrastructure/tests/src/references.ts rename to _infrastructure/tests/src/index.ts index c98f947c38..4a40f35d49 100644 --- a/_infrastructure/tests/src/references.ts +++ b/_infrastructure/tests/src/index.ts @@ -1,13 +1,12 @@ -/// -/// - -/// +/// +/// +/// module DT { var fs = require('fs'); var path = require('path'); - export class ReferenceIndex { + export class FileIndex { fileMap: {[path:string]:File}; @@ -15,7 +14,7 @@ module DT { } - collectReferences(files: File[], callback: () => void): void { + parseFiles(files: File[], callback: () => void): void { this.fileMap = Object.create(null); files.forEach((file) => { this.fileMap[file.fullPath] = file; diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index d1f0f952e2..53274587ad 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -1,5 +1,5 @@ -/// -/// +/// +/// module DT { ///////////////////////////////// diff --git a/_infrastructure/tests/src/reporter/reporter.ts b/_infrastructure/tests/src/reporter/reporter.ts index d4556e0cc0..5f01c43ab5 100644 --- a/_infrastructure/tests/src/reporter/reporter.ts +++ b/_infrastructure/tests/src/reporter/reporter.ts @@ -1,3 +1,6 @@ +/// +/// + module DT { ///////////////////////////////// // Test reporter interface diff --git a/_infrastructure/tests/src/timer.ts b/_infrastructure/tests/src/timer.ts index e03a941668..446ea1b34f 100644 --- a/_infrastructure/tests/src/timer.ts +++ b/_infrastructure/tests/src/timer.ts @@ -1,5 +1,5 @@ -/// -/// +/// +/// module DT { ///////////////////////////////// diff --git a/_infrastructure/tests/src/tsc.ts b/_infrastructure/tests/src/tsc.ts index 5a2c803c80..6bd3fb2591 100644 --- a/_infrastructure/tests/src/tsc.ts +++ b/_infrastructure/tests/src/tsc.ts @@ -1,7 +1,6 @@ -/// -/// - -/// +/// +/// +/// module DT { var fs = require('fs'); diff --git a/_infrastructure/tests/src/util.ts b/_infrastructure/tests/src/util.ts index 3b441991fd..3d63ce1421 100644 --- a/_infrastructure/tests/src/util.ts +++ b/_infrastructure/tests/src/util.ts @@ -1,3 +1,5 @@ +/// + module DT { var referenceTagExp = //g; diff --git a/package.json b/package.json index 0119109e94..bb4c816f6e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,13 @@ { - "private": true, - "name": "DefinitelyTyped", - "version": "0.0.0", - "scripts": { - "test": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7" - } + "private": true, + "name": "DefinitelyTyped", + "version": "0.0.0", + "scripts": { + "test": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7" + }, + "dependencies": { + "git-wrapper": "~0.1.1", + "glob": "~3.2.9", + "source-map-support": "~0.2.5" + } } From 35765e6305881906943a2bb2cae5fee89e927a5d Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 21:18:42 +0100 Subject: [PATCH 04/13] added test-module.d.ts --- test-module/test-module.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 test-module/test-module.d.ts diff --git a/test-module/test-module.d.ts b/test-module/test-module.d.ts new file mode 100644 index 0000000000..e73a9981c2 --- /dev/null +++ b/test-module/test-module.d.ts @@ -0,0 +1,6 @@ +declare module 'test-module' { + interface Such { + amaze(): void; + } + export function wow(): Such; +} From 76ffe56a44b64aabd8ff980ab85af9c11f80e9d3 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 22:30:42 +0100 Subject: [PATCH 05/13] first setup for incremental testing combines reference parser and git changes still very dev mode --- _infrastructure/tests/runner.js | 231 +++++--- _infrastructure/tests/runner.ts | 555 ++++++++++-------- _infrastructure/tests/src/changes.ts | 58 +- _infrastructure/tests/src/file.ts | 56 +- _infrastructure/tests/src/host/exec.ts | 34 +- _infrastructure/tests/src/index.ts | 140 +++-- _infrastructure/tests/src/printer.ts | 172 +++--- .../tests/src/reporter/reporter.ts | 58 +- _infrastructure/tests/src/suite/suite.ts | 139 ++--- _infrastructure/tests/src/suite/syntax.ts | 28 +- _infrastructure/tests/src/suite/testEval.ts | 27 +- _infrastructure/tests/src/suite/tscParams.ts | 92 +-- _infrastructure/tests/src/timer.ts | 74 +-- _infrastructure/tests/src/tsc.ts | 70 +-- _infrastructure/tests/src/util.ts | 37 +- 15 files changed, 981 insertions(+), 790 deletions(-) diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index ee80513a21..1a7c4fc987 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -47,6 +47,8 @@ var Exec = function () { /// var DT; (function (DT) { + 'use-strict'; + var path = require('path'); ///////////////////////////////// @@ -55,30 +57,17 @@ var DT; ///////////////////////////////// var File = (function () { function File(baseDir, filePathWithName) { + this.references = []; this.baseDir = baseDir; this.filePathWithName = filePathWithName; - this.references = []; this.ext = path.extname(this.filePathWithName); this.file = path.basename(this.filePathWithName, this.ext); this.dir = path.dirname(this.filePathWithName); + this.formatName = path.join(this.dir, this.file + this.ext); + this.fullPath = path.join(this.baseDir, this.dir, this.file + this.ext); + // lock it (shallow) + // Object.freeze(this); } - Object.defineProperty(File.prototype, "formatName", { - // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' - get: function () { - return path.join(this.dir, this.file + this.ext); - }, - enumerable: true, - configurable: true - }); - - Object.defineProperty(File.prototype, "fullPath", { - get: function () { - return path.join(this.baseDir, this.dir, this.file + this.ext); - }, - enumerable: true, - configurable: true - }); - File.prototype.toString = function () { return '[File ' + this.filePathWithName + ']'; }; @@ -91,6 +80,7 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; var fs = require('fs'); var Tsc = (function () { @@ -132,6 +122,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + ///////////////////////////////// // Timer.start starts a timer // Timer.end stops the timer and sets asString to the pretty print value @@ -171,6 +163,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + var referenceTagExp = //g; function endsWith(str, suffix) { @@ -201,6 +195,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + var fs = require('fs'); var path = require('path'); @@ -219,6 +215,17 @@ var DT; }); }; + FileIndex.prototype.hasFile = function (target) { + return target in this.fileMap; + }; + + FileIndex.prototype.getFile = function (target) { + if (target in this.fileMap) { + return this.fileMap[target]; + } + return null; + }; + FileIndex.prototype.loadReferences = function (files, callback) { var _this = this; var queue = files.slice(0); @@ -276,6 +283,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + var fs = require('fs'); var path = require('path'); var Git = require('git-wrapper'); @@ -283,7 +292,8 @@ var DT; var GitChanges = (function () { function GitChanges(baseDir) { this.baseDir = baseDir; - this.options = []; + this.options = {}; + this.paths = []; var dir = path.join(baseDir, '.git'); if (!fs.existsSync(dir)) { throw new Error('cannot locate git-dir: ' + dir); @@ -291,19 +301,20 @@ var DT; this.options['git-dir'] = dir; } GitChanges.prototype.getChanges = function (callback) { + var _this = this; //git diff --name-only HEAD~1 var git = new Git(this.options); var opts = {}; var args = ['--name-only HEAD~1']; git.exec('diff', opts, args, function (err, msg) { if (err) { - callback(err, null); + callback(err); return; } - var paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); + _this.paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); // console.log(paths); - callback(null, paths); + callback(null); }); }; return GitChanges; @@ -314,6 +325,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + ///////////////////////////////// // All the common things that we pring are functions of this class ///////////////////////////////// @@ -430,6 +443,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + ///////////////////////////////// @@ -463,6 +478,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + ///////////////////////////////// @@ -549,6 +566,8 @@ var __extends = this.__extends || function (d, b) { }; var DT; (function (DT) { + 'use-strict'; + ///////////////////////////////// // .d.ts syntax inspection ///////////////////////////////// @@ -570,6 +589,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + ///////////////////////////////// // Compile with *-tests.ts ///////////////////////////////// @@ -591,6 +612,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + var fs = require('fs'); ///////////////////////////////// @@ -665,6 +688,8 @@ var DT; /// var DT; (function (DT) { + 'use-strict'; + require('source-map-support').install(); var fs = require('fs'); @@ -673,17 +698,6 @@ var DT; var tsExp = /\.ts$/; - // TOD0 remove this after dev! - var testNames = [ - 'async/', - 'jquery/jquery.d', - 'angularjs/angular.d', - 'pixi/' - ]; - - /* if (process.env.TRAVIS) { - testNames = null; - } */ DT.DEFAULT_TSC_VERSION = "0.9.1.1"; var Test = (function () { @@ -731,6 +745,8 @@ var DT; ///////////////////////////////// // The main class to kick things off ///////////////////////////////// + // TODO move to bluebird (Promises) + // TODO move to lazy.js (functional) var TestRunner = (function () { function TestRunner(dtPath, options) { if (typeof options === "undefined") { options = { tscVersion: DT.DEFAULT_TSC_VERSION }; } @@ -741,8 +757,9 @@ var DT; this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; this.index = new DT.FileIndex(this.options); + this.changes = new DT.GitChanges(this.dtPath); - // should be async + // should be async (way faster) // only includes .d.ts or -tests.ts or -test.ts or .ts var filesName = glob.sync('**/*.ts', { cwd: dtPath }); this.files = filesName.filter(function (fileName) { @@ -751,29 +768,50 @@ var DT; return new DT.File(dtPath, fileName); }); } - TestRunner.prototype.checkAcceptFile = function (fileName) { - var ok = tsExp.test(fileName); - ok = ok && fileName.indexOf('_infrastructure') < 0; - ok = ok && fileName.indexOf('node_modules/') < 0; - ok = ok && /^[a-z]/i.test(fileName); - - //TODO remove this dev code - ok = ok && (!testNames || testNames.some(function (pattern) { - return fileName.indexOf(pattern) > -1; - })); - return ok; - }; - TestRunner.prototype.addSuite = function (suite) { this.suites.push(suite); }; TestRunner.prototype.run = function () { - var _this = this; this.timer = new DT.Timer(); this.timer.start(); + // we need promises + this.doGetChanges(); + }; + + TestRunner.prototype.checkAcceptFile = function (fileName) { + var ok = tsExp.test(fileName); + ok = ok && fileName.indexOf('_infrastructure') < 0; + ok = ok && fileName.indexOf('node_modules/') < 0; + ok = ok && /^[a-z]/i.test(fileName); + return ok; + }; + + TestRunner.prototype.doGetChanges = function () { + var _this = this; + this.changes.getChanges(function (err) { + if (err) { + throw err; + } + console.log(''); + console.log('changes:'); + console.log('---'); + + _this.changes.paths.forEach(function (file) { + console.log(file); + }); + console.log('---'); + + // chain + _this.doGetReferences(); + }); + }; + + TestRunner.prototype.doGetReferences = function () { + var _this = this; this.index.parseFiles(this.files, function () { + console.log(''); console.log('files:'); console.log('---'); _this.files.forEach(function (file) { @@ -783,29 +821,80 @@ var DT; }); }); console.log('---'); - _this.getChanges(); + + // chain + _this.doCollectTargets(); }); }; - TestRunner.prototype.getChanges = function () { + TestRunner.prototype.doCollectTargets = function () { + // TODO clean this up when functional (do we need changeMap?) var _this = this; - var changes = new DT.GitChanges(this.dtPath); - changes.getChanges(function (err, changes) { - if (err) { - throw err; + // bake map for lookup + var changeMap = this.changes.paths.filter(function (full) { + return _this.checkAcceptFile(full); + }).map(function (local) { + return path.resolve(_this.dtPath, local); + }).reduce(function (memo, full) { + var file = _this.index.getFile(full); + if (!file) { + // what does it mean? deleted? + console.log('not in index: ' + full); + return memo; } - console.log('changes:'); - console.log('---'); - changes.forEach(function (file) { - console.log(file); - }); - console.log('---'); + memo[full] = file; + return memo; + }, Object.create(null)); - _this.runTests(); + // collect referring files (and also log) + var touched = Object.create(null); + console.log(''); + console.log('relevant changes:'); + console.log('---'); + Object.keys(changeMap).sort().forEach(function (src) { + touched[src] = changeMap[src]; + console.log(changeMap[src].formatName); }); + console.log('---'); + + // terrible loop (whatever) + // just add stuff until there is nothing new added + // TODO improve it + var added; + do { + added = 0; + this.files.forEach(function (file) { + // lol getter + if (file.fullPath in touched) { + return; + } + + // check if one of our references is touched + file.references.some(function (ref) { + if (ref.fullPath in touched) { + // add us + touched[file.fullPath] = file; + added++; + return true; + } + return false; + }); + }); + } while(added > 0); + + console.log(''); + console.log('touched:'); + console.log('---'); + var files = Object.keys(touched).sort().map(function (src) { + console.log(touched[src].formatName); + return touched[src]; + }); + console.log('---'); + + this.runTests(files); }; - TestRunner.prototype.runTests = function () { + TestRunner.prototype.runTests = function (files) { var _this = this; var syntaxChecking = new DT.SyntaxChecking(this.options); var testEval = new DT.TestEval(this.options); @@ -814,9 +903,10 @@ var DT; this.addSuite(testEval); } - var typings = syntaxChecking.filterTargetFiles(this.files).length; - var testFiles = testEval.filterTargetFiles(this.files).length; - this.print = new DT.Print(this.options.tscVersion, typings, testFiles, this.files.length); + var typings = syntaxChecking.filterTargetFiles(files).length; + var testFiles = testEval.filterTargetFiles(files).length; + + this.print = new DT.Print(this.options.tscVersion, typings, testFiles, files.length); this.print.printHeader(); if (this.options.findNotRequiredTscparams) { @@ -830,7 +920,7 @@ var DT; suite.testReporter = suite.testReporter || new DT.DefaultTestReporter(_this.print); _this.print.printSuiteHeader(suite.testSuiteName); - var targetFiles = suite.filterTargetFiles(_this.files); + var targetFiles = suite.filterTargetFiles(files); suite.start(targetFiles, function (testResult, index) { _this.testCompleteCallback(testResult, index); }, function (suite) { @@ -840,7 +930,7 @@ var DT; }); } else { _this.timer.end(); - _this.allTestCompleteCallback(); + _this.allTestCompleteCallback(files); } }; executor(); @@ -864,7 +954,7 @@ var DT; this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); }; - TestRunner.prototype.allTestCompleteCallback = function () { + TestRunner.prototype.allTestCompleteCallback = function (files) { var _this = this; var testEval = this.suites.filter(function (suite) { return suite instanceof DT.TestEval; @@ -875,11 +965,13 @@ var DT; }).reduce(function (a, b) { return a.indexOf(b) < 0 ? a.concat([b]) : a; }, []); - var typings = this.files.map(function (file) { + + var typings = files.map(function (file) { return file.dir; }).reduce(function (a, b) { return a.indexOf(b) < 0 ? a.concat([b]) : a; }, []); + var withoutTestTypings = typings.filter(function (typing) { return existsTestTypings.indexOf(typing) < 0; }); @@ -892,6 +984,7 @@ var DT; this.print.printDiv(); this.print.printElapsedTime(this.timer.asString, this.timer.time); + this.suites.filter(function (suite) { return suite.printErrorCount; }).forEach(function (suite) { diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index 555ec54ab2..f2ca6a0592 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -19,291 +19,352 @@ /// module DT { - require('source-map-support').install(); + 'use-strict'; - var fs = require('fs'); - var path = require('path'); - var glob = require('glob'); + require('source-map-support').install(); - var tsExp = /\.ts$/; + var fs = require('fs'); + var path = require('path'); + var glob = require('glob'); - // TOD0 remove this after dev! - var testNames = [ - 'async/', - 'jquery/jquery.d', - 'angularjs/angular.d', - 'pixi/' - ]; - /* if (process.env.TRAVIS) { - testNames = null; - } */ + var tsExp = /\.ts$/; - export var DEFAULT_TSC_VERSION = "0.9.1.1"; + export var DEFAULT_TSC_VERSION = "0.9.1.1"; - export class Test { - constructor(public suite: ITestSuite, public tsfile: File, public options?: TscExecOptions) { - } + export class Test { + constructor(public suite: ITestSuite, public tsfile: File, public options?: TscExecOptions) { + } - public run(callback: (result: TestResult) => void) { - Tsc.run(this.tsfile.filePathWithName, this.options, (execResult) => { - var testResult = new TestResult(); - testResult.hostedBy = this.suite; - testResult.targetFile = this.tsfile; - testResult.options = this.options; + public run(callback: (result: TestResult) => void) { + Tsc.run(this.tsfile.filePathWithName, this.options, (execResult) => { + var testResult = new TestResult(); + testResult.hostedBy = this.suite; + testResult.targetFile = this.tsfile; + testResult.options = this.options; - testResult.stdout = execResult.stdout; - testResult.stderr = execResult.stderr; - testResult.exitCode = execResult.exitCode; + testResult.stdout = execResult.stdout; + testResult.stderr = execResult.stderr; + testResult.exitCode = execResult.exitCode; - callback(testResult); - }); - } - } - ///////////////////////////////// - // Test results - ///////////////////////////////// - export class TestResult { - hostedBy: ITestSuite; - targetFile: File; - options: TscExecOptions; + callback(testResult); + }); + } + } - stdout: string; - stderr: string; - exitCode: number; + ///////////////////////////////// + // Test results + ///////////////////////////////// + export class TestResult { + hostedBy: ITestSuite; + targetFile: File; + options: TscExecOptions; - public get success(): boolean { - return this.exitCode === 0; - } - } - export interface ITestRunnerOptions { - tscVersion:string; - findNotRequiredTscparams?:boolean; - } + stdout: string; + stderr: string; + exitCode: number; - ///////////////////////////////// - // The main class to kick things off - ///////////////////////////////// - export class TestRunner { - files: File[]; - timer: Timer; - suites: ITestSuite[] = []; - private index: FileIndex; - private print: Print; + public get success(): boolean { + return this.exitCode === 0; + } + } - constructor(public dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { - this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; + export interface ITestRunnerOptions { + tscVersion:string; + findNotRequiredTscparams?:boolean; + } + ///////////////////////////////// + // The main class to kick things off + ///////////////////////////////// + // TODO move to bluebird (Promises) + // TODO move to lazy.js (functional) + export class TestRunner { + files: File[]; + timer: Timer; + suites: ITestSuite[] = []; - this.index = new FileIndex(this.options); + private index: FileIndex; + private changes: GitChanges; + private print: Print; - // should be async - // only includes .d.ts or -tests.ts or -test.ts or .ts - var filesName = glob.sync('**/*.ts', { cwd: dtPath }); - this.files = filesName - .filter((fileName) => { - return this.checkAcceptFile(fileName); - }) - .sort() - .map((fileName) => { - return new File(dtPath, fileName); - }); - } + constructor(public dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { + this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - public checkAcceptFile(fileName: string): boolean { - var ok = tsExp.test(fileName); - ok = ok && fileName.indexOf('_infrastructure') < 0; - ok = ok && fileName.indexOf('node_modules/') < 0; - ok = ok && /^[a-z]/i.test(fileName); + this.index = new FileIndex(this.options); + this.changes = new GitChanges(this.dtPath); - //TODO remove this dev code - ok = ok && (!testNames || testNames.some((pattern) => { - return fileName.indexOf(pattern) > -1; - })); - return ok; - } + // should be async (way faster) + // only includes .d.ts or -tests.ts or -test.ts or .ts + var filesName = glob.sync('**/*.ts', { cwd: dtPath }); + this.files = filesName.filter((fileName) => { + return this.checkAcceptFile(fileName); + }).sort().map((fileName) => { + return new File(dtPath, fileName); + }); + } - public addSuite(suite: ITestSuite):void { - this.suites.push(suite); - } + public addSuite(suite: ITestSuite): void { + this.suites.push(suite); + } - public run():void { - this.timer = new Timer(); - this.timer.start(); + public run(): void { + this.timer = new Timer(); + this.timer.start(); - this.index.parseFiles(this.files, () => { - console.log('files:'); - console.log('---'); - this.files.forEach((file) => { - console.log(file.filePathWithName); - file.references.forEach((file) => { - console.log(' - %s', file.filePathWithName); - }); - }); - console.log('---'); - this.getChanges(); - }); - } + // we need promises + this.doGetChanges(); + } - public getChanges():void { - var changes = new GitChanges(this.dtPath); - changes.getChanges((err, changes: string[]) => { - if (err) { - throw err; - } - console.log('changes:'); - console.log('---'); - changes.forEach((file) => { - console.log(file); - }); - console.log('---'); + private checkAcceptFile(fileName: string): boolean { + var ok = tsExp.test(fileName); + ok = ok && fileName.indexOf('_infrastructure') < 0; + ok = ok && fileName.indexOf('node_modules/') < 0; + ok = ok && /^[a-z]/i.test(fileName); + return ok; + } - this.runTests(); - }); - } + private doGetChanges(): void { + this.changes.getChanges((err) => { + if (err) { + throw err; + } + console.log(''); + console.log('changes:'); + console.log('---'); - public runTests():void { + this.changes.paths.forEach((file) => { + console.log(file); + }); + console.log('---'); - var syntaxChecking = new SyntaxChecking(this.options); - var testEval = new TestEval(this.options); - if (!this.options.findNotRequiredTscparams) { - this.addSuite(syntaxChecking); - this.addSuite(testEval); - } + // chain + this.doGetReferences(); + }); + } - var typings = syntaxChecking.filterTargetFiles(this.files).length; - var testFiles = testEval.filterTargetFiles(this.files).length; - this.print = new Print(this.options.tscVersion, typings, testFiles, this.files.length); - this.print.printHeader(); + private doGetReferences(): void { + this.index.parseFiles(this.files, () => { + console.log(''); + console.log('files:'); + console.log('---'); + this.files.forEach((file) => { + console.log(file.filePathWithName); + file.references.forEach((file) => { + console.log(' - %s', file.filePathWithName); + }); + }); + console.log('---'); - if (this.options.findNotRequiredTscparams) { - this.addSuite(new FindNotRequiredTscparams(this.options, this.print)); - } + // chain + this.doCollectTargets(); + }); + } - var count = 0; - var executor = () => { - var suite = this.suites[count]; - if (suite) { - suite.testReporter = suite.testReporter || new DefaultTestReporter(this.print); + private doCollectTargets(): void { - this.print.printSuiteHeader(suite.testSuiteName); - var targetFiles = suite.filterTargetFiles(this.files); - suite.start( - targetFiles, - (testResult, index) => { - this.testCompleteCallback(testResult, index); - }, - (suite) => { - this.suiteCompleteCallback(suite); - count++; - executor(); - }); - } else { - this.timer.end(); - this.allTestCompleteCallback(); - } - }; - executor(); - } + // TODO clean this up when functional (do we need changeMap?) - private testCompleteCallback(testResult: TestResult, index: number) { - var reporter = testResult.hostedBy.testReporter; - if (testResult.success) { - reporter.printPositiveCharacter(index, testResult); - } else { - reporter.printNegativeCharacter(index, testResult); - } - } + // bake map for lookup + var changeMap = this.changes.paths.filter((full) => { + return this.checkAcceptFile(full); + }).map((local) => { + return path.resolve(this.dtPath, local); + }).reduce((memo, full) => { + var file = this.index.getFile(full); + if (!file) { + // what does it mean? deleted? + console.log('not in index: ' + full); + return memo; + } + memo[full] = file; + return memo; + }, Object.create(null)); - private suiteCompleteCallback(suite: ITestSuite) { - this.print.printBreak(); + // collect referring files (and also log) + var touched = Object.create(null); + console.log(''); + console.log('relevant changes:'); + console.log('---'); + Object.keys(changeMap).sort().forEach((src) => { + touched[src] = changeMap[src]; + console.log(changeMap[src].formatName); + }); + console.log('---'); - this.print.printDiv(); - this.print.printElapsedTime(suite.timer.asString, suite.timer.time); - this.print.printSuccessCount(suite.okTests.length, suite.testResults.length); - this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); - } + // terrible loop (whatever) + // just add stuff until there is nothing new added + // TODO improve it + var added:number; + do { + added = 0; + this.files.forEach((file) => { + // lol getter + if (file.fullPath in touched) { + return; + } + // check if one of our references is touched + file.references.some((ref) => { + if (ref.fullPath in touched) { + // add us + touched[file.fullPath] = file; + added++; + return true; + } + return false; + }); + }); + } + while(added > 0); - private allTestCompleteCallback() { - var testEval = this.suites.filter(suite => suite instanceof TestEval)[0]; - if (testEval) { - var existsTestTypings: string[] = testEval.testResults - .map((testResult) => { - return testResult.targetFile.dir; - }) - .reduce((a: string[], b: string) => { - return a.indexOf(b) < 0 ? a.concat([b]) : a; - }, []); - var typings: string[] = this.files - .map((file) => { - return file.dir; - }) - .reduce((a: string[], b: string) => { - return a.indexOf(b) < 0 ? a.concat([b]) : a; - }, []); - var withoutTestTypings: string[] = typings - .filter((typing) => { - return existsTestTypings.indexOf(typing) < 0; - }); - this.print.printDiv(); - this.print.printTypingsWithoutTest(withoutTestTypings); - } + console.log(''); + console.log('touched:'); + console.log('---'); + var files: File[] = Object.keys(touched).sort().map((src) => { + console.log(touched[src].formatName); + return touched[src]; + }); + console.log('---'); - this.print.printDiv(); - this.print.printTotalMessage(); + this.runTests(files); + } - this.print.printDiv(); - this.print.printElapsedTime(this.timer.asString, this.timer.time); - this.suites - .filter((suite: ITestSuite) => { - return suite.printErrorCount; - }) - .forEach((suite: ITestSuite) => { - this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); - }); - if (testEval) { - this.print.printSuiteErrorCount("Without tests", withoutTestTypings.length, typings.length, '\33[33m\33[1m'); - } + private runTests(files: File[]): void { - this.print.printDiv(); - if (this.suites.some((suite) => { - return suite.ngTests.length !== 0 - })) { - this.print.printErrorsHeader(); + var syntaxChecking = new SyntaxChecking(this.options); + var testEval = new TestEval(this.options); + if (!this.options.findNotRequiredTscparams) { + this.addSuite(syntaxChecking); + this.addSuite(testEval); + } - this.suites - .filter((suite) => { - return suite.ngTests.length !== 0; - }) - .forEach((suite) => { - suite.ngTests.forEach((testResult) => { - this.print.printErrorsForFile(testResult); - }); - this.print.printBoldDiv(); - }); + var typings = syntaxChecking.filterTargetFiles(files).length; + var testFiles = testEval.filterTargetFiles(files).length; - process.exit(1); - } - } - } + this.print = new Print(this.options.tscVersion, typings, testFiles, files.length); + this.print.printHeader(); - var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); - var findNotRequiredTscparams = process.argv.some(arg => arg == "--try-without-tscparams"); - var tscVersionIndex = process.argv.indexOf("--tsc-version"); - var tscVersion = DEFAULT_TSC_VERSION; - if (-1 < tscVersionIndex) { - tscVersion = process.argv[tscVersionIndex + 1]; - } + if (this.options.findNotRequiredTscparams) { + this.addSuite(new FindNotRequiredTscparams(this.options, this.print)); + } - console.log('--'); - console.log(' dtPath %s', dtPath); - console.log(' tscVersion %s', tscVersion); - console.log(' findNotRequiredTscparams %s', findNotRequiredTscparams); - console.log('--'); - console.log(''); + var count = 0; + var executor = () => { + var suite = this.suites[count]; + if (suite) { + suite.testReporter = suite.testReporter || new DefaultTestReporter(this.print); - var runner = new TestRunner(dtPath, { - tscVersion: tscVersion, - findNotRequiredTscparams: findNotRequiredTscparams - }); - runner.run(); + this.print.printSuiteHeader(suite.testSuiteName); + var targetFiles = suite.filterTargetFiles(files); + suite.start(targetFiles, (testResult, index) => { + this.testCompleteCallback(testResult, index); + }, (suite) => { + this.suiteCompleteCallback(suite); + count++; + executor(); + }); + } + else { + this.timer.end(); + this.allTestCompleteCallback(files); + } + }; + executor(); + } + + private testCompleteCallback(testResult: TestResult, index: number) { + var reporter = testResult.hostedBy.testReporter; + if (testResult.success) { + reporter.printPositiveCharacter(index, testResult); + } + else { + reporter.printNegativeCharacter(index, testResult); + } + } + + private suiteCompleteCallback(suite: ITestSuite) { + this.print.printBreak(); + + this.print.printDiv(); + this.print.printElapsedTime(suite.timer.asString, suite.timer.time); + this.print.printSuccessCount(suite.okTests.length, suite.testResults.length); + this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); + } + + private allTestCompleteCallback(files: File[]) { + var testEval = this.suites.filter(suite => suite instanceof TestEval)[0]; + if (testEval) { + var existsTestTypings: string[] = testEval.testResults.map((testResult) => { + return testResult.targetFile.dir; + }).reduce((a: string[], b: string) => { + return a.indexOf(b) < 0 ? a.concat([b]) : a; + }, []); + + var typings: string[] = files.map((file) => { + return file.dir; + }).reduce((a: string[], b: string) => { + return a.indexOf(b) < 0 ? a.concat([b]) : a; + }, []); + + var withoutTestTypings: string[] = typings.filter((typing) => { + return existsTestTypings.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); + + this.suites.filter((suite: ITestSuite) => { + return suite.printErrorCount; + }).forEach((suite: ITestSuite) => { + this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); + }); + if (testEval) { + this.print.printSuiteErrorCount("Without tests", withoutTestTypings.length, typings.length, '\33[33m\33[1m'); + } + + this.print.printDiv(); + if (this.suites.some((suite) => { + return suite.ngTests.length !== 0 + })) { + this.print.printErrorsHeader(); + + this.suites.filter((suite) => { + return suite.ngTests.length !== 0; + }).forEach((suite) => { + suite.ngTests.forEach((testResult) => { + this.print.printErrorsForFile(testResult); + }); + this.print.printBoldDiv(); + }); + + process.exit(1); + } + } + } + + var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); + var findNotRequiredTscparams = process.argv.some(arg => arg == "--try-without-tscparams"); + var tscVersionIndex = process.argv.indexOf("--tsc-version"); + var tscVersion = DEFAULT_TSC_VERSION; + if (-1 < tscVersionIndex) { + tscVersion = process.argv[tscVersionIndex + 1]; + } + + console.log('--'); + console.log(' dtPath %s', dtPath); + console.log(' tscVersion %s', tscVersion); + console.log(' findNotRequiredTscparams %s', findNotRequiredTscparams); + console.log('--'); + console.log(''); + + var runner = new TestRunner(dtPath, { + tscVersion: tscVersion, + findNotRequiredTscparams: findNotRequiredTscparams + }); + runner.run(); } diff --git a/_infrastructure/tests/src/changes.ts b/_infrastructure/tests/src/changes.ts index 5f28f24c5f..3bc7334e02 100644 --- a/_infrastructure/tests/src/changes.ts +++ b/_infrastructure/tests/src/changes.ts @@ -1,37 +1,39 @@ /// module DT { + 'use-strict'; - var fs = require('fs'); - var path = require('path'); - var Git = require('git-wrapper'); + var fs = require('fs'); + var path = require('path'); + var Git = require('git-wrapper'); - export class GitChanges { + export class GitChanges { - options = []; + options = {}; + paths: string[] = []; - constructor(public baseDir: string) { - var dir = path.join(baseDir, '.git'); - if (!fs.existsSync(dir)) { - throw new Error('cannot locate git-dir: ' + dir); - } - this.options['git-dir'] = dir; - } + constructor(public baseDir: string) { + var dir = path.join(baseDir, '.git'); + if (!fs.existsSync(dir)) { + throw new Error('cannot locate git-dir: ' + dir); + } + this.options['git-dir'] = dir; + } - getChanges(callback: (err, paths: string[]) => void): void { - //git diff --name-only HEAD~1 - var git = new Git(this.options); - var opts = {}; - var args = ['--name-only HEAD~1']; - git.exec('diff', opts, args, (err, msg: string) => { - if (err) { - callback(err, null); - return; - } - var paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); - // console.log(paths); - callback(null, paths); - }); - } - } + getChanges(callback: (err) => void): void { + //git diff --name-only HEAD~1 + var git = new Git(this.options); + var opts = {}; + var args = ['--name-only HEAD~1']; + git.exec('diff', opts, args, (err, msg: string) => { + if (err) { + callback(err); + return; + } + this.paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); + // console.log(paths); + callback(null); + }); + } + } } diff --git a/_infrastructure/tests/src/file.ts b/_infrastructure/tests/src/file.ts index bd7f62dc86..4bb81e9857 100644 --- a/_infrastructure/tests/src/file.ts +++ b/_infrastructure/tests/src/file.ts @@ -1,35 +1,39 @@ /// module DT { - var path = require('path'); + 'use-strict'; - ///////////////////////////////// - // Given a document root + ts file pattern this class returns: - // all the TS files OR just tests OR just definition files - ///////////////////////////////// - export class File { - dir: string; - file: string; - ext: string; - references: File[] = []; + var path = require('path'); - constructor(public baseDir: string, public filePathWithName: string) { - this.ext = path.extname(this.filePathWithName); - this.file = path.basename(this.filePathWithName, this.ext); - this.dir = path.dirname(this.filePathWithName); - } + ///////////////////////////////// + // Given a document root + ts file pattern this class returns: + // all the TS files OR just tests OR just definition files + ///////////////////////////////// + export class File { + baseDir: string; + filePathWithName: string; + dir: string; + file: string; + ext: string; + formatName: string; + fullPath: string; + references: File[] = []; - // From '/complete/path/to/file' to 'specfolder/specfile.d.ts' - public get formatName(): string { - return path.join(this.dir, this.file + this.ext); - } + constructor(baseDir: string, filePathWithName: string) { + this.baseDir = baseDir; + this.filePathWithName = filePathWithName; + this.ext = path.extname(this.filePathWithName); + this.file = path.basename(this.filePathWithName, this.ext); + this.dir = path.dirname(this.filePathWithName); + this.formatName = path.join(this.dir, this.file + this.ext); + this.fullPath = path.join(this.baseDir, this.dir, this.file + this.ext); - public get fullPath(): string { - return path.join(this.baseDir, this.dir, this.file + this.ext); - } + // lock it (shallow) + // Object.freeze(this); + } - toString() { - return '[File ' + this.filePathWithName + ']'; - } - } + toString() { + return '[File ' + this.filePathWithName + ']'; + } + } } diff --git a/_infrastructure/tests/src/host/exec.ts b/_infrastructure/tests/src/host/exec.ts index 9ff896c3f8..07a9d66626 100644 --- a/_infrastructure/tests/src/host/exec.ts +++ b/_infrastructure/tests/src/host/exec.ts @@ -15,32 +15,32 @@ // Allows for executing a program with command-line arguments and reading the result interface IExec { - exec: (filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) => void; + exec: (filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) => void; } class ExecResult { - public stdout = ""; - public stderr = ""; - public exitCode: number; + public stdout = ""; + public stderr = ""; + public exitCode: number; } class NodeExec implements IExec { - public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { - var nodeExec = require('child_process').exec; + public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { + var nodeExec = require('child_process').exec; - var result = new ExecResult(); - result.exitCode = null; - var cmdLine = filename + ' ' + cmdLineArgs.join(' '); + var result = new ExecResult(); + result.exitCode = null; + var cmdLine = filename + ' ' + cmdLineArgs.join(' '); - var process = nodeExec(cmdLine, {maxBuffer: 1 * 1024 * 1024}, function (error, stdout, stderr) { - result.stdout = stdout; - result.stderr = stderr; - result.exitCode = error ? error.code : 0; - handleResult(result); - }); - } + var process = nodeExec(cmdLine, {maxBuffer: 1 * 1024 * 1024}, function (error, stdout, stderr) { + result.stdout = stdout; + result.stderr = stderr; + result.exitCode = error ? error.code : 0; + handleResult(result); + }); + } } var Exec: IExec = function (): IExec { - return new NodeExec(); + return new NodeExec(); }(); diff --git a/_infrastructure/tests/src/index.ts b/_infrastructure/tests/src/index.ts index 4a40f35d49..7c9561af93 100644 --- a/_infrastructure/tests/src/index.ts +++ b/_infrastructure/tests/src/index.ts @@ -3,77 +3,91 @@ /// module DT { - var fs = require('fs'); - var path = require('path'); + 'use-strict'; - export class FileIndex { + var fs = require('fs'); + var path = require('path'); - fileMap: {[path:string]:File}; + export class FileIndex { - constructor(public options: ITestRunnerOptions) { + fileMap: {[path:string]:File + }; - } + constructor(public options: ITestRunnerOptions) { - parseFiles(files: File[], callback: () => void): void { - this.fileMap = Object.create(null); - files.forEach((file) => { - this.fileMap[file.fullPath] = file; - }); - this.loadReferences(files, () => { - callback(); - }); - } + } - private loadReferences(files: File[], callback: () => void): void { - var queue = files.slice(0); - var active = []; - var max = 50; - var next = () => { - if (queue.length === 0 && active.length === 0) { - callback(); - return; - } - // queue paralel - while (queue.length > 0 && active.length < max) { - var file = queue.pop(); - active.push(file); - this.parseFile(file, (file) => { - active.splice(active.indexOf(file), 1); - next(); - }); - } - }; - process.nextTick(next); - } + parseFiles(files: File[], callback: () => void): void { + this.fileMap = Object.create(null); + files.forEach((file) => { + this.fileMap[file.fullPath] = file; + }); + this.loadReferences(files, () => { + callback(); + }); + } - private parseFile(file: File, callback: (file: File) => void): void { - fs.readFile(file.filePathWithName, { - encoding: 'utf8', - flag: 'r' - }, (err, content) => { - if (err) { - // just blow up? - throw err; - } - // console.log('----'); - // console.log(file.filePathWithName); + hasFile(target: string): boolean { + return target in this.fileMap; + } - file.references = extractReferenceTags(content).map((ref: string) => { - return path.resolve(path.dirname(file.fullPath), ref); - }).reduce((memo: File[], ref: string) => { - if (ref in this.fileMap) { - memo.push(this.fileMap[ref]); - } - else { - console.log('not mapped? -> ' + ref); - } - return memo; - }, []); + getFile(target: string): File { + if (target in this.fileMap) { + return this.fileMap[target]; + } + return null; + } - // console.log(file.references); + private loadReferences(files: File[], callback: () => void): void { + var queue = files.slice(0); + var active = []; + var max = 50; + var next = () => { + if (queue.length === 0 && active.length === 0) { + callback(); + return; + } + // queue paralel + while (queue.length > 0 && active.length < max) { + var file = queue.pop(); + active.push(file); + this.parseFile(file, (file) => { + active.splice(active.indexOf(file), 1); + next(); + }); + } + }; + process.nextTick(next); + } - callback(file); - }); - } - } + private parseFile(file: File, callback: (file: File) => void): void { + fs.readFile(file.filePathWithName, { + encoding: 'utf8', + flag: 'r' + }, (err, content) => { + if (err) { + // just blow up? + throw err; + } + // console.log('----'); + // console.log(file.filePathWithName); + + file.references = extractReferenceTags(content).map((ref: string) => { + return path.resolve(path.dirname(file.fullPath), ref); + }).reduce((memo: File[], ref: string) => { + if (ref in this.fileMap) { + memo.push(this.fileMap[ref]); + } + else { + console.log('not mapped? -> ' + ref); + } + return memo; + }, []); + + // console.log(file.references); + + callback(file); + }); + } + } } diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index 53274587ad..585ca5aa3a 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -2,110 +2,112 @@ /// module DT { - ///////////////////////////////// - // All the common things that we pring are functions of this class - ///////////////////////////////// - export class Print { + 'use-strict'; - WIDTH = 77; + ///////////////////////////////// + // All the common things that we pring are functions of this class + ///////////////////////////////// + export class Print { - constructor(public version: string, public typings: number, public tests: number, public tsFiles: number) { - } + WIDTH = 77; - public out(s: any): Print { - process.stdout.write(s); - return this; - } + constructor(public version: string, public typings: number, public tests: number, public tsFiles: number) { + } - public repeat(s: string, times: number): string { - return new Array(times + 1).join(s); - } + public out(s: any): Print { + process.stdout.write(s); + return this; + } - public printHeader() { - this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); - this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); - this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); - this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); - this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); - } + public repeat(s: string, times: number): string { + return new Array(times + 1).join(s); + } - public printSuiteHeader(title: string) { - var left = Math.floor((this.WIDTH - title.length ) / 2) - 1; - var right = Math.ceil((this.WIDTH - title.length ) / 2) - 1; - this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); - this.out(title); - this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); - } + public printHeader() { + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); + this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); + this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); + this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); + } - public printDiv() { - this.out('-----------------------------------------------------------------------------\n'); - } + public printSuiteHeader(title: string) { + var left = Math.floor((this.WIDTH - title.length ) / 2) - 1; + var right = Math.ceil((this.WIDTH - title.length ) / 2) - 1; + this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); + this.out(title); + this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); + } - public printBoldDiv() { - this.out('=============================================================================\n'); - } + public printDiv() { + this.out('-----------------------------------------------------------------------------\n'); + } - public printErrorsHeader() { - this.out('=============================================================================\n'); - this.out(' \33[34m\33[1mErrors in files\33[0m \n'); - this.out('=============================================================================\n'); - } + public printBoldDiv() { + this.out('=============================================================================\n'); + } - public printErrorsForFile(testResult: TestResult) { - this.out('----------------- For file:' + testResult.targetFile.formatName); - this.printBreak().out(testResult.stderr).printBreak(); - } + public printErrorsHeader() { + this.out('=============================================================================\n'); + this.out(' \33[34m\33[1mErrors in files\33[0m \n'); + this.out('=============================================================================\n'); + } - public printBreak(): Print { - this.out('\n'); - return this; - } + public printErrorsForFile(testResult: TestResult) { + this.out('----------------- For file:' + testResult.targetFile.formatName); + this.printBreak().out(testResult.stderr).printBreak(); + } - public clearCurrentLine(): Print { - this.out("\r\33[K"); - return this; - } + public printBreak(): Print { + this.out('\n'); + return this; + } - public printSuccessCount(current: number, total: number) { - this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - } + public clearCurrentLine(): Print { + this.out("\r\33[K"); + return this; + } - public printFailedCount(current: number, total: number) { - this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - } + public printSuccessCount(current: number, total: number) { + this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } - public printTypingsWithoutTestsMessage() { - this.out(' \33[36m\33[1mTyping without tests\33[0m\n'); - } + public printFailedCount(current: number, total: number) { + this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } - public printTotalMessage() { - this.out(' \33[36m\33[1mTotal\33[0m\n'); - } + public printTypingsWithoutTestsMessage() { + this.out(' \33[36m\33[1mTyping without tests\33[0m\n'); + } - public printElapsedTime(time: string, s: number) { - this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); - } + public printTotalMessage() { + this.out(' \33[36m\33[1mTotal\33[0m\n'); + } - public printSuiteErrorCount(errorHeadline: string, current: number, total: number, valuesColor = '\33[31m\33[1m') { - this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); - this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); - } + public printElapsedTime(time: string, s: number) { + this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); + } - public printTypingsWithoutTestName(file: string) { - this.out(' - \33[33m\33[1m' + file + '\33[0m\n'); - } + public printSuiteErrorCount(errorHeadline: string, current: number, total: number, valuesColor = '\33[31m\33[1m') { + this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); + this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } - public printTypingsWithoutTest(withoutTestTypings: string[]) { - if (withoutTestTypings.length > 0) { - this.printTypingsWithoutTestsMessage(); + public printTypingsWithoutTestName(file: string) { + this.out(' - \33[33m\33[1m' + file + '\33[0m\n'); + } - this.printDiv(); - withoutTestTypings.forEach((t) => { - this.printTypingsWithoutTestName(t); - }); - } - } - } + public printTypingsWithoutTest(withoutTestTypings: string[]) { + if (withoutTestTypings.length > 0) { + this.printTypingsWithoutTestsMessage(); + + this.printDiv(); + withoutTestTypings.forEach((t) => { + this.printTypingsWithoutTestName(t); + }); + } + } + } } diff --git a/_infrastructure/tests/src/reporter/reporter.ts b/_infrastructure/tests/src/reporter/reporter.ts index 5f01c43ab5..edab8a6a04 100644 --- a/_infrastructure/tests/src/reporter/reporter.ts +++ b/_infrastructure/tests/src/reporter/reporter.ts @@ -2,38 +2,40 @@ /// module DT { - ///////////////////////////////// - // Test reporter interface - // for example, . and x - ///////////////////////////////// - export interface ITestReporter { - printPositiveCharacter(index: number, testResult: TestResult):void; - printNegativeCharacter(index: number, testResult: TestResult):void; - } + 'use-strict'; - ///////////////////////////////// - // Default test reporter - ///////////////////////////////// - export class DefaultTestReporter implements ITestReporter { - constructor(public print: Print) { - } + ///////////////////////////////// + // Test reporter interface + // for example, . and x + ///////////////////////////////// + export interface ITestReporter { + printPositiveCharacter(index: number, testResult: TestResult):void; + printNegativeCharacter(index: number, testResult: TestResult):void; + } - public printPositiveCharacter(index: number, testResult: TestResult) { - this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); + ///////////////////////////////// + // Default test reporter + ///////////////////////////////// + export class DefaultTestReporter implements ITestReporter { + constructor(public print: Print) { + } - this.printBreakIfNeeded(index); - } + public printPositiveCharacter(index: number, testResult: TestResult) { + this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); - public printNegativeCharacter(index: number, testResult: TestResult) { - this.print.out("x"); + this.printBreakIfNeeded(index); + } - this.printBreakIfNeeded(index); - } + public printNegativeCharacter(index: number, testResult: TestResult) { + this.print.out("x"); - private printBreakIfNeeded(index: number) { - if (index % this.print.WIDTH === 0) { - this.print.printBreak(); - } - } - } + this.printBreakIfNeeded(index); + } + + private printBreakIfNeeded(index: number) { + if (index % this.print.WIDTH === 0) { + this.print.printBreak(); + } + } + } } diff --git a/_infrastructure/tests/src/suite/suite.ts b/_infrastructure/tests/src/suite/suite.ts index 32013cce24..33d69af06e 100644 --- a/_infrastructure/tests/src/suite/suite.ts +++ b/_infrastructure/tests/src/suite/suite.ts @@ -1,83 +1,86 @@ /// module DT { - ///////////////////////////////// - // The interface for test suite - ///////////////////////////////// - export interface ITestSuite { - testSuiteName:string; - errorHeadline:string; - filterTargetFiles(files: File[]):File[]; + 'use-strict'; - start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void):void; + ///////////////////////////////// + // The interface for test suite + ///////////////////////////////// + export interface ITestSuite { + testSuiteName:string; + errorHeadline:string; + filterTargetFiles(files: File[]):File[]; - testResults:TestResult[]; - okTests:TestResult[]; - ngTests:TestResult[]; - timer:Timer; + start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void):void; - testReporter:ITestReporter; - printErrorCount:boolean; - } + testResults:TestResult[]; + okTests:TestResult[]; + ngTests:TestResult[]; + timer:Timer; - ///////////////////////////////// - // Base class for test suite - ///////////////////////////////// - export class TestSuiteBase implements ITestSuite { - timer: Timer = new Timer(); - testResults: TestResult[] = []; - testReporter: ITestReporter; - printErrorCount = true; + testReporter:ITestReporter; + printErrorCount:boolean; + } - constructor(public options: ITestRunnerOptions, public testSuiteName: string, public errorHeadline: string) { - } + ///////////////////////////////// + // Base class for test suite + ///////////////////////////////// + export class TestSuiteBase implements ITestSuite { + timer: Timer = new Timer(); + testResults: TestResult[] = []; + testReporter: ITestReporter; + printErrorCount = true; - public filterTargetFiles(files: File[]): File[] { - throw new Error("please implement this method"); - } + constructor(public options: ITestRunnerOptions, public testSuiteName: string, public errorHeadline: string) { + } - public start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void): void { - targetFiles = this.filterTargetFiles(targetFiles); - this.timer.start(); - var count = 0; - // exec test is async process. serialize. - var executor = () => { - var targetFile = targetFiles[count]; - if (targetFile) { - this.runTest(targetFile, (result) => { - testCallback(result, count + 1); - count++; - executor(); - }); - } else { - this.timer.end(); - this.finish(suiteCallback); - } - }; - executor(); - } + public filterTargetFiles(files: File[]): File[] { + throw new Error("please implement this method"); + } - public runTest(targetFile: File, callback: (result: TestResult) => void): void { - new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run((result) => { - this.testResults.push(result); - callback(result); - }); - } + public start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void): void { + targetFiles = this.filterTargetFiles(targetFiles); + this.timer.start(); + var count = 0; + // exec test is async process. serialize. + var executor = () => { + var targetFile = targetFiles[count]; + if (targetFile) { + this.runTest(targetFile, (result) => { + testCallback(result, count + 1); + count++; + executor(); + }); + } + else { + this.timer.end(); + this.finish(suiteCallback); + } + }; + executor(); + } - public finish(suiteCallback: (suite: ITestSuite) => void) { - suiteCallback(this); - } + public runTest(targetFile: File, callback: (result: TestResult) => void): void { + new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run((result) => { + this.testResults.push(result); + callback(result); + }); + } - public get okTests(): TestResult[] { - return this.testResults.filter((r) => { - return r.success; - }); - } + public finish(suiteCallback: (suite: ITestSuite) => void) { + suiteCallback(this); + } - public get ngTests(): TestResult[] { - return this.testResults.filter((r) => { - return !r.success - }); - } - } + public get okTests(): TestResult[] { + return this.testResults.filter((r) => { + return r.success; + }); + } + + public get ngTests(): TestResult[] { + return this.testResults.filter((r) => { + return !r.success + }); + } + } } diff --git a/_infrastructure/tests/src/suite/syntax.ts b/_infrastructure/tests/src/suite/syntax.ts index f36c26d99b..ff4b3bdb4b 100644 --- a/_infrastructure/tests/src/suite/syntax.ts +++ b/_infrastructure/tests/src/suite/syntax.ts @@ -2,19 +2,21 @@ /// module DT { - ///////////////////////////////// - // .d.ts syntax inspection - ///////////////////////////////// - export class SyntaxChecking extends TestSuiteBase { + 'use-strict'; - constructor(options: ITestRunnerOptions) { - super(options, "Syntax checking", "Syntax error"); - } + ///////////////////////////////// + // .d.ts syntax inspection + ///////////////////////////////// + export class SyntaxChecking extends TestSuiteBase { - public filterTargetFiles(files: File[]): File[] { - return files.filter((file) => { - return DT.endsWith(file.formatName.toUpperCase(), '.D.TS'); - }); - } - } + constructor(options: ITestRunnerOptions) { + super(options, "Syntax checking", "Syntax error"); + } + + public filterTargetFiles(files: File[]): File[] { + return files.filter((file) => { + return DT.endsWith(file.formatName.toUpperCase(), '.D.TS'); + }); + } + } } diff --git a/_infrastructure/tests/src/suite/testEval.ts b/_infrastructure/tests/src/suite/testEval.ts index 860e179431..13a6598874 100644 --- a/_infrastructure/tests/src/suite/testEval.ts +++ b/_infrastructure/tests/src/suite/testEval.ts @@ -2,20 +2,21 @@ /// module DT { - ///////////////////////////////// - // Compile with *-tests.ts - ///////////////////////////////// - export class TestEval extends TestSuiteBase { + 'use-strict'; + ///////////////////////////////// + // Compile with *-tests.ts + ///////////////////////////////// + export class TestEval extends TestSuiteBase { - constructor(options) { - super(options, "Typing tests", "Failed tests"); - } + constructor(options) { + super(options, "Typing tests", "Failed tests"); + } - public filterTargetFiles(files: File[]): File[] { - return files.filter((file) => { - return DT.endsWith(file.formatName.toUpperCase(), '-TESTS.TS') - }); - } - } + public filterTargetFiles(files: File[]): File[] { + return files.filter((file) => { + return DT.endsWith(file.formatName.toUpperCase(), '-TESTS.TS') + }); + } + } } diff --git a/_infrastructure/tests/src/suite/tscParams.ts b/_infrastructure/tests/src/suite/tscParams.ts index 510e15ce77..e4bb4f4c15 100644 --- a/_infrastructure/tests/src/suite/tscParams.ts +++ b/_infrastructure/tests/src/suite/tscParams.ts @@ -2,56 +2,58 @@ /// module DT { - var fs = require('fs'); + 'use-strict'; - ///////////////////////////////// - // Try compile without .tscparams - // It may indicate that it is compatible with --noImplicitAny maybe... - ///////////////////////////////// - export class FindNotRequiredTscparams extends TestSuiteBase { - testReporter: ITestReporter; - printErrorCount = false; + var fs = require('fs'); - constructor(options: ITestRunnerOptions, private print: Print) { - super(options, "Find not required .tscparams files", "New arrival!"); + ///////////////////////////////// + // Try compile without .tscparams + // It may indicate that it is compatible with --noImplicitAny maybe... + ///////////////////////////////// + export class FindNotRequiredTscparams extends TestSuiteBase { + testReporter: ITestReporter; + printErrorCount = false; - this.testReporter = { - printPositiveCharacter: (index: number, testResult: TestResult) => { - this.print - .clearCurrentLine() - .printTypingsWithoutTestName(testResult.targetFile.formatName); - }, - printNegativeCharacter: (index: number, testResult: TestResult) => { - } - } - } + constructor(options: ITestRunnerOptions, private print: Print) { + super(options, "Find not required .tscparams files", "New arrival!"); - public filterTargetFiles(files: File[]): File[] { - return files.filter((file) => { - return fs.existsSync(file.filePathWithName + '.tscparams'); - }); - } + this.testReporter = { + printPositiveCharacter: (index: number, testResult: TestResult) => { + this.print + .clearCurrentLine() + .printTypingsWithoutTestName(testResult.targetFile.formatName); + }, + printNegativeCharacter: (index: number, testResult: TestResult) => { + } + } + } - public runTest(targetFile: File, callback: (result: TestResult) => void): void { - this.print.clearCurrentLine().out(targetFile.formatName); - new Test(this, targetFile, { - tscVersion: this.options.tscVersion, - useTscParams: false, - checkNoImplicitAny: true - }).run(result=> { - this.testResults.push(result); - callback(result); - }); - } + public filterTargetFiles(files: File[]): File[] { + return files.filter((file) => { + return fs.existsSync(file.filePathWithName + '.tscparams'); + }); + } - public finish(suiteCallback: (suite: ITestSuite)=>void) { - this.print.clearCurrentLine(); - suiteCallback(this); - } + public runTest(targetFile: File, callback: (result: TestResult) => void): void { + this.print.clearCurrentLine().out(targetFile.formatName); + new Test(this, targetFile, { + tscVersion: this.options.tscVersion, + useTscParams: false, + checkNoImplicitAny: true + }).run(result=> { + this.testResults.push(result); + callback(result); + }); + } - public get ngTests(): TestResult[] { - // Do not show ng test results - return []; - } - } + public finish(suiteCallback: (suite: ITestSuite)=>void) { + this.print.clearCurrentLine(); + suiteCallback(this); + } + + public get ngTests(): TestResult[] { + // Do not show ng test results + return []; + } + } } diff --git a/_infrastructure/tests/src/timer.ts b/_infrastructure/tests/src/timer.ts index 446ea1b34f..1bf965a825 100644 --- a/_infrastructure/tests/src/timer.ts +++ b/_infrastructure/tests/src/timer.ts @@ -2,46 +2,48 @@ /// module DT { - ///////////////////////////////// - // Timer.start starts a timer - // Timer.end stops the timer and sets asString to the pretty print value - ///////////////////////////////// - export class Timer { - startTime: number; - time = 0; - asString: string; + 'use-strict'; - public start() { - this.time = 0; - this.startTime = this.now(); - } + ///////////////////////////////// + // Timer.start starts a timer + // Timer.end stops the timer and sets asString to the pretty print value + ///////////////////////////////// + export class Timer { + startTime: number; + time = 0; + asString: string; - public now(): number { - return Date.now(); - } + public start() { + this.time = 0; + this.startTime = this.now(); + } - public end() { - this.time = (this.now() - this.startTime) / 1000; - this.asString = Timer.prettyDate(this.startTime, this.now()); - } + public now(): number { + return Date.now(); + } - public static prettyDate(date1: number, date2: number): string { - var diff = ((date2 - date1) / 1000); - var day_diff = Math.floor(diff / 86400); + public end() { + this.time = (this.now() - this.startTime) / 1000; + this.asString = Timer.prettyDate(this.startTime, this.now()); + } - if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) { - return null; - } + public static prettyDate(date1: number, date2: number): string { + var diff = ((date2 - date1) / 1000); + var day_diff = Math.floor(diff / 86400); - return ( (day_diff == 0 && ( - diff < 60 && (diff + " seconds") || - diff < 120 && "1 minute" || - diff < 3600 && Math.floor(diff / 60) + " minutes" || - diff < 7200 && "1 hour" || - diff < 86400 && Math.floor(diff / 3600) + " hours") || - day_diff == 1 && "Yesterday" || - day_diff < 7 && day_diff + " days" || - day_diff < 31 && Math.ceil(day_diff / 7) + " weeks")); - } - } + if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) { + return null; + } + + return ( (day_diff == 0 && ( + diff < 60 && (diff + " seconds") || + diff < 120 && "1 minute" || + diff < 3600 && Math.floor(diff / 60) + " minutes" || + diff < 7200 && "1 hour" || + diff < 86400 && Math.floor(diff / 3600) + " hours") || + day_diff == 1 && "Yesterday" || + day_diff < 7 && day_diff + " days" || + day_diff < 31 && Math.ceil(day_diff / 7) + " weeks")); + } + } } diff --git a/_infrastructure/tests/src/tsc.ts b/_infrastructure/tests/src/tsc.ts index 6bd3fb2591..845b8557fe 100644 --- a/_infrastructure/tests/src/tsc.ts +++ b/_infrastructure/tests/src/tsc.ts @@ -3,42 +3,44 @@ /// module DT { - var fs = require('fs'); + 'use-strict'; + var fs = require('fs'); - export interface TscExecOptions { - tscVersion?:string; - useTscParams?:boolean; - checkNoImplicitAny?:boolean; - } + export interface TscExecOptions { + tscVersion?:string; + useTscParams?:boolean; + checkNoImplicitAny?:boolean; + } - export class Tsc { - public static run(tsfile: string, options: TscExecOptions, callback: (result: ExecResult) => void) { - options = options || {}; - options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; - if (typeof options.checkNoImplicitAny === "undefined") { - options.checkNoImplicitAny = true; - } - if (typeof options.useTscParams === "undefined") { - options.useTscParams = true; - } + export class Tsc { + public static run(tsfile: string, options: TscExecOptions, callback: (result: ExecResult) => void) { + options = options || {}; + options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; + if (typeof options.checkNoImplicitAny === "undefined") { + options.checkNoImplicitAny = true; + } + if (typeof options.useTscParams === "undefined") { + options.useTscParams = true; + } - if (!fs.existsSync(tsfile)) { - throw new Error(tsfile + " not exists"); - } + if (!fs.existsSync(tsfile)) { + throw new Error(tsfile + " not exists"); + } - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!fs.existsSync(tscPath)) { - throw new Error(tscPath + ' is not exists'); - } - var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { - command += '@' + tsfile + '.tscparams'; - } else if (options.checkNoImplicitAny) { - command += '--noImplicitAny'; - } - Exec.exec(command, [tsfile], (execResult) => { - callback(execResult); - }); - } - } + var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + if (!fs.existsSync(tscPath)) { + throw new Error(tscPath + ' is not exists'); + } + var command = 'node ' + tscPath + ' --module commonjs '; + if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { + command += '@' + tsfile + '.tscparams'; + } + else if (options.checkNoImplicitAny) { + command += '--noImplicitAny'; + } + Exec.exec(command, [tsfile], (execResult) => { + callback(execResult); + }); + } + } } diff --git a/_infrastructure/tests/src/util.ts b/_infrastructure/tests/src/util.ts index 3d63ce1421..39115b1eb4 100644 --- a/_infrastructure/tests/src/util.ts +++ b/_infrastructure/tests/src/util.ts @@ -1,27 +1,28 @@ /// module DT { + 'use-strict'; - var referenceTagExp = //g; + var referenceTagExp = //g; - export function endsWith(str: string, suffix: string) { - return str.indexOf(suffix, str.length - suffix.length) !== -1; - } + export function endsWith(str: string, suffix: string) { + return str.indexOf(suffix, str.length - suffix.length) !== -1; + } - export function extractReferenceTags(source: string): string[] { - var ret: string[] = []; - var match: RegExpExecArray; + export function extractReferenceTags(source: string): string[] { + var ret: string[] = []; + var match: RegExpExecArray; - if (!referenceTagExp.global) { - throw new Error('referenceTagExp RegExp must have global flag'); - } - referenceTagExp.lastIndex = 0; + if (!referenceTagExp.global) { + throw new Error('referenceTagExp RegExp must have global flag'); + } + referenceTagExp.lastIndex = 0; - while ((match = referenceTagExp.exec(source))) { - if (match.length > 0 && match[1].length > 0) { - ret.push(match[1]); - } - } - return ret; - } + while ((match = referenceTagExp.exec(source))) { + if (match.length > 0 && match[1].length > 0) { + ret.push(match[1]); + } + } + return ret; + } } From 461def4a53233621a6ef8d8bc8b2eed77ed98c4a Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 22:35:19 +0100 Subject: [PATCH 06/13] added definitions for test-module amaze --- test-module/test-addon.ts | 5 +++++ test-module/test-module.d.ts | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 test-module/test-addon.ts diff --git a/test-module/test-addon.ts b/test-module/test-addon.ts new file mode 100644 index 0000000000..14c5ba0d6f --- /dev/null +++ b/test-module/test-addon.ts @@ -0,0 +1,5 @@ +/// + +declare module 'test-addon' { + export function addon(): Such; +} diff --git a/test-module/test-module.d.ts b/test-module/test-module.d.ts index e73a9981c2..4ec2bdda49 100644 --- a/test-module/test-module.d.ts +++ b/test-module/test-module.d.ts @@ -1,6 +1,6 @@ +interface Such { + amaze(): void; +} declare module 'test-module' { - interface Such { - amaze(): void; - } export function wow(): Such; } From c47743cdd50253082404dec95edfac7a88c0fc72 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Thu, 27 Feb 2014 22:47:23 +0100 Subject: [PATCH 07/13] added method to test-module --- test-module/test-module.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test-module/test-module.d.ts b/test-module/test-module.d.ts index 4ec2bdda49..294e7ea433 100644 --- a/test-module/test-module.d.ts +++ b/test-module/test-module.d.ts @@ -1,5 +1,6 @@ interface Such { amaze(): void; + much(): void; } declare module 'test-module' { export function wow(): Such; From ca2fa0719bc77a96bb0e8fa571da8c2696489a06 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Fri, 28 Feb 2014 20:57:07 +0100 Subject: [PATCH 08/13] converted tester to promises & lazy iteration added bluebird & lazy.js dependencies checking-in used definitions added tsd.json parallelized async with bluebird most iteration uses lazy.js re-factored test flow to promise structure (instead of chain) renamed some stuff linted some quotes cleaned up output added some color --- .gitignore | 11 +- _infrastructure/tests/_ref.d.ts | 2 +- _infrastructure/tests/runner.js | 784 +++++----- _infrastructure/tests/runner.ts | 352 ++--- _infrastructure/tests/src/changes.ts | 19 +- _infrastructure/tests/src/exec.ts | 30 + _infrastructure/tests/src/file.ts | 5 +- _infrastructure/tests/src/host/exec.ts | 46 - _infrastructure/tests/src/index.ts | 107 +- _infrastructure/tests/src/printer.ts | 62 +- .../tests/src/reporter/reporter.ts | 4 +- _infrastructure/tests/src/suite/suite.ts | 51 +- _infrastructure/tests/src/suite/syntax.ts | 16 +- _infrastructure/tests/src/suite/testEval.ts | 17 +- _infrastructure/tests/src/suite/tscParams.ts | 32 +- _infrastructure/tests/src/timer.ts | 18 +- _infrastructure/tests/src/tsc.ts | 63 +- _infrastructure/tests/src/util.ts | 14 +- .../tests/typings/bluebird/bluebird.d.ts | 655 +++++++++ .../tests/typings/lazy.js/lazy.js.d.ts | 252 ++++ _infrastructure/tests/typings/node/node.d.ts | 1258 +++++++++++++++++ _infrastructure/tests/typings/tsd.d.ts | 3 + package.json | 4 +- tsd.json | 15 + 24 files changed, 3054 insertions(+), 766 deletions(-) create mode 100644 _infrastructure/tests/src/exec.ts delete mode 100644 _infrastructure/tests/src/host/exec.ts create mode 100644 _infrastructure/tests/typings/bluebird/bluebird.d.ts create mode 100644 _infrastructure/tests/typings/lazy.js/lazy.js.d.ts create mode 100644 _infrastructure/tests/typings/node/node.d.ts create mode 100644 _infrastructure/tests/typings/tsd.d.ts create mode 100644 tsd.json diff --git a/.gitignore b/.gitignore index 548f1e3979..8fc575c216 100644 --- a/.gitignore +++ b/.gitignore @@ -29,8 +29,9 @@ Properties !_infrastructure/tests/*/*.js !_infrastructure/tests/*/*/*.js !_infrastructure/tests/*/*/*/*.js - -.idea -*.iml - -node_modules + +.idea +*.iml +*.js.map + +node_modules diff --git a/_infrastructure/tests/_ref.d.ts b/_infrastructure/tests/_ref.d.ts index 1743cad05c..5a0809f3b8 100644 --- a/_infrastructure/tests/_ref.d.ts +++ b/_infrastructure/tests/_ref.d.ts @@ -1 +1 @@ -/// +/// diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index 1a7c4fc987..b11be80a39 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -1,53 +1,41 @@ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +var DT; +(function (DT) { + 'use strict'; -var ExecResult = (function () { - function ExecResult() { - this.stdout = ""; - this.stderr = ""; - } - return ExecResult; -})(); + var Promise = require('bluebird'); + var nodeExec = require('child_process').exec; -var NodeExec = (function () { - function NodeExec() { - } - NodeExec.prototype.exec = function (filename, cmdLineArgs, handleResult) { - var nodeExec = require('child_process').exec; + var ExecResult = (function () { + function ExecResult() { + this.stdout = ''; + this.stderr = ''; + } + return ExecResult; + })(); + DT.ExecResult = ExecResult; - var result = new ExecResult(); - result.exitCode = null; - var cmdLine = filename + ' ' + cmdLineArgs.join(' '); + function exec(filename, cmdLineArgs) { + return new Promise(function (resolve) { + var result = new ExecResult(); + result.exitCode = null; - var process = nodeExec(cmdLine, { maxBuffer: 1 * 1024 * 1024 }, function (error, stdout, stderr) { - result.stdout = stdout; - result.stderr = stderr; - result.exitCode = error ? error.code : 0; - handleResult(result); + var cmdLine = filename + ' ' + cmdLineArgs.join(' '); + + nodeExec(cmdLine, { maxBuffer: 1 * 1024 * 1024 }, function (error, stdout, stderr) { + result.error = error; + result.stdout = stdout; + result.stderr = stderr; + result.exitCode = error ? error.code : 0; + resolve(result); + }); }); - }; - return NodeExec; -})(); - -var Exec = function () { - return new NodeExec(); -}(); + } + DT.exec = exec; +})(DT || (DT = {})); /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; var path = require('path'); @@ -58,6 +46,7 @@ var DT; var File = (function () { function File(baseDir, filePathWithName) { this.references = []; + // why choose? this.baseDir = baseDir; this.filePathWithName = filePathWithName; this.ext = path.extname(this.filePathWithName); @@ -65,7 +54,7 @@ var DT; this.dir = path.dirname(this.filePathWithName); this.formatName = path.join(this.dir, this.file + this.ext); this.fullPath = path.join(this.baseDir, this.dir, this.file + this.ext); - // lock it (shallow) + // lock it (shallow) (needs `use strict` in each file to work) // Object.freeze(this); } File.prototype.toString = function () { @@ -75,43 +64,44 @@ var DT; })(); DT.File = File; })(DT || (DT = {})); -/// -/// -/// +/// +/// +/// var DT; (function (DT) { - 'use-strict'; + 'use strict'; + var fs = require('fs'); + var Promise = require('bluebird'); + var Tsc = (function () { function Tsc() { } - Tsc.run = function (tsfile, options, callback) { - options = options || {}; - options.tscVersion = options.tscVersion || DT.DEFAULT_TSC_VERSION; - if (typeof options.checkNoImplicitAny === "undefined") { - options.checkNoImplicitAny = true; - } - if (typeof options.useTscParams === "undefined") { - options.useTscParams = true; - } - - if (!fs.existsSync(tsfile)) { - throw new Error(tsfile + " not exists"); - } - - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!fs.existsSync(tscPath)) { - throw new Error(tscPath + ' is not exists'); - } - var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { - command += '@' + tsfile + '.tscparams'; - } else if (options.checkNoImplicitAny) { - command += '--noImplicitAny'; - } - Exec.exec(command, [tsfile], function (execResult) { - callback(execResult); + Tsc.run = function (tsfile, options) { + return Promise.attempt(function () { + options = options || {}; + options.tscVersion = options.tscVersion || DT.DEFAULT_TSC_VERSION; + if (typeof options.checkNoImplicitAny === 'undefined') { + options.checkNoImplicitAny = true; + } + if (typeof options.useTscParams === 'undefined') { + options.useTscParams = true; + } + if (!fs.existsSync(tsfile)) { + throw new Error(tsfile + ' not exists'); + } + var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + if (!fs.existsSync(tscPath)) { + throw new Error(tscPath + ' is not exists'); + } + var command = 'node ' + tscPath + ' --module commonjs '; + if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { + command += '@' + tsfile + '.tscparams'; + } else if (options.checkNoImplicitAny) { + command += '--noImplicitAny'; + } + return DT.exec(command, [tsfile]); }); }; return Tsc; @@ -122,7 +112,7 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; ///////////////////////////////// // Timer.start starts a timer @@ -154,7 +144,7 @@ var DT; return null; } - return (day_diff == 0 && (diff < 60 && (diff + " seconds") || diff < 120 && "1 minute" || diff < 3600 && Math.floor(diff / 60) + " minutes" || diff < 7200 && "1 hour" || diff < 86400 && Math.floor(diff / 3600) + " hours") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days" || day_diff < 31 && Math.ceil(day_diff / 7) + " weeks"); + return (day_diff == 0 && (diff < 60 && (diff + ' seconds') || diff < 120 && '1 minute' || diff < 3600 && Math.floor(diff / 60) + ' minutes' || diff < 7200 && '1 hour' || diff < 86400 && Math.floor(diff / 3600) + ' hours') || day_diff == 1 && 'Yesterday' || day_diff < 7 && day_diff + ' days' || day_diff < 31 && Math.ceil(day_diff / 7) + ' weeks'); }; return Timer; })(); @@ -163,7 +153,11 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; + + var fs = require('fs'); + var Lazy = require('lazy.js'); + var Promise = require('bluebird'); var referenceTagExp = //g; @@ -189,32 +183,37 @@ var DT; return ret; } DT.extractReferenceTags = extractReferenceTags; + + function fileExists(target) { + return new Promise(function (resolve, reject) { + fs.exists(target, function (bool) { + resolve(bool); + }); + }); + } + DT.fileExists = fileExists; })(DT || (DT = {})); /// /// /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; var fs = require('fs'); var path = require('path'); + var Lazy = require('lazy.js'); + var Promise = require('bluebird'); + var readFile = Promise.promisify(fs.readFile); + + ///////////////////////////////// + // Track all files in the repo: map full path to File objects + ///////////////////////////////// var FileIndex = (function () { function FileIndex(options) { this.options = options; } - FileIndex.prototype.parseFiles = function (files, callback) { - var _this = this; - this.fileMap = Object.create(null); - files.forEach(function (file) { - _this.fileMap[file.fullPath] = file; - }); - this.loadReferences(files, function () { - callback(); - }); - }; - FileIndex.prototype.hasFile = function (target) { return target in this.fileMap; }; @@ -226,42 +225,54 @@ var DT; return null; }; - FileIndex.prototype.loadReferences = function (files, callback) { + FileIndex.prototype.parseFiles = function (files) { var _this = this; - var queue = files.slice(0); - var active = []; - var max = 50; - var next = function () { - if (queue.length === 0 && active.length === 0) { - callback(); - return; - } - - while (queue.length > 0 && active.length < max) { - var file = queue.pop(); - active.push(file); - _this.parseFile(file, function (file) { - active.splice(active.indexOf(file), 1); - next(); - }); - } - }; - process.nextTick(next); + return Promise.attempt(function () { + _this.fileMap = Object.create(null); + files.forEach(function (file) { + _this.fileMap[file.fullPath] = file; + }); + return _this.loadReferences(files); + }); }; - FileIndex.prototype.parseFile = function (file, callback) { + FileIndex.prototype.loadReferences = function (files) { var _this = this; - fs.readFile(file.filePathWithName, { + return new Promise(function (resolve, reject) { + var queue = files.slice(0); + var active = []; + var max = 50; + var next = function () { + if (queue.length === 0 && active.length === 0) { + resolve(); + return; + } + + while (queue.length > 0 && active.length < max) { + var file = queue.pop(); + active.push(file); + _this.parseFile(file).then(function (file) { + active.splice(active.indexOf(file), 1); + next(); + }).catch(function (err) { + queue = []; + active = []; + reject(err); + }); + } + }; + next(); + }); + }; + + // TODO replace with a stream? + FileIndex.prototype.parseFile = function (file) { + var _this = this; + return readFile(file.filePathWithName, { encoding: 'utf8', flag: 'r' - }, function (err, content) { - if (err) { - throw err; - } - - // console.log('----'); - // console.log(file.filePathWithName); - file.references = DT.extractReferenceTags(content).map(function (ref) { + }).then(function (content) { + file.references = Lazy(DT.extractReferenceTags(content)).map(function (ref) { return path.resolve(path.dirname(file.fullPath), ref); }).reduce(function (memo, ref) { if (ref in _this.fileMap) { @@ -272,8 +283,8 @@ var DT; return memo; }, []); - // console.log(file.references); - callback(file); + // return the object + return file; }); }; return FileIndex; @@ -283,11 +294,12 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; var fs = require('fs'); var path = require('path'); var Git = require('git-wrapper'); + var Promise = require('bluebird'); var GitChanges = (function () { function GitChanges(baseDir) { @@ -299,22 +311,16 @@ var DT; throw new Error('cannot locate git-dir: ' + dir); } this.options['git-dir'] = dir; + + this.git = new Git(this.options); + this.git.exec = Promise.promisify(this.git.exec); } - GitChanges.prototype.getChanges = function (callback) { + GitChanges.prototype.getChanges = function () { var _this = this; - //git diff --name-only HEAD~1 - var git = new Git(this.options); var opts = {}; var args = ['--name-only HEAD~1']; - git.exec('diff', opts, args, function (err, msg) { - if (err) { - callback(err); - return; - } + return this.git.exec('diff', opts, args).then(function (msg) { _this.paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); - - // console.log(paths); - callback(null); }); }; return GitChanges; @@ -325,19 +331,20 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; - ///////////////////////////////// - // All the common things that we pring are functions of this class + // All the common things that we print are functions of this class ///////////////////////////////// var Print = (function () { - function Print(version, typings, tests, tsFiles) { + function Print(version) { this.version = version; + this.WIDTH = 77; + } + Print.prototype.init = function (typings, tests, tsFiles) { this.typings = typings; this.tests = tests; this.tsFiles = tsFiles; - this.WIDTH = 77; - } + }; + Print.prototype.out = function (s) { process.stdout.write(s); return this; @@ -347,9 +354,15 @@ var DT; return new Array(times + 1).join(s); }; + Print.prototype.printChangeHeader = function () { + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mDefinitelyTyped Diff Detector 0.1.0\33[0m \n'); + this.out('=============================================================================\n'); + }; + Print.prototype.printHeader = function () { this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); + this.out(' \33[36m\33[1mDefinitelyTyped Test Runner 0.5.0\33[0m\n'); this.out('=============================================================================\n'); this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); @@ -360,9 +373,9 @@ var DT; Print.prototype.printSuiteHeader = function (title) { var left = Math.floor((this.WIDTH - title.length) / 2) - 1; var right = Math.ceil((this.WIDTH - title.length) / 2) - 1; - this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); + this.out(this.repeat('=', left)).out(' \33[34m\33[1m'); this.out(title); - this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); + this.out('\33[0m ').out(this.repeat('=', right)).printBreak(); }; Print.prototype.printDiv = function () { @@ -390,16 +403,18 @@ var DT; }; Print.prototype.clearCurrentLine = function () { - this.out("\r\33[K"); + this.out('\r\33[K'); return this; }; Print.prototype.printSuccessCount = function (current, total) { - this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + var arb = (total === 0) ? 0 : (current / total); + this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); }; Print.prototype.printFailedCount = function (current, total) { - this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + var arb = (total === 0) ? 0 : (current / total); + this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); }; Print.prototype.printTypingsWithoutTestsMessage = function () { @@ -414,10 +429,31 @@ var DT; this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); }; - Print.prototype.printSuiteErrorCount = function (errorHeadline, current, total, valuesColor) { - if (typeof valuesColor === "undefined") { valuesColor = '\33[31m\33[1m'; } + Print.prototype.printSuiteErrorCount = function (errorHeadline, current, total, warn) { + if (typeof warn === "undefined") { warn = false; } + var arb = (total === 0) ? 0 : (current / total); this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); - this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + if (warn) { + this.out(': \33[31m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } else { + this.out(': \33[33m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } + }; + + Print.prototype.printSubHeader = function (file) { + this.out(' \33[36m\33[1m' + file + '\33[0m\n'); + }; + + Print.prototype.printLine = function (file) { + this.out(file + '\n'); + }; + + Print.prototype.printElement = function (file) { + this.out(' - ' + file + '\n'); + }; + + Print.prototype.printElement2 = function (file) { + this.out(' - ' + file + '\n'); }; Print.prototype.printTypingsWithoutTestName = function (file) { @@ -443,8 +479,6 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; - ///////////////////////////////// @@ -461,7 +495,7 @@ var DT; }; DefaultTestReporter.prototype.printNegativeCharacter = function (index, testResult) { - this.print.out("x"); + this.print.out('x'); this.printBreakIfNeeded(index); }; @@ -478,7 +512,9 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; + + var Promise = require('bluebird'); @@ -495,42 +531,31 @@ var DT; this.printErrorCount = true; } TestSuiteBase.prototype.filterTargetFiles = function (files) { - throw new Error("please implement this method"); + throw new Error('please implement this method'); }; - TestSuiteBase.prototype.start = function (targetFiles, testCallback, suiteCallback) { + TestSuiteBase.prototype.start = function (targetFiles, testCallback) { var _this = this; - targetFiles = this.filterTargetFiles(targetFiles); this.timer.start(); - var count = 0; - - // exec test is async process. serialize. - var executor = function () { - var targetFile = targetFiles[count]; - if (targetFile) { - _this.runTest(targetFile, function (result) { + return this.filterTargetFiles(targetFiles).then(function (targetFiles) { + return Promise.reduce(targetFiles, function (count, targetFile) { + return _this.runTest(targetFile).then(function (result) { testCallback(result, count + 1); - count++; - executor(); + return count++; }); - } else { - _this.timer.end(); - _this.finish(suiteCallback); - } - }; - executor(); - }; - - TestSuiteBase.prototype.runTest = function (targetFile, callback) { - var _this = this; - new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion }).run(function (result) { - _this.testResults.push(result); - callback(result); + }, 0); + }).then(function (count) { + _this.timer.end(); + return _this; }); }; - TestSuiteBase.prototype.finish = function (suiteCallback) { - suiteCallback(this); + TestSuiteBase.prototype.runTest = function (targetFile) { + var _this = this; + return new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion }).run().then(function (result) { + _this.testResults.push(result); + return result; + }); }; Object.defineProperty(TestSuiteBase.prototype, "okTests", { @@ -566,7 +591,11 @@ var __extends = this.__extends || function (d, b) { }; var DT; (function (DT) { - 'use-strict'; + 'use strict'; + + var Promise = require('bluebird'); + + var endDts = /.ts$/i; ///////////////////////////////// // .d.ts syntax inspection @@ -574,12 +603,12 @@ var DT; var SyntaxChecking = (function (_super) { __extends(SyntaxChecking, _super); function SyntaxChecking(options) { - _super.call(this, options, "Syntax checking", "Syntax error"); + _super.call(this, options, 'Syntax checking', 'Syntax error'); } SyntaxChecking.prototype.filterTargetFiles = function (files) { - return files.filter(function (file) { - return DT.endsWith(file.formatName.toUpperCase(), '.D.TS'); - }); + return Promise.cast(files.filter(function (file) { + return endDts.test(file.formatName); + })); }; return SyntaxChecking; })(DT.TestSuiteBase); @@ -589,7 +618,11 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; + 'use strict'; + + var Promise = require('bluebird'); + + var endTestDts = /-test.ts$/i; ///////////////////////////////// // Compile with *-tests.ts @@ -597,24 +630,25 @@ var DT; var TestEval = (function (_super) { __extends(TestEval, _super); function TestEval(options) { - _super.call(this, options, "Typing tests", "Failed tests"); + _super.call(this, options, 'Typing tests', 'Failed tests'); } TestEval.prototype.filterTargetFiles = function (files) { - return files.filter(function (file) { - return DT.endsWith(file.formatName.toUpperCase(), '-TESTS.TS'); - }); + return Promise.cast(files.filter(function (file) { + return endTestDts.test(file.formatName); + })); }; return TestEval; })(DT.TestSuiteBase); DT.TestEval = TestEval; })(DT || (DT = {})); -/// -/// +/// +/// var DT; (function (DT) { - 'use-strict'; + 'use strict'; var fs = require('fs'); + var Promise = require('bluebird'); ///////////////////////////////// // Try compile without .tscparams @@ -624,7 +658,7 @@ var DT; __extends(FindNotRequiredTscparams, _super); function FindNotRequiredTscparams(options, print) { var _this = this; - _super.call(this, options, "Find not required .tscparams files", "New arrival!"); + _super.call(this, options, 'Find not required .tscparams files', 'New arrival!'); this.print = print; this.printErrorCount = false; @@ -637,29 +671,28 @@ var DT; }; } FindNotRequiredTscparams.prototype.filterTargetFiles = function (files) { - return files.filter(function (file) { - return fs.existsSync(file.filePathWithName + '.tscparams'); + return Promise.filter(files, function (file) { + return new Promise(function (resolve) { + fs.exists(file.filePathWithName + '.tscparams', resolve); + }); }); }; - FindNotRequiredTscparams.prototype.runTest = function (targetFile, callback) { + FindNotRequiredTscparams.prototype.runTest = function (targetFile) { var _this = this; this.print.clearCurrentLine().out(targetFile.formatName); - new DT.Test(this, targetFile, { + + return new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true - }).run(function (result) { + }).run().then(function (result) { _this.testResults.push(result); - callback(result); + _this.print.clearCurrentLine(); + return result; }); }; - FindNotRequiredTscparams.prototype.finish = function (suiteCallback) { - this.print.clearCurrentLine(); - suiteCallback(this); - }; - Object.defineProperty(FindNotRequiredTscparams.prototype, "ngTests", { get: function () { // Do not show ng test results @@ -672,8 +705,8 @@ var DT; })(DT.TestSuiteBase); DT.FindNotRequiredTscparams = FindNotRequiredTscparams; })(DT || (DT = {})); -/// -/// +/// +/// /// /// /// @@ -688,27 +721,33 @@ var DT; /// var DT; (function (DT) { - 'use-strict'; - require('source-map-support').install(); + // hacky typing + var Lazy = require('lazy.js'); + var Promise = require('bluebird'); + var fs = require('fs'); var path = require('path'); var glob = require('glob'); + var assert = require('assert'); var tsExp = /\.ts$/; - DT.DEFAULT_TSC_VERSION = "0.9.1.1"; + DT.DEFAULT_TSC_VERSION = '0.9.1.1'; + ///////////////////////////////// + // Single test + ///////////////////////////////// var Test = (function () { function Test(suite, tsfile, options) { this.suite = suite; this.tsfile = tsfile; this.options = options; } - Test.prototype.run = function (callback) { + Test.prototype.run = function () { var _this = this; - DT.Tsc.run(this.tsfile.filePathWithName, this.options, function (execResult) { + return DT.Tsc.run(this.tsfile.filePathWithName, this.options).then(function (execResult) { var testResult = new TestResult(); testResult.hostedBy = _this.suite; testResult.targetFile = _this.tsfile; @@ -718,7 +757,7 @@ var DT; testResult.stderr = execResult.stderr; testResult.exitCode = execResult.exitCode; - callback(testResult); + return testResult; }); }; return Test; @@ -745,12 +784,9 @@ var DT; ///////////////////////////////// // The main class to kick things off ///////////////////////////////// - // TODO move to bluebird (Promises) - // TODO move to lazy.js (functional) var TestRunner = (function () { function TestRunner(dtPath, options) { if (typeof options === "undefined") { options = { tscVersion: DT.DEFAULT_TSC_VERSION }; } - var _this = this; this.dtPath = dtPath; this.options = options; this.suites = []; @@ -758,28 +794,12 @@ var DT; this.index = new DT.FileIndex(this.options); this.changes = new DT.GitChanges(this.dtPath); - - // should be async (way faster) - // only includes .d.ts or -tests.ts or -test.ts or .ts - var filesName = glob.sync('**/*.ts', { cwd: dtPath }); - this.files = filesName.filter(function (fileName) { - return _this.checkAcceptFile(fileName); - }).sort().map(function (fileName) { - return new DT.File(dtPath, fileName); - }); + this.print = new DT.Print(this.options.tscVersion); } TestRunner.prototype.addSuite = function (suite) { this.suites.push(suite); }; - TestRunner.prototype.run = function () { - this.timer = new DT.Timer(); - this.timer.start(); - - // we need promises - this.doGetChanges(); - }; - TestRunner.prototype.checkAcceptFile = function (fileName) { var ok = tsExp.test(fileName); ok = ok && fileName.indexOf('_infrastructure') < 0; @@ -788,155 +808,181 @@ var DT; return ok; }; - TestRunner.prototype.doGetChanges = function () { + TestRunner.prototype.run = function () { var _this = this; - this.changes.getChanges(function (err) { - if (err) { - throw err; - } - console.log(''); - console.log('changes:'); - console.log('---'); + this.timer = new DT.Timer(); + this.timer.start(); - _this.changes.paths.forEach(function (file) { - console.log(file); + // only includes .d.ts or -tests.ts or -test.ts or .ts + return Promise.promisify(glob).call(glob, '**/*.ts', { + cwd: dtPath + }).then(function (filesNames) { + _this.files = Lazy(filesNames).filter(function (fileName) { + return _this.checkAcceptFile(fileName); + }).map(function (fileName) { + return new DT.File(dtPath, fileName); + }).toArray(); + + _this.print.printChangeHeader(); + + return Promise.all([ + _this.doParseFiles(), + _this.doGetChanges() + ]); + }).then(function () { + return _this.doCollectTargets(); + }).then(function (files) { + return _this.runTests(files); + }).then(function () { + return !_this.suites.some(function (suite) { + return suite.ngTests.length !== 0; }); - console.log('---'); - - // chain - _this.doGetReferences(); }); }; - TestRunner.prototype.doGetReferences = function () { - var _this = this; - this.index.parseFiles(this.files, function () { - console.log(''); - console.log('files:'); - console.log('---'); - _this.files.forEach(function (file) { - console.log(file.filePathWithName); - file.references.forEach(function (file) { - console.log(' - %s', file.filePathWithName); - }); + TestRunner.prototype.doParseFiles = function () { + return this.index.parseFiles(this.files).then(function () { + /* + this.print.printSubHeader('Files:'); + this.print.printDiv(); + this.files.forEach((file) => { + this.print.printLine(file.filePathWithName); + file.references.forEach((file) => { + this.print.printElement(file.filePathWithName); }); - console.log('---'); - + }); + this.print.printBreak();*/ // chain - _this.doCollectTargets(); - }); + }).thenReturn(); + }; + + TestRunner.prototype.doGetChanges = function () { + var _this = this; + return this.changes.getChanges().then(function () { + _this.print.printSubHeader('All changes'); + _this.print.printDiv(); + + Lazy(_this.changes.paths).each(function (file) { + _this.print.printLine(file); + }); + }).thenReturn(); }; TestRunner.prototype.doCollectTargets = function () { - // TODO clean this up when functional (do we need changeMap?) var _this = this; - // bake map for lookup - var changeMap = this.changes.paths.filter(function (full) { - return _this.checkAcceptFile(full); - }).map(function (local) { - return path.resolve(_this.dtPath, local); - }).reduce(function (memo, full) { - var file = _this.index.getFile(full); - if (!file) { - // what does it mean? deleted? - console.log('not in index: ' + full); - return memo; - } - memo[full] = file; - return memo; - }, Object.create(null)); + return new Promise(function (resolve) { + // bake map for lookup + var changeMap = Object.create(null); - // collect referring files (and also log) - var touched = Object.create(null); - console.log(''); - console.log('relevant changes:'); - console.log('---'); - Object.keys(changeMap).sort().forEach(function (src) { - touched[src] = changeMap[src]; - console.log(changeMap[src].formatName); - }); - console.log('---'); - - // terrible loop (whatever) - // just add stuff until there is nothing new added - // TODO improve it - var added; - do { - added = 0; - this.files.forEach(function (file) { - // lol getter - if (file.fullPath in touched) { + Lazy(_this.changes.paths).filter(function (full) { + return _this.checkAcceptFile(full); + }).map(function (local) { + return path.resolve(_this.dtPath, local); + }).each(function (full) { + var file = _this.index.getFile(full); + if (!file) { + // TODO figure out what to do here + // what does it mean? deleted? + console.log('not in index: ' + full); return; } - - // check if one of our references is touched - file.references.some(function (ref) { - if (ref.fullPath in touched) { - // add us - touched[file.fullPath] = file; - added++; - return true; - } - return false; - }); + changeMap[full] = file; }); - } while(added > 0); - console.log(''); - console.log('touched:'); - console.log('---'); - var files = Object.keys(touched).sort().map(function (src) { - console.log(touched[src].formatName); - return touched[src]; + _this.print.printDiv(); + _this.print.printSubHeader('Relevant changes'); + _this.print.printDiv(); + + Object.keys(changeMap).sort().forEach(function (src) { + _this.print.printLine(changeMap[src].formatName); + }); + + // terrible loop (whatever) + // just add stuff until there is nothing new added + // TODO improve it + var added; + var files = _this.files.slice(0); + do { + added = 0; + + for (var i = files.length - 1; i >= 0; i--) { + var file = files[i]; + if (file.fullPath in changeMap) { + _this.files.splice(i, 1); + continue; + } + + for (var j = 0, jj = file.references.length; j < jj; j++) { + if (file.references[j].fullPath in changeMap) { + // add us + changeMap[file.fullPath] = file; + added++; + break; + } + } + } + } while(added > 0); + + _this.print.printDiv(); + _this.print.printSubHeader('Reference mapped'); + _this.print.printDiv(); + + var result = Object.keys(changeMap).sort().map(function (src) { + _this.print.printLine(changeMap[src].formatName); + changeMap[src].references.forEach(function (file) { + _this.print.printElement(file.formatName); + }); + return changeMap[src]; + }); + resolve(result); }); - console.log('---'); - - this.runTests(files); }; TestRunner.prototype.runTests = function (files) { var _this = this; - var syntaxChecking = new DT.SyntaxChecking(this.options); - var testEval = new DT.TestEval(this.options); - if (!this.options.findNotRequiredTscparams) { - this.addSuite(syntaxChecking); - this.addSuite(testEval); - } + return Promise.attempt(function () { + assert(Array.isArray(files), 'files must be array'); - var typings = syntaxChecking.filterTargetFiles(files).length; - var testFiles = testEval.filterTargetFiles(files).length; + var syntaxChecking = new DT.SyntaxChecking(_this.options); + var testEval = new DT.TestEval(_this.options); - this.print = new DT.Print(this.options.tscVersion, typings, testFiles, files.length); - this.print.printHeader(); + if (!_this.options.findNotRequiredTscparams) { + _this.addSuite(syntaxChecking); + _this.addSuite(testEval); + } - if (this.options.findNotRequiredTscparams) { - this.addSuite(new DT.FindNotRequiredTscparams(this.options, this.print)); - } + return Promise.all([ + syntaxChecking.filterTargetFiles(files), + testEval.filterTargetFiles(files) + ]); + }).spread(function (syntaxFiles, testFiles) { + _this.print.init(syntaxFiles.length, testFiles.length, files.length); + _this.print.printHeader(); - var count = 0; - var executor = function () { - var suite = _this.suites[count]; - if (suite) { + if (_this.options.findNotRequiredTscparams) { + _this.addSuite(new DT.FindNotRequiredTscparams(_this.options, _this.print)); + } + + return Promise.reduce(_this.suites, function (count, suite) { suite.testReporter = suite.testReporter || new DT.DefaultTestReporter(_this.print); _this.print.printSuiteHeader(suite.testSuiteName); - var targetFiles = suite.filterTargetFiles(files); - suite.start(targetFiles, function (testResult, index) { - _this.testCompleteCallback(testResult, index); - }, function (suite) { - _this.suiteCompleteCallback(suite); - count++; - executor(); + return suite.filterTargetFiles(files).then(function (targetFiles) { + return suite.start(targetFiles, function (testResult, index) { + _this.printTestComplete(testResult, index); + }); + }).then(function (suite) { + _this.printSuiteComplete(suite); + return count++; }); - } else { - _this.timer.end(); - _this.allTestCompleteCallback(files); - } - }; - executor(); + }, 0); + }).then(function (count) { + _this.timer.end(); + _this.finaliseTests(files); + }); }; - TestRunner.prototype.testCompleteCallback = function (testResult, index) { + TestRunner.prototype.printTestComplete = function (testResult, index) { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { reporter.printPositiveCharacter(index, testResult); @@ -945,7 +991,7 @@ var DT; } }; - TestRunner.prototype.suiteCompleteCallback = function (suite) { + TestRunner.prototype.printSuiteComplete = function (suite) { this.print.printBreak(); this.print.printDiv(); @@ -954,19 +1000,20 @@ var DT; this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); }; - TestRunner.prototype.allTestCompleteCallback = function (files) { + TestRunner.prototype.finaliseTests = function (files) { var _this = this; - var testEval = this.suites.filter(function (suite) { + var testEval = Lazy(this.suites).filter(function (suite) { return suite instanceof DT.TestEval; - })[0]; + }).first(); + if (testEval) { - var existsTestTypings = testEval.testResults.map(function (testResult) { + var existsTestTypings = Lazy(testEval.testResults).map(function (testResult) { return testResult.targetFile.dir; }).reduce(function (a, b) { return a.indexOf(b) < 0 ? a.concat([b]) : a; }, []); - var typings = files.map(function (file) { + var typings = Lazy(files).map(function (file) { return file.dir; }).reduce(function (a, b) { return a.indexOf(b) < 0 ? a.concat([b]) : a; @@ -975,6 +1022,7 @@ var DT; var withoutTestTypings = typings.filter(function (typing) { return existsTestTypings.indexOf(typing) < 0; }); + this.print.printDiv(); this.print.printTypingsWithoutTest(withoutTestTypings); } @@ -991,10 +1039,11 @@ var DT; _this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); }); if (testEval) { - this.print.printSuiteErrorCount("Without tests", withoutTestTypings.length, typings.length, '\33[33m\33[1m'); + this.print.printSuiteErrorCount('Without tests', withoutTestTypings.length, typings.length, true); } this.print.printDiv(); + if (this.suites.some(function (suite) { return suite.ngTests.length !== 0; })) { @@ -1008,8 +1057,6 @@ var DT; }); _this.print.printBoldDiv(); }); - - process.exit(1); } }; return TestRunner; @@ -1018,25 +1065,26 @@ var DT; var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); var findNotRequiredTscparams = process.argv.some(function (arg) { - return arg == "--try-without-tscparams"; + return arg == '--try-without-tscparams'; }); - var tscVersionIndex = process.argv.indexOf("--tsc-version"); + var tscVersionIndex = process.argv.indexOf('--tsc-version'); var tscVersion = DT.DEFAULT_TSC_VERSION; - if (-1 < tscVersionIndex) { + + if (tscVersionIndex > -1) { tscVersion = process.argv[tscVersionIndex + 1]; } - console.log('--'); - console.log(' dtPath %s', dtPath); - console.log(' tscVersion %s', tscVersion); - console.log(' findNotRequiredTscparams %s', findNotRequiredTscparams); - console.log('--'); - console.log(''); - var runner = new TestRunner(dtPath, { tscVersion: tscVersion, findNotRequiredTscparams: findNotRequiredTscparams }); - runner.run(); + runner.run().then(function (success) { + if (!success) { + process.exit(1); + } + }).catch(function (err) { + throw err; + process.exit(2); + }); })(DT || (DT = {})); //# sourceMappingURL=runner.js.map diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index f2ca6a0592..4f3a4389c9 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -1,6 +1,6 @@ -/// +/// -/// +/// /// /// @@ -19,24 +19,30 @@ /// module DT { - 'use-strict'; - require('source-map-support').install(); + // hacky typing + var Lazy: LazyJS.LazyStatic = require('lazy.js'); + var Promise: typeof Promise = require('bluebird'); + var fs = require('fs'); var path = require('path'); var glob = require('glob'); + var assert = require('assert'); var tsExp = /\.ts$/; - export var DEFAULT_TSC_VERSION = "0.9.1.1"; + export var DEFAULT_TSC_VERSION = '0.9.1.1'; + ///////////////////////////////// + // Single test + ///////////////////////////////// export class Test { constructor(public suite: ITestSuite, public tsfile: File, public options?: TscExecOptions) { } - public run(callback: (result: TestResult) => void) { - Tsc.run(this.tsfile.filePathWithName, this.options, (execResult) => { + public run(): Promise { + return Tsc.run(this.tsfile.filePathWithName, this.options).then((execResult: ExecResult) => { var testResult = new TestResult(); testResult.hostedBy = this.suite; testResult.targetFile = this.tsfile; @@ -46,7 +52,7 @@ module DT { testResult.stderr = execResult.stderr; testResult.exitCode = execResult.exitCode; - callback(testResult); + return testResult; }); } } @@ -76,12 +82,10 @@ module DT { ///////////////////////////////// // The main class to kick things off ///////////////////////////////// - // TODO move to bluebird (Promises) - // TODO move to lazy.js (functional) export class TestRunner { - files: File[]; - timer: Timer; - suites: ITestSuite[] = []; + private files: File[]; + private timer: Timer; + private suites: ITestSuite[] = []; private index: FileIndex; private changes: GitChanges; @@ -92,29 +96,13 @@ module DT { this.index = new FileIndex(this.options); this.changes = new GitChanges(this.dtPath); - - // should be async (way faster) - // only includes .d.ts or -tests.ts or -test.ts or .ts - var filesName = glob.sync('**/*.ts', { cwd: dtPath }); - this.files = filesName.filter((fileName) => { - return this.checkAcceptFile(fileName); - }).sort().map((fileName) => { - return new File(dtPath, fileName); - }); + this.print = new Print(this.options.tscVersion); } public addSuite(suite: ITestSuite): void { this.suites.push(suite); } - public run(): void { - this.timer = new Timer(); - this.timer.start(); - - // we need promises - this.doGetChanges(); - } - private checkAcceptFile(fileName: string): boolean { var ok = tsExp.test(fileName); ok = ok && fileName.indexOf('_infrastructure') < 0; @@ -123,155 +111,179 @@ module DT { return ok; } - private doGetChanges(): void { - this.changes.getChanges((err) => { - if (err) { - throw err; - } - console.log(''); - console.log('changes:'); - console.log('---'); + public run(): Promise { + this.timer = new Timer(); + this.timer.start(); - this.changes.paths.forEach((file) => { - console.log(file); + // only includes .d.ts or -tests.ts or -test.ts or .ts + return Promise.promisify(glob).call(glob, '**/*.ts', { + cwd: dtPath + }).then((filesNames: string[]) => { + this.files = Lazy(filesNames).filter((fileName) => { + return this.checkAcceptFile(fileName); + }).map((fileName: string) => { + return new File(dtPath, fileName); + }).toArray(); + + this.print.printChangeHeader(); + + return Promise.all([ + this.doParseFiles(), + this.doGetChanges() + ]); + }).then(() => { + return this.doCollectTargets(); + }).then((files) => { + return this.runTests(files); + }).then(() => { + return !this.suites.some((suite) => { + return suite.ngTests.length !== 0 }); - console.log('---'); - - // chain - this.doGetReferences(); }); } - private doGetReferences(): void { - this.index.parseFiles(this.files, () => { - console.log(''); - console.log('files:'); - console.log('---'); - this.files.forEach((file) => { - console.log(file.filePathWithName); - file.references.forEach((file) => { - console.log(' - %s', file.filePathWithName); - }); - }); - console.log('---'); - + private doParseFiles(): Promise { + return this.index.parseFiles(this.files).then(() => { + /* + this.print.printSubHeader('Files:'); + this.print.printDiv(); + this.files.forEach((file) => { + this.print.printLine(file.filePathWithName); + file.references.forEach((file) => { + this.print.printElement(file.filePathWithName); + }); + }); + this.print.printBreak();*/ // chain - this.doCollectTargets(); - }); + }).thenReturn(); } - private doCollectTargets(): void { + private doGetChanges(): Promise { + return this.changes.getChanges().then(() => { + this.print.printSubHeader('All changes'); + this.print.printDiv(); - // TODO clean this up when functional (do we need changeMap?) + Lazy(this.changes.paths).each((file) => { + this.print.printLine(file); + }); + }).thenReturn(); + } - // bake map for lookup - var changeMap = this.changes.paths.filter((full) => { - return this.checkAcceptFile(full); - }).map((local) => { - return path.resolve(this.dtPath, local); - }).reduce((memo, full) => { - var file = this.index.getFile(full); - if (!file) { - // what does it mean? deleted? - console.log('not in index: ' + full); - return memo; - } - memo[full] = file; - return memo; - }, Object.create(null)); + private doCollectTargets(): Promise { + return new Promise((resolve) => { - // collect referring files (and also log) - var touched = Object.create(null); - console.log(''); - console.log('relevant changes:'); - console.log('---'); - Object.keys(changeMap).sort().forEach((src) => { - touched[src] = changeMap[src]; - console.log(changeMap[src].formatName); - }); - console.log('---'); + // bake map for lookup + var changeMap = Object.create(null); - // terrible loop (whatever) - // just add stuff until there is nothing new added - // TODO improve it - var added:number; - do { - added = 0; - this.files.forEach((file) => { - // lol getter - if (file.fullPath in touched) { + Lazy(this.changes.paths).filter((full) => { + return this.checkAcceptFile(full); + }).map((local) => { + return path.resolve(this.dtPath, local); + }).each((full) => { + var file = this.index.getFile(full); + if (!file) { + // TODO figure out what to do here + // what does it mean? deleted? + console.log('not in index: ' + full); return; } - // check if one of our references is touched - file.references.some((ref) => { - if (ref.fullPath in touched) { - // add us - touched[file.fullPath] = file; - added++; - return true; - } - return false; - }); + changeMap[full] = file; }); - } - while(added > 0); - console.log(''); - console.log('touched:'); - console.log('---'); - var files: File[] = Object.keys(touched).sort().map((src) => { - console.log(touched[src].formatName); - return touched[src]; + this.print.printDiv(); + this.print.printSubHeader('Relevant changes'); + this.print.printDiv(); + + Object.keys(changeMap).sort().forEach((src) => { + this.print.printLine(changeMap[src].formatName); + }); + + // terrible loop (whatever) + // just add stuff until there is nothing new added + // TODO improve it + var added: number; + var files = this.files.slice(0); + do { + added = 0; + + for (var i = files.length - 1; i >= 0; i--) { + var file = files[i]; + if (file.fullPath in changeMap) { + this.files.splice(i, 1); + continue; + } + // check if one of our references is touched + for (var j = 0, jj = file.references.length; j < jj; j++) { + if (file.references[j].fullPath in changeMap) { + // add us + changeMap[file.fullPath] = file; + added++; + break; + } + } + } + } + while (added > 0); + + this.print.printDiv(); + this.print.printSubHeader('Reference mapped'); + this.print.printDiv(); + + var result: File[] = Object.keys(changeMap).sort().map((src) => { + this.print.printLine(changeMap[src].formatName); + changeMap[src].references.forEach((file: File) => { + this.print.printElement(file.formatName); + }); + return changeMap[src]; + }); + resolve(result); }); - console.log('---'); - - this.runTests(files); } - private runTests(files: File[]): void { + private runTests(files: File[]): Promise { + return Promise.attempt(() => { + assert(Array.isArray(files), 'files must be array'); - var syntaxChecking = new SyntaxChecking(this.options); - var testEval = new TestEval(this.options); - if (!this.options.findNotRequiredTscparams) { - this.addSuite(syntaxChecking); - this.addSuite(testEval); - } + var syntaxChecking = new SyntaxChecking(this.options); + var testEval = new TestEval(this.options); - var typings = syntaxChecking.filterTargetFiles(files).length; - var testFiles = testEval.filterTargetFiles(files).length; + if (!this.options.findNotRequiredTscparams) { + this.addSuite(syntaxChecking); + this.addSuite(testEval); + } - this.print = new Print(this.options.tscVersion, typings, testFiles, files.length); - this.print.printHeader(); + return Promise.all([ + syntaxChecking.filterTargetFiles(files), + testEval.filterTargetFiles(files) + ]); + }).spread((syntaxFiles, testFiles) => { + this.print.init(syntaxFiles.length, testFiles.length, files.length); + this.print.printHeader(); - if (this.options.findNotRequiredTscparams) { - this.addSuite(new FindNotRequiredTscparams(this.options, this.print)); - } + if (this.options.findNotRequiredTscparams) { + this.addSuite(new FindNotRequiredTscparams(this.options, this.print)); + } - var count = 0; - var executor = () => { - var suite = this.suites[count]; - if (suite) { + return Promise.reduce(this.suites, (count, suite: ITestSuite) => { suite.testReporter = suite.testReporter || new DefaultTestReporter(this.print); this.print.printSuiteHeader(suite.testSuiteName); - var targetFiles = suite.filterTargetFiles(files); - suite.start(targetFiles, (testResult, index) => { - this.testCompleteCallback(testResult, index); - }, (suite) => { - this.suiteCompleteCallback(suite); - count++; - executor(); + return suite.filterTargetFiles(files).then((targetFiles) => { + return suite.start(targetFiles, (testResult, index) => { + this.printTestComplete(testResult, index); + }); + }).then((suite) => { + this.printSuiteComplete(suite); + return count++; }); - } - else { - this.timer.end(); - this.allTestCompleteCallback(files); - } - }; - executor(); + }, 0); + }).then((count) => { + this.timer.end(); + this.finaliseTests(files); + }); } - private testCompleteCallback(testResult: TestResult, index: number) { + private printTestComplete(testResult: TestResult, index: number): void { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { reporter.printPositiveCharacter(index, testResult); @@ -281,7 +293,7 @@ module DT { } } - private suiteCompleteCallback(suite: ITestSuite) { + private printSuiteComplete(suite: ITestSuite): void { this.print.printBreak(); this.print.printDiv(); @@ -290,16 +302,19 @@ module DT { this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); } - private allTestCompleteCallback(files: File[]) { - var testEval = this.suites.filter(suite => suite instanceof TestEval)[0]; + private finaliseTests(files: File[]): void { + var testEval: TestEval = Lazy(this.suites).filter((suite) => { + return suite instanceof TestEval; + }).first(); + if (testEval) { - var existsTestTypings: string[] = testEval.testResults.map((testResult) => { + var existsTestTypings: string[] = Lazy(testEval.testResults).map((testResult) => { return testResult.targetFile.dir; }).reduce((a: string[], b: string) => { return a.indexOf(b) < 0 ? a.concat([b]) : a; }, []); - var typings: string[] = files.map((file) => { + var typings: string[] = Lazy(files).map((file) => { return file.dir; }).reduce((a: string[], b: string) => { return a.indexOf(b) < 0 ? a.concat([b]) : a; @@ -308,6 +323,7 @@ module DT { var withoutTestTypings: string[] = typings.filter((typing) => { return existsTestTypings.indexOf(typing) < 0; }); + this.print.printDiv(); this.print.printTypingsWithoutTest(withoutTestTypings); } @@ -324,10 +340,11 @@ module DT { this.print.printSuiteErrorCount(suite.errorHeadline, suite.ngTests.length, suite.testResults.length); }); if (testEval) { - this.print.printSuiteErrorCount("Without tests", withoutTestTypings.length, typings.length, '\33[33m\33[1m'); + this.print.printSuiteErrorCount('Without tests', withoutTestTypings.length, typings.length, true); } this.print.printDiv(); + if (this.suites.some((suite) => { return suite.ngTests.length !== 0 })) { @@ -341,30 +358,29 @@ module DT { }); this.print.printBoldDiv(); }); - - process.exit(1); } } } var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); - var findNotRequiredTscparams = process.argv.some(arg => arg == "--try-without-tscparams"); - var tscVersionIndex = process.argv.indexOf("--tsc-version"); + var findNotRequiredTscparams = process.argv.some(arg => arg == '--try-without-tscparams'); + var tscVersionIndex = process.argv.indexOf('--tsc-version'); var tscVersion = DEFAULT_TSC_VERSION; - if (-1 < tscVersionIndex) { + + if (tscVersionIndex > -1) { tscVersion = process.argv[tscVersionIndex + 1]; } - console.log('--'); - console.log(' dtPath %s', dtPath); - console.log(' tscVersion %s', tscVersion); - console.log(' findNotRequiredTscparams %s', findNotRequiredTscparams); - console.log('--'); - console.log(''); - var runner = new TestRunner(dtPath, { tscVersion: tscVersion, findNotRequiredTscparams: findNotRequiredTscparams }); - runner.run(); + runner.run().then((success) => { + if (!success) { + process.exit(1); + } + }).catch((err) => { + throw err; + process.exit(2); + }); } diff --git a/_infrastructure/tests/src/changes.ts b/_infrastructure/tests/src/changes.ts index 3bc7334e02..f3ec262084 100644 --- a/_infrastructure/tests/src/changes.ts +++ b/_infrastructure/tests/src/changes.ts @@ -1,14 +1,16 @@ /// module DT { - 'use-strict'; + 'use strict'; var fs = require('fs'); var path = require('path'); var Git = require('git-wrapper'); + var Promise: typeof Promise = require('bluebird'); export class GitChanges { + git; options = {}; paths: string[] = []; @@ -18,21 +20,16 @@ module DT { throw new Error('cannot locate git-dir: ' + dir); } this.options['git-dir'] = dir; + + this.git = new Git(this.options); + this.git.exec = Promise.promisify(this.git.exec); } - getChanges(callback: (err) => void): void { - //git diff --name-only HEAD~1 - var git = new Git(this.options); + getChanges(): Promise { var opts = {}; var args = ['--name-only HEAD~1']; - git.exec('diff', opts, args, (err, msg: string) => { - if (err) { - callback(err); - return; - } + return this.git.exec('diff', opts, args).then((msg: string) => { this.paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); - // console.log(paths); - callback(null); }); } } diff --git a/_infrastructure/tests/src/exec.ts b/_infrastructure/tests/src/exec.ts new file mode 100644 index 0000000000..3c3f3c0e02 --- /dev/null +++ b/_infrastructure/tests/src/exec.ts @@ -0,0 +1,30 @@ +module DT { + 'use strict'; + + var Promise: typeof Promise = require('bluebird'); + var nodeExec = require('child_process').exec; + + export class ExecResult { + error; + stdout = ''; + stderr = ''; + exitCode: number; + } + + export function exec(filename: string, cmdLineArgs: string[]): Promise { + return new Promise((resolve) => { + var result = new ExecResult(); + result.exitCode = null; + + var cmdLine = filename + ' ' + cmdLineArgs.join(' '); + + nodeExec(cmdLine, {maxBuffer: 1 * 1024 * 1024}, (error, stdout, stderr) => { + result.error = error; + result.stdout = stdout; + result.stderr = stderr; + result.exitCode = error ? error.code : 0; + resolve(result); + }); + }); + } +} diff --git a/_infrastructure/tests/src/file.ts b/_infrastructure/tests/src/file.ts index 4bb81e9857..389936b491 100644 --- a/_infrastructure/tests/src/file.ts +++ b/_infrastructure/tests/src/file.ts @@ -1,7 +1,7 @@ /// module DT { - 'use-strict'; + 'use strict'; var path = require('path'); @@ -20,6 +20,7 @@ module DT { references: File[] = []; constructor(baseDir: string, filePathWithName: string) { + // why choose? this.baseDir = baseDir; this.filePathWithName = filePathWithName; this.ext = path.extname(this.filePathWithName); @@ -28,7 +29,7 @@ module DT { this.formatName = path.join(this.dir, this.file + this.ext); this.fullPath = path.join(this.baseDir, this.dir, this.file + this.ext); - // lock it (shallow) + // lock it (shallow) (needs `use strict` in each file to work) // Object.freeze(this); } diff --git a/_infrastructure/tests/src/host/exec.ts b/_infrastructure/tests/src/host/exec.ts deleted file mode 100644 index 07a9d66626..0000000000 --- a/_infrastructure/tests/src/host/exec.ts +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -// Allows for executing a program with command-line arguments and reading the result -interface IExec { - exec: (filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) => void; -} - -class ExecResult { - public stdout = ""; - public stderr = ""; - public exitCode: number; -} - -class NodeExec implements IExec { - public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void): void { - var nodeExec = require('child_process').exec; - - var result = new ExecResult(); - result.exitCode = null; - var cmdLine = filename + ' ' + cmdLineArgs.join(' '); - - var process = nodeExec(cmdLine, {maxBuffer: 1 * 1024 * 1024}, function (error, stdout, stderr) { - result.stdout = stdout; - result.stderr = stderr; - result.exitCode = error ? error.code : 0; - handleResult(result); - }); - } -} - -var Exec: IExec = function (): IExec { - return new NodeExec(); -}(); diff --git a/_infrastructure/tests/src/index.ts b/_infrastructure/tests/src/index.ts index 7c9561af93..183c1e64db 100644 --- a/_infrastructure/tests/src/index.ts +++ b/_infrastructure/tests/src/index.ts @@ -3,28 +3,25 @@ /// module DT { - 'use-strict'; + 'use strict'; var fs = require('fs'); var path = require('path'); + var Lazy = require('lazy.js'); + var Promise: typeof Promise = require('bluebird'); + var readFile = Promise.promisify(fs.readFile); + + ///////////////////////////////// + // Track all files in the repo: map full path to File objects + ///////////////////////////////// export class FileIndex { - fileMap: {[path:string]:File - }; + fileMap: {[fullPath:string]:File}; + options: ITestRunnerOptions; - constructor(public options: ITestRunnerOptions) { - - } - - parseFiles(files: File[], callback: () => void): void { - this.fileMap = Object.create(null); - files.forEach((file) => { - this.fileMap[file.fullPath] = file; - }); - this.loadReferences(files, () => { - callback(); - }); + constructor(options: ITestRunnerOptions) { + this.options = options; } hasFile(target: string): boolean { @@ -38,43 +35,53 @@ module DT { return null; } - private loadReferences(files: File[], callback: () => void): void { - var queue = files.slice(0); - var active = []; - var max = 50; - var next = () => { - if (queue.length === 0 && active.length === 0) { - callback(); - return; - } - // queue paralel - while (queue.length > 0 && active.length < max) { - var file = queue.pop(); - active.push(file); - this.parseFile(file, (file) => { - active.splice(active.indexOf(file), 1); - next(); - }); - } - }; - process.nextTick(next); + parseFiles(files: File[]): Promise { + return Promise.attempt(() => { + this.fileMap = Object.create(null); + files.forEach((file) => { + this.fileMap[file.fullPath] = file; + }); + return this.loadReferences(files); + }); } - private parseFile(file: File, callback: (file: File) => void): void { - fs.readFile(file.filePathWithName, { + private loadReferences(files: File[]): Promise { + return new Promise((resolve, reject) => { + var queue = files.slice(0); + var active = []; + var max = 50; + var next = () => { + if (queue.length === 0 && active.length === 0) { + resolve(); + return; + } + // queue paralel + while (queue.length > 0 && active.length < max) { + var file = queue.pop(); + active.push(file); + this.parseFile(file).then((file) => { + active.splice(active.indexOf(file), 1); + next(); + }).catch((err) => { + queue = []; + active = []; + reject(err); + }); + } + }; + next(); + }); + } + + // TODO replace with a stream? + private parseFile(file: File): Promise { + return readFile(file.filePathWithName, { encoding: 'utf8', flag: 'r' - }, (err, content) => { - if (err) { - // just blow up? - throw err; - } - // console.log('----'); - // console.log(file.filePathWithName); - - file.references = extractReferenceTags(content).map((ref: string) => { + }).then((content) => { + file.references = Lazy(extractReferenceTags(content)).map((ref) => { return path.resolve(path.dirname(file.fullPath), ref); - }).reduce((memo: File[], ref: string) => { + }).reduce((memo: File[], ref) => { if (ref in this.fileMap) { memo.push(this.fileMap[ref]); } @@ -83,10 +90,8 @@ module DT { } return memo; }, []); - - // console.log(file.references); - - callback(file); + // return the object + return file; }); } } diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index 585ca5aa3a..613251b825 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -2,16 +2,26 @@ /// module DT { - 'use-strict'; ///////////////////////////////// - // All the common things that we pring are functions of this class + // All the common things that we print are functions of this class ///////////////////////////////// export class Print { WIDTH = 77; - constructor(public version: string, public typings: number, public tests: number, public tsFiles: number) { + typings: number; + tests: number; + tsFiles: number + + constructor(public version: string){ + + } + + public init(typings: number, tests: number, tsFiles: number) { + this.typings = typings; + this.tests = tests; + this.tsFiles = tsFiles; } public out(s: any): Print { @@ -23,9 +33,15 @@ module DT { return new Array(times + 1).join(s); } + public printChangeHeader() { + this.out('=============================================================================\n'); + this.out(' \33[36m\33[1mDefinitelyTyped Diff Detector 0.1.0\33[0m \n'); + this.out('=============================================================================\n'); + } + public printHeader() { this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.4.0\33[0m\n'); + this.out(' \33[36m\33[1mDefinitelyTyped Test Runner 0.5.0\33[0m\n'); this.out('=============================================================================\n'); this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n'); this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); @@ -36,9 +52,9 @@ module DT { public printSuiteHeader(title: string) { var left = Math.floor((this.WIDTH - title.length ) / 2) - 1; var right = Math.ceil((this.WIDTH - title.length ) / 2) - 1; - this.out(this.repeat("=", left)).out(" \33[34m\33[1m"); + this.out(this.repeat('=', left)).out(' \33[34m\33[1m'); this.out(title); - this.out("\33[0m ").out(this.repeat("=", right)).printBreak(); + this.out('\33[0m ').out(this.repeat('=', right)).printBreak(); } public printDiv() { @@ -66,16 +82,18 @@ module DT { } public clearCurrentLine(): Print { - this.out("\r\33[K"); + this.out('\r\33[K'); return this; } public printSuccessCount(current: number, total: number) { - this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + var arb = (total === 0) ? 0 : (current / total); + this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); } public printFailedCount(current: number, total: number) { - this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + var arb = (total === 0) ? 0 : (current / total); + this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); } public printTypingsWithoutTestsMessage() { @@ -90,9 +108,31 @@ module DT { this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n'); } - public printSuiteErrorCount(errorHeadline: string, current: number, total: number, valuesColor = '\33[31m\33[1m') { + public printSuiteErrorCount(errorHeadline: string, current: number, total: number, warn: boolean = false) { + var arb = (total === 0) ? 0 : (current / total); this.out(' \33[36m\33[1m').out(errorHeadline).out(this.repeat(' ', 16 - errorHeadline.length)); - this.out(':\33[0m ' + valuesColor + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + if (warn) { + this.out(': \33[31m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } + else { + this.out(': \33[33m\33[1m' + (arb * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n'); + } + } + + public printSubHeader(file: string) { + this.out(' \33[36m\33[1m' + file + '\33[0m\n'); + } + + public printLine(file: string) { + this.out(file + '\n'); + } + + public printElement(file: string) { + this.out(' - ' + file + '\n'); + } + + public printElement2(file: string) { + this.out(' - ' + file + '\n'); } public printTypingsWithoutTestName(file: string) { diff --git a/_infrastructure/tests/src/reporter/reporter.ts b/_infrastructure/tests/src/reporter/reporter.ts index edab8a6a04..783eae287a 100644 --- a/_infrastructure/tests/src/reporter/reporter.ts +++ b/_infrastructure/tests/src/reporter/reporter.ts @@ -2,8 +2,6 @@ /// module DT { - 'use-strict'; - ///////////////////////////////// // Test reporter interface // for example, . and x @@ -27,7 +25,7 @@ module DT { } public printNegativeCharacter(index: number, testResult: TestResult) { - this.print.out("x"); + this.print.out('x'); this.printBreakIfNeeded(index); } diff --git a/_infrastructure/tests/src/suite/suite.ts b/_infrastructure/tests/src/suite/suite.ts index 33d69af06e..4de10ce1d6 100644 --- a/_infrastructure/tests/src/suite/suite.ts +++ b/_infrastructure/tests/src/suite/suite.ts @@ -1,7 +1,9 @@ /// module DT { - 'use-strict'; + 'use strict'; + + var Promise: typeof Promise = require('bluebird'); ///////////////////////////////// // The interface for test suite @@ -9,9 +11,9 @@ module DT { export interface ITestSuite { testSuiteName:string; errorHeadline:string; - filterTargetFiles(files: File[]):File[]; + filterTargetFiles(files: File[]): Promise; - start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void):void; + start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void): Promise; testResults:TestResult[]; okTests:TestResult[]; @@ -34,41 +36,30 @@ module DT { constructor(public options: ITestRunnerOptions, public testSuiteName: string, public errorHeadline: string) { } - public filterTargetFiles(files: File[]): File[] { - throw new Error("please implement this method"); + public filterTargetFiles(files: File[]): Promise { + throw new Error('please implement this method'); } - public start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void, suiteCallback: (suite: ITestSuite) => void): void { - targetFiles = this.filterTargetFiles(targetFiles); + public start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void): Promise { this.timer.start(); - var count = 0; - // exec test is async process. serialize. - var executor = () => { - var targetFile = targetFiles[count]; - if (targetFile) { - this.runTest(targetFile, (result) => { + return this.filterTargetFiles(targetFiles).then((targetFiles) => { + return Promise.reduce(targetFiles, (count, targetFile) => { + return this.runTest(targetFile).then((result) => { testCallback(result, count + 1); - count++; - executor(); + return count++; }); - } - else { - this.timer.end(); - this.finish(suiteCallback); - } - }; - executor(); - } - - public runTest(targetFile: File, callback: (result: TestResult) => void): void { - new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run((result) => { - this.testResults.push(result); - callback(result); + }, 0); + }).then((count: number) => { + this.timer.end(); + return this; }); } - public finish(suiteCallback: (suite: ITestSuite) => void) { - suiteCallback(this); + public runTest(targetFile: File): Promise { + return new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run().then((result) => { + this.testResults.push(result); + return result; + }); } public get okTests(): TestResult[] { diff --git a/_infrastructure/tests/src/suite/syntax.ts b/_infrastructure/tests/src/suite/syntax.ts index ff4b3bdb4b..41808b2a2b 100644 --- a/_infrastructure/tests/src/suite/syntax.ts +++ b/_infrastructure/tests/src/suite/syntax.ts @@ -2,7 +2,11 @@ /// module DT { - 'use-strict'; + 'use strict'; + + var Promise: typeof Promise = require('bluebird'); + + var endDts = /.ts$/i; ///////////////////////////////// // .d.ts syntax inspection @@ -10,13 +14,13 @@ module DT { export class SyntaxChecking extends TestSuiteBase { constructor(options: ITestRunnerOptions) { - super(options, "Syntax checking", "Syntax error"); + super(options, 'Syntax checking', 'Syntax error'); } - public filterTargetFiles(files: File[]): File[] { - return files.filter((file) => { - return DT.endsWith(file.formatName.toUpperCase(), '.D.TS'); - }); + public filterTargetFiles(files: File[]): Promise { + return Promise.cast(files.filter((file) => { + return endDts.test(file.formatName); + })); } } } diff --git a/_infrastructure/tests/src/suite/testEval.ts b/_infrastructure/tests/src/suite/testEval.ts index 13a6598874..bc842549fc 100644 --- a/_infrastructure/tests/src/suite/testEval.ts +++ b/_infrastructure/tests/src/suite/testEval.ts @@ -2,20 +2,25 @@ /// module DT { - 'use-strict'; + 'use strict'; + + var Promise: typeof Promise = require('bluebird'); + + var endTestDts = /-test.ts$/i; + ///////////////////////////////// // Compile with *-tests.ts ///////////////////////////////// export class TestEval extends TestSuiteBase { constructor(options) { - super(options, "Typing tests", "Failed tests"); + super(options, 'Typing tests', 'Failed tests'); } - public filterTargetFiles(files: File[]): File[] { - return files.filter((file) => { - return DT.endsWith(file.formatName.toUpperCase(), '-TESTS.TS') - }); + public filterTargetFiles(files: File[]): Promise { + return Promise.cast(files.filter((file) => { + return endTestDts.test(file.formatName); + })); } } diff --git a/_infrastructure/tests/src/suite/tscParams.ts b/_infrastructure/tests/src/suite/tscParams.ts index e4bb4f4c15..cef23d622b 100644 --- a/_infrastructure/tests/src/suite/tscParams.ts +++ b/_infrastructure/tests/src/suite/tscParams.ts @@ -1,10 +1,11 @@ -/// -/// +/// +/// module DT { - 'use-strict'; + 'use strict'; var fs = require('fs'); + var Promise: typeof Promise = require('bluebird'); ///////////////////////////////// // Try compile without .tscparams @@ -15,7 +16,7 @@ module DT { printErrorCount = false; constructor(options: ITestRunnerOptions, private print: Print) { - super(options, "Find not required .tscparams files", "New arrival!"); + super(options, 'Find not required .tscparams files', 'New arrival!'); this.testReporter = { printPositiveCharacter: (index: number, testResult: TestResult) => { @@ -28,29 +29,28 @@ module DT { } } - public filterTargetFiles(files: File[]): File[] { - return files.filter((file) => { - return fs.existsSync(file.filePathWithName + '.tscparams'); + public filterTargetFiles(files: File[]): Promise { + return Promise.filter(files, (file) => { + return new Promise((resolve) => { + fs.exists(file.filePathWithName + '.tscparams', resolve); + }); }); } - public runTest(targetFile: File, callback: (result: TestResult) => void): void { + public runTest(targetFile: File): Promise { this.print.clearCurrentLine().out(targetFile.formatName); - new Test(this, targetFile, { + + return new Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true - }).run(result=> { + }).run().then((result: TestResult) => { this.testResults.push(result); - callback(result); + this.print.clearCurrentLine(); + return result }); } - public finish(suiteCallback: (suite: ITestSuite)=>void) { - this.print.clearCurrentLine(); - suiteCallback(this); - } - public get ngTests(): TestResult[] { // Do not show ng test results return []; diff --git a/_infrastructure/tests/src/timer.ts b/_infrastructure/tests/src/timer.ts index 1bf965a825..009527109a 100644 --- a/_infrastructure/tests/src/timer.ts +++ b/_infrastructure/tests/src/timer.ts @@ -2,7 +2,7 @@ /// module DT { - 'use-strict'; + 'use strict'; ///////////////////////////////// // Timer.start starts a timer @@ -36,14 +36,14 @@ module DT { } return ( (day_diff == 0 && ( - diff < 60 && (diff + " seconds") || - diff < 120 && "1 minute" || - diff < 3600 && Math.floor(diff / 60) + " minutes" || - diff < 7200 && "1 hour" || - diff < 86400 && Math.floor(diff / 3600) + " hours") || - day_diff == 1 && "Yesterday" || - day_diff < 7 && day_diff + " days" || - day_diff < 31 && Math.ceil(day_diff / 7) + " weeks")); + diff < 60 && (diff + ' seconds') || + diff < 120 && '1 minute' || + diff < 3600 && Math.floor(diff / 60) + ' minutes' || + diff < 7200 && '1 hour' || + diff < 86400 && Math.floor(diff / 3600) + ' hours') || + day_diff == 1 && 'Yesterday' || + day_diff < 7 && day_diff + ' days' || + day_diff < 31 && Math.ceil(day_diff / 7) + ' weeks')); } } } diff --git a/_infrastructure/tests/src/tsc.ts b/_infrastructure/tests/src/tsc.ts index 845b8557fe..16287be532 100644 --- a/_infrastructure/tests/src/tsc.ts +++ b/_infrastructure/tests/src/tsc.ts @@ -1,11 +1,14 @@ -/// -/// -/// +/// +/// +/// module DT { - 'use-strict'; + 'use strict'; + var fs = require('fs'); + var Promise: typeof Promise = require('bluebird'); + export interface TscExecOptions { tscVersion?:string; useTscParams?:boolean; @@ -13,33 +16,31 @@ module DT { } export class Tsc { - public static run(tsfile: string, options: TscExecOptions, callback: (result: ExecResult) => void) { - options = options || {}; - options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; - if (typeof options.checkNoImplicitAny === "undefined") { - options.checkNoImplicitAny = true; - } - if (typeof options.useTscParams === "undefined") { - options.useTscParams = true; - } - - if (!fs.existsSync(tsfile)) { - throw new Error(tsfile + " not exists"); - } - - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!fs.existsSync(tscPath)) { - throw new Error(tscPath + ' is not exists'); - } - var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { - command += '@' + tsfile + '.tscparams'; - } - else if (options.checkNoImplicitAny) { - command += '--noImplicitAny'; - } - Exec.exec(command, [tsfile], (execResult) => { - callback(execResult); + public static run(tsfile: string, options: TscExecOptions): Promise { + return Promise.attempt(() => { + options = options || {}; + options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; + if (typeof options.checkNoImplicitAny === 'undefined') { + options.checkNoImplicitAny = true; + } + if (typeof options.useTscParams === 'undefined') { + options.useTscParams = true; + } + if (!fs.existsSync(tsfile)) { + throw new Error(tsfile + ' not exists'); + } + var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + if (!fs.existsSync(tscPath)) { + throw new Error(tscPath + ' is not exists'); + } + var command = 'node ' + tscPath + ' --module commonjs '; + if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { + command += '@' + tsfile + '.tscparams'; + } + else if (options.checkNoImplicitAny) { + command += '--noImplicitAny'; + } + return exec(command, [tsfile]); }); } } diff --git a/_infrastructure/tests/src/util.ts b/_infrastructure/tests/src/util.ts index 39115b1eb4..a712bb90cd 100644 --- a/_infrastructure/tests/src/util.ts +++ b/_infrastructure/tests/src/util.ts @@ -1,7 +1,11 @@ /// module DT { - 'use-strict'; + 'use strict'; + + var fs = require('fs'); + var Lazy: LazyJS.LazyStatic = require('lazy.js'); + var Promise: typeof Promise = require('bluebird'); var referenceTagExp = //g; @@ -25,4 +29,12 @@ module DT { } return ret; } + + export function fileExists(target: string): Promise { + return new Promise((resolve, reject) => { + fs.exists(target, (bool: boolean) => { + resolve(bool); + }); + }); + } } diff --git a/_infrastructure/tests/typings/bluebird/bluebird.d.ts b/_infrastructure/tests/typings/bluebird/bluebird.d.ts new file mode 100644 index 0000000000..2e8de4ca80 --- /dev/null +++ b/_infrastructure/tests/typings/bluebird/bluebird.d.ts @@ -0,0 +1,655 @@ +// Type definitions for bluebird 1.0.0 +// Project: https://github.com/petkaantonov/bluebird +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +// ES6 model with generics overload was sourced and trans-multiplied from es6-promises.d.ts +// By: Campredon + +// Warning: recommended to use `tsc > v1.0.0` (critical bugs in generic code: +// - https://github.com/borisyankov/DefinitelyTyped/issues/1563 +// - https://github.com/borisyankov/DefinitelyTyped/tree/def/bluebird + +// Note: replicate changes to all overloads in both definition and test file +// Note: keep both static and instance members inline (so similar) + +// TODO fix remaining TODO annotations in both definition and test + +// TODO verify support to have no return statement in handlers to get a Promise (more overloads?) + +declare class Promise implements Promise.Thenable { + /** + * Create a new promise. The passed in function will receive functions `resolve` and `reject` as its arguments which can be called to seal the fate of the created promise. + */ + constructor(callback: (resolve: (thenable: Promise.Thenable) => void, reject: (error: any) => void) => void); + constructor(callback: (resolve: (result: R) => void, reject: (error: any) => void) => void); + + /** + * Promises/A+ `.then()` with progress handler. Returns a new promise chained from this promise. The new promise will be rejected or resolved dedefer on the passed `fulfilledHandler`, `rejectedHandler` and the state of this promise. + */ + then(onFulfill: (value: R) => Promise.Thenable, onReject: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; + then(onFulfill: (value: R) => Promise.Thenable, onReject?: (error: any) => U, onProgress?: (note: any) => any): Promise; + then(onFulfill: (value: R) => U, onReject: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; + then(onFulfill?: (value: R) => U, onReject?: (error: any) => U, onProgress?: (note: any) => any): Promise; + + /** + * This is a catch-all exception handler, shortcut for calling `.then(null, handler)` on this promise. Any exception happening in a `.then`-chain will propagate to nearest `.catch` handler. + * + * Alias `.caught();` for compatibility with earlier ECMAScript version. + */ + catch(onReject?: (error: any) => Promise.Thenable): Promise; + caught(onReject?: (error: any) => Promise.Thenable): Promise; + + catch(onReject?: (error: any) => U): Promise; + caught(onReject?: (error: any) => U): Promise; + + /** + * This extends `.catch` to work more like catch-clauses in languages like Java or C#. Instead of manually checking `instanceof` or `.name === "SomeError"`, you may specify a number of error constructors which are eligible for this catch handler. The catch handler that is first met that has eligible constructors specified, is the one that will be called. + * + * This method also supports predicate-based filters. If you pass a predicate function instead of an error constructor, the predicate will receive the error as an argument. The return result of the predicate will be used determine whether the error handler should be called. + * + * Alias `.caught();` for compatibility with earlier ECMAScript version. + */ + catch(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable): Promise; + caught(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable): Promise; + + catch(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise; + caught(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise; + + catch(ErrorClass: Function, onReject: (error: any) => Promise.Thenable): Promise; + caught(ErrorClass: Function, onReject: (error: any) => Promise.Thenable): Promise; + + catch(ErrorClass: Function, onReject: (error: any) => U): Promise; + caught(ErrorClass: Function, onReject: (error: any) => U): Promise; + + /** + * Like `.catch` but instead of catching all types of exceptions, it only catches those that don't originate from thrown errors but rather from explicit rejections. + */ + error(onReject: (reason: any) => Promise.Thenable): Promise; + error(onReject: (reason: any) => U): Promise; + + /** + * Pass a handler that will be called regardless of this promise's fate. Returns a new promise chained from this promise. There are special semantics for `.finally()` in that the final value cannot be modified from the handler. + * + * Alias `.lastly();` for compatibility with earlier ECMAScript version. + */ + finally(handler: (value: R) => Promise.Thenable): Promise; + finally(handler: (value: R) => R): Promise; + + lastly(handler: (value: R) => Promise.Thenable): Promise; + lastly(handler: (value: R) => R): Promise; + + /** + * Create a promise that follows this promise, but is bound to the given `thisArg` value. A bound promise will call its handlers with the bound value set to `this`. Additionally promises derived from a bound promise will also be bound promises with the same `thisArg` binding as the original promise. + */ + bind(thisArg: any): Promise; + + /** + * Like `.then()`, but any unhandled rejection that ends up here will be thrown as an error. + */ + done(onFulfilled: (value: R) => Promise.Thenable, onRejected: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; + done(onFulfilled: (value: R) => Promise.Thenable, onRejected?: (error: any) => U, onProgress?: (note: any) => any): Promise; + done(onFulfilled: (value: R) => U, onRejected: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; + done(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U, onProgress?: (note: any) => any): Promise; + + /** + * Shorthand for `.then(null, null, handler);`. Attach a progress handler that will be called if this promise is progressed. Returns a new promise chained from this promise. + */ + progressed(handler: (note: any) => any): Promise; + + /** + * Same as calling `Promise.delay(this, ms)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + delay(ms: number): Promise; + + /** + * Returns a promise that will be fulfilled with this promise's fulfillment value or rejection reason. However, if this promise is not fulfilled or rejected within `ms` milliseconds, the returned promise is rejected with a `Promise.TimeoutError` instance. + * + * You may specify a custom error message with the `message` parameter. + */ + timeout(ms: number, message?: string): Promise; + + /** + * Register a node-style callback on this promise. When this promise is is either fulfilled or rejected, the node callback will be called back with the node.js convention where error reason is the first argument and success value is the second argument. The error argument will be `null` in case of success. + * Returns back this promise instead of creating a new one. If the `callback` argument is not a function, this method does not do anything. + */ + nodeify(callback: (err: any, value?: R) => void): Promise; + nodeify(...sink: any[]): void; + + /** + * Marks this promise as cancellable. Promises by default are not cancellable after v0.11 and must be marked as such for `.cancel()` to have any effect. Marking a promise as cancellable is infectious and you don't need to remark any descendant promise. + */ + cancellable(): Promise; + + /** + * Cancel this promise. The cancellation will propagate to farthest cancellable ancestor promise which is still pending. + * + * That ancestor will then be rejected with a `CancellationError` (get a reference from `Promise.CancellationError`) object as the rejection reason. + * + * In a promise rejection handler you may check for a cancellation by seeing if the reason object has `.name === "Cancel"`. + * + * Promises are by default not cancellable. Use `.cancellable()` to mark a promise as cancellable. + */ + // TODO what to do with this? + cancel(): Promise; + + /** + * Like `.then()`, but cancellation of the the returned promise or any of its descendant will not propagate cancellation to this promise or this promise's ancestors. + */ + fork(onFulfilled: (value: R) => Promise.Thenable, onRejected: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; + fork(onFulfilled: (value: R) => Promise.Thenable, onRejected?: (error: any) => U, onProgress?: (note: any) => any): Promise; + fork(onFulfilled: (value: R) => U, onRejected: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; + fork(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U, onProgress?: (note: any) => any): Promise; + + /** + * Create an uncancellable promise based on this promise. + */ + uncancellable(): Promise; + + /** + * See if this promise can be cancelled. + */ + isCancellable(): boolean; + + /** + * See if this `promise` has been fulfilled. + */ + isFulfilled(): boolean; + + /** + * See if this `promise` has been rejected. + */ + isRejected(): boolean; + + /** + * See if this `promise` is still defer. + */ + isPending(): boolean; + + /** + * See if this `promise` is resolved -> either fulfilled or rejected. + */ + isResolved(): boolean; + + /** + * Synchronously inspect the state of this `promise`. The `PromiseInspection` will represent the state of the promise as snapshotted at the time of calling `.inspect()`. + */ + inspect(): Promise.Inspection; + + /** + * This is a convenience method for doing: + * + * + * promise.then(function(obj){ + * return obj[propertyName].call(obj, arg...); + * }); + * + */ + call(propertyName: string, ...args: any[]): Promise; + + /** + * This is a convenience method for doing: + * + * + * promise.then(function(obj){ + * return obj[propertyName]; + * }); + * + */ + // TODO find way to fix get() + // get(propertyName: string): Promise; + + /** + * Convenience method for: + * + * + * .then(function() { + * return value; + * }); + * + * + * in the case where `value` doesn't change its value. That means `value` is bound at the time of calling `.return()` + * + * Alias `.thenReturn();` for compatibility with earlier ECMAScript version. + */ + return(value: U): Promise; + thenReturn(value: U): Promise; + return(): Promise; + thenReturn(): Promise; + + /** + * Convenience method for: + * + * + * .then(function() { + * throw reason; + * }); + * + * Same limitations apply as with `.return()`. + * + * Alias `.thenThrow();` for compatibility with earlier ECMAScript version. + */ + throw(reason: Error): Promise; + thenThrow(reason: Error): Promise; + + /** + * Convert to String. + */ + toString(): string; + + /** + * This is implicitly called by `JSON.stringify` when serializing the object. Returns a serialized representation of the `Promise`. + */ + toJSON(): Object; + + /** + * Like calling `.then`, but the fulfillment value or rejection reason is assumed to be an array, which is flattened to the formal parameters of the handlers. + */ + // TODO how to model instance.spread()? like Q? + spread(onFulfill: Function, onReject?: (reason: any) => Promise.Thenable): Promise; + spread(onFulfill: Function, onReject?: (reason: any) => U): Promise; + /* + // TODO or something like this? + spread(onFulfill: (...values: W[]) => Promise.Thenable, onReject?: (reason: any) => Promise.Thenable): Promise; + spread(onFulfill: (...values: W[]) => Promise.Thenable, onReject?: (reason: any) => U): Promise; + spread(onFulfill: (...values: W[]) => U, onReject?: (reason: any) => Promise.Thenable): Promise; + spread(onFulfill: (...values: W[]) => U, onReject?: (reason: any) => U): Promise; + */ + /** + * Same as calling `Promise.all(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + all(): Promise; + + /** + * Same as calling `Promise.props(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO how to model instance.props()? + props(): Promise; + + /** + * Same as calling `Promise.settle(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + settle(): Promise[]>; + + /** + * Same as calling `Promise.any(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + any(): Promise; + + /** + * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + some(count: number): Promise; + + /** + * Same as calling `Promise.race(thisPromise, count)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + race(): Promise; + + /** + * Same as calling `Promise.map(thisPromise, mapper)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + map(mapper: (item: Q, index: number, arrayLength: number) => Promise.Thenable): Promise; + map(mapper: (item: Q, index: number, arrayLength: number) => U): Promise; + + /** + * Same as calling `Promise.reduce(thisPromise, Function reducer, initialValue)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + reduce(reducer: (memo: U, item: Q, index: number, arrayLength: number) => Promise.Thenable, initialValue?: U): Promise; + reduce(reducer: (memo: U, item: Q, index: number, arrayLength: number) => U, initialValue?: U): Promise; + + /** + * Same as calling ``Promise.filter(thisPromise, filterer)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + // TODO type inference from array-resolving promise? + filter(filterer: (item: U, index: number, arrayLength: number) => Promise.Thenable): Promise; + filter(filterer: (item: U, index: number, arrayLength: number) => boolean): Promise; +} + +declare module Promise { + + export interface Thenable { + then(onFulfilled: (value: R) => Thenable, onRejected: (error: any) => Thenable): Thenable; + then(onFulfilled: (value: R) => Thenable, onRejected?: (error: any) => U): Thenable; + then(onFulfilled: (value: R) => U, onRejected: (error: any) => Thenable): Thenable; + then(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): Thenable; + } + + export interface Resolver { + /** + * Resolve the underlying promise with `value` as the resolution value. If `value` is a thenable or a promise, the underlying promise will assume its state. + */ + resolve(value: R): void; + + /** + * Reject the underlying promise with `reason` as the rejection reason. + */ + reject(reason: any): void; + + /** + * Progress the underlying promise with `value` as the progression value. + */ + progress(value: any): void; + + /** + * Gives you a callback representation of the `PromiseResolver`. Note that this is not a method but a property. The callback accepts error object in first argument and success values on the 2nd parameter and the rest, I.E. node js conventions. + * + * If the the callback is called with multiple success values, the resolver fullfills its promise with an array of the values. + */ + // TODO specify resolver callback + callback: Function; + } + + export interface Inspection { + /** + * See if the underlying promise was fulfilled at the creation time of this inspection object. + */ + isFulfilled(): boolean; + + /** + * See if the underlying promise was rejected at the creation time of this inspection object. + */ + isRejected(): boolean; + + /** + * See if the underlying promise was defer at the creation time of this inspection object. + */ + isPending(): boolean; + + /** + * Get the fulfillment value of the underlying promise. Throws if the promise wasn't fulfilled at the creation time of this inspection object. + * + * throws `TypeError` + */ + value(): R; + + /** + * Get the rejection reason for the underlying promise. Throws if the promise wasn't rejected at the creation time of this inspection object. + * + * throws `TypeError` + */ + error(): any; + } + + /** + * Start the chain of promises with `Promise.try`. Any synchronous exceptions will be turned into rejections on the returned promise. + * + * Note about second argument: if it's specifically a true array, its values become respective arguments for the function call. Otherwise it is passed as is as the first argument for the function call. + * + * Alias for `attempt();` for compatibility with earlier ECMAScript version. + */ + // TODO find way to enable try() without tsc borking + // see also: https://typescript.codeplex.com/workitem/2194 + /* + export function try(fn: () => Promise.Thenable, args?: any[], ctx?: any): Promise; + export function try(fn: () => R, args?: any[], ctx?: any): Promise; + */ + + export function attempt(fn: () => Promise.Thenable, args?: any[], ctx?: any): Promise; + export function attempt(fn: () => R, args?: any[], ctx?: any): Promise; + + /** + * Returns a new function that wraps the given function `fn`. The new function will always return a promise that is fulfilled with the original functions return values or rejected with thrown exceptions from the original function. + * This method is convenient when a function can sometimes return synchronously or throw synchronously. + */ + export function method(fn: Function): Function; + + /** + * Create a promise that is resolved with the given `value`. If `value` is a thenable or promise, the returned promise will assume its state. + */ + export function resolve(value: Promise.Thenable): Promise; + export function resolve(value: R): Promise; + + /** + * Create a promise that is rejected with the given `reason`. + */ + export function reject(reason: any): Promise; + + /** + * Create a promise with undecided fate and return a `PromiseResolver` to control it. See resolution?: Promise(#promise-resolution). + */ + export function defer(): Promise.Resolver; + + /** + * Cast the given `value` to a trusted promise. If `value` is already a trusted `Promise`, it is returned as is. If `value` is not a thenable, a fulfilled is: Promise returned with `value` as its fulfillment value. If `value` is a thenable (Promise-like object, like those returned by jQuery's `$.ajax`), returns a trusted that: Promise assimilates the state of the thenable. + */ + export function cast(value: Promise.Thenable): Promise; + export function cast(value: R): Promise; + + /** + * Sugar for `Promise.resolve(undefined).bind(thisArg);`. See `.bind()`. + */ + export function bind(thisArg: any): Promise; + + /** + * See if `value` is a trusted Promise. + */ + export function is(value: any): boolean; + + /** + * Call this right after the library is loaded to enabled long stack traces. Long stack traces cannot be disabled after being enabled, and cannot be enabled after promises have alread been created. Long stack traces imply a substantial performance penalty, around 4-5x for throughput and 0.5x for latency. + */ + export function longStackTraces(): void; + + /** + * Returns a promise that will be fulfilled with `value` (or `undefined`) after given `ms` milliseconds. If `value` is a promise, the delay will start counting down when it is fulfilled and the returned promise will be fulfilled with the fulfillment value of the `value` promise. + */ + // TODO enable more overloads + export function delay(value: Promise.Thenable, ms: number): Promise; + export function delay(value: R, ms: number): Promise; + export function delay(ms: number): Promise; + + /** + * Returns a function that will wrap the given `nodeFunction`. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument. + * + * If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them. + * + * If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`. + */ + // TODO how to model promisify? + export function promisify(nodeFunction: Function, receiver?: any): Function; + + /** + * Promisifies the entire object by going through the object's properties and creating an async equivalent of each function on the object and its prototype chain. The promisified method name will be the original method name postfixed with `Async`. Returns the input object. + * + * Note that the original methods on the object are not overwritten but new methods are created with the `Async`-postfix. For example, if you `promisifyAll()` the node.js `fs` object use `fs.statAsync()` to call the promisified `stat` method. + */ + // TODO how to model promisifyAll? + export function promisifyAll(target: Object): Object; + + /** + * Returns a function that can use `yield` to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch. + */ + // TODO fix coroutine GeneratorFunction + export function coroutine(generatorFunction: Function): Function; + + /** + * Spawn a coroutine which may yield promises to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch. + */ + // TODO fix spawn GeneratorFunction + export function spawn(generatorFunction: Function): Promise; + + /** + * This is relevant to browser environments with no module loader. + * + * Release control of the `Promise` namespace to whatever it was before this library was loaded. Returns a reference to the library namespace so you can attach it to something else. + */ + export function noConflict(): typeof Promise; + + /** + * Add `handler` as the handler to call when there is a possibly unhandled rejection. The default handler logs the error stack to stderr or `console.error` in browsers. + * + * Passing no value or a non-function will have the effect of removing any kind of handling for possibly unhandled rejections. + */ + export function onPossiblyUnhandledRejection(handler: (reason: any) => any): void; + + /** + * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled when all the items in the array are fulfilled. The promise's fulfillment value is an array with fulfillment values at respective positions to the original array. If any promise in the array rejects, the returned promise is rejected with the rejection reason. + */ + // TODO enable more overloads + // promise of array with promises of value + export function all(values: Thenable[]>): Promise; + // promise of array with values + export function all(values: Thenable): Promise; + // array with promises of value + export function all(values: Thenable[]): Promise; + // array with values + export function all(values: R[]): Promise; + + /** + * Like ``Promise.all`` but for object properties instead of array items. Returns a promise that is fulfilled when all the properties of the object are fulfilled. The promise's fulfillment value is an object with fulfillment values at respective keys to the original object. If any promise in the object rejects, the returned promise is rejected with the rejection reason. + * + * If `object` is a trusted `Promise`, then it will be treated as a promise for object rather than for its properties. All other objects are treated for their properties as is returned by `Object.keys` - the object's own enumerable properties. + * + * *The original object is not modified.* + */ + // TODO verify this is correct + // trusted promise for object + export function props(object: Promise): Promise; + // object + export function props(object: Object): Promise; + + /** + * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled when all the items in the array are either fulfilled or rejected. The fulfillment value is an array of ``PromiseInspection`` instances at respective positions in relation to the input array. + * + * *original: The array is not modified. The input array sparsity is retained in the resulting array.* + */ + // promise of array with promises of value + export function settle(values: Thenable[]>): Promise[]>; + // promise of array with values + export function settle(values: Thenable): Promise[]>; + // array with promises of value + export function settle(values: Thenable[]): Promise[]>; + // array with values + export function settle(values: R[]): Promise[]>; + + /** + * Like `Promise.some()`, with 1 as `count`. However, if the promise fulfills, the fulfillment value is not an array of 1 but the value directly. + */ + // promise of array with promises of value + export function any(values: Thenable[]>): Promise; + // promise of array with values + export function any(values: Thenable): Promise; + // array with promises of value + export function any(values: Thenable[]): Promise; + // array with values + export function any(values: R[]): Promise; + + /** + * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled or rejected as soon as a promise in the array is fulfilled or rejected with the respective rejection reason or fulfillment value. + * + * **Note** If you pass empty array or a sparse array with no values, or a promise/thenable for such, it will be forever pending. + */ + // promise of array with promises of value + export function race(values: Thenable[]>): Promise; + // promise of array with values + export function race(values: Thenable): Promise; + // array with promises of value + export function race(values: Thenable[]): Promise; + // array with values + export function race(values: R[]): Promise; + + /** + * Initiate a competetive race between multiple promises or values (values will become immediately fulfilled promises). When `count` amount of promises have been fulfilled, the returned promise is fulfilled with an array that contains the fulfillment values of the winners in order of resolution. + * + * If too many promises are rejected so that the promise can never become fulfilled, it will be immediately rejected with an array of rejection reasons in the order they were thrown in. + * + * *The original array is not modified.* + */ + // promise of array with promises of value + export function some(values: Thenable[]>, count: number): Promise; + // promise of array with values + export function some(values: Thenable, count: number): Promise; + // array with promises of value + export function some(values: Thenable[], count: number): Promise; + // array with values + export function some(values: R[], count: number): Promise; + + /** + * Like `Promise.all()` but instead of having to pass an array, the array is generated from the passed variadic arguments. + */ + // variadic array with promises of value + export function join(...values: Thenable[]): Promise; + // variadic array with values + export function join(...values: R[]): Promise; + + /** + * Map an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `mapper` function with the signature `(item, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well. + * + * If the `mapper` function returns promises or thenables, the returned promise will wait for all the mapped results to be resolved as well. + * + * *The original array is not modified.* + */ + // promise of array with promises of value + export function map(values: Thenable[]>, mapper: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function map(values: Thenable[]>, mapper: (item: R, index: number, arrayLength: number) => U): Promise; + + // promise of array with values + export function map(values: Thenable, mapper: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function map(values: Thenable, mapper: (item: R, index: number, arrayLength: number) => U): Promise; + + // array with promises of value + export function map(values: Thenable[], mapper: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function map(values: Thenable[], mapper: (item: R, index: number, arrayLength: number) => U): Promise; + + // array with values + export function map(values: R[], mapper: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function map(values: R[], mapper: (item: R, index: number, arrayLength: number) => U): Promise; + + /** + * Reduce an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `reducer` function with the signature `(total, current, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well. + * + * If the reducer function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration. + * + * *The original array is not modified. If no `intialValue` is given and the array doesn't contain at least 2 items, the callback will not be called and `undefined` is returned. If `initialValue` is given and the array doesn't have at least 1 item, `initialValue` is returned.* + */ + // promise of array with promises of value + export function reduce(values: Thenable[]>, reducer: (total: U, current: R, index: number, arrayLength: number) => Thenable, initialValue?: U): Promise; + export function reduce(values: Thenable[]>, reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise; + + // promise of array with values + export function reduce(values: Thenable, reducer: (total: U, current: R, index: number, arrayLength: number) => Thenable, initialValue?: U): Promise; + export function reduce(values: Thenable, reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise; + + // array with promises of value + export function reduce(values: Thenable[], reducer: (total: U, current: R, index: number, arrayLength: number) => Thenable, initialValue?: U): Promise; + export function reduce(values: Thenable[], reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise; + + // array with values + export function reduce(values: R[], reducer: (total: U, current: R, index: number, arrayLength: number) => Thenable, initialValue?: U): Promise; + export function reduce(values: R[], reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise; + + /** + * Filter an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `filterer` function with the signature `(item, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well. + * + * The return values from the filtered functions are coerced to booleans, with the exception of promises and thenables which are awaited for their eventual result. + * + * *The original array is not modified. + */ + // promise of array with promises of value + export function filter(values: Thenable[]>, filterer: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function filter(values: Thenable[]>, filterer: (item: R, index: number, arrayLength: number) => boolean): Promise; + + // promise of array with values + export function filter(values: Thenable, filterer: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function filter(values: Thenable, filterer: (item: R, index: number, arrayLength: number) => boolean): Promise; + + // array with promises of value + export function filter(values: Thenable[], filterer: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function filter(values: Thenable[], filterer: (item: R, index: number, arrayLength: number) => boolean): Promise; + + // array with values + export function filter(values: R[], filterer: (item: R, index: number, arrayLength: number) => Thenable): Promise; + export function filter(values: R[], filterer: (item: R, index: number, arrayLength: number) => boolean): Promise; +} + +declare module 'bluebird' { +export = Promise; +} diff --git a/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts b/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts new file mode 100644 index 0000000000..b9367b73bc --- /dev/null +++ b/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts @@ -0,0 +1,252 @@ +// Type definitions for Lazy.js 0.3.2 +// Project: https://github.com/dtao/lazy.js/ +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module LazyJS { + + interface LazyStatic { + (value: string):StringLikeSequence; + + (value: T[]):ArrayLikeSequence; + (value: any[]):ArrayLikeSequence; + (value: Object):ObjectLikeSequence; + (value: Object):ObjectLikeSequence; + + strict():LazyStatic; + + generate(generatorFn: GeneratorCallback, length?: number):GeneratedSequence; + + range(to: number):GeneratedSequence; + range(from: number, to: number, step?: number):GeneratedSequence; + + repeat(value: T, count?: number):GeneratedSequence; + + on(eventType: string):Sequence; + + readFile(path: string):StringLikeSequence; + makeHttpRequest(path: string):StringLikeSequence; + } + + interface ArrayLike { + length:number; + [index:number]:T; + } + + interface Callback { + ():void; + } + + interface ErrorCallback { + (error: any):void; + } + + interface ValueCallback { + (value: T):void; + } + + interface GetKeyCallback { + (value: T):string; + } + + interface TestCallback { + (value: T):boolean; + } + + interface MapCallback { + (value: T):U; + } + + interface MapStringCallback { + (value: string):string; + } + + interface NumberCallback { + (value: T):number; + } + + interface MemoCallback { + (memo: U, value: T):U; + } + + interface GeneratorCallback { + (index: number):T; + } + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + interface Iterator { + new (sequence: Sequence):Iterator; + current():T; + moveNext():boolean; + } + + interface GeneratedSequence extends Sequence { + new(generatorFn: GeneratorCallback, length: number):GeneratedSequence; + length():number; + } + + interface AsyncSequence extends SequenceBase { + each(callback: ValueCallback):AsyncHandle; + } + + interface AsyncHandle { + cancel():void; + onComplete(callback: Callback):void; + onError(callback: ErrorCallback):void; + } + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + module Sequence { + function define(methodName: string[], overrides: Object): Function; + } + + interface Sequence extends SequenceBase { + each(eachFn: ValueCallback):Sequence; + } + + interface SequenceBase extends SequenceBaser { + first():any; + first(count: number):Sequence; + indexOf(value: any, startIndex?: number):Sequence; + + last():any; + last(count: number):Sequence; + lastIndexOf(value: any):Sequence; + + reverse():Sequence; + } + + interface SequenceBaser { + // TODO improve define() (needs ugly overload) + async(interval: number):AsyncSequence; + chunk(size: number):Sequence; + compact():Sequence; + concat(var_args: T[]):Sequence; + consecutive(length: number):Sequence; + contains(value: T):boolean; + countBy(keyFn: GetKeyCallback): ObjectLikeSequence; + countBy(propertyName: string): ObjectLikeSequence; + dropWhile(predicateFn: TestCallback): Sequence; + every(predicateFn: TestCallback): boolean; + filter(predicateFn: TestCallback): Sequence; + find(predicateFn: TestCallback): Sequence; + findWhere(properties: Object): Sequence; + + flatten(): Sequence; + groupBy(keyFn: GetKeyCallback): ObjectLikeSequence; + initial(count?: number): Sequence; + intersection(var_args: T[]): Sequence; + invoke(methodName: string): Sequence; + isEmpty(): boolean; + join(delimiter?: string): string; + map(mapFn: MapCallback): Sequence; + + max(valueFn?: NumberCallback): T; + min(valueFn?: NumberCallback): T; + pluck(propertyName: string): Sequence; + reduce(aggregatorFn: MemoCallback, memo?: U): U; + reduceRight(aggregatorFn: MemoCallback, memo: U): U; + reject(predicateFn: TestCallback): Sequence; + rest(count?: number): Sequence; + shuffle(): Sequence; + some(predicateFn?: TestCallback): boolean; + sortBy(sortFn: NumberCallback): Sequence; + sortedIndex(value: T): Sequence; + sum(valueFn?: NumberCallback): Sequence; + takeWhile(predicateFn: TestCallback): Sequence; + union(var_args: T[]): Sequence; + uniq(): Sequence; + where(properties: Object): Sequence; + without(var_args: T[]): Sequence; + zip(var_args: T[]): Sequence; + + toArray(): T[]; + toObject(): Object; + } + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + module ArrayLikeSequence { + function define(methodName: string[], overrides: Object): Function; + } + + interface ArrayLikeSequence extends Sequence { + // define()X; + concat(): ArrayLikeSequence; + first(count?: number): ArrayLikeSequence; + get(index: number): T; + length(): number; + map(mapFn: MapCallback): ArrayLikeSequence; + pop(): ArrayLikeSequence; + rest(count?: number): ArrayLikeSequence; + reverse(): ArrayLikeSequence; + shift(): ArrayLikeSequence; + slice(begin: number, end?: number): ArrayLikeSequence; + } + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + module ObjectLikeSequence { + function define(methodName: string[], overrides: Object): Function; + } + + interface ObjectLikeSequence extends Sequence { + assign(other: Object): ObjectLikeSequence; + // throws error + //async(): X; + defaults(defaults: Object): ObjectLikeSequence; + functions(): Sequence; + get(property: string): ObjectLikeSequence; + invert(): ObjectLikeSequence; + keys(): StringLikeSequence; + omit(properties: string[]): ObjectLikeSequence; + pairs(): Sequence; + pick(properties: string[]): ObjectLikeSequence; + toArray(): T[]; + toObject(): Object; + values(): Sequence; + } + + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + module StringLikeSequence { + function define(methodName: string[], overrides: Object): Function; + } + + interface StringLikeSequence extends SequenceBaser { + charAt(index: number): string; + charCodeAt(index: number): number; + contains(value: string): boolean; + endsWith(suffix: string): boolean; + + first(): string; + first(count: number): StringLikeSequence; + + indexOf(substring: string, startIndex?: number): number; + + last(): string; + last(count: number): StringLikeSequence; + + lastIndexOf(substring: string, startIndex?: number): number; + mapString(mapFn: MapStringCallback): StringLikeSequence; + match(pattern: RegExp): StringLikeSequence; + reverse(): StringLikeSequence; + + split(delimiter: string): StringLikeSequence; + split(delimiter: RegExp): StringLikeSequence; + + startsWith(prefix: string): boolean; + substring(start: number, stop?: number): StringLikeSequence; + toLowerCase(): StringLikeSequence; + toUpperCase(): StringLikeSequence; + } +} + +declare var Lazy: LazyJS.LazyStatic; + +declare module 'lazy.js' { +export = Lazy; +} + diff --git a/_infrastructure/tests/typings/node/node.d.ts b/_infrastructure/tests/typings/node/node.d.ts new file mode 100644 index 0000000000..dca0164b08 --- /dev/null +++ b/_infrastructure/tests/typings/node/node.d.ts @@ -0,0 +1,1258 @@ +// Type definitions for Node.js v0.10.1 +// Project: http://nodejs.org/ +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/************************************************ +* * +* Node.js v0.10.1 API * +* * +************************************************/ + +/************************************************ +* * +* GLOBAL * +* * +************************************************/ +declare var process: NodeProcess; +declare var global: any; + +declare var __filename: string; +declare var __dirname: string; + +declare function setTimeout(callback: (...args: any[]) => void , ms: number , ...args: any[]): NodeTimer; +declare function clearTimeout(timeoutId: NodeTimer): void; +declare function setInterval(callback: (...args: any[]) => void , ms: number , ...args: any[]): NodeTimer; +declare function clearInterval(intervalId: NodeTimer): void; +declare function setImmediate(callback: (...args: any[]) => void , ...args: any[]): any; +declare function clearImmediate(immediateId: any): void; + +declare var require: { + (id: string): any; + resolve(id:string): string; + cache: any; + extensions: any; + main: any; +} + +declare var module: { + exports: any; + require(id: string): any; + id: string; + filename: string; + loaded: boolean; + parent: any; + children: any[]; +} + +// Same as module.exports +declare var exports: any; +declare var SlowBuffer: { + new (str: string, encoding?: string): NodeBuffer; + new (size: number): NodeBuffer; + new (array: any[]): NodeBuffer; + prototype: NodeBuffer; + isBuffer(obj: any): boolean; + byteLength(string: string, encoding?: string): number; + concat(list: NodeBuffer[], totalLength?: number): NodeBuffer; +}; +declare var Buffer: { + new (str: string, encoding?: string): NodeBuffer; + new (size: number): NodeBuffer; + new (array: any[]): NodeBuffer; + prototype: NodeBuffer; + isBuffer(obj: any): boolean; + byteLength(string: string, encoding?: string): number; + concat(list: NodeBuffer[], totalLength?: number): NodeBuffer; +} + +/************************************************ +* * +* INTERFACES * +* * +************************************************/ + +interface ErrnoException extends Error { + errno?: any; + code?: string; + path?: string; + syscall?: string; +} + +interface NodeEventEmitter { + addListener(event: string, listener: Function): NodeEventEmitter; + on(event: string, listener: Function): NodeEventEmitter; + once(event: string, listener: Function): NodeEventEmitter; + removeListener(event: string, listener: Function): NodeEventEmitter; + removeAllListeners(event?: string): NodeEventEmitter; + setMaxListeners(n: number): void; + listeners(event: string): Function[]; + emit(event: string, ...args: any[]): boolean; +} + +interface ReadableStream extends NodeEventEmitter { + readable: boolean; + read(size?: number): any; + setEncoding(encoding: string): void; + pause(): void; + resume(): void; + pipe(destination: T, options?: { end?: boolean; }): T; + unpipe(destination?: T): void; + unshift(chunk: string): void; + unshift(chunk: NodeBuffer): void; + wrap(oldStream: ReadableStream): ReadableStream; +} + +interface WritableStream extends NodeEventEmitter { + writable: boolean; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; +} + +interface ReadWriteStream extends ReadableStream, WritableStream { } + +interface NodeProcess extends NodeEventEmitter { + stdout: WritableStream; + stderr: WritableStream; + stdin: ReadableStream; + argv: string[]; + execPath: string; + abort(): void; + chdir(directory: string): void; + cwd(): string; + env: any; + exit(code?: number): void; + getgid(): number; + setgid(id: number): void; + setgid(id: string): void; + getuid(): number; + setuid(id: number): void; + setuid(id: string): void; + version: string; + versions: { http_parser: string; node: string; v8: string; ares: string; uv: string; zlib: string; openssl: string; }; + config: { + target_defaults: { + cflags: any[]; + default_configuration: string; + defines: string[]; + include_dirs: string[]; + libraries: string[]; + }; + variables: { + clang: number; + host_arch: string; + node_install_npm: boolean; + node_install_waf: boolean; + node_prefix: string; + node_shared_openssl: boolean; + node_shared_v8: boolean; + node_shared_zlib: boolean; + node_use_dtrace: boolean; + node_use_etw: boolean; + node_use_openssl: boolean; + target_arch: string; + v8_no_strict_aliasing: number; + v8_use_snapshot: boolean; + visibility: string; + }; + }; + kill(pid: number, signal?: string): void; + pid: number; + title: string; + arch: string; + platform: string; + memoryUsage(): { rss: number; heapTotal: number; heapUsed: number; }; + nextTick(callback: Function): void; + umask(mask?: number): number; + uptime(): number; + hrtime(time?:number[]): number[]; + + // Worker + send?(message: any, sendHandle?: any): void; +} + +// Buffer class +interface NodeBuffer { + [index: number]: number; + write(string: string, offset?: number, length?: number, encoding?: string): number; + toString(encoding?: string, start?: number, end?: number): string; + length: number; + copy(targetBuffer: NodeBuffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number; + slice(start?: number, end?: number): NodeBuffer; + readUInt8(offset: number, noAsset?: boolean): number; + readUInt16LE(offset: number, noAssert?: boolean): number; + readUInt16BE(offset: number, noAssert?: boolean): number; + readUInt32LE(offset: number, noAssert?: boolean): number; + readUInt32BE(offset: number, noAssert?: boolean): number; + readInt8(offset: number, noAssert?: boolean): number; + readInt16LE(offset: number, noAssert?: boolean): number; + readInt16BE(offset: number, noAssert?: boolean): number; + readInt32LE(offset: number, noAssert?: boolean): number; + readInt32BE(offset: number, noAssert?: boolean): number; + readFloatLE(offset: number, noAssert?: boolean): number; + readFloatBE(offset: number, noAssert?: boolean): number; + readDoubleLE(offset: number, noAssert?: boolean): number; + readDoubleBE(offset: number, noAssert?: boolean): number; + writeUInt8(value: number, offset: number, noAssert?: boolean): void; + writeUInt16LE(value: number, offset: number, noAssert?: boolean): void; + writeUInt16BE(value: number, offset: number, noAssert?: boolean): void; + writeUInt32LE(value: number, offset: number, noAssert?: boolean): void; + writeUInt32BE(value: number, offset: number, noAssert?: boolean): void; + writeInt8(value: number, offset: number, noAssert?: boolean): void; + writeInt16LE(value: number, offset: number, noAssert?: boolean): void; + writeInt16BE(value: number, offset: number, noAssert?: boolean): void; + writeInt32LE(value: number, offset: number, noAssert?: boolean): void; + writeInt32BE(value: number, offset: number, noAssert?: boolean): void; + writeFloatLE(value: number, offset: number, noAssert?: boolean): void; + writeFloatBE(value: number, offset: number, noAssert?: boolean): void; + writeDoubleLE(value: number, offset: number, noAssert?: boolean): void; + writeDoubleBE(value: number, offset: number, noAssert?: boolean): void; + fill(value: any, offset?: number, end?: number): void; +} + +interface NodeTimer { + ref() : void; + unref() : void; +} + +/************************************************ +* * +* MODULES * +* * +************************************************/ +declare module "querystring" { + export function stringify(obj: any, sep?: string, eq?: string): string; + export function parse(str: string, sep?: string, eq?: string, options?: { maxKeys?: number; }): any; + export function escape(): any; + export function unescape(): any; +} + +declare module "events" { + export class EventEmitter implements NodeEventEmitter { + static listenerCount(emitter: EventEmitter, event: string): number; + + addListener(event: string, listener: Function): EventEmitter; + on(event: string, listener: Function): EventEmitter; + once(event: string, listener: Function): EventEmitter; + removeListener(event: string, listener: Function): EventEmitter; + removeAllListeners(event?: string): EventEmitter; + setMaxListeners(n: number): void; + listeners(event: string): Function[]; + emit(event: string, ...args: any[]): boolean; + } +} + +declare module "http" { + import events = require("events"); + import net = require("net"); + import stream = require("stream"); + + export interface Server extends NodeEventEmitter { + listen(port: number, hostname?: string, backlog?: number, callback?: Function): void; + listen(path: string, callback?: Function): void; + listen(handle: any, listeningListener?: Function): void; + close(cb?: any): void; + maxHeadersCount: number; + } + export interface ServerRequest extends NodeEventEmitter, ReadableStream { + method: string; + url: string; + headers: any; + trailers: string; + httpVersion: string; + setEncoding(encoding?: string): void; + pause(): void; + resume(): void; + connection: net.NodeSocket; + } + export interface ServerResponse extends NodeEventEmitter, WritableStream { + // Extended base methods + write(buffer: NodeBuffer): boolean; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + write(str: string, encoding?: string, fd?: string): boolean; + + writeContinue(): void; + writeHead(statusCode: number, reasonPhrase?: string, headers?: any): void; + writeHead(statusCode: number, headers?: any): void; + statusCode: number; + setHeader(name: string, value: string): void; + sendDate: boolean; + getHeader(name: string): string; + removeHeader(name: string): void; + write(chunk: any, encoding?: string): any; + addTrailers(headers: any): void; + + // Extended base methods + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; + end(data?: any, encoding?: string): void; + } + export interface ClientRequest extends NodeEventEmitter, WritableStream { + // Extended base methods + write(buffer: NodeBuffer): boolean; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + write(str: string, encoding?: string, fd?: string): boolean; + + write(chunk: any, encoding?: string): void; + abort(): void; + setTimeout(timeout: number, callback?: Function): void; + setNoDelay(noDelay?: Function): void; + setSocketKeepAlive(enable?: boolean, initialDelay?: number): void; + + // Extended base methods + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; + end(data?: any, encoding?: string): void; + } + export interface ClientResponse extends NodeEventEmitter, ReadableStream { + statusCode: number; + httpVersion: string; + headers: any; + trailers: any; + setEncoding(encoding?: string): void; + pause(): void; + resume(): void; + } + export interface Agent { maxSockets: number; sockets: any; requests: any; } + + export var STATUS_CODES: any; + export function createServer(requestListener?: (request: ServerRequest, response: ServerResponse) =>void ): Server; + export function createClient(port?: number, host?: string): any; + export function request(options: any, callback?: Function): ClientRequest; + export function get(options: any, callback?: Function): ClientRequest; + export var globalAgent: Agent; +} + +declare module "cluster" { + import child = require("child_process"); + import events = require("events"); + + export interface ClusterSettings { + exec?: string; + args?: string[]; + silent?: boolean; + } + + export class Worker extends events.EventEmitter { + id: string; + process: child.ChildProcess; + suicide: boolean; + send(message: any, sendHandle?: any): void; + kill(signal?: string): void; + destroy(signal?: string): void; + disconnect(): void; + } + + export var settings: ClusterSettings; + export var isMaster: boolean; + export var isWorker: boolean; + export function setupMaster(settings?: ClusterSettings): void; + export function fork(env?: any): Worker; + export function disconnect(callback?: Function): void; + export var worker: Worker; + export var workers: Worker[]; + + // Event emitter + export function addListener(event: string, listener: Function): void; + export function on(event: string, listener: Function): any; + export function once(event: string, listener: Function): void; + export function removeListener(event: string, listener: Function): void; + export function removeAllListeners(event?: string): void; + export function setMaxListeners(n: number): void; + export function listeners(event: string): Function[]; + export function emit(event: string, ...args: any[]): boolean; +} + +declare module "zlib" { + import stream = require("stream"); + export interface ZlibOptions { chunkSize?: number; windowBits?: number; level?: number; memLevel?: number; strategy?: number; dictionary?: any; } + + export interface Gzip extends ReadWriteStream { } + export interface Gunzip extends ReadWriteStream { } + export interface Deflate extends ReadWriteStream { } + export interface Inflate extends ReadWriteStream { } + export interface DeflateRaw extends ReadWriteStream { } + export interface InflateRaw extends ReadWriteStream { } + export interface Unzip extends ReadWriteStream { } + + export function createGzip(options?: ZlibOptions): Gzip; + export function createGunzip(options?: ZlibOptions): Gunzip; + export function createDeflate(options?: ZlibOptions): Deflate; + export function createInflate(options?: ZlibOptions): Inflate; + export function createDeflateRaw(options?: ZlibOptions): DeflateRaw; + export function createInflateRaw(options?: ZlibOptions): InflateRaw; + export function createUnzip(options?: ZlibOptions): Unzip; + + export function deflate(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + export function deflateRaw(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + export function gzip(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + export function gunzip(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + export function inflate(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + export function inflateRaw(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + export function unzip(buf: NodeBuffer, callback: (error: Error, result: any) =>void ): void; + + // Constants + export var Z_NO_FLUSH: number; + export var Z_PARTIAL_FLUSH: number; + export var Z_SYNC_FLUSH: number; + export var Z_FULL_FLUSH: number; + export var Z_FINISH: number; + export var Z_BLOCK: number; + export var Z_TREES: number; + export var Z_OK: number; + export var Z_STREAM_END: number; + export var Z_NEED_DICT: number; + export var Z_ERRNO: number; + export var Z_STREAM_ERROR: number; + export var Z_DATA_ERROR: number; + export var Z_MEM_ERROR: number; + export var Z_BUF_ERROR: number; + export var Z_VERSION_ERROR: number; + export var Z_NO_COMPRESSION: number; + export var Z_BEST_SPEED: number; + export var Z_BEST_COMPRESSION: number; + export var Z_DEFAULT_COMPRESSION: number; + export var Z_FILTERED: number; + export var Z_HUFFMAN_ONLY: number; + export var Z_RLE: number; + export var Z_FIXED: number; + export var Z_DEFAULT_STRATEGY: number; + export var Z_BINARY: number; + export var Z_TEXT: number; + export var Z_ASCII: number; + export var Z_UNKNOWN: number; + export var Z_DEFLATED: number; + export var Z_NULL: number; +} + +declare module "os" { + export function tmpDir(): string; + export function hostname(): string; + export function type(): string; + export function platform(): string; + export function arch(): string; + export function release(): string; + export function uptime(): number; + export function loadavg(): number[]; + export function totalmem(): number; + export function freemem(): number; + export function cpus(): { model: string; speed: number; times: { user: number; nice: number; sys: number; idle: number; irq: number; }; }[]; + export function networkInterfaces(): any; + export var EOL: string; +} + +declare module "https" { + import tls = require("tls"); + import events = require("events"); + import http = require("http"); + + export interface ServerOptions { + pfx?: any; + key?: any; + passphrase?: string; + cert?: any; + ca?: any; + crl?: any; + ciphers?: string; + honorCipherOrder?: boolean; + requestCert?: boolean; + rejectUnauthorized?: boolean; + NPNProtocols?: any; + SNICallback?: (servername: string) => any; + } + + export interface RequestOptions { + host?: string; + hostname?: string; + port?: number; + path?: string; + method?: string; + headers?: any; + auth?: string; + agent?: any; + pfx?: any; + key?: any; + passphrase?: string; + cert?: any; + ca?: any; + ciphers?: string; + rejectUnauthorized?: boolean; + } + + export interface NodeAgent { + maxSockets: number; + sockets: any; + requests: any; + } + export var Agent: { + new (options?: RequestOptions): NodeAgent; + }; + export interface Server extends tls.Server { } + export function createServer(options: ServerOptions, requestListener?: Function): Server; + export function request(options: RequestOptions, callback?: (res: NodeEventEmitter) =>void ): http.ClientRequest; + export function get(options: RequestOptions, callback?: (res: NodeEventEmitter) =>void ): http.ClientRequest; + export var globalAgent: NodeAgent; +} + +declare module "punycode" { + export function decode(string: string): string; + export function encode(string: string): string; + export function toUnicode(domain: string): string; + export function toASCII(domain: string): string; + export var ucs2: ucs2; + interface ucs2 { + decode(string: string): string; + encode(codePoints: number[]): string; + } + export var version: any; +} + +declare module "repl" { + import stream = require("stream"); + import events = require("events"); + + export interface ReplOptions { + prompt?: string; + input?: ReadableStream; + output?: WritableStream; + terminal?: boolean; + eval?: Function; + useColors?: boolean; + useGlobal?: boolean; + ignoreUndefined?: boolean; + writer?: Function; + } + export function start(options: ReplOptions): NodeEventEmitter; +} + +declare module "readline" { + import events = require("events"); + import stream = require("stream"); + + export interface ReadLine extends NodeEventEmitter { + setPrompt(prompt: string, length: number): void; + prompt(preserveCursor?: boolean): void; + question(query: string, callback: Function): void; + pause(): void; + resume(): void; + close(): void; + write(data: any, key?: any): void; + } + export interface ReadLineOptions { + input: ReadableStream; + output: WritableStream; + completer?: Function; + terminal?: boolean; + } + export function createInterface(options: ReadLineOptions): ReadLine; +} + +declare module "vm" { + export interface Context { } + export interface Script { + runInThisContext(): void; + runInNewContext(sandbox?: Context): void; + } + export function runInThisContext(code: string, filename?: string): void; + export function runInNewContext(code: string, sandbox?: Context, filename?: string): void; + export function runInContext(code: string, context: Context, filename?: string): void; + export function createContext(initSandbox?: Context): Context; + export function createScript(code: string, filename?: string): Script; +} + +declare module "child_process" { + import events = require("events"); + import stream = require("stream"); + + export interface ChildProcess extends NodeEventEmitter { + stdin: WritableStream; + stdout: ReadableStream; + stderr: ReadableStream; + pid: number; + kill(signal?: string): void; + send(message: any, sendHandle: any): void; + disconnect(): void; + } + + export function spawn(command: string, args?: string[], options?: { + cwd?: string; + stdio?: any; + custom?: any; + env?: any; + detached?: boolean; + }): ChildProcess; + export function exec(command: string, options: { + cwd?: string; + stdio?: any; + customFds?: any; + env?: any; + encoding?: string; + timeout?: number; + maxBuffer?: number; + killSignal?: string; + }, callback: (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) =>void ): ChildProcess; + export function exec(command: string, callback: (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) =>void ): ChildProcess; + export function execFile(file: string, args: string[], options: { + cwd?: string; + stdio?: any; + customFds?: any; + env?: any; + encoding?: string; + timeout?: number; + maxBuffer?: string; + killSignal?: string; + }, callback: (error: Error, stdout: NodeBuffer, stderr: NodeBuffer) =>void ): ChildProcess; + export function fork(modulePath: string, args?: string[], options?: { + cwd?: string; + env?: any; + encoding?: string; + }): ChildProcess; +} + +declare module "url" { + export interface Url { + href: string; + protocol: string; + auth: string; + hostname: string; + port: string; + host: string; + pathname: string; + search: string; + query: string; + slashes: boolean; + } + + export interface UrlOptions { + protocol?: string; + auth?: string; + hostname?: string; + port?: string; + host?: string; + pathname?: string; + search?: string; + query?: any; + } + + export function parse(urlStr: string, parseQueryString?: boolean , slashesDenoteHost?: boolean ): Url; + export function format(url: UrlOptions): string; + export function resolve(from: string, to: string): string; +} + +declare module "dns" { + export function lookup(domain: string, family: number, callback: (err: Error, address: string, family: number) =>void ): string; + export function lookup(domain: string, callback: (err: Error, address: string, family: number) =>void ): string; + export function resolve(domain: string, rrtype: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolve(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolve4(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolve6(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolveMx(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolveTxt(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolveSrv(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolveNs(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function resolveCname(domain: string, callback: (err: Error, addresses: string[]) =>void ): string[]; + export function reverse(ip: string, callback: (err: Error, domains: string[]) =>void ): string[]; +} + +declare module "net" { + import stream = require("stream"); + + export interface NodeSocket extends ReadWriteStream { + // Extended base methods + write(buffer: NodeBuffer): boolean; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + write(str: string, encoding?: string, fd?: string): boolean; + + connect(port: number, host?: string, connectionListener?: Function): void; + connect(path: string, connectionListener?: Function): void; + bufferSize: number; + setEncoding(encoding?: string): void; + write(data: any, encoding?: string, callback?: Function): void; + destroy(): void; + pause(): void; + resume(): void; + setTimeout(timeout: number, callback?: Function): void; + setNoDelay(noDelay?: boolean): void; + setKeepAlive(enable?: boolean, initialDelay?: number): void; + address(): { port: number; family: string; address: string; }; + remoteAddress: string; + remotePort: number; + bytesRead: number; + bytesWritten: number; + + // Extended base methods + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; + end(data?: any, encoding?: string): void; + } + + export var Socket: { + new (options?: { fd?: string; type?: string; allowHalfOpen?: boolean; }): NodeSocket; + }; + + export interface Server extends NodeSocket { + listen(port: number, host?: string, backlog?: number, listeningListener?: Function): void; + listen(path: string, listeningListener?: Function): void; + listen(handle: any, listeningListener?: Function): void; + close(callback?: Function): void; + address(): { port: number; family: string; address: string; }; + maxConnections: number; + connections: number; + } + export function createServer(connectionListener?: (socket: NodeSocket) =>void ): Server; + export function createServer(options?: { allowHalfOpen?: boolean; }, connectionListener?: (socket: NodeSocket) =>void ): Server; + export function connect(options: { allowHalfOpen?: boolean; }, connectionListener?: Function): NodeSocket; + export function connect(port: number, host?: string, connectionListener?: Function): NodeSocket; + export function connect(path: string, connectionListener?: Function): NodeSocket; + export function createConnection(options: { allowHalfOpen?: boolean; }, connectionListener?: Function): NodeSocket; + export function createConnection(port: number, host?: string, connectionListener?: Function): NodeSocket; + export function createConnection(path: string, connectionListener?: Function): NodeSocket; + export function isIP(input: string): number; + export function isIPv4(input: string): boolean; + export function isIPv6(input: string): boolean; +} + +declare module "dgram" { + import events = require("events"); + + export function createSocket(type: string, callback?: Function): Socket; + + interface Socket extends NodeEventEmitter { + send(buf: NodeBuffer, offset: number, length: number, port: number, address: string, callback?: Function): void; + bind(port: number, address?: string): void; + close(): void; + address: { address: string; family: string; port: number; }; + setBroadcast(flag: boolean): void; + setMulticastTTL(ttl: number): void; + setMulticastLoopback(flag: boolean): void; + addMembership(multicastAddress: string, multicastInterface?: string): void; + dropMembership(multicastAddress: string, multicastInterface?: string): void; + } +} + +declare module "fs" { + import stream = require("stream"); + + interface Stats { + isFile(): boolean; + isDirectory(): boolean; + isBlockDevice(): boolean; + isCharacterDevice(): boolean; + isSymbolicLink(): boolean; + isFIFO(): boolean; + isSocket(): boolean; + dev: number; + ino: number; + mode: number; + nlink: number; + uid: number; + gid: number; + rdev: number; + size: number; + blksize: number; + blocks: number; + atime: Date; + mtime: Date; + ctime: Date; + } + + interface FSWatcher extends NodeEventEmitter { + close(): void; + } + + export interface ReadStream extends ReadableStream { } + export interface WriteStream extends WritableStream { } + + export function rename(oldPath: string, newPath: string, callback?: (err?: ErrnoException) => void): void; + export function renameSync(oldPath: string, newPath: string): void; + export function truncate(path: string, callback?: (err?: ErrnoException) => void): void; + export function truncate(path: string, len: number, callback?: (err?: ErrnoException) => void): void; + export function truncateSync(path: string, len?: number): void; + export function ftruncate(fd: number, callback?: (err?: ErrnoException) => void): void; + export function ftruncate(fd: number, len: number, callback?: (err?: ErrnoException) => void): void; + export function ftruncateSync(fd: number, len?: number): void; + export function chown(path: string, uid: number, gid: number, callback?: (err?: ErrnoException) => void): void; + export function chownSync(path: string, uid: number, gid: number): void; + export function fchown(fd: number, uid: number, gid: number, callback?: (err?: ErrnoException) => void): void; + export function fchownSync(fd: number, uid: number, gid: number): void; + export function lchown(path: string, uid: number, gid: number, callback?: (err?: ErrnoException) => void): void; + export function lchownSync(path: string, uid: number, gid: number): void; + export function chmod(path: string, mode: number, callback?: (err?: ErrnoException) => void): void; + export function chmod(path: string, mode: string, callback?: (err?: ErrnoException) => void): void; + export function chmodSync(path: string, mode: number): void; + export function chmodSync(path: string, mode: string): void; + export function fchmod(fd: number, mode: number, callback?: (err?: ErrnoException) => void): void; + export function fchmod(fd: number, mode: string, callback?: (err?: ErrnoException) => void): void; + export function fchmodSync(fd: number, mode: number): void; + export function fchmodSync(fd: number, mode: string): void; + export function lchmod(path: string, mode: number, callback?: (err?: ErrnoException) => void): void; + export function lchmod(path: string, mode: string, callback?: (err?: ErrnoException) => void): void; + export function lchmodSync(path: string, mode: number): void; + export function lchmodSync(path: string, mode: string): void; + export function stat(path: string, callback?: (err: ErrnoException, stats: Stats) => any): void; + export function lstat(path: string, callback?: (err: ErrnoException, stats: Stats) => any): void; + export function fstat(fd: number, callback?: (err: ErrnoException, stats: Stats) => any): void; + export function statSync(path: string): Stats; + export function lstatSync(path: string): Stats; + export function fstatSync(fd: number): Stats; + export function link(srcpath: string, dstpath: string, callback?: (err?: ErrnoException) => void): void; + export function linkSync(srcpath: string, dstpath: string): void; + export function symlink(srcpath: string, dstpath: string, type?: string, callback?: (err?: ErrnoException) => void): void; + export function symlinkSync(srcpath: string, dstpath: string, type?: string): void; + export function readlink(path: string, callback?: (err: ErrnoException, linkString: string) => any): void; + export function readlinkSync(path: string): string; + export function realpath(path: string, callback?: (err: ErrnoException, resolvedPath: string) => any): void; + export function realpath(path: string, cache: {[path: string]: string}, callback: (err: ErrnoException, resolvedPath: string) =>any): void; + export function realpathSync(path: string, cache?: {[path: string]: string}): void; + export function unlink(path: string, callback?: (err?: ErrnoException) => void): void; + export function unlinkSync(path: string): void; + export function rmdir(path: string, callback?: (err?: ErrnoException) => void): void; + export function rmdirSync(path: string): void; + export function mkdir(path: string, callback?: (err?: ErrnoException) => void): void; + export function mkdir(path: string, mode: number, callback?: (err?: ErrnoException) => void): void; + export function mkdir(path: string, mode: string, callback?: (err?: ErrnoException) => void): void; + export function mkdirSync(path: string, mode?: number): void; + export function mkdirSync(path: string, mode?: string): void; + export function readdir(path: string, callback?: (err: ErrnoException, files: string[]) => void): void; + export function readdirSync(path: string): string[]; + export function close(fd: number, callback?: (err?: ErrnoException) => void): void; + export function closeSync(fd: number): void; + export function open(path: string, flags: string, callback?: (err: ErrnoException, fd: number) => any): void; + export function open(path: string, flags: string, mode: number, callback?: (err: ErrnoException, fd: number) => any): void; + export function open(path: string, flags: string, mode: string, callback?: (err: ErrnoException, fd: number) => any): void; + export function openSync(path: string, flags: string, mode?: number): number; + export function openSync(path: string, flags: string, mode?: string): number; + export function utimes(path: string, atime: number, mtime: number, callback?: (err?: ErrnoException) => void): void; + export function utimesSync(path: string, atime: number, mtime: number): void; + export function futimes(fd: number, atime: number, mtime: number, callback?: (err?: ErrnoException) => void): void; + export function futimesSync(fd: number, atime: number, mtime: number): void; + export function fsync(fd: number, callback?: (err?: ErrnoException) => void): void; + export function fsyncSync(fd: number): void; + export function write(fd: number, buffer: NodeBuffer, offset: number, length: number, position: number, callback?: (err: ErrnoException, written: number, buffer: NodeBuffer) => void): void; + export function writeSync(fd: number, buffer: NodeBuffer, offset: number, length: number, position: number): number; + export function read(fd: number, buffer: NodeBuffer, offset: number, length: number, position: number, callback?: (err: ErrnoException, bytesRead: number, buffer: NodeBuffer) => void): void; + export function readSync(fd: number, buffer: NodeBuffer, offset: number, length: number, position: number): number; + export function readFile(filename: string, options: { encoding?: string; flag?: string; }, callback: (err: ErrnoException, data: any) => void): void; + export function readFile(filename: string, callback: (err: ErrnoException, data: NodeBuffer) => void ): void; + export function readFileSync(filename: string, options?: { flag?: string; }): NodeBuffer; + export function readFileSync(filename: string, options: { encoding: string; flag?: string; }): string; + export function writeFile(filename: string, data: any, callback?: (err: ErrnoException) => void): void; + export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: ErrnoException) => void): void; + export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: ErrnoException) => void): void; + export function writeFileSync(filename: string, data: any, options?: { encoding?: string; mode?: number; flag?: string; }): void; + export function writeFileSync(filename: string, data: any, options?: { encoding?: string; mode?: string; flag?: string; }): void; + export function appendFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: ErrnoException) => void): void; + export function appendFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: ErrnoException) => void): void; + export function appendFile(filename: string, data: any, callback?: (err: ErrnoException) => void): void; + export function appendFileSync(filename: string, data: any, options?: { encoding?: string; mode?: number; flag?: string; }): void; + export function appendFileSync(filename: string, data: any, options?: { encoding?: string; mode?: string; flag?: string; }): void; + export function watchFile(filename: string, listener: (curr: Stats, prev: Stats) => void): void; + export function watchFile(filename: string, options: { persistent?: boolean; interval?: number; }, listener: (curr: Stats, prev: Stats) => void): void; + export function unwatchFile(filename: string, listener?: (curr: Stats, prev: Stats) => void): void; + export function watch(filename: string, listener?: (event: string, filename: string) => any): FSWatcher; + export function watch(filename: string, options: { persistent?: boolean; }, listener?: (event: string, filename: string) => any): FSWatcher; + export function exists(path: string, callback?: (exists: boolean) => void): void; + export function existsSync(path: string): boolean; + export function createReadStream(path: string, options?: { + flags?: string; + encoding?: string; + fd?: string; + mode?: number; + bufferSize?: number; + }): ReadStream; + export function createReadStream(path: string, options?: { + flags?: string; + encoding?: string; + fd?: string; + mode?: string; + bufferSize?: number; + }): ReadStream; + export function createWriteStream(path: string, options?: { + flags?: string; + encoding?: string; + string?: string; + }): WriteStream; +} + +declare module "path" { + export function normalize(p: string): string; + export function join(...paths: any[]): string; + export function resolve(...pathSegments: any[]): string; + export function relative(from: string, to: string): string; + export function dirname(p: string): string; + export function basename(p: string, ext?: string): string; + export function extname(p: string): string; + export var sep: string; +} + +declare module "string_decoder" { + export interface NodeStringDecoder { + write(buffer: NodeBuffer): string; + detectIncompleteChar(buffer: NodeBuffer): number; + } + export var StringDecoder: { + new (encoding: string): NodeStringDecoder; + }; +} + +declare module "tls" { + import crypto = require("crypto"); + import net = require("net"); + import stream = require("stream"); + + var CLIENT_RENEG_LIMIT: number; + var CLIENT_RENEG_WINDOW: number; + + export interface TlsOptions { + pfx?: any; //string or buffer + key?: any; //string or buffer + passphrase?: string; + cert?: any; + ca?: any; //string or buffer + crl?: any; //string or string array + ciphers?: string; + honorCipherOrder?: any; + requestCert?: boolean; + rejectUnauthorized?: boolean; + NPNProtocols?: any; //array or Buffer; + SNICallback?: (servername: string) => any; + } + + export interface ConnectionOptions { + host?: string; + port?: number; + socket?: net.NodeSocket; + pfx?: any; //string | Buffer + key?: any; //string | Buffer + passphrase?: string; + cert?: any; //string | Buffer + ca?: any; //Array of string | Buffer + rejectUnauthorized?: boolean; + NPNProtocols?: any; //Array of string | Buffer + servername?: string; + } + + export interface Server extends net.Server { + // Extended base methods + listen(port: number, host?: string, backlog?: number, listeningListener?: Function): void; + listen(path: string, listeningListener?: Function): void; + listen(handle: any, listeningListener?: Function): void; + + listen(port: number, host?: string, callback?: Function): void; + close(): void; + address(): { port: number; family: string; address: string; }; + addContext(hostName: string, credentials: { + key: string; + cert: string; + ca: string; + }): void; + maxConnections: number; + connections: number; + } + + export interface ClearTextStream extends ReadWriteStream { + authorized: boolean; + authorizationError: Error; + getPeerCertificate(): any; + getCipher: { + name: string; + version: string; + }; + address: { + port: number; + family: string; + address: string; + }; + remoteAddress: string; + remotePort: number; + } + + export interface SecurePair { + encrypted: any; + cleartext: any; + } + + export function createServer(options: TlsOptions, secureConnectionListener?: (cleartextStream: ClearTextStream) =>void ): Server; + export function connect(options: TlsOptions, secureConnectionListener?: () =>void ): ClearTextStream; + export function connect(port: number, host?: string, options?: ConnectionOptions, secureConnectListener?: () =>void ): ClearTextStream; + export function connect(port: number, options?: ConnectionOptions, secureConnectListener?: () =>void ): ClearTextStream; + export function createSecurePair(credentials?: crypto.Credentials, isServer?: boolean, requestCert?: boolean, rejectUnauthorized?: boolean): SecurePair; +} + +declare module "crypto" { + export interface CredentialDetails { + pfx: string; + key: string; + passphrase: string; + cert: string; + ca: any; //string | string array + crl: any; //string | string array + ciphers: string; + } + export interface Credentials { context?: any; } + export function createCredentials(details: CredentialDetails): Credentials; + export function createHash(algorithm: string): Hash; + export function createHmac(algorithm: string, key: string): Hmac; + interface Hash { + update(data: any, input_encoding?: string): Hash; + digest(encoding?: string): string; + } + interface Hmac { + update(data: any): void; + digest(encoding?: string): void; + } + export function createCipher(algorithm: string, password: any): Cipher; + export function createCipheriv(algorithm: string, key: any, iv: any): Cipher; + interface Cipher { + update(data: any, input_encoding?: string, output_encoding?: string): string; + final(output_encoding?: string): string; + setAutoPadding(auto_padding: boolean): void; + createDecipher(algorithm: string, password: any): Decipher; + createDecipheriv(algorithm: string, key: any, iv: any): Decipher; + } + interface Decipher { + update(data: any, input_encoding?: string, output_encoding?: string): void; + final(output_encoding?: string): string; + setAutoPadding(auto_padding: boolean): void; + } + export function createSign(algorithm: string): Signer; + interface Signer { + update(data: any): void; + sign(private_key: string, output_format: string): string; + } + export function createVerify(algorith: string): Verify; + interface Verify { + update(data: any): void; + verify(object: string, signature: string, signature_format?: string): boolean; + } + export function createDiffieHellman(prime_length: number): DiffieHellman; + export function createDiffieHellman(prime: number, encoding?: string): DiffieHellman; + interface DiffieHellman { + generateKeys(encoding?: string): string; + computeSecret(other_public_key: string, input_encoding?: string, output_encoding?: string): string; + getPrime(encoding?: string): string; + getGenerator(encoding: string): string; + getPublicKey(encoding?: string): string; + getPrivateKey(encoding?: string): string; + setPublicKey(public_key: string, encoding?: string): void; + setPrivateKey(public_key: string, encoding?: string): void; + } + export function getDiffieHellman(group_name: string): DiffieHellman; + export function pbkdf2(password: string, salt: string, iterations: number, keylen: number, callback: (err: Error, derivedKey: string) => any): void; + export function randomBytes(size: number): NodeBuffer; + export function randomBytes(size: number, callback: (err: Error, buf: NodeBuffer) =>void ): void; + export function pseudoRandomBytes(size: number): NodeBuffer; + export function pseudoRandomBytes(size: number, callback: (err: Error, buf: NodeBuffer) =>void ): void; +} + +declare module "stream" { + import events = require("events"); + + export interface ReadableOptions { + highWaterMark?: number; + encoding?: string; + objectMode?: boolean; + } + + export class Readable extends events.EventEmitter implements ReadableStream { + readable: boolean; + constructor(opts?: ReadableOptions); + _read(size: number): void; + read(size?: number): any; + setEncoding(encoding: string): void; + pause(): void; + resume(): void; + pipe(destination: T, options?: { end?: boolean; }): T; + unpipe(destination?: T): void; + unshift(chunk: string): void; + unshift(chunk: NodeBuffer): void; + wrap(oldStream: ReadableStream): ReadableStream; + push(chunk: any, encoding?: string): boolean; + } + + export interface WritableOptions { + highWaterMark?: number; + decodeStrings?: boolean; + } + + export class Writable extends events.EventEmitter implements WritableStream { + writable: boolean; + constructor(opts?: WritableOptions); + _write(data: NodeBuffer, encoding: string, callback: Function): void; + _write(data: string, encoding: string, callback: Function): void; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; + } + + export interface DuplexOptions extends ReadableOptions, WritableOptions { + allowHalfOpen?: boolean; + } + + // Note: Duplex extends both Readable and Writable. + export class Duplex extends Readable implements ReadWriteStream { + writable: boolean; + constructor(opts?: DuplexOptions); + _write(data: NodeBuffer, encoding: string, callback: Function): void; + _write(data: string, encoding: string, callback: Function): void; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; + } + + export interface TransformOptions extends ReadableOptions, WritableOptions {} + + // Note: Transform lacks the _read and _write methods of Readable/Writable. + export class Transform extends events.EventEmitter implements ReadWriteStream { + readable: boolean; + writable: boolean; + constructor(opts?: TransformOptions); + _transform(chunk: NodeBuffer, encoding: string, callback: Function): void; + _transform(chunk: string, encoding: string, callback: Function): void; + _flush(callback: Function): void; + read(size?: number): any; + setEncoding(encoding: string): void; + pause(): void; + resume(): void; + pipe(destination: T, options?: { end?: boolean; }): T; + unpipe(destination?: T): void; + unshift(chunk: string): void; + unshift(chunk: NodeBuffer): void; + wrap(oldStream: ReadableStream): ReadableStream; + push(chunk: any, encoding?: string): boolean; + write(buffer: NodeBuffer, cb?: Function): boolean; + write(str: string, cb?: Function): boolean; + write(str: string, encoding?: string, cb?: Function): boolean; + end(): void; + end(buffer: NodeBuffer, cb?: Function): void; + end(str: string, cb?: Function): void; + end(str: string, encoding?: string, cb?: Function): void; + } + + export class PassThrough extends Transform {} +} + +declare module "util" { + export interface InspectOptions { + showHidden?: boolean; + depth?: number; + colors?: boolean; + customInspect?: boolean; + } + + export function format(format: any, ...param: any[]): string; + export function debug(string: string): void; + export function error(...param: any[]): void; + export function puts(...param: any[]): void; + export function print(...param: any[]): void; + export function log(string: string): void; + export function inspect(object: any, showHidden?: boolean, depth?: number, color?: boolean): string; + export function inspect(object: any, options: InspectOptions): string; + export function isArray(object: any): boolean; + export function isRegExp(object: any): boolean; + export function isDate(object: any): boolean; + export function isError(object: any): boolean; + export function inherits(constructor: any, superConstructor: any): void; +} + +declare module "assert" { + function internal (value: any, message?: string): void; + module internal { + export class AssertionError implements Error { + name: string; + message: string; + actual: any; + expected: any; + operator: string; + generatedMessage: boolean; + + constructor(options?: {message?: string; actual?: any; expected?: any; + operator?: string; stackStartFunction?: Function}); + } + + export function fail(actual?: any, expected?: any, message?: string, operator?: string): void; + export function ok(value: any, message?: string): void; + export function equal(actual: any, expected: any, message?: string): void; + export function notEqual(actual: any, expected: any, message?: string): void; + export function deepEqual(actual: any, expected: any, message?: string): void; + export function notDeepEqual(acutal: any, expected: any, message?: string): void; + export function strictEqual(actual: any, expected: any, message?: string): void; + export function notStrictEqual(actual: any, expected: any, message?: string): void; + export var throws: { + (block: Function, message?: string): void; + (block: Function, error: Function, message?: string): void; + (block: Function, error: RegExp, message?: string): void; + (block: Function, error: (err: any) => boolean, message?: string): void; + } + + export var doesNotThrow: { + (block: Function, message?: string): void; + (block: Function, error: Function, message?: string): void; + (block: Function, error: RegExp, message?: string): void; + (block: Function, error: (err: any) => boolean, message?: string): void; + } + + export function ifError(value: any): void; + } + + export = internal; +} + +declare module "tty" { + import net = require("net"); + + export function isatty(fd: number): boolean; + export interface ReadStream extends net.NodeSocket { + isRaw: boolean; + setRawMode(mode: boolean): void; + } + export interface WriteStream extends net.NodeSocket { + columns: number; + rows: number; + } +} + +declare module "domain" { + import events = require("events"); + + export class Domain extends events.EventEmitter { + run(fn: Function): void; + add(emitter: NodeEventEmitter): void; + remove(emitter: NodeEventEmitter): void; + bind(cb: (err: Error, data: any) => any): any; + intercept(cb: (data: any) => any): any; + dispose(): void; + + addListener(event: string, listener: Function): Domain; + on(event: string, listener: Function): Domain; + once(event: string, listener: Function): Domain; + removeListener(event: string, listener: Function): Domain; + removeAllListeners(event?: string): Domain; + } + + export function create(): Domain; +} diff --git a/_infrastructure/tests/typings/tsd.d.ts b/_infrastructure/tests/typings/tsd.d.ts new file mode 100644 index 0000000000..717e8e49a3 --- /dev/null +++ b/_infrastructure/tests/typings/tsd.d.ts @@ -0,0 +1,3 @@ +/// +/// +/// diff --git a/package.json b/package.json index bb4c816f6e..a18c4fcd70 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,8 @@ "dependencies": { "git-wrapper": "~0.1.1", "glob": "~3.2.9", - "source-map-support": "~0.2.5" + "source-map-support": "~0.2.5", + "bluebird": "~1.0.7", + "lazy.js": "~0.3.2" } } diff --git a/tsd.json b/tsd.json new file mode 100644 index 0000000000..60f508161b --- /dev/null +++ b/tsd.json @@ -0,0 +1,15 @@ +{ + "version": "v4", + "repo": "borisyankov/DefinitelyTyped", + "ref": "master", + "path": "_infrastructure/tests/typings", + "bundle": "_infrastructure/tests/typings/tsd.d.ts", + "installed": { + "bluebird/bluebird.d.ts": { + "commit": "bac5b4311465c7052467ec315d0cbb1c640a69d6" + }, + "node/node.d.ts": { + "commit": "bac5b4311465c7052467ec315d0cbb1c640a69d6" + } + } +} From 8c80a0a768b59df05aac4c8da60e54ab58ab1735 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Sat, 1 Mar 2014 00:34:19 +0100 Subject: [PATCH 09/13] reworked tester flow and resolver rewrote the reference loop - ditched triple loop - added reference map with low-fi queue resolver simplified promise flow ditched some methods move print code to helpers --- _infrastructure/tests/runner.js | 182 ++++++++++++++++++------------- _infrastructure/tests/runner.ts | 187 +++++++++++++++++++------------- 2 files changed, 223 insertions(+), 146 deletions(-) diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index b11be80a39..e3eca657fd 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -825,12 +825,15 @@ var DT; _this.print.printChangeHeader(); - return Promise.all([ - _this.doParseFiles(), - _this.doGetChanges() - ]); + return _this.changes.getChanges().then(function () { + _this.printAllChanges(_this.changes.paths); + }); }).then(function () { - return _this.doCollectTargets(); + return _this.index.parseFiles(_this.files).then(function () { + // this.printFiles(this.files); + }); + }).then(function () { + return _this.collectTargets(_this.files, _this.changes.paths); }).then(function (files) { return _this.runTests(files); }).then(function () { @@ -840,41 +843,13 @@ var DT; }); }; - TestRunner.prototype.doParseFiles = function () { - return this.index.parseFiles(this.files).then(function () { - /* - this.print.printSubHeader('Files:'); - this.print.printDiv(); - this.files.forEach((file) => { - this.print.printLine(file.filePathWithName); - file.references.forEach((file) => { - this.print.printElement(file.filePathWithName); - }); - }); - this.print.printBreak();*/ - // chain - }).thenReturn(); - }; - - TestRunner.prototype.doGetChanges = function () { - var _this = this; - return this.changes.getChanges().then(function () { - _this.print.printSubHeader('All changes'); - _this.print.printDiv(); - - Lazy(_this.changes.paths).each(function (file) { - _this.print.printLine(file); - }); - }).thenReturn(); - }; - - TestRunner.prototype.doCollectTargets = function () { + TestRunner.prototype.collectTargets = function (files, changes) { var _this = this; return new Promise(function (resolve) { - // bake map for lookup + // filter changes and bake map for easy lookup var changeMap = Object.create(null); - Lazy(_this.changes.paths).filter(function (full) { + Lazy(changes).filter(function (full) { return _this.checkAcceptFile(full); }).map(function (local) { return path.resolve(_this.dtPath, local); @@ -889,51 +864,50 @@ var DT; changeMap[full] = file; }); - _this.print.printDiv(); - _this.print.printSubHeader('Relevant changes'); - _this.print.printDiv(); + _this.printRelChanges(changeMap); - Object.keys(changeMap).sort().forEach(function (src) { - _this.print.printLine(changeMap[src].formatName); + // bake reverse reference map (referenced to referrers) + var refMap = Object.create(null); + + Lazy(files).each(function (file) { + Lazy(file.references).each(function (ref) { + if (ref.fullPath in refMap) { + refMap[ref.fullPath].push(file); + } else { + refMap[ref.fullPath] = [file]; + } + }); }); - // terrible loop (whatever) - // just add stuff until there is nothing new added - // TODO improve it - var added; - var files = _this.files.slice(0); - do { - added = 0; + // this.printRefMap(refMap); + // map out files linked to changes + // - queue holds files touched by a change + // - pre-fill with actually changed files + // - loop queue, if current not seen: + // - add to result + // - from refMap queue all files referring to current + var adding = Object.create(null); + var queue = Lazy(changeMap).values().toArray(); - for (var i = files.length - 1; i >= 0; i--) { - var file = files[i]; - if (file.fullPath in changeMap) { - _this.files.splice(i, 1); - continue; - } - - for (var j = 0, jj = file.references.length; j < jj; j++) { - if (file.references[j].fullPath in changeMap) { - // add us - changeMap[file.fullPath] = file; - added++; - break; - } + while (queue.length > 0) { + var next = queue.shift(); + var fp = next.fullPath; + if (adding[fp]) { + continue; + } + adding[fp] = next; + if (fp in refMap) { + var arr = refMap[fp]; + for (var i = 0, ii = arr.length; i < ii; i++) { + // just add it and skip expensive checks + queue.push(arr[i]); } } - } while(added > 0); + } - _this.print.printDiv(); - _this.print.printSubHeader('Reference mapped'); - _this.print.printDiv(); + _this.printTests(adding); - var result = Object.keys(changeMap).sort().map(function (src) { - _this.print.printLine(changeMap[src].formatName); - changeMap[src].references.forEach(function (file) { - _this.print.printElement(file.formatName); - }); - return changeMap[src]; - }); + var result = Lazy(adding).values().toArray(); resolve(result); }); }; @@ -967,6 +941,7 @@ var DT; suite.testReporter = suite.testReporter || new DT.DefaultTestReporter(_this.print); _this.print.printSuiteHeader(suite.testSuiteName); + return suite.filterTargetFiles(files).then(function (targetFiles) { return suite.start(targetFiles, function (testResult, index) { _this.printTestComplete(testResult, index); @@ -982,6 +957,67 @@ var DT; }); }; + TestRunner.prototype.printTests = function (adding) { + var _this = this; + this.print.printDiv(); + this.print.printSubHeader('Testing'); + this.print.printDiv(); + + Object.keys(adding).sort().map(function (src) { + _this.print.printLine(adding[src].formatName); + return adding[src]; + }); + }; + TestRunner.prototype.printFiles = function (files) { + var _this = this; + this.print.printDiv(); + this.print.printSubHeader('Files:'); + this.print.printDiv(); + + files.forEach(function (file) { + _this.print.printLine(file.filePathWithName); + file.references.forEach(function (file) { + _this.print.printElement(file.filePathWithName); + }); + }); + }; + + TestRunner.prototype.printAllChanges = function (paths) { + var _this = this; + this.print.printSubHeader('All changes'); + this.print.printDiv(); + + paths.sort().forEach(function (line) { + _this.print.printLine(line); + }); + }; + + TestRunner.prototype.printRelChanges = function (changeMap) { + var _this = this; + this.print.printDiv(); + this.print.printSubHeader('Relevant changes'); + this.print.printDiv(); + + Object.keys(changeMap).sort().forEach(function (src) { + _this.print.printLine(changeMap[src].formatName); + }); + }; + + TestRunner.prototype.printRefMap = function (refMap) { + var _this = this; + this.print.printDiv(); + this.print.printSubHeader('Referring'); + this.print.printDiv(); + + Object.keys(refMap).sort().forEach(function (src) { + var ref = _this.index.getFile(src); + _this.print.printLine(ref.formatName); + refMap[src].forEach(function (file) { + _this.print.printLine(' - ' + file.formatName); + }); + }); + }; + TestRunner.prototype.printTestComplete = function (testResult, index) { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index 4f3a4389c9..8dfcc7b4cc 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -79,6 +79,14 @@ module DT { findNotRequiredTscparams?:boolean; } + export interface FileDict { + [fullPath:string]: File; + } + + export interface FileArrDict { + [fullPath:string]: File[]; + } + ///////////////////////////////// // The main class to kick things off ///////////////////////////////// @@ -127,12 +135,15 @@ module DT { this.print.printChangeHeader(); - return Promise.all([ - this.doParseFiles(), - this.doGetChanges() - ]); + return this.changes.getChanges().then(() => { + this.printAllChanges(this.changes.paths); + }); }).then(() => { - return this.doCollectTargets(); + return this.index.parseFiles(this.files).then(() => { + // this.printFiles(this.files); + }); + }).then(() => { + return this.collectTargets(this.files, this.changes.paths); }).then((files) => { return this.runTests(files); }).then(() => { @@ -142,40 +153,13 @@ module DT { }); } - private doParseFiles(): Promise { - return this.index.parseFiles(this.files).then(() => { - /* - this.print.printSubHeader('Files:'); - this.print.printDiv(); - this.files.forEach((file) => { - this.print.printLine(file.filePathWithName); - file.references.forEach((file) => { - this.print.printElement(file.filePathWithName); - }); - }); - this.print.printBreak();*/ - // chain - }).thenReturn(); - } - - private doGetChanges(): Promise { - return this.changes.getChanges().then(() => { - this.print.printSubHeader('All changes'); - this.print.printDiv(); - - Lazy(this.changes.paths).each((file) => { - this.print.printLine(file); - }); - }).thenReturn(); - } - - private doCollectTargets(): Promise { + private collectTargets(files: File[], changes: string[]): Promise { return new Promise((resolve) => { - // bake map for lookup - var changeMap = Object.create(null); + // filter changes and bake map for easy lookup + var changeMap: FileDict = Object.create(null); - Lazy(this.changes.paths).filter((full) => { + Lazy(changes).filter((full) => { return this.checkAcceptFile(full); }).map((local) => { return path.resolve(this.dtPath, local); @@ -190,52 +174,52 @@ module DT { changeMap[full] = file; }); - this.print.printDiv(); - this.print.printSubHeader('Relevant changes'); - this.print.printDiv(); + this.printRelChanges(changeMap); - Object.keys(changeMap).sort().forEach((src) => { - this.print.printLine(changeMap[src].formatName); + // bake reverse reference map (referenced to referrers) + var refMap: FileArrDict = Object.create(null); + + Lazy(files).each((file) => { + Lazy(file.references).each((ref) => { + if (ref.fullPath in refMap) { + refMap[ref.fullPath].push(file); + } + else { + refMap[ref.fullPath] = [file]; + } + }); }); - // terrible loop (whatever) - // just add stuff until there is nothing new added - // TODO improve it - var added: number; - var files = this.files.slice(0); - do { - added = 0; + // this.printRefMap(refMap); - for (var i = files.length - 1; i >= 0; i--) { - var file = files[i]; - if (file.fullPath in changeMap) { - this.files.splice(i, 1); - continue; - } - // check if one of our references is touched - for (var j = 0, jj = file.references.length; j < jj; j++) { - if (file.references[j].fullPath in changeMap) { - // add us - changeMap[file.fullPath] = file; - added++; - break; - } + // map out files linked to changes + // - queue holds files touched by a change + // - pre-fill with actually changed files + // - loop queue, if current not seen: + // - add to result + // - from refMap queue all files referring to current + var adding: FileDict = Object.create(null); + var queue = Lazy(changeMap).values().toArray(); + + while (queue.length > 0) { + var next = queue.shift(); + var fp = next.fullPath; + if (adding[fp]) { + continue; + } + adding[fp] = next; + if (fp in refMap) { + var arr = refMap[fp]; + for (var i = 0, ii = arr.length; i < ii; i++) { + // just add it and skip expensive checks + queue.push(arr[i]); } } } - while (added > 0); - this.print.printDiv(); - this.print.printSubHeader('Reference mapped'); - this.print.printDiv(); + this.printTests(adding); - var result: File[] = Object.keys(changeMap).sort().map((src) => { - this.print.printLine(changeMap[src].formatName); - changeMap[src].references.forEach((file: File) => { - this.print.printElement(file.formatName); - }); - return changeMap[src]; - }); + var result: File[] = Lazy(adding).values().toArray(); resolve(result); }); } @@ -268,6 +252,7 @@ module DT { suite.testReporter = suite.testReporter || new DefaultTestReporter(this.print); this.print.printSuiteHeader(suite.testSuiteName); + return suite.filterTargetFiles(files).then((targetFiles) => { return suite.start(targetFiles, (testResult, index) => { this.printTestComplete(testResult, index); @@ -283,6 +268,62 @@ module DT { }); } + private printTests(adding: FileDict): void { + this.print.printDiv(); + this.print.printSubHeader('Testing'); + this.print.printDiv(); + + Object.keys(adding).sort().map((src) => { + this.print.printLine(adding[src].formatName); + return adding[src]; + }); + } + private printFiles(files: File[]): void { + this.print.printDiv(); + this.print.printSubHeader('Files:'); + this.print.printDiv(); + + files.forEach((file) => { + this.print.printLine(file.filePathWithName); + file.references.forEach((file) => { + this.print.printElement(file.filePathWithName); + }); + }); + } + + private printAllChanges(paths: string[]): void { + this.print.printSubHeader('All changes'); + this.print.printDiv(); + + paths.sort().forEach((line) => { + this.print.printLine(line); + }); + } + + private printRelChanges(changeMap: FileDict): void { + this.print.printDiv(); + this.print.printSubHeader('Relevant changes'); + this.print.printDiv(); + + Object.keys(changeMap).sort().forEach((src) => { + this.print.printLine(changeMap[src].formatName); + }); + } + + private printRefMap(refMap: FileArrDict): void { + this.print.printDiv(); + this.print.printSubHeader('Referring'); + this.print.printDiv(); + + Object.keys(refMap).sort().forEach((src) => { + var ref = this.index.getFile(src); + this.print.printLine(ref.formatName); + refMap[src].forEach((file) => { + this.print.printLine(' - ' + file.formatName); + }); + }); + } + private printTestComplete(testResult: TestResult, index: number): void { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { From 4f99c0eb3c990a5e4e90e1b741d8c49a22d902be Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Sat, 1 Mar 2014 02:03:01 +0100 Subject: [PATCH 10/13] tester refactored & handle removed files abort test if a referred file is removed reworked main promise flow moved code from runner to index.ts moved code from runner to print.ts added logic to handle file removal to index.ts expanded state in index.ts dropped property File::formatName --- _infrastructure/tests/runner.js | 443 ++++++++++-------- _infrastructure/tests/runner.ts | 215 ++------- _infrastructure/tests/src/changes.ts | 10 +- _infrastructure/tests/src/file.ts | 12 +- _infrastructure/tests/src/index.ts | 130 ++++- _infrastructure/tests/src/printer.ts | 92 +++- _infrastructure/tests/src/suite/syntax.ts | 2 +- _infrastructure/tests/src/suite/testEval.ts | 2 +- _infrastructure/tests/src/suite/tscParams.ts | 4 +- .../tests/typings/lazy.js/lazy.js.d.ts | 4 +- 10 files changed, 512 insertions(+), 402 deletions(-) diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index e3eca657fd..c6ab61c8a4 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -52,7 +52,6 @@ var DT; this.ext = path.extname(this.filePathWithName); this.file = path.basename(this.filePathWithName, this.ext); this.dir = path.dirname(this.filePathWithName); - this.formatName = path.join(this.dir, this.file + this.ext); this.fullPath = path.join(this.baseDir, this.dir, this.file + this.ext); // lock it (shallow) (needs `use strict` in each file to work) // Object.freeze(this); @@ -202,6 +201,7 @@ var DT; var fs = require('fs'); var path = require('path'); + var glob = require('glob'); var Lazy = require('lazy.js'); var Promise = require('bluebird'); @@ -211,7 +211,8 @@ var DT; // Track all files in the repo: map full path to File objects ///////////////////////////////// var FileIndex = (function () { - function FileIndex(options) { + function FileIndex(runner, options) { + this.runner = runner; this.options = options; } FileIndex.prototype.hasFile = function (target) { @@ -225,14 +226,76 @@ var DT; return null; }; - FileIndex.prototype.parseFiles = function (files) { + FileIndex.prototype.setFile = function (file) { + if (file.fullPath in this.fileMap) { + throw new Error('cannot overwrite file'); + } + this.fileMap[file.fullPath] = file; + }; + + FileIndex.prototype.readIndex = function () { + var _this = this; + this.fileMap = Object.create(null); + + return Promise.promisify(glob).call(glob, '**/*.ts', { + cwd: this.runner.dtPath + }).then(function (filesNames) { + _this.files = Lazy(filesNames).filter(function (fileName) { + return _this.runner.checkAcceptFile(fileName); + }).map(function (fileName) { + var file = new DT.File(_this.runner.dtPath, fileName); + _this.fileMap[file.fullPath] = file; + return file; + }).toArray(); + }); + }; + + FileIndex.prototype.collectDiff = function (changes) { + var _this = this; + return new Promise(function (resolve) { + // filter changes and bake map for easy lookup + _this.changed = Object.create(null); + _this.removed = Object.create(null); + + Lazy(changes).filter(function (full) { + return _this.runner.checkAcceptFile(full); + }).uniq().each(function (local) { + var full = path.resolve(_this.runner.dtPath, local); + var file = _this.getFile(full); + if (!file) { + // TODO figure out what to do here + // what does it mean? deleted?ss + file = new DT.File(_this.runner.dtPath, local); + _this.setFile(file); + _this.removed[full] = file; + // console.log('not in index? %', file.fullPath); + } else { + _this.changed[full] = file; + } + }); + + // console.log('changed:\n' + Object.keys(this.changed).join('\n')); + // console.log('removed:\n' + Object.keys(this.removed).join('\n')); + resolve(); + }); + }; + + FileIndex.prototype.parseFiles = function () { + var _this = this; + return this.loadReferences(this.files).then(function () { + return _this.getMissingReferences(); + }); + }; + + FileIndex.prototype.getMissingReferences = function () { var _this = this; return Promise.attempt(function () { - _this.fileMap = Object.create(null); - files.forEach(function (file) { - _this.fileMap[file.fullPath] = file; + _this.missing = Object.create(null); + Lazy(_this.removed).keys().each(function (removed) { + if (removed in _this.refMap) { + _this.missing[removed] = _this.refMap[removed]; + } }); - return _this.loadReferences(files); }); }; @@ -262,6 +325,19 @@ var DT; } }; next(); + }).then(function () { + // bake reverse reference map (referenced to referrers) + _this.refMap = Object.create(null); + + Lazy(files).each(function (file) { + Lazy(file.references).each(function (ref) { + if (ref.fullPath in _this.refMap) { + _this.refMap[ref.fullPath].push(file); + } else { + _this.refMap[ref.fullPath] = [file]; + } + }); + }); }); }; @@ -287,11 +363,43 @@ var DT; return file; }); }; + + FileIndex.prototype.collectTargets = function () { + var _this = this; + return new Promise(function (resolve) { + // map out files linked to changes + // - queue holds files touched by a change + // - pre-fill with actually changed files + // - loop queue, if current not seen: + // - add to result + // - from refMap queue all files referring to current + var result = Object.create(null); + var queue = Lazy(_this.changed).values().toArray(); + + while (queue.length > 0) { + var next = queue.shift(); + var fp = next.fullPath; + if (result[fp]) { + continue; + } + result[fp] = next; + if (fp in _this.refMap) { + var arr = _this.refMap[fp]; + for (var i = 0, ii = arr.length; i < ii; i++) { + // just add it and skip expensive checks + queue.push(arr[i]); + } + } + } + resolve(Lazy(result).values().toArray()); + }); + }; return FileIndex; })(); DT.FileIndex = FileIndex; })(DT || (DT = {})); /// +/// var DT; (function (DT) { 'use strict'; @@ -302,11 +410,10 @@ var DT; var Promise = require('bluebird'); var GitChanges = (function () { - function GitChanges(baseDir) { - this.baseDir = baseDir; + function GitChanges(runner) { + this.runner = runner; this.options = {}; - this.paths = []; - var dir = path.join(baseDir, '.git'); + var dir = path.join(this.runner.dtPath, '.git'); if (!fs.existsSync(dir)) { throw new Error('cannot locate git-dir: ' + dir); } @@ -315,12 +422,11 @@ var DT; this.git = new Git(this.options); this.git.exec = Promise.promisify(this.git.exec); } - GitChanges.prototype.getChanges = function () { - var _this = this; + GitChanges.prototype.readChanges = function () { var opts = {}; var args = ['--name-only HEAD~1']; return this.git.exec('diff', opts, args).then(function (msg) { - _this.paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); + return msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); }); }; return GitChanges; @@ -393,7 +499,7 @@ var DT; }; Print.prototype.printErrorsForFile = function (testResult) { - this.out('----------------- For file:' + testResult.targetFile.formatName); + this.out('----------------- For file:' + testResult.targetFile.filePathWithName); this.printBreak().out(testResult.stderr).printBreak(); }; @@ -471,6 +577,101 @@ var DT; }); } }; + + Print.prototype.printTestComplete = function (testResult, index) { + var reporter = testResult.hostedBy.testReporter; + if (testResult.success) { + reporter.printPositiveCharacter(index, testResult); + } else { + reporter.printNegativeCharacter(index, testResult); + } + }; + + Print.prototype.printSuiteComplete = function (suite) { + this.printBreak(); + + this.printDiv(); + this.printElapsedTime(suite.timer.asString, suite.timer.time); + this.printSuccessCount(suite.okTests.length, suite.testResults.length); + this.printFailedCount(suite.ngTests.length, suite.testResults.length); + }; + + Print.prototype.printTests = function (adding) { + var _this = this; + this.printDiv(); + this.printSubHeader('Testing'); + this.printDiv(); + + Object.keys(adding).sort().map(function (src) { + _this.printLine(adding[src].filePathWithName); + return adding[src]; + }); + }; + + Print.prototype.printFiles = function (files) { + var _this = this; + this.printDiv(); + this.printSubHeader('Files:'); + this.printDiv(); + + files.forEach(function (file) { + _this.printLine(file.filePathWithName); + file.references.forEach(function (file) { + _this.printElement(file.filePathWithName); + }); + }); + }; + + Print.prototype.printMissing = function (index, refMap) { + var _this = this; + this.printDiv(); + this.printSubHeader('Missing references'); + this.printDiv(); + + Object.keys(refMap).sort().forEach(function (src) { + var ref = index.getFile(src); + _this.printLine('\33[31m\33[1m' + ref.filePathWithName + '\33[0m'); + refMap[src].forEach(function (file) { + _this.printElement(file.filePathWithName); + }); + }); + }; + + Print.prototype.printAllChanges = function (paths) { + var _this = this; + this.printSubHeader('All changes'); + this.printDiv(); + + paths.sort().forEach(function (line) { + _this.printLine(line); + }); + }; + + Print.prototype.printRelChanges = function (changeMap) { + var _this = this; + this.printDiv(); + this.printSubHeader('Relevant changes'); + this.printDiv(); + + Object.keys(changeMap).sort().forEach(function (src) { + _this.printLine(changeMap[src].filePathWithName); + }); + }; + + Print.prototype.printRefMap = function (index, refMap) { + var _this = this; + this.printDiv(); + this.printSubHeader('Referring'); + this.printDiv(); + + Object.keys(refMap).sort().forEach(function (src) { + var ref = index.getFile(src); + _this.printLine(ref.filePathWithName); + refMap[src].forEach(function (file) { + _this.printLine(' - ' + file.filePathWithName); + }); + }); + }; return Print; })(); DT.Print = Print; @@ -607,7 +808,7 @@ var DT; } SyntaxChecking.prototype.filterTargetFiles = function (files) { return Promise.cast(files.filter(function (file) { - return endDts.test(file.formatName); + return endDts.test(file.filePathWithName); })); }; return SyntaxChecking; @@ -634,7 +835,7 @@ var DT; } TestEval.prototype.filterTargetFiles = function (files) { return Promise.cast(files.filter(function (file) { - return endTestDts.test(file.formatName); + return endTestDts.test(file.filePathWithName); })); }; return TestEval; @@ -664,7 +865,7 @@ var DT; this.testReporter = { printPositiveCharacter: function (index, testResult) { - _this.print.clearCurrentLine().printTypingsWithoutTestName(testResult.targetFile.formatName); + _this.print.clearCurrentLine().printTypingsWithoutTestName(testResult.targetFile.filePathWithName); }, printNegativeCharacter: function (index, testResult) { } @@ -680,7 +881,7 @@ var DT; FindNotRequiredTscparams.prototype.runTest = function (targetFile) { var _this = this; - this.print.clearCurrentLine().out(targetFile.formatName); + this.print.clearCurrentLine().out(targetFile.filePathWithName); return new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion, @@ -729,7 +930,6 @@ var DT; var fs = require('fs'); var path = require('path'); - var glob = require('glob'); var assert = require('assert'); var tsExp = /\.ts$/; @@ -792,8 +992,9 @@ var DT; this.suites = []; this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - this.index = new DT.FileIndex(this.options); - this.changes = new DT.GitChanges(this.dtPath); + this.index = new DT.FileIndex(this, this.options); + this.changes = new DT.GitChanges(this); + this.print = new DT.Print(this.options.tscVersion); } TestRunner.prototype.addSuite = function (suite) { @@ -813,102 +1014,37 @@ var DT; this.timer = new DT.Timer(); this.timer.start(); + this.print.printChangeHeader(); + // only includes .d.ts or -tests.ts or -test.ts or .ts - return Promise.promisify(glob).call(glob, '**/*.ts', { - cwd: dtPath - }).then(function (filesNames) { - _this.files = Lazy(filesNames).filter(function (fileName) { - return _this.checkAcceptFile(fileName); - }).map(function (fileName) { - return new DT.File(dtPath, fileName); - }).toArray(); - - _this.print.printChangeHeader(); - - return _this.changes.getChanges().then(function () { - _this.printAllChanges(_this.changes.paths); - }); + return this.index.readIndex().then(function () { + return _this.changes.readChanges(); + }).then(function (changes) { + _this.print.printAllChanges(changes); + return _this.index.collectDiff(changes); }).then(function () { - return _this.index.parseFiles(_this.files).then(function () { - // this.printFiles(this.files); - }); + return _this.index.parseFiles(); }).then(function () { - return _this.collectTargets(_this.files, _this.changes.paths); - }).then(function (files) { - return _this.runTests(files); - }).then(function () { - return !_this.suites.some(function (suite) { - return suite.ngTests.length !== 0; - }); - }); - }; + // 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(); - TestRunner.prototype.collectTargets = function (files, changes) { - var _this = this; - return new Promise(function (resolve) { - // filter changes and bake map for easy lookup - var changeMap = Object.create(null); - - Lazy(changes).filter(function (full) { - return _this.checkAcceptFile(full); - }).map(function (local) { - return path.resolve(_this.dtPath, local); - }).each(function (full) { - var file = _this.index.getFile(full); - if (!file) { - // TODO figure out what to do here - // what does it mean? deleted? - console.log('not in index: ' + full); - return; - } - changeMap[full] = file; - }); - - _this.printRelChanges(changeMap); - - // bake reverse reference map (referenced to referrers) - var refMap = Object.create(null); - - Lazy(files).each(function (file) { - Lazy(file.references).each(function (ref) { - if (ref.fullPath in refMap) { - refMap[ref.fullPath].push(file); - } else { - refMap[ref.fullPath] = [file]; - } - }); - }); - - // this.printRefMap(refMap); - // map out files linked to changes - // - queue holds files touched by a change - // - pre-fill with actually changed files - // - loop queue, if current not seen: - // - add to result - // - from refMap queue all files referring to current - var adding = Object.create(null); - var queue = Lazy(changeMap).values().toArray(); - - while (queue.length > 0) { - var next = queue.shift(); - var fp = next.fullPath; - if (adding[fp]) { - continue; - } - adding[fp] = next; - if (fp in refMap) { - var arr = refMap[fp]; - for (var i = 0, ii = arr.length; i < ii; i++) { - // just add it and skip expensive checks - queue.push(arr[i]); - } - } + // bail + return Promise.delay(500).return(false); } - _this.printTests(adding); - - var result = Lazy(adding).values().toArray(); - resolve(result); + // this.print.printFiles(this.files); + return _this.index.collectTargets().then(function (files) { + // this.print.printTests(files); + return _this.runTests(files); + }).then(function () { + return !_this.suites.some(function (suite) { + return suite.ngTests.length !== 0; + }); + }); }); }; @@ -944,10 +1080,10 @@ var DT; return suite.filterTargetFiles(files).then(function (targetFiles) { return suite.start(targetFiles, function (testResult, index) { - _this.printTestComplete(testResult, index); + _this.print.printTestComplete(testResult, index); }); }).then(function (suite) { - _this.printSuiteComplete(suite); + _this.print.printSuiteComplete(suite); return count++; }); }, 0); @@ -957,85 +1093,6 @@ var DT; }); }; - TestRunner.prototype.printTests = function (adding) { - var _this = this; - this.print.printDiv(); - this.print.printSubHeader('Testing'); - this.print.printDiv(); - - Object.keys(adding).sort().map(function (src) { - _this.print.printLine(adding[src].formatName); - return adding[src]; - }); - }; - TestRunner.prototype.printFiles = function (files) { - var _this = this; - this.print.printDiv(); - this.print.printSubHeader('Files:'); - this.print.printDiv(); - - files.forEach(function (file) { - _this.print.printLine(file.filePathWithName); - file.references.forEach(function (file) { - _this.print.printElement(file.filePathWithName); - }); - }); - }; - - TestRunner.prototype.printAllChanges = function (paths) { - var _this = this; - this.print.printSubHeader('All changes'); - this.print.printDiv(); - - paths.sort().forEach(function (line) { - _this.print.printLine(line); - }); - }; - - TestRunner.prototype.printRelChanges = function (changeMap) { - var _this = this; - this.print.printDiv(); - this.print.printSubHeader('Relevant changes'); - this.print.printDiv(); - - Object.keys(changeMap).sort().forEach(function (src) { - _this.print.printLine(changeMap[src].formatName); - }); - }; - - TestRunner.prototype.printRefMap = function (refMap) { - var _this = this; - this.print.printDiv(); - this.print.printSubHeader('Referring'); - this.print.printDiv(); - - Object.keys(refMap).sort().forEach(function (src) { - var ref = _this.index.getFile(src); - _this.print.printLine(ref.formatName); - refMap[src].forEach(function (file) { - _this.print.printLine(' - ' + file.formatName); - }); - }); - }; - - TestRunner.prototype.printTestComplete = function (testResult, index) { - var reporter = testResult.hostedBy.testReporter; - if (testResult.success) { - reporter.printPositiveCharacter(index, testResult); - } else { - reporter.printNegativeCharacter(index, testResult); - } - }; - - TestRunner.prototype.printSuiteComplete = function (suite) { - this.print.printBreak(); - - this.print.printDiv(); - this.print.printElapsedTime(suite.timer.asString, suite.timer.time); - this.print.printSuccessCount(suite.okTests.length, suite.testResults.length); - this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); - }; - TestRunner.prototype.finaliseTests = function (files) { var _this = this; var testEval = Lazy(this.suites).filter(function (suite) { diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index 8dfcc7b4cc..126b824b20 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -27,7 +27,6 @@ module DT { var fs = require('fs'); var path = require('path'); - var glob = require('glob'); var assert = require('assert'); var tsExp = /\.ts$/; @@ -79,31 +78,23 @@ module DT { findNotRequiredTscparams?:boolean; } - export interface FileDict { - [fullPath:string]: File; - } - - export interface FileArrDict { - [fullPath:string]: File[]; - } - ///////////////////////////////// // The main class to kick things off ///////////////////////////////// export class TestRunner { - private files: File[]; private timer: Timer; private suites: ITestSuite[] = []; - private index: FileIndex; - private changes: GitChanges; - private print: Print; + public changes: GitChanges; + public index: FileIndex; + public print: Print; constructor(public dtPath: string, public options: ITestRunnerOptions = {tscVersion: DT.DEFAULT_TSC_VERSION}) { this.options.findNotRequiredTscparams = !!this.options.findNotRequiredTscparams; - this.index = new FileIndex(this.options); - this.changes = new GitChanges(this.dtPath); + this.index = new FileIndex(this, this.options); + this.changes = new GitChanges(this); + this.print = new Print(this.options.tscVersion); } @@ -111,7 +102,7 @@ module DT { this.suites.push(suite); } - private checkAcceptFile(fileName: string): boolean { + public checkAcceptFile(fileName: string): boolean { var ok = tsExp.test(fileName); ok = ok && fileName.indexOf('_infrastructure') < 0; ok = ok && fileName.indexOf('node_modules/') < 0; @@ -123,104 +114,35 @@ module DT { this.timer = new Timer(); this.timer.start(); + this.print.printChangeHeader(); + // only includes .d.ts or -tests.ts or -test.ts or .ts - return Promise.promisify(glob).call(glob, '**/*.ts', { - cwd: dtPath - }).then((filesNames: string[]) => { - this.files = Lazy(filesNames).filter((fileName) => { - return this.checkAcceptFile(fileName); - }).map((fileName: string) => { - return new File(dtPath, fileName); - }).toArray(); - - this.print.printChangeHeader(); - - return this.changes.getChanges().then(() => { - this.printAllChanges(this.changes.paths); - }); + return this.index.readIndex().then(() => { + return this.changes.readChanges(); + }).then((changes: string[]) => { + this.print.printAllChanges(changes); + return this.index.collectDiff(changes); }).then(() => { - return this.index.parseFiles(this.files).then(() => { - // this.printFiles(this.files); - }); + return this.index.parseFiles(); }).then(() => { - return this.collectTargets(this.files, this.changes.paths); - }).then((files) => { - return this.runTests(files); - }).then(() => { - return !this.suites.some((suite) => { - return suite.ngTests.length !== 0 - }); - }); - } + // this.print.printRefMap(this.index, this.index.refMap); - private collectTargets(files: File[], changes: string[]): Promise { - return new Promise((resolve) => { + if (Lazy(this.index.missing).some((arr: any[]) => arr.length > 0)) { + this.print.printMissing(this.index, this.index.missing); + this.print.printBoldDiv(); + // bail + return Promise.delay(500).return(false); + } + // this.print.printFiles(this.files); + return this.index.collectTargets().then((files) => { + // this.print.printTests(files); - // filter changes and bake map for easy lookup - var changeMap: FileDict = Object.create(null); - - Lazy(changes).filter((full) => { - return this.checkAcceptFile(full); - }).map((local) => { - return path.resolve(this.dtPath, local); - }).each((full) => { - var file = this.index.getFile(full); - if (!file) { - // TODO figure out what to do here - // what does it mean? deleted? - console.log('not in index: ' + full); - return; - } - changeMap[full] = file; - }); - - this.printRelChanges(changeMap); - - // bake reverse reference map (referenced to referrers) - var refMap: FileArrDict = Object.create(null); - - Lazy(files).each((file) => { - Lazy(file.references).each((ref) => { - if (ref.fullPath in refMap) { - refMap[ref.fullPath].push(file); - } - else { - refMap[ref.fullPath] = [file]; - } + return this.runTests(files); + }).then(() => { + return !this.suites.some((suite) => { + return suite.ngTests.length !== 0 }); }); - - // this.printRefMap(refMap); - - // map out files linked to changes - // - queue holds files touched by a change - // - pre-fill with actually changed files - // - loop queue, if current not seen: - // - add to result - // - from refMap queue all files referring to current - var adding: FileDict = Object.create(null); - var queue = Lazy(changeMap).values().toArray(); - - while (queue.length > 0) { - var next = queue.shift(); - var fp = next.fullPath; - if (adding[fp]) { - continue; - } - adding[fp] = next; - if (fp in refMap) { - var arr = refMap[fp]; - for (var i = 0, ii = arr.length; i < ii; i++) { - // just add it and skip expensive checks - queue.push(arr[i]); - } - } - } - - this.printTests(adding); - - var result: File[] = Lazy(adding).values().toArray(); - resolve(result); }); } @@ -255,10 +177,10 @@ module DT { return suite.filterTargetFiles(files).then((targetFiles) => { return suite.start(targetFiles, (testResult, index) => { - this.printTestComplete(testResult, index); + this.print.printTestComplete(testResult, index); }); }).then((suite) => { - this.printSuiteComplete(suite); + this.print.printSuiteComplete(suite); return count++; }); }, 0); @@ -268,81 +190,6 @@ module DT { }); } - private printTests(adding: FileDict): void { - this.print.printDiv(); - this.print.printSubHeader('Testing'); - this.print.printDiv(); - - Object.keys(adding).sort().map((src) => { - this.print.printLine(adding[src].formatName); - return adding[src]; - }); - } - private printFiles(files: File[]): void { - this.print.printDiv(); - this.print.printSubHeader('Files:'); - this.print.printDiv(); - - files.forEach((file) => { - this.print.printLine(file.filePathWithName); - file.references.forEach((file) => { - this.print.printElement(file.filePathWithName); - }); - }); - } - - private printAllChanges(paths: string[]): void { - this.print.printSubHeader('All changes'); - this.print.printDiv(); - - paths.sort().forEach((line) => { - this.print.printLine(line); - }); - } - - private printRelChanges(changeMap: FileDict): void { - this.print.printDiv(); - this.print.printSubHeader('Relevant changes'); - this.print.printDiv(); - - Object.keys(changeMap).sort().forEach((src) => { - this.print.printLine(changeMap[src].formatName); - }); - } - - private printRefMap(refMap: FileArrDict): void { - this.print.printDiv(); - this.print.printSubHeader('Referring'); - this.print.printDiv(); - - Object.keys(refMap).sort().forEach((src) => { - var ref = this.index.getFile(src); - this.print.printLine(ref.formatName); - refMap[src].forEach((file) => { - this.print.printLine(' - ' + file.formatName); - }); - }); - } - - private printTestComplete(testResult: TestResult, index: number): void { - var reporter = testResult.hostedBy.testReporter; - if (testResult.success) { - reporter.printPositiveCharacter(index, testResult); - } - else { - reporter.printNegativeCharacter(index, testResult); - } - } - - private printSuiteComplete(suite: ITestSuite): void { - this.print.printBreak(); - - this.print.printDiv(); - this.print.printElapsedTime(suite.timer.asString, suite.timer.time); - this.print.printSuccessCount(suite.okTests.length, suite.testResults.length); - this.print.printFailedCount(suite.ngTests.length, suite.testResults.length); - } - private finaliseTests(files: File[]): void { var testEval: TestEval = Lazy(this.suites).filter((suite) => { return suite instanceof TestEval; diff --git a/_infrastructure/tests/src/changes.ts b/_infrastructure/tests/src/changes.ts index f3ec262084..a56c4029eb 100644 --- a/_infrastructure/tests/src/changes.ts +++ b/_infrastructure/tests/src/changes.ts @@ -1,4 +1,5 @@ /// +/// module DT { 'use strict'; @@ -12,10 +13,9 @@ module DT { git; options = {}; - paths: string[] = []; - constructor(public baseDir: string) { - var dir = path.join(baseDir, '.git'); + constructor(private runner: TestRunner) { + var dir = path.join(this.runner.dtPath, '.git'); if (!fs.existsSync(dir)) { throw new Error('cannot locate git-dir: ' + dir); } @@ -25,11 +25,11 @@ module DT { this.git.exec = Promise.promisify(this.git.exec); } - getChanges(): Promise { + public readChanges(): Promise { var opts = {}; var args = ['--name-only HEAD~1']; return this.git.exec('diff', opts, args).then((msg: string) => { - this.paths = msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); + return msg.replace(/^\s+/, '').replace(/\s+$/, '').split(/\r?\n/g); }); } } diff --git a/_infrastructure/tests/src/file.ts b/_infrastructure/tests/src/file.ts index 389936b491..2c69f8f885 100644 --- a/_infrastructure/tests/src/file.ts +++ b/_infrastructure/tests/src/file.ts @@ -5,6 +5,14 @@ module DT { var path = require('path'); + export interface FileDict { + [fullPath:string]: File; + } + + export interface FileArrDict { + [fullPath:string]: File[]; + } + ///////////////////////////////// // Given a document root + ts file pattern this class returns: // all the TS files OR just tests OR just definition files @@ -15,7 +23,6 @@ module DT { dir: string; file: string; ext: string; - formatName: string; fullPath: string; references: File[] = []; @@ -26,14 +33,13 @@ module DT { this.ext = path.extname(this.filePathWithName); this.file = path.basename(this.filePathWithName, this.ext); this.dir = path.dirname(this.filePathWithName); - this.formatName = path.join(this.dir, this.file + this.ext); this.fullPath = path.join(this.baseDir, this.dir, this.file + this.ext); // lock it (shallow) (needs `use strict` in each file to work) // Object.freeze(this); } - toString() { + toString(): string { return '[File ' + this.filePathWithName + ']'; } } diff --git a/_infrastructure/tests/src/index.ts b/_infrastructure/tests/src/index.ts index 183c1e64db..7f5878dcb3 100644 --- a/_infrastructure/tests/src/index.ts +++ b/_infrastructure/tests/src/index.ts @@ -7,7 +7,8 @@ module DT { var fs = require('fs'); var path = require('path'); - var Lazy = require('lazy.js'); + var glob = require('glob'); + var Lazy: LazyJS.LazyStatic = require('lazy.js'); var Promise: typeof Promise = require('bluebird'); var readFile = Promise.promisify(fs.readFile); @@ -17,31 +18,95 @@ module DT { ///////////////////////////////// export class FileIndex { - fileMap: {[fullPath:string]:File}; + files: File[]; + fileMap: FileDict; + refMap: FileArrDict; options: ITestRunnerOptions; + changed: FileDict; + removed: FileDict; + missing: FileArrDict; - constructor(options: ITestRunnerOptions) { + constructor(private runner: TestRunner, options: ITestRunnerOptions) { this.options = options; } - hasFile(target: string): boolean { + public hasFile(target: string): boolean { return target in this.fileMap; } - getFile(target: string): File { + public getFile(target: string): File { if (target in this.fileMap) { return this.fileMap[target]; } return null; } - parseFiles(files: File[]): Promise { - return Promise.attempt(() => { - this.fileMap = Object.create(null); - files.forEach((file) => { + public setFile(file: File): void { + if (file.fullPath in this.fileMap) { + throw new Error('cannot overwrite file'); + } + this.fileMap[file.fullPath] = file; + } + + public readIndex(): Promise { + this.fileMap = Object.create(null); + + return Promise.promisify(glob).call(glob, '**/*.ts', { + cwd: this.runner.dtPath + }).then((filesNames: string[]) => { + this.files = Lazy(filesNames).filter((fileName) => { + return this.runner.checkAcceptFile(fileName); + }).map((fileName: string) => { + var file = new File(this.runner.dtPath, fileName); this.fileMap[file.fullPath] = file; + return file; + }).toArray(); + }); + } + + public collectDiff(changes: string[]): Promise { + return new Promise((resolve) => { + // filter changes and bake map for easy lookup + this.changed = Object.create(null); + this.removed = Object.create(null); + + Lazy(changes).filter((full) => { + return this.runner.checkAcceptFile(full); + }).uniq().each((local) => { + var full = path.resolve(this.runner.dtPath, local); + var file = this.getFile(full); + if (!file) { + // TODO figure out what to do here + // what does it mean? deleted?ss + file = new File(this.runner.dtPath, local); + this.setFile(file); + this.removed[full] = file; + // console.log('not in index? %', file.fullPath); + } + else { + this.changed[full] = file; + } + }); + // console.log('changed:\n' + Object.keys(this.changed).join('\n')); + // console.log('removed:\n' + Object.keys(this.removed).join('\n')); + resolve(); + }); + } + + public parseFiles(): Promise { + return this.loadReferences(this.files).then(() => { + return this.getMissingReferences(); + }); + } + + private getMissingReferences(): Promise { + return Promise.attempt(() => { + this.missing = Object.create(null); + Lazy(this.removed).keys().each((removed) => { + if (removed in this.refMap) { + this.missing[removed] = this.refMap[removed]; + } }); - return this.loadReferences(files); }); } @@ -70,6 +135,20 @@ module DT { } }; next(); + }).then(() => { + // bake reverse reference map (referenced to referrers) + this.refMap = Object.create(null); + + Lazy(files).each((file) => { + Lazy(file.references).each((ref) => { + if (ref.fullPath in this.refMap) { + this.refMap[ref.fullPath].push(file); + } + else { + this.refMap[ref.fullPath] = [file]; + } + }); + }); }); } @@ -94,5 +173,36 @@ module DT { return file; }); } + + public collectTargets(): Promise { + return new Promise((resolve) => { + // map out files linked to changes + // - queue holds files touched by a change + // - pre-fill with actually changed files + // - loop queue, if current not seen: + // - add to result + // - from refMap queue all files referring to current + + var result: FileDict = Object.create(null); + var queue = Lazy(this.changed).values().toArray(); + + while (queue.length > 0) { + var next = queue.shift(); + var fp = next.fullPath; + if (result[fp]) { + continue; + } + result[fp] = next; + if (fp in this.refMap) { + var arr = this.refMap[fp]; + for (var i = 0, ii = arr.length; i < ii; i++) { + // just add it and skip expensive checks + queue.push(arr[i]); + } + } + } + resolve(Lazy(result).values().toArray()); + }); + } } } diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index 613251b825..9be9a79aa5 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -72,7 +72,7 @@ module DT { } public printErrorsForFile(testResult: TestResult) { - this.out('----------------- For file:' + testResult.targetFile.formatName); + this.out('----------------- For file:' + testResult.targetFile.filePathWithName); this.printBreak().out(testResult.stderr).printBreak(); } @@ -149,5 +149,95 @@ module DT { }); } } + + public printTestComplete(testResult: TestResult, index: number): void { + var reporter = testResult.hostedBy.testReporter; + if (testResult.success) { + reporter.printPositiveCharacter(index, testResult); + } + else { + reporter.printNegativeCharacter(index, testResult); + } + } + + public printSuiteComplete(suite: ITestSuite): void { + this.printBreak(); + + this.printDiv(); + this.printElapsedTime(suite.timer.asString, suite.timer.time); + this.printSuccessCount(suite.okTests.length, suite.testResults.length); + this.printFailedCount(suite.ngTests.length, suite.testResults.length); + } + + public printTests(adding: FileDict): void { + this.printDiv(); + this.printSubHeader('Testing'); + this.printDiv(); + + Object.keys(adding).sort().map((src) => { + this.printLine(adding[src].filePathWithName); + return adding[src]; + }); + } + + public printFiles(files: File[]): void { + this.printDiv(); + this.printSubHeader('Files:'); + this.printDiv(); + + files.forEach((file) => { + this.printLine(file.filePathWithName); + file.references.forEach((file) => { + this.printElement(file.filePathWithName); + }); + }); + } + + public printMissing(index: FileIndex, refMap: FileArrDict): void { + this.printDiv(); + this.printSubHeader('Missing references'); + this.printDiv(); + + Object.keys(refMap).sort().forEach((src) => { + var ref = index.getFile(src); + this.printLine('\33[31m\33[1m' + ref.filePathWithName + '\33[0m'); + refMap[src].forEach((file) => { + this.printElement(file.filePathWithName); + }); + }); + } + + public printAllChanges(paths: string[]): void { + this.printSubHeader('All changes'); + this.printDiv(); + + paths.sort().forEach((line) => { + this.printLine(line); + }); + } + + public printRelChanges(changeMap: FileDict): void { + this.printDiv(); + this.printSubHeader('Relevant changes'); + this.printDiv(); + + Object.keys(changeMap).sort().forEach((src) => { + this.printLine(changeMap[src].filePathWithName); + }); + } + + public printRefMap(index: FileIndex, refMap: FileArrDict): void { + this.printDiv(); + this.printSubHeader('Referring'); + this.printDiv(); + + Object.keys(refMap).sort().forEach((src) => { + var ref = index.getFile(src); + this.printLine(ref.filePathWithName); + refMap[src].forEach((file) => { + this.printLine(' - ' + file.filePathWithName); + }); + }); + } } } diff --git a/_infrastructure/tests/src/suite/syntax.ts b/_infrastructure/tests/src/suite/syntax.ts index 41808b2a2b..2e0d0b71aa 100644 --- a/_infrastructure/tests/src/suite/syntax.ts +++ b/_infrastructure/tests/src/suite/syntax.ts @@ -19,7 +19,7 @@ module DT { public filterTargetFiles(files: File[]): Promise { return Promise.cast(files.filter((file) => { - return endDts.test(file.formatName); + return endDts.test(file.filePathWithName); })); } } diff --git a/_infrastructure/tests/src/suite/testEval.ts b/_infrastructure/tests/src/suite/testEval.ts index bc842549fc..95044daa7b 100644 --- a/_infrastructure/tests/src/suite/testEval.ts +++ b/_infrastructure/tests/src/suite/testEval.ts @@ -19,7 +19,7 @@ module DT { public filterTargetFiles(files: File[]): Promise { return Promise.cast(files.filter((file) => { - return endTestDts.test(file.formatName); + return endTestDts.test(file.filePathWithName); })); } } diff --git a/_infrastructure/tests/src/suite/tscParams.ts b/_infrastructure/tests/src/suite/tscParams.ts index cef23d622b..bb064dd818 100644 --- a/_infrastructure/tests/src/suite/tscParams.ts +++ b/_infrastructure/tests/src/suite/tscParams.ts @@ -22,7 +22,7 @@ module DT { printPositiveCharacter: (index: number, testResult: TestResult) => { this.print .clearCurrentLine() - .printTypingsWithoutTestName(testResult.targetFile.formatName); + .printTypingsWithoutTestName(testResult.targetFile.filePathWithName); }, printNegativeCharacter: (index: number, testResult: TestResult) => { } @@ -38,7 +38,7 @@ module DT { } public runTest(targetFile: File): Promise { - this.print.clearCurrentLine().out(targetFile.formatName); + this.print.clearCurrentLine().out(targetFile.filePathWithName); return new Test(this, targetFile, { tscVersion: this.options.tscVersion, diff --git a/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts b/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts index b9367b73bc..b2909dfceb 100644 --- a/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts +++ b/_infrastructure/tests/typings/lazy.js/lazy.js.d.ts @@ -6,12 +6,12 @@ declare module LazyJS { interface LazyStatic { - (value: string):StringLikeSequence; (value: T[]):ArrayLikeSequence; (value: any[]):ArrayLikeSequence; (value: Object):ObjectLikeSequence; (value: Object):ObjectLikeSequence; + (value: string):StringLikeSequence; strict():LazyStatic; @@ -200,7 +200,7 @@ declare module LazyJS { functions(): Sequence; get(property: string): ObjectLikeSequence; invert(): ObjectLikeSequence; - keys(): StringLikeSequence; + keys(): Sequence; omit(properties: string[]): ObjectLikeSequence; pairs(): Sequence; pick(properties: string[]): ObjectLikeSequence; From b134395b23a93521aa0936aca5162b88f19322df Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Sat, 1 Mar 2014 02:16:12 +0100 Subject: [PATCH 11/13] tuned tester tuned output to show changes / removals test filter RegExp finds: - file-tests.d.ts - file-test.d.ts --- _infrastructure/tests/runner.js | 34 ++++++++++++++++++--- _infrastructure/tests/runner.ts | 6 ++-- _infrastructure/tests/src/printer.ts | 24 +++++++++++++-- _infrastructure/tests/src/suite/testEval.ts | 3 +- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index c6ab61c8a4..e21bbd42be 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -608,10 +608,20 @@ var DT; }); }; + Print.prototype.printQueue = function (files) { + var _this = this; + this.printDiv(); + this.printSubHeader('Queued for testing'); + this.printDiv(); + + files.forEach(function (file) { + _this.printLine(file.filePathWithName); + }); + }; Print.prototype.printFiles = function (files) { var _this = this; this.printDiv(); - this.printSubHeader('Files:'); + this.printSubHeader('Files'); this.printDiv(); files.forEach(function (file) { @@ -650,7 +660,18 @@ var DT; Print.prototype.printRelChanges = function (changeMap) { var _this = this; this.printDiv(); - this.printSubHeader('Relevant changes'); + this.printSubHeader('Interesting files'); + this.printDiv(); + + Object.keys(changeMap).sort().forEach(function (src) { + _this.printLine(changeMap[src].filePathWithName); + }); + }; + + Print.prototype.printRemovals = function (changeMap) { + var _this = this; + this.printDiv(); + this.printSubHeader('Removed files'); this.printDiv(); Object.keys(changeMap).sort().forEach(function (src) { @@ -823,7 +844,7 @@ var DT; var Promise = require('bluebird'); - var endTestDts = /-test.ts$/i; + var endTestDts = /-tests?.ts$/i; ///////////////////////////////// // Compile with *-tests.ts @@ -1023,6 +1044,8 @@ var DT; _this.print.printAllChanges(changes); return _this.index.collectDiff(changes); }).then(function () { + _this.print.printRemovals(_this.index.removed); + _this.print.printRelChanges(_this.index.changed); return _this.index.parseFiles(); }).then(function () { // this.print.printRefMap(this.index, this.index.refMap); @@ -1033,12 +1056,13 @@ var DT; _this.print.printBoldDiv(); // bail - return Promise.delay(500).return(false); + return Promise.cast(false); } // this.print.printFiles(this.files); return _this.index.collectTargets().then(function (files) { - // this.print.printTests(files); + _this.print.printQueue(files); + return _this.runTests(files); }).then(function () { return !_this.suites.some(function (suite) { diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index 126b824b20..4e72001b88 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -123,6 +123,8 @@ module DT { this.print.printAllChanges(changes); return this.index.collectDiff(changes); }).then(() => { + this.print.printRemovals(this.index.removed); + this.print.printRelChanges(this.index.changed); return this.index.parseFiles(); }).then(() => { // this.print.printRefMap(this.index, this.index.refMap); @@ -131,11 +133,11 @@ module DT { this.print.printMissing(this.index, this.index.missing); this.print.printBoldDiv(); // bail - return Promise.delay(500).return(false); + return Promise.cast(false); } // this.print.printFiles(this.files); return this.index.collectTargets().then((files) => { - // this.print.printTests(files); + this.print.printQueue(files); return this.runTests(files); }).then(() => { diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index 9be9a79aa5..962d7b644d 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -180,9 +180,19 @@ module DT { }); } + public printQueue(files: File[]): void { + this.printDiv(); + this.printSubHeader('Queued for testing'); + this.printDiv(); + + files.forEach((file) => { + this.printLine(file.filePathWithName); + }); + + } public printFiles(files: File[]): void { this.printDiv(); - this.printSubHeader('Files:'); + this.printSubHeader('Files'); this.printDiv(); files.forEach((file) => { @@ -218,7 +228,17 @@ module DT { public printRelChanges(changeMap: FileDict): void { this.printDiv(); - this.printSubHeader('Relevant changes'); + this.printSubHeader('Interesting files'); + this.printDiv(); + + Object.keys(changeMap).sort().forEach((src) => { + this.printLine(changeMap[src].filePathWithName); + }); + } + + public printRemovals(changeMap: FileDict): void { + this.printDiv(); + this.printSubHeader('Removed files'); this.printDiv(); Object.keys(changeMap).sort().forEach((src) => { diff --git a/_infrastructure/tests/src/suite/testEval.ts b/_infrastructure/tests/src/suite/testEval.ts index 95044daa7b..410d8998ba 100644 --- a/_infrastructure/tests/src/suite/testEval.ts +++ b/_infrastructure/tests/src/suite/testEval.ts @@ -6,7 +6,7 @@ module DT { var Promise: typeof Promise = require('bluebird'); - var endTestDts = /-test.ts$/i; + var endTestDts = /-tests?.ts$/i; ///////////////////////////////// // Compile with *-tests.ts @@ -23,5 +23,4 @@ module DT { })); } } - } From a0e4fc304fbfd712fc0f56c64fb446fe737cb816 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Sat, 1 Mar 2014 22:40:41 +0100 Subject: [PATCH 12/13] parallelized tester added TestQueue class suites now have TestQueue instance that queues and runs Tests defaults to two (or more if more cores available) - Travis has '1.5 virtual cores' (weird) dropped index parameter from suite callback added index parameter to Reporter dropped double Suite::filterTargetFiles calls small fix in bluebird defs also print some system info in tester header --- _infrastructure/tests/runner.js | 134 +++++++++++++----- _infrastructure/tests/runner.ts | 59 +++++++- _infrastructure/tests/src/printer.ts | 19 ++- .../tests/src/reporter/reporter.ts | 19 +-- _infrastructure/tests/src/suite/suite.ts | 21 +-- _infrastructure/tests/src/suite/tscParams.ts | 8 +- _infrastructure/tests/src/tsc.ts | 17 ++- .../tests/typings/bluebird/bluebird.d.ts | 1 + 8 files changed, 210 insertions(+), 68 deletions(-) diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index e21bbd42be..0512c05e27 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -78,7 +78,8 @@ var DT; function Tsc() { } Tsc.run = function (tsfile, options) { - return Promise.attempt(function () { + var tscPath; + return new Promise.attempt(function () { options = options || {}; options.tscVersion = options.tscVersion || DT.DEFAULT_TSC_VERSION; if (typeof options.checkNoImplicitAny === 'undefined') { @@ -87,15 +88,21 @@ var DT; if (typeof options.useTscParams === 'undefined') { options.useTscParams = true; } - if (!fs.existsSync(tsfile)) { + return DT.fileExists(tsfile); + }).then(function (exists) { + if (!exists) { throw new Error(tsfile + ' not exists'); } - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!fs.existsSync(tscPath)) { + tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + return DT.fileExists(tscPath); + }).then(function (exists) { + if (!exists) { throw new Error(tscPath + ' is not exists'); } + return DT.fileExists(tsfile + '.tscparams'); + }).then(function (exists) { var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { + if (options.useTscParams && exists) { command += '@' + tsfile + '.tscparams'; } else if (options.checkNoImplicitAny) { command += '--noImplicitAny'; @@ -437,6 +444,8 @@ var DT; /// var DT; (function (DT) { + var os = require('os'); + ///////////////////////////////// // All the common things that we print are functions of this class ///////////////////////////////// @@ -462,11 +471,14 @@ var DT; Print.prototype.printChangeHeader = function () { this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped Diff Detector 0.1.0\33[0m \n'); + this.out(' \33[36m\33[1mDefinitelyTyped Diff Detector 0.1.0\33[0m \n'); this.out('=============================================================================\n'); }; - Print.prototype.printHeader = function () { + Print.prototype.printHeader = function (options) { + var totalMem = Math.round(os.totalmem() / 1024 / 1024) + ' mb'; + var freemem = Math.round(os.freemem() / 1024 / 1024) + ' mb'; + this.out('=============================================================================\n'); this.out(' \33[36m\33[1mDefinitelyTyped Test Runner 0.5.0\33[0m\n'); this.out('=============================================================================\n'); @@ -474,6 +486,10 @@ var DT; this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); + this.out(' \33[36m\33[1mTotal Memory :\33[0m ' + totalMem + '\n'); + this.out(' \33[36m\33[1mFree Memory :\33[0m ' + freemem + '\n'); + this.out(' \33[36m\33[1mCores :\33[0m ' + os.cpus().length + '\n'); + this.out(' \33[36m\33[1mConcurrent :\33[0m ' + options.concurrent + '\n'); }; Print.prototype.printSuiteHeader = function (title) { @@ -578,12 +594,12 @@ var DT; } }; - Print.prototype.printTestComplete = function (testResult, index) { + Print.prototype.printTestComplete = function (testResult) { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { - reporter.printPositiveCharacter(index, testResult); + reporter.printPositiveCharacter(testResult); } else { - reporter.printNegativeCharacter(index, testResult); + reporter.printNegativeCharacter(testResult); } }; @@ -709,17 +725,18 @@ var DT; var DefaultTestReporter = (function () { function DefaultTestReporter(print) { this.print = print; + this.index = 0; } - DefaultTestReporter.prototype.printPositiveCharacter = function (index, testResult) { + DefaultTestReporter.prototype.printPositiveCharacter = function (testResult) { this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); - - this.printBreakIfNeeded(index); + this.index++; + this.printBreakIfNeeded(this.index); }; - DefaultTestReporter.prototype.printNegativeCharacter = function (index, testResult) { + DefaultTestReporter.prototype.printNegativeCharacter = function (testResult) { this.print.out('x'); - - this.printBreakIfNeeded(index); + this.index++; + this.printBreakIfNeeded(this.index); }; DefaultTestReporter.prototype.printBreakIfNeeded = function (index) { @@ -751,6 +768,7 @@ var DT; this.timer = new DT.Timer(); this.testResults = []; this.printErrorCount = true; + this.queue = new DT.TestQueue(options.concurrent); } TestSuiteBase.prototype.filterTargetFiles = function (files) { throw new Error('please implement this method'); @@ -759,14 +777,15 @@ var DT; TestSuiteBase.prototype.start = function (targetFiles, testCallback) { var _this = this; this.timer.start(); + return this.filterTargetFiles(targetFiles).then(function (targetFiles) { - return Promise.reduce(targetFiles, function (count, targetFile) { + // tests get queued for multi-threading + return Promise.all(targetFiles.map(function (targetFile) { return _this.runTest(targetFile).then(function (result) { - testCallback(result, count + 1); - return count++; + testCallback(result); }); - }, 0); - }).then(function (count) { + })); + }).then(function () { _this.timer.end(); return _this; }); @@ -774,7 +793,9 @@ var DT; TestSuiteBase.prototype.runTest = function (targetFile) { var _this = this; - return new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion }).run().then(function (result) { + return this.queue.run(new DT.Test(this, targetFile, { + tscVersion: this.options.tscVersion + })).then(function (result) { _this.testResults.push(result); return result; }); @@ -885,10 +906,10 @@ var DT; this.printErrorCount = false; this.testReporter = { - printPositiveCharacter: function (index, testResult) { + printPositiveCharacter: function (testResult) { _this.print.clearCurrentLine().printTypingsWithoutTestName(testResult.targetFile.filePathWithName); }, - printNegativeCharacter: function (index, testResult) { + printNegativeCharacter: function (testResult) { } }; } @@ -904,11 +925,11 @@ var DT; var _this = this; this.print.clearCurrentLine().out(targetFile.filePathWithName); - return new DT.Test(this, targetFile, { + return this.queue.run(new DT.Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true - }).run().then(function (result) { + })).then(function (result) { _this.testResults.push(result); _this.print.clearCurrentLine(); return result; @@ -949,6 +970,7 @@ var DT; var Lazy = require('lazy.js'); var Promise = require('bluebird'); + var os = require('os'); var fs = require('fs'); var path = require('path'); var assert = require('assert'); @@ -985,6 +1007,55 @@ var DT; })(); DT.Test = Test; + ///////////////////////////////// + // Parallel execute Tests + ///////////////////////////////// + var TestQueue = (function () { + function TestQueue(concurrent) { + this.queue = []; + this.active = []; + this.concurrent = Math.max(1, concurrent); + } + // add to queue and return a promise + TestQueue.prototype.run = function (test) { + var _this = this; + var defer = Promise.defer(); + + // add a closure to queue + this.queue.push(function () { + // when activate, add test to active list + _this.active.push(test); + + // run it + var p = test.run(); + p.then(defer.resolve.bind(defer), defer.reject.bind(defer)); + p.finally(function () { + var i = _this.active.indexOf(test); + if (i > -1) { + _this.active.splice(i, 1); + } + _this.step(); + }); + }); + this.step(); + + // defer it + return defer.promise; + }; + + TestQueue.prototype.step = function () { + var _this = this; + // setTimeout to make it flush + setTimeout(function () { + while (_this.queue.length > 0 && _this.active.length < _this.concurrent) { + _this.queue.pop().call(null); + } + }, 1); + }; + return TestQueue; + })(); + DT.TestQueue = TestQueue; + ///////////////////////////////// // Test results ///////////////////////////////// @@ -1091,7 +1162,7 @@ var DT; ]); }).spread(function (syntaxFiles, testFiles) { _this.print.init(syntaxFiles.length, testFiles.length, files.length); - _this.print.printHeader(); + _this.print.printHeader(_this.options); if (_this.options.findNotRequiredTscparams) { _this.addSuite(new DT.FindNotRequiredTscparams(_this.options, _this.print)); @@ -1102,10 +1173,8 @@ var DT; _this.print.printSuiteHeader(suite.testSuiteName); - return suite.filterTargetFiles(files).then(function (targetFiles) { - return suite.start(targetFiles, function (testResult, index) { - _this.print.printTestComplete(testResult, index); - }); + return suite.start(files, function (testResult) { + _this.print.printTestComplete(testResult); }).then(function (suite) { _this.print.printSuiteComplete(suite); return count++; @@ -1186,12 +1255,13 @@ var DT; }); var tscVersionIndex = process.argv.indexOf('--tsc-version'); var tscVersion = DT.DEFAULT_TSC_VERSION; + var cpuCores = os.cpus().length; if (tscVersionIndex > -1) { tscVersion = process.argv[tscVersionIndex + 1]; } - var runner = new TestRunner(dtPath, { + concurrent: Math.max(cpuCores, 2), tscVersion: tscVersion, findNotRequiredTscparams: findNotRequiredTscparams }); diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index 4e72001b88..fafe2b1454 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -25,6 +25,7 @@ module DT { var Lazy: LazyJS.LazyStatic = require('lazy.js'); var Promise: typeof Promise = require('bluebird'); + var os = require('os'); var fs = require('fs'); var path = require('path'); var assert = require('assert'); @@ -56,6 +57,52 @@ module DT { } } + ///////////////////////////////// + // Parallel execute Tests + ///////////////////////////////// + export class TestQueue { + + private queue: Function[] = []; + private active: Test[] = []; + private concurrent: number; + + constructor(concurrent: number) { + this.concurrent = Math.max(1, concurrent); + } + + // add to queue and return a promise + run(test: Test): Promise { + var defer = Promise.defer(); + // add a closure to queue + this.queue.push(() => { + // when activate, add test to active list + this.active.push(test); + // run it + var p = test.run(); + p.then(defer.resolve.bind(defer), defer.reject.bind(defer)); + p.finally(() => { + var i = this.active.indexOf(test); + if (i > -1) { + this.active.splice(i, 1); + } + this.step(); + }); + }); + this.step(); + // defer it + return defer.promise; + } + + private step(): void { + // setTimeout to make it flush + setTimeout(() => { + while (this.queue.length > 0 && this.active.length < this.concurrent) { + this.queue.pop().call(null); + } + }, 1); + } + } + ///////////////////////////////// // Test results ///////////////////////////////// @@ -75,6 +122,7 @@ module DT { export interface ITestRunnerOptions { tscVersion:string; + concurrent?:number; findNotRequiredTscparams?:boolean; } @@ -166,7 +214,7 @@ module DT { ]); }).spread((syntaxFiles, testFiles) => { this.print.init(syntaxFiles.length, testFiles.length, files.length); - this.print.printHeader(); + this.print.printHeader(this.options); if (this.options.findNotRequiredTscparams) { this.addSuite(new FindNotRequiredTscparams(this.options, this.print)); @@ -177,10 +225,8 @@ module DT { this.print.printSuiteHeader(suite.testSuiteName); - return suite.filterTargetFiles(files).then((targetFiles) => { - return suite.start(targetFiles, (testResult, index) => { - this.print.printTestComplete(testResult, index); - }); + return suite.start(files, (testResult) => { + this.print.printTestComplete(testResult); }).then((suite) => { this.print.printSuiteComplete(suite); return count++; @@ -256,12 +302,13 @@ module DT { var findNotRequiredTscparams = process.argv.some(arg => arg == '--try-without-tscparams'); var tscVersionIndex = process.argv.indexOf('--tsc-version'); var tscVersion = DEFAULT_TSC_VERSION; + var cpuCores = os.cpus().length; if (tscVersionIndex > -1) { tscVersion = process.argv[tscVersionIndex + 1]; } - var runner = new TestRunner(dtPath, { + concurrent: Math.max(cpuCores, 2), tscVersion: tscVersion, findNotRequiredTscparams: findNotRequiredTscparams }); diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index 962d7b644d..565c4268a7 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -3,6 +3,8 @@ module DT { + var os = require('os'); + ///////////////////////////////// // All the common things that we print are functions of this class ///////////////////////////////// @@ -35,11 +37,14 @@ module DT { public printChangeHeader() { this.out('=============================================================================\n'); - this.out(' \33[36m\33[1mDefinitelyTyped Diff Detector 0.1.0\33[0m \n'); + this.out(' \33[36m\33[1mDefinitelyTyped Diff Detector 0.1.0\33[0m \n'); this.out('=============================================================================\n'); } - public printHeader() { + public printHeader(options: ITestRunnerOptions) { + var totalMem = Math.round(os.totalmem() / 1024 / 1024) + ' mb'; + var freemem = Math.round(os.freemem() / 1024 / 1024) + ' mb'; + this.out('=============================================================================\n'); this.out(' \33[36m\33[1mDefinitelyTyped Test Runner 0.5.0\33[0m\n'); this.out('=============================================================================\n'); @@ -47,6 +52,10 @@ module DT { this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n'); this.out(' \33[36m\33[1mTests :\33[0m ' + this.tests + '\n'); this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n'); + this.out(' \33[36m\33[1mTotal Memory :\33[0m ' + totalMem + '\n'); + this.out(' \33[36m\33[1mFree Memory :\33[0m ' + freemem + '\n'); + this.out(' \33[36m\33[1mCores :\33[0m ' + os.cpus().length + '\n'); + this.out(' \33[36m\33[1mConcurrent :\33[0m ' + options.concurrent + '\n'); } public printSuiteHeader(title: string) { @@ -150,13 +159,13 @@ module DT { } } - public printTestComplete(testResult: TestResult, index: number): void { + public printTestComplete(testResult: TestResult): void { var reporter = testResult.hostedBy.testReporter; if (testResult.success) { - reporter.printPositiveCharacter(index, testResult); + reporter.printPositiveCharacter(testResult); } else { - reporter.printNegativeCharacter(index, testResult); + reporter.printNegativeCharacter(testResult); } } diff --git a/_infrastructure/tests/src/reporter/reporter.ts b/_infrastructure/tests/src/reporter/reporter.ts index 783eae287a..736ac413cc 100644 --- a/_infrastructure/tests/src/reporter/reporter.ts +++ b/_infrastructure/tests/src/reporter/reporter.ts @@ -7,27 +7,30 @@ module DT { // for example, . and x ///////////////////////////////// export interface ITestReporter { - printPositiveCharacter(index: number, testResult: TestResult):void; - printNegativeCharacter(index: number, testResult: TestResult):void; + printPositiveCharacter(testResult: TestResult):void; + printNegativeCharacter(testResult: TestResult):void; } ///////////////////////////////// // Default test reporter ///////////////////////////////// export class DefaultTestReporter implements ITestReporter { + + index = 0; + constructor(public print: Print) { } - public printPositiveCharacter(index: number, testResult: TestResult) { + public printPositiveCharacter(testResult: TestResult) { this.print.out('\33[36m\33[1m' + '.' + '\33[0m'); - - this.printBreakIfNeeded(index); + this.index++; + this.printBreakIfNeeded(this.index); } - public printNegativeCharacter(index: number, testResult: TestResult) { + public printNegativeCharacter( testResult: TestResult) { this.print.out('x'); - - this.printBreakIfNeeded(index); + this.index++; + this.printBreakIfNeeded(this.index); } private printBreakIfNeeded(index: number) { diff --git a/_infrastructure/tests/src/suite/suite.ts b/_infrastructure/tests/src/suite/suite.ts index 4de10ce1d6..5909f87c63 100644 --- a/_infrastructure/tests/src/suite/suite.ts +++ b/_infrastructure/tests/src/suite/suite.ts @@ -32,31 +32,36 @@ module DT { testResults: TestResult[] = []; testReporter: ITestReporter; printErrorCount = true; + queue: TestQueue; constructor(public options: ITestRunnerOptions, public testSuiteName: string, public errorHeadline: string) { + this.queue = new TestQueue(options.concurrent); } public filterTargetFiles(files: File[]): Promise { throw new Error('please implement this method'); } - public start(targetFiles: File[], testCallback: (result: TestResult, index: number) => void): Promise { + public start(targetFiles: File[], testCallback: (result: TestResult) => void): Promise { this.timer.start(); + return this.filterTargetFiles(targetFiles).then((targetFiles) => { - return Promise.reduce(targetFiles, (count, targetFile) => { + // tests get queued for multi-threading + return Promise.all(targetFiles.map((targetFile) => { return this.runTest(targetFile).then((result) => { - testCallback(result, count + 1); - return count++; - }); - }, 0); - }).then((count: number) => { + testCallback(result); + }) + })); + }).then(() => { this.timer.end(); return this; }); } public runTest(targetFile: File): Promise { - return new Test(this, targetFile, {tscVersion: this.options.tscVersion}).run().then((result) => { + return this.queue.run(new Test(this, targetFile, { + tscVersion: this.options.tscVersion + })).then((result) => { this.testResults.push(result); return result; }); diff --git a/_infrastructure/tests/src/suite/tscParams.ts b/_infrastructure/tests/src/suite/tscParams.ts index bb064dd818..12e3f46017 100644 --- a/_infrastructure/tests/src/suite/tscParams.ts +++ b/_infrastructure/tests/src/suite/tscParams.ts @@ -19,12 +19,12 @@ module DT { super(options, 'Find not required .tscparams files', 'New arrival!'); this.testReporter = { - printPositiveCharacter: (index: number, testResult: TestResult) => { + printPositiveCharacter: (testResult: TestResult) => { this.print .clearCurrentLine() .printTypingsWithoutTestName(testResult.targetFile.filePathWithName); }, - printNegativeCharacter: (index: number, testResult: TestResult) => { + printNegativeCharacter: (testResult: TestResult) => { } } } @@ -40,11 +40,11 @@ module DT { public runTest(targetFile: File): Promise { this.print.clearCurrentLine().out(targetFile.filePathWithName); - return new Test(this, targetFile, { + return this.queue.run(new Test(this, targetFile, { tscVersion: this.options.tscVersion, useTscParams: false, checkNoImplicitAny: true - }).run().then((result: TestResult) => { + })).then((result) => { this.testResults.push(result); this.print.clearCurrentLine(); return result diff --git a/_infrastructure/tests/src/tsc.ts b/_infrastructure/tests/src/tsc.ts index 16287be532..9730a1394c 100644 --- a/_infrastructure/tests/src/tsc.ts +++ b/_infrastructure/tests/src/tsc.ts @@ -17,7 +17,8 @@ module DT { export class Tsc { public static run(tsfile: string, options: TscExecOptions): Promise { - return Promise.attempt(() => { + var tscPath; + return new Promise.attempt(() => { options = options || {}; options.tscVersion = options.tscVersion || DEFAULT_TSC_VERSION; if (typeof options.checkNoImplicitAny === 'undefined') { @@ -26,15 +27,21 @@ module DT { if (typeof options.useTscParams === 'undefined') { options.useTscParams = true; } - if (!fs.existsSync(tsfile)) { + return fileExists(tsfile); + }).then((exists) => { + if (!exists) { throw new Error(tsfile + ' not exists'); } - var tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; - if (!fs.existsSync(tscPath)) { + tscPath = './_infrastructure/tests/typescript/' + options.tscVersion + '/tsc.js'; + return fileExists(tscPath); + }).then((exists) => { + if (!exists) { throw new Error(tscPath + ' is not exists'); } + return fileExists(tsfile + '.tscparams'); + }).then((exists) => { var command = 'node ' + tscPath + ' --module commonjs '; - if (options.useTscParams && fs.existsSync(tsfile + '.tscparams')) { + if (options.useTscParams && exists) { command += '@' + tsfile + '.tscparams'; } else if (options.checkNoImplicitAny) { diff --git a/_infrastructure/tests/typings/bluebird/bluebird.d.ts b/_infrastructure/tests/typings/bluebird/bluebird.d.ts index 2e8de4ca80..45db9d86a3 100644 --- a/_infrastructure/tests/typings/bluebird/bluebird.d.ts +++ b/_infrastructure/tests/typings/bluebird/bluebird.d.ts @@ -75,6 +75,7 @@ declare class Promise implements Promise.Thenable { */ finally(handler: (value: R) => Promise.Thenable): Promise; finally(handler: (value: R) => R): Promise; + finally(handler: (value: R) => void): Promise; lastly(handler: (value: R) => Promise.Thenable): Promise; lastly(handler: (value: R) => R): Promise; From 08c355739769fd6fbcc46ef17fa556f2d8a796cc Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Sat, 1 Mar 2014 23:05:08 +0100 Subject: [PATCH 13/13] added tester cli options added optimist dependency sourced optimist definition changed cli options to use optimist expose verbose info printers as options added skipTests option added npm scripts (lazy) $ npm run test $ npm run dry $ npm run all $ npm run list $ npm run help bumped default tsc version to 0.9.7 tester disabled test filer tuned suite regexs tuned npm run commands added print helpers --- _infrastructure/tests/runner.js | 84 ++++++++++++++----- _infrastructure/tests/runner.ts | 71 ++++++++++++---- _infrastructure/tests/src/printer.ts | 11 ++- _infrastructure/tests/src/suite/suite.ts | 2 +- _infrastructure/tests/src/suite/syntax.ts | 2 +- _infrastructure/tests/src/suite/testEval.ts | 2 +- _infrastructure/tests/src/timer.ts | 3 +- .../tests/typings/optimist/optimist.d.ts | 48 +++++++++++ _infrastructure/tests/typings/tsd.d.ts | 1 + package.json | 30 ++++++- 10 files changed, 205 insertions(+), 49 deletions(-) create mode 100644 _infrastructure/tests/typings/optimist/optimist.d.ts diff --git a/_infrastructure/tests/runner.js b/_infrastructure/tests/runner.js index 0512c05e27..6495699236 100644 --- a/_infrastructure/tests/runner.js +++ b/_infrastructure/tests/runner.js @@ -127,10 +127,12 @@ var DT; var Timer = (function () { function Timer() { this.time = 0; + this.asString = ''; } Timer.prototype.start = function () { this.time = 0; this.startTime = this.now(); + this.asString = ''; }; Timer.prototype.now = function () { @@ -566,6 +568,10 @@ var DT; this.out(' \33[36m\33[1m' + file + '\33[0m\n'); }; + Print.prototype.printWarnCode = function (str) { + this.out(' \33[31m\33[1m<' + str.toLowerCase().replace(/ +/g, '-') + '>\33[0m\n'); + }; + Print.prototype.printLine = function (file) { this.out(file + '\n'); }; @@ -634,6 +640,12 @@ var DT; _this.printLine(file.filePathWithName); }); }; + + Print.prototype.printTestAll = function () { + this.printDiv(); + this.printSubHeader('Ignoring changes, testing all files'); + }; + Print.prototype.printFiles = function (files) { var _this = this; this.printDiv(); @@ -838,7 +850,7 @@ var DT; var Promise = require('bluebird'); - var endDts = /.ts$/i; + var endDts = /\w\.ts$/i; ///////////////////////////////// // .d.ts syntax inspection @@ -865,7 +877,7 @@ var DT; var Promise = require('bluebird'); - var endTestDts = /-tests?.ts$/i; + var endTestDts = /\w-tests?\.ts$/i; ///////////////////////////////// // Compile with *-tests.ts @@ -977,7 +989,7 @@ var DT; var tsExp = /\.ts$/; - DT.DEFAULT_TSC_VERSION = '0.9.1.1'; + DT.DEFAULT_TSC_VERSION = '0.9.7'; ///////////////////////////////// // Single test @@ -1119,7 +1131,9 @@ var DT; _this.print.printRelChanges(_this.index.changed); return _this.index.parseFiles(); }).then(function () { - // this.print.printRefMap(this.index, this.index.refMap); + if (_this.options.printRefMap) { + _this.print.printRefMap(_this.index, _this.index.refMap); + } if (Lazy(_this.index.missing).some(function (arr) { return arr.length > 0; })) { @@ -1129,12 +1143,17 @@ var DT; // bail return Promise.cast(false); } - - // this.print.printFiles(this.files); + if (_this.options.printFiles) { + _this.print.printFiles(_this.index.files); + } return _this.index.collectTargets().then(function (files) { - _this.print.printQueue(files); - - return _this.runTests(files); + if (_this.options.testChanges) { + _this.print.printQueue(files); + return _this.runTests(files); + } else { + _this.print.printTestAll(); + return _this.runTests(_this.index.files); + } }).then(function () { return !_this.suites.some(function (suite) { return suite.ngTests.length !== 0; @@ -1173,6 +1192,11 @@ var DT; _this.print.printSuiteHeader(suite.testSuiteName); + if (_this.options.skipTests) { + _this.print.printWarnCode('skipped test'); + return Promise.cast(count++); + } + return suite.start(files, function (testResult) { _this.print.printTestComplete(testResult); }).then(function (suite) { @@ -1249,23 +1273,39 @@ var DT; })(); DT.TestRunner = TestRunner; + var optimist = require('optimist')(process.argv); + optimist.default('try-without-tscparams', false); + optimist.default('single-thread', false); + optimist.default('tsc-version', DT.DEFAULT_TSC_VERSION); + + optimist.default('test-changes', false); + optimist.default('skip-tests', false); + optimist.default('print-files', false); + optimist.default('print-refmap', false); + + optimist.boolean('help'); + optimist.describe('help', 'print help'); + optimist.alias('h', 'help'); + + var argv = optimist.argv; + var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); - var findNotRequiredTscparams = process.argv.some(function (arg) { - return arg == '--try-without-tscparams'; - }); - var tscVersionIndex = process.argv.indexOf('--tsc-version'); - var tscVersion = DT.DEFAULT_TSC_VERSION; var cpuCores = os.cpus().length; - if (tscVersionIndex > -1) { - tscVersion = process.argv[tscVersionIndex + 1]; + if (argv.help) { + optimist.showHelp(); + process.exit(0); } - var runner = new TestRunner(dtPath, { - concurrent: Math.max(cpuCores, 2), - tscVersion: tscVersion, - findNotRequiredTscparams: findNotRequiredTscparams - }); - runner.run().then(function (success) { + + new TestRunner(dtPath, { + concurrent: argv['single-thread'] ? 1 : Math.max(cpuCores, 2), + tscVersion: argv['tsc-version'], + testChanges: argv['test-changes'], + skipTests: argv['skip-tests'], + printFiles: argv['print-files'], + printRefMap: argv['print-refmap'], + findNotRequiredTscparams: argv['try-without-tscparam'] + }).run().then(function (success) { if (!success) { process.exit(1); } diff --git a/_infrastructure/tests/runner.ts b/_infrastructure/tests/runner.ts index fafe2b1454..6e71d621a1 100644 --- a/_infrastructure/tests/runner.ts +++ b/_infrastructure/tests/runner.ts @@ -32,7 +32,7 @@ module DT { var tsExp = /\.ts$/; - export var DEFAULT_TSC_VERSION = '0.9.1.1'; + export var DEFAULT_TSC_VERSION = '0.9.7'; ///////////////////////////////// // Single test @@ -123,6 +123,10 @@ module DT { export interface ITestRunnerOptions { tscVersion:string; concurrent?:number; + testChanges?:boolean; + skipTests?:boolean; + printFiles?:boolean; + printRefMap?:boolean; findNotRequiredTscparams?:boolean; } @@ -175,19 +179,27 @@ module DT { this.print.printRelChanges(this.index.changed); return this.index.parseFiles(); }).then(() => { - // this.print.printRefMap(this.index, this.index.refMap); - + if (this.options.printRefMap) { + this.print.printRefMap(this.index, this.index.refMap); + } if (Lazy(this.index.missing).some((arr: any[]) => arr.length > 0)) { this.print.printMissing(this.index, this.index.missing); this.print.printBoldDiv(); // bail return Promise.cast(false); } - // this.print.printFiles(this.files); + if (this.options.printFiles) { + this.print.printFiles(this.index.files); + } return this.index.collectTargets().then((files) => { - this.print.printQueue(files); - - return this.runTests(files); + if (this.options.testChanges) { + this.print.printQueue(files); + return this.runTests(files); + } + else { + this.print.printTestAll(); + return this.runTests(this.index.files) + } }).then(() => { return !this.suites.some((suite) => { return suite.ngTests.length !== 0 @@ -225,6 +237,11 @@ module DT { this.print.printSuiteHeader(suite.testSuiteName); + if (this.options.skipTests) { + this.print.printWarnCode('skipped test'); + return Promise.cast(count++); + } + return suite.start(files, (testResult) => { this.print.printTestComplete(testResult); }).then((suite) => { @@ -298,21 +315,39 @@ module DT { } } + var optimist: Optimist = require('optimist')(process.argv); + optimist.default('try-without-tscparams', false); + optimist.default('single-thread', false); + optimist.default('tsc-version', DEFAULT_TSC_VERSION); + + optimist.default('test-changes', false); + optimist.default('skip-tests', false); + optimist.default('print-files', false); + optimist.default('print-refmap', false); + + optimist.boolean('help'); + optimist.describe('help', 'print help'); + optimist.alias('h', 'help'); + + var argv: any = optimist.argv; + var dtPath = path.resolve(path.dirname((module).filename), '..', '..'); - var findNotRequiredTscparams = process.argv.some(arg => arg == '--try-without-tscparams'); - var tscVersionIndex = process.argv.indexOf('--tsc-version'); - var tscVersion = DEFAULT_TSC_VERSION; var cpuCores = os.cpus().length; - if (tscVersionIndex > -1) { - tscVersion = process.argv[tscVersionIndex + 1]; + if (argv.help) { + optimist.showHelp(); + process.exit(0); } - var runner = new TestRunner(dtPath, { - concurrent: Math.max(cpuCores, 2), - tscVersion: tscVersion, - findNotRequiredTscparams: findNotRequiredTscparams - }); - runner.run().then((success) => { + + new TestRunner(dtPath, { + concurrent: argv['single-thread'] ? 1 : Math.max(cpuCores, 2), + tscVersion: argv['tsc-version'], + testChanges: argv['test-changes'], + skipTests: argv['skip-tests'], + printFiles: argv['print-files'], + printRefMap: argv['print-refmap'], + findNotRequiredTscparams: argv['try-without-tscparam'] + }).run().then((success) => { if (!success) { process.exit(1); } diff --git a/_infrastructure/tests/src/printer.ts b/_infrastructure/tests/src/printer.ts index 565c4268a7..9ee01f3c6d 100644 --- a/_infrastructure/tests/src/printer.ts +++ b/_infrastructure/tests/src/printer.ts @@ -132,6 +132,10 @@ module DT { this.out(' \33[36m\33[1m' + file + '\33[0m\n'); } + public printWarnCode(str: string) { + this.out(' \33[31m\33[1m<' + str.toLowerCase().replace(/ +/g, '-') + '>\33[0m\n'); + } + public printLine(file: string) { this.out(file + '\n'); } @@ -197,8 +201,13 @@ module DT { files.forEach((file) => { this.printLine(file.filePathWithName); }); - } + + public printTestAll(): void { + this.printDiv(); + this.printSubHeader('Ignoring changes, testing all files'); + } + public printFiles(files: File[]): void { this.printDiv(); this.printSubHeader('Files'); diff --git a/_infrastructure/tests/src/suite/suite.ts b/_infrastructure/tests/src/suite/suite.ts index 5909f87c63..8624da0d37 100644 --- a/_infrastructure/tests/src/suite/suite.ts +++ b/_infrastructure/tests/src/suite/suite.ts @@ -50,7 +50,7 @@ module DT { return Promise.all(targetFiles.map((targetFile) => { return this.runTest(targetFile).then((result) => { testCallback(result); - }) + }); })); }).then(() => { this.timer.end(); diff --git a/_infrastructure/tests/src/suite/syntax.ts b/_infrastructure/tests/src/suite/syntax.ts index 2e0d0b71aa..9211c3fdef 100644 --- a/_infrastructure/tests/src/suite/syntax.ts +++ b/_infrastructure/tests/src/suite/syntax.ts @@ -6,7 +6,7 @@ module DT { var Promise: typeof Promise = require('bluebird'); - var endDts = /.ts$/i; + var endDts = /\w\.ts$/i; ///////////////////////////////// // .d.ts syntax inspection diff --git a/_infrastructure/tests/src/suite/testEval.ts b/_infrastructure/tests/src/suite/testEval.ts index 410d8998ba..abc4d50935 100644 --- a/_infrastructure/tests/src/suite/testEval.ts +++ b/_infrastructure/tests/src/suite/testEval.ts @@ -6,7 +6,7 @@ module DT { var Promise: typeof Promise = require('bluebird'); - var endTestDts = /-tests?.ts$/i; + var endTestDts = /\w-tests?\.ts$/i; ///////////////////////////////// // Compile with *-tests.ts diff --git a/_infrastructure/tests/src/timer.ts b/_infrastructure/tests/src/timer.ts index 009527109a..0f74743471 100644 --- a/_infrastructure/tests/src/timer.ts +++ b/_infrastructure/tests/src/timer.ts @@ -11,11 +11,12 @@ module DT { export class Timer { startTime: number; time = 0; - asString: string; + asString: string = '' public start() { this.time = 0; this.startTime = this.now(); + this.asString = ''; } public now(): number { diff --git a/_infrastructure/tests/typings/optimist/optimist.d.ts b/_infrastructure/tests/typings/optimist/optimist.d.ts new file mode 100644 index 0000000000..d79dcfaf0e --- /dev/null +++ b/_infrastructure/tests/typings/optimist/optimist.d.ts @@ -0,0 +1,48 @@ +// https://github.com/substack/node-optimist +// sourced from https://github.com/soywiz/typescript-node-definitions/blob/master/optimist.d.ts +// rehacked by @Bartvds + +declare module Optimist { + export interface Argv { + _: string[]; + } + export interface Optimist { + default(name: string, value: any): Optimist; + default(args: any): Optimist; + + boolean(name: string): Optimist; + boolean(names: string[]): Optimist; + + string(name: string): Optimist; + string(names: string[]): Optimist; + + wrap(columns): Optimist; + + help(): Optimist; + showHelp(fn?: Function): Optimist; + + usage(message: string): Optimist; + + demand(key: string): Optimist; + demand(key: number): Optimist; + demand(key: string[]): Optimist; + + alias(key: string, alias: string): Optimist; + + describe(key: string, desc: string): Optimist; + + options(key: string, opt: any): Optimist; + + check(fn: Function); + + parse(args: string[]): Optimist; + + argv: Argv; + } +} +interface Optimist extends Optimist.Optimist { + (args: string[]): Optimist.Optimist; +} +declare module 'optimist' { + export = Optimist; +} diff --git a/_infrastructure/tests/typings/tsd.d.ts b/_infrastructure/tests/typings/tsd.d.ts index 717e8e49a3..371833438e 100644 --- a/_infrastructure/tests/typings/tsd.d.ts +++ b/_infrastructure/tests/typings/tsd.d.ts @@ -1,3 +1,4 @@ /// /// /// +/// diff --git a/package.json b/package.json index a18c4fcd70..aded689516 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,37 @@ { "private": true, "name": "DefinitelyTyped", - "version": "0.0.0", + "version": "0.0.1", + "homepage": "https://github.com/borisyankov/DefinitelyTyped", + "repository": { + "type": "git", + "url": "https://github.com/borisyankov/DefinitelyTyped.git" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/borisyankov/DefinitelyTyped/blob/master/LICENSE" + } + ], + "bugs": { + "url": "https://github.com/borisyankov/DefinitelyTyped/issues" + }, + "engines": { + "node": ">= 0.10.0" + }, "scripts": { - "test": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7" + "test": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7 --test-changes", + "all": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7", + "dry": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7 --skip-tests --test-changes", + "list": "node ./_infrastructure/tests/runner.js --tsc-version 0.9.7 --skip-tests --print-files --print-refmap", + "help": "node ./_infrastructure/tests/runner.js -h" }, "dependencies": { + "source-map-support": "~0.2.5", "git-wrapper": "~0.1.1", "glob": "~3.2.9", - "source-map-support": "~0.2.5", "bluebird": "~1.0.7", - "lazy.js": "~0.3.2" + "lazy.js": "~0.3.2", + "optimist": "~0.6.1" } }