From e9ed87d7d2facb8829763bc7aabeece6636635c3 Mon Sep 17 00:00:00 2001 From: Boris Yankov Date: Wed, 19 Jun 2013 01:24:28 +0300 Subject: [PATCH] Fix test runner files --- _infrastructure/tests/src/exec.js | 26 +++-- _infrastructure/tests/src/io.js | 142 ++++++++++++++++------------ _infrastructure/tests/src/io.ts | 34 ++++--- _infrastructure/tests/testRunner.js | 2 +- 4 files changed, 114 insertions(+), 90 deletions(-) diff --git a/_infrastructure/tests/src/exec.js b/_infrastructure/tests/src/exec.js index b29e4c0175..8c18ab42d8 100644 --- a/_infrastructure/tests/src/exec.js +++ b/_infrastructure/tests/src/exec.js @@ -1,12 +1,14 @@ -var ExecResult = (function () { +var ExecResult = (function () { function ExecResult() { this.stdout = ""; this.stderr = ""; } return ExecResult; })(); + var WindowsScriptHostExec = (function () { - function WindowsScriptHostExec() { } + function WindowsScriptHostExec() { + } WindowsScriptHostExec.prototype.exec = function (filename, cmdLineArgs, handleResult) { var result = new ExecResult(); var shell = new ActiveXObject('WScript.Shell'); @@ -18,26 +20,31 @@ var WindowsScriptHostExec = (function () { handleResult(result); return; } - while(process.Status != 0) { + + while (process.Status != 0) { } + result.exitCode = process.ExitCode; - if(!process.StdOut.AtEndOfStream) { + if (!process.StdOut.AtEndOfStream) result.stdout = process.StdOut.ReadAll(); - } - if(!process.StdErr.AtEndOfStream) { + if (!process.StdErr.AtEndOfStream) result.stderr = process.StdErr.ReadAll(); - } + handleResult(result); }; return WindowsScriptHostExec; })(); + var NodeExec = (function () { - function NodeExec() { } + 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; @@ -47,9 +54,10 @@ var NodeExec = (function () { }; return NodeExec; })(); + var Exec = (function () { var global = Function("return this;").call(null); - if(typeof global.ActiveXObject !== "undefined") { + if (typeof global.ActiveXObject !== "undefined") { return new WindowsScriptHostExec(); } else { return new NodeExec(); diff --git a/_infrastructure/tests/src/io.js b/_infrastructure/tests/src/io.js index 591fc94431..772e97c024 100644 --- a/_infrastructure/tests/src/io.js +++ b/_infrastructure/tests/src/io.js @@ -1,15 +1,17 @@ -var IOUtils; +var IOUtils; (function (IOUtils) { function createDirectoryStructure(ioHost, dirName) { - if(ioHost.directoryExists(dirName)) { + if (ioHost.directoryExists(dirName)) { return; } + var parentDirectory = ioHost.dirName(dirName); - if(parentDirectory != "") { + if (parentDirectory != "") { createDirectoryStructure(ioHost, parentDirectory); } ioHost.createDirectory(dirName); } + function createFileAndFolderStructure(ioHost, fileName, useUTF8) { var path = ioHost.resolvePath(fileName); var dirName = ioHost.dirName(path); @@ -17,9 +19,10 @@ var IOUtils; return ioHost.createFile(path, useUTF8); } IOUtils.createFileAndFolderStructure = createFileAndFolderStructure; + function throwIOError(message, error) { var errorMessage = message; - if(error && error.message) { + if (error && error.message) { errorMessage += (" " + error.message); } throw new Error(errorMessage); @@ -31,20 +34,24 @@ var IO = (function () { function getWindowsScriptHostIO() { var fso = new ActiveXObject("Scripting.FileSystemObject"); var streamObjectPool = []; + function getStreamObject() { - if(streamObjectPool.length > 0) { + 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++) { + for (var i = 0; i < WScript.Arguments.length; i++) { args[i] = WScript.Arguments.Item(i); } + return { readFile: function (path) { try { @@ -55,11 +62,12 @@ var IO = (function () { streamObj.LoadFromFile(path); var bomChar = streamObj.ReadText(2); streamObj.Position = 0; - if((bomChar.charCodeAt(0) == 254 && bomChar.charCodeAt(1) == 255) || (bomChar.charCodeAt(0) == 255 && bomChar.charCodeAt(1) == 254)) { + 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) == 239 && bomChar.charCodeAt(1) == 187) { + } else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) { streamObj.Charset = 'utf-8'; } + var str = streamObj.ReadText(-1); streamObj.Close(); releaseStreamObject(streamObj); @@ -84,19 +92,18 @@ var IO = (function () { }, findFile: function (rootPath, partialFilePath) { var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath; - while(true) { - if(fso.FileExists(path)) { + + while (true) { + if (fso.FileExists(path)) { try { var content = this.readFile(path); - return { - content: content, - path: path - }; + return { content: content, path: path }; } catch (err) { } } else { rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath)); - if(rootPath == "") { + + if (rootPath == "") { return null; } else { path = fso.BuildPath(rootPath, partialFilePath); @@ -106,7 +113,7 @@ var IO = (function () { }, deleteFile: function (path) { try { - if(fso.FileExists(path)) { + if (fso.FileExists(path)) { fso.DeleteFile(path, true); } } catch (e) { @@ -130,8 +137,8 @@ var IO = (function () { streamObj.SaveToFile(path, 2); } catch (saveError) { IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError); - }finally { - if(streamObj.State != 0) { + } finally { + if (streamObj.State != 0) { streamObj.Close(); } releaseStreamObject(streamObj); @@ -147,7 +154,7 @@ var IO = (function () { }, createDirectory: function (path) { try { - if(!this.directoryExists(path)) { + if (!this.directoryExists(path)) { fso.CreateFolder(path); } } catch (e) { @@ -155,27 +162,33 @@ var IO = (function () { } }, dir: function (path, spec, options) { - options = options || { - }; + options = options || {}; function filesInFolder(folder, root) { var paths = []; var fc; - if(options.recursive) { + + 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()) { - if(!spec || fc.item().Name.match(spec)) { + + 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) { @@ -208,19 +221,21 @@ var IO = (function () { }; } ; + 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 254: - if(buffer[1] == 255) { + switch (buffer[0]) { + case 0xFE: + if (buffer[1] == 0xFF) { var i = 0; - while((i + 1) < buffer.length) { + while ((i + 1) < buffer.length) { var temp = buffer[i]; buffer[i] = buffer[i + 1]; buffer[i + 1] = temp; @@ -229,16 +244,17 @@ var IO = (function () { return buffer.toString("ucs2", 2); } break; - case 255: - if(buffer[1] == 254) { + case 0xFF: + if (buffer[1] == 0xFE) { return buffer.toString("ucs2", 2); } break; - case 239: - if(buffer[1] == 187) { + case 0xEF: + if (buffer[1] == 0xBB) { return buffer.toString("utf8", 3); } } + return buffer.toString(); } catch (e) { IOUtils.throwIOError("Error reading file \"" + file + "\".", e); @@ -258,16 +274,18 @@ var IO = (function () { createFile: function (path, useUTF8) { function mkdirRecursiveSync(path) { var stats = _fs.statSync(path); - if(stats.isFile()) { + if (stats.isFile()) { IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null); - } else if(stats.isDirectory()) { + } else if (stats.isDirectory()) { return; } else { mkdirRecursiveSync(_path.dirname(path)); - _fs.mkdirSync(path, 775); + _fs.mkdirSync(path, 0775); } } + mkdirRecursiveSync(_path.dirname(path)); + try { var fd = _fs.openSync(path, 'w'); } catch (e) { @@ -287,26 +305,29 @@ var IO = (function () { }; }, dir: function dir(path, spec, options) { - options = options || { - }; + options = options || {}; + function filesInFolder(folder) { var paths = []; + var files = _fs.readdirSync(folder); - for(var i = 0; i < files.length; i++) { + for (var i = 0; i < files.length; i++) { var stat = _fs.statSync(folder + "/" + files[i]); - if(options.recursive && stat.isDirectory()) { + if (options.recursive && stat.isDirectory()) { paths = paths.concat(filesInFolder(folder + "/" + files[i])); - } else if(stat.isFile() && (!spec || files[i].match(spec))) { + } else if (stat.isFile() && (!spec || files[i].match(spec))) { paths.push(folder + "/" + files[i]); } } + return paths; } + return filesInFolder(path); }, createDirectory: function (path) { try { - if(!this.directoryExists(path)) { + if (!this.directoryExists(path)) { _fs.mkdirSync(path); } } catch (e) { @@ -324,19 +345,18 @@ var IO = (function () { }, findFile: function (rootPath, partialFilePath) { var path = rootPath + "/" + partialFilePath; - while(true) { - if(_fs.existsSync(path)) { + + while (true) { + if (_fs.existsSync(path)) { try { var content = this.readFile(path); - return { - content: content, - path: path - }; + return { content: content, path: path }; } catch (err) { } } else { var parentPath = _path.resolve(rootPath, ".."); - if(rootPath === parentPath) { + + if (rootPath === parentPath) { return null; } else { rootPath = parentPath; @@ -375,13 +395,15 @@ var IO = (function () { watchFile: function (filename, callback) { var firstRun = true; var processingChange = false; + var fileChanged = function (curr, prev) { - if(!firstRun) { - if(curr.mtime < prev.mtime) { + if (!firstRun) { + if (curr.mtime < prev.mtime) { return; } + _fs.unwatchFile(filename, fileChanged); - if(!processingChange) { + if (!processingChange) { processingChange = true; callback(filename); setTimeout(function () { @@ -390,11 +412,9 @@ var IO = (function () { } } firstRun = false; - _fs.watchFile(filename, { - persistent: true, - interval: 500 - }, fileChanged); + _fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged); }; + fileChanged(); return { filename: filename, @@ -415,11 +435,9 @@ var IO = (function () { }; } ; - if(typeof ActiveXObject === "function") { - return getWindowsScriptHostIO(); - } else if(typeof require === "function") { - return getNodeIO(); - } else { + + if (typeof ActiveXObject === "function") + return getWindowsScriptHostIO(); else if (typeof require === "function") + return getNodeIO(); else return null; - } })(); diff --git a/_infrastructure/tests/src/io.ts b/_infrastructure/tests/src/io.ts index 641ace09de..3c68154a15 100644 --- a/_infrastructure/tests/src/io.ts +++ b/_infrastructure/tests/src/io.ts @@ -25,11 +25,11 @@ interface IFileWatcher { interface IIO { readFile(path: string): string; writeFile(path: string, contents: string): void; - createFile(path: string, useUTF8?: bool): ITextWriter; + createFile(path: string, useUTF8?: boolean): ITextWriter; deleteFile(path: string): void; - dir(path: string, re?: RegExp, options?: { recursive?: bool; deep?: number; }): string[]; - fileExists(path: string): bool; - directoryExists(path: string): bool; + dir(path: string, re?: RegExp, options?: { recursive?: boolean; }): string[]; + fileExists(path: string): boolean; + directoryExists(path: string): boolean; createDirectory(path: string): void; resolvePath(path: string): string; dirName(path: string): string; @@ -60,7 +60,7 @@ module IOUtils { } // Creates a file including its directory structure if not already present - export function createFileAndFolderStructure(ioHost: IIO, fileName: string, useUTF8?: bool) { + export function createFileAndFolderStructure(ioHost: IIO, fileName: string, useUTF8?: boolean) { var path = ioHost.resolvePath(fileName); var dirName = ioHost.dirName(path); createDirectoryStructure(ioHost, dirName); @@ -78,7 +78,7 @@ module IOUtils { // Declare dependencies needed for all supported hosts declare class Enumerator { - public atEnd(): bool; + public atEnd(): boolean; public moveNext(); public item(): any; constructor (o: any); @@ -160,7 +160,7 @@ var IO = (function() { file.Close(); }, - fileExists: function(path: string): bool { + fileExists: function(path: string): boolean { return fso.FileExists(path); }, @@ -236,7 +236,7 @@ var IO = (function() { }, directoryExists: function(path) { - return fso.FolderExists(path); + return fso.FolderExists(path); }, createDirectory: function(path) { @@ -250,7 +250,7 @@ var IO = (function() { }, dir: function(path, spec?, options?) { - options = options || <{ recursive?: bool; deep?: number; }>{}; + options = options || <{ recursive?: boolean; }>{}; function filesInFolder(folder, root): string[]{ var paths = []; var fc: Enumerator; @@ -302,7 +302,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) { @@ -365,7 +365,7 @@ var IO = (function() { IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e); } }, - fileExists: function(path): bool { + fileExists: function(path): boolean { return _fs.existsSync(path); }, createFile: function(path, useUTF8?) { @@ -395,18 +395,16 @@ var IO = (function() { }; }, dir: function dir(path, spec?, options?) { - options = options || <{ recursive?: bool; deep?: number; }>{}; + options = options || <{ recursive?: boolean; }>{}; - function filesInFolder(folder: string, deep?: number): string[]{ + function filesInFolder(folder: string): 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)); - } + paths = paths.concat(filesInFolder(folder + "/" + files[i])); } else if (stat.isFile() && (!spec || files[i].match(spec))) { paths.push(folder + "/" + files[i]); } @@ -415,7 +413,7 @@ var IO = (function() { return paths; } - return filesInFolder(path, 0); + return filesInFolder(path); }, createDirectory: function(path: string): void { try { @@ -427,7 +425,7 @@ var IO = (function() { } }, - directoryExists: function(path: string): bool { + directoryExists: function(path: string): boolean { return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory(); }, resolvePath: function(path: string): string { diff --git a/_infrastructure/tests/testRunner.js b/_infrastructure/tests/testRunner.js index 14040f24b7..a2f54dd5fe 100644 --- a/_infrastructure/tests/testRunner.js +++ b/_infrastructure/tests/testRunner.js @@ -485,7 +485,7 @@ var IO = (function () { var cfg = { root: '.', pattern: /.\-tests\.ts/g, - tsc: 'node ./_infrastructure/tests/typescript_0.8.3/tsc.js ', + tsc: 'node ./_infrastructure/tests/typescript/tsc.js ', exclude: { '.git': true, '.gitignore': true,