@@ -5,6 +5,7 @@ var os = require("os");
55var path = require ( "path" ) ;
66var child_process = require ( "child_process" ) ;
77var Linter = require ( "tslint" ) ;
8+ var readline = require ( "readline" ) ;
89
910// Variables
1011var compilerDirectory = "src/compiler/" ;
@@ -673,9 +674,14 @@ function cleanTestDirs() {
673674}
674675
675676// used to pass data from jake command line directly to run.js
676- function writeTestConfigFile ( tests , light , testConfigFile ) {
677+ function writeTestConfigFile ( testConfigFile , tests , light , stackTraceLimit ) {
677678 console . log ( 'Running test(s): ' + tests ) ;
678- var testConfigContents = JSON . stringify ( { test : [ tests ] , light : light } ) ;
679+ var testConfig = { test : [ tests ] , light : light } ;
680+ if ( / ^ ( \d + | f u l l ) $ / . test ( stackTraceLimit ) ) {
681+ testConfig . stackTraceLimit = stackTraceLimit ;
682+ }
683+
684+ var testConfigContents = JSON . stringify ( testConfig ) ;
679685 fs . writeFileSync ( 'test.config' , testConfigContents ) ;
680686}
681687
@@ -685,6 +691,124 @@ function deleteTemporaryProjectOutput() {
685691 }
686692}
687693
694+ function runTestsAndWriteOutput ( file ) {
695+ cleanTestDirs ( ) ;
696+ var tests = process . env . test || process . env . tests || process . env . t ;
697+ var light = process . env . light || false ;
698+ var testConfigFile = 'test.config' ;
699+ if ( fs . existsSync ( testConfigFile ) ) {
700+ fs . unlinkSync ( testConfigFile ) ;
701+ }
702+
703+ if ( tests || light ) {
704+ writeTestConfigFile ( testConfigFile , tests , light , 10 ) ;
705+ }
706+
707+ if ( tests && tests . toLocaleLowerCase ( ) === "rwc" ) {
708+ testTimeout = 100000 ;
709+ }
710+
711+ var args = [ ] ;
712+ args . push ( "-R" , "TAP" ) ;
713+ args . push ( "--no-colors" ) ;
714+ args . push ( "-t" , testTimeout ) ;
715+ if ( tests ) {
716+ args . push ( "-g" , '"' + tests + '"' ) ;
717+ }
718+ args . push ( run ) ;
719+
720+ var cmd = "mocha " + args . join ( " " ) ;
721+ console . log ( cmd ) ;
722+ var ex = jake . createExec ( [ cmd ] , { windowsVerbatimArguments : true } ) ;
723+ var out = fs . createWriteStream ( file ) ;
724+ var tapRange = / ^ ( \d + ) \. \. ( \d + ) (?: $ | \r \n ? | \n ) / ;
725+ var tapOk = / ^ o k \s / ;
726+ var tapNotOk = / ^ n o t \s o k / ;
727+ var tapComment = / ^ # / ;
728+ var typeError = / ^ \s + T y p e E r r o r : / ;
729+ var progress = new ProgressBar ( "Running tests..." ) ;
730+ var expectedTestCount = 0 ;
731+ var testCount = 0 ;
732+ var failureCount = 0 ;
733+ var successCount = 0 ;
734+ var comments = [ ] ;
735+ var typeErrorCount = 0 ;
736+
737+ ex . addListener ( "stdout" , function ( output ) {
738+ var m = tapRange . exec ( output ) ;
739+ if ( m ) {
740+ expectedTestCount = parseInt ( m [ 2 ] ) ;
741+ return ;
742+ }
743+
744+ out . write ( output ) ;
745+
746+ if ( tapOk . test ( output ) ) {
747+ successCount ++ ;
748+ }
749+ else if ( tapNotOk . test ( output ) ) {
750+ failureCount ++ ;
751+ }
752+ else {
753+ if ( tapComment . test ( output ) ) {
754+ comments . push ( output . toString ( ) . trim ( ) ) ;
755+ }
756+ else if ( typeError . test ( output ) ) {
757+ typeErrorCount ++ ;
758+ }
759+ return ;
760+ }
761+
762+ testCount ++ ;
763+
764+ var percentComplete = testCount * 100 / expectedTestCount ;
765+ updateProgress ( percentComplete ) ;
766+ } ) ;
767+
768+ function updateProgress ( percentComplete ) {
769+ progress . update ( percentComplete ,
770+ /*foregroundColor*/ failureCount > 0
771+ ? "red"
772+ : successCount === expectedTestCount
773+ ? "green"
774+ : "cyan" ,
775+ /*backgroundColor*/ "gray"
776+ ) ;
777+ }
778+
779+ ex . addListener ( "stderr" , function ( output ) {
780+ progress . hide ( ) ;
781+ process . stderr . write ( output . toString ( ) . trim ( ) + os . EOL ) ;
782+ progress . show ( ) ;
783+ } ) ;
784+ ex . addListener ( "cmdEnd" , function ( ) {
785+ if ( progress . visible ) {
786+ updateProgress ( 100 ) ;
787+ process . stdout . write ( "done." + os . EOL ) ;
788+ }
789+
790+ console . log ( comments . join ( os . EOL ) ) ;
791+ deleteTemporaryProjectOutput ( ) ;
792+ complete ( ) ;
793+ } ) ;
794+ ex . addListener ( "error" , function ( e , status ) {
795+ if ( progress . visible ) {
796+ updateProgress ( 100 ) ;
797+ process . stdout . write ( "done." + os . EOL ) ;
798+ }
799+
800+ console . log ( comments . join ( os . EOL ) ) ;
801+
802+ if ( typeErrorCount ) {
803+ console . log ( "# type errors: %s" , typeErrorCount ) ;
804+ }
805+
806+ deleteTemporaryProjectOutput ( ) ;
807+ fail ( "Process exited with code " + status ) ;
808+ } ) ;
809+ ex . run ( ) ;
810+ }
811+
688812function runConsoleTests ( defaultReporter , defaultSubsets ) {
689813 cleanTestDirs ( ) ;
690814 var debug = process . env . debug || process . env . d ;
@@ -696,7 +820,7 @@ function runConsoleTests(defaultReporter, defaultSubsets) {
696820 }
697821
698822 if ( tests || light ) {
699- writeTestConfigFile ( tests , light , testConfigFile ) ;
823+ writeTestConfigFile ( testConfigFile , tests , light ) ;
700824 }
701825
702826 if ( tests && tests . toLocaleLowerCase ( ) === "rwc" ) {
@@ -749,6 +873,10 @@ task("runtests", ["build-rules", "tests", builtLocalDirectory], function() {
749873 runConsoleTests ( 'mocha-fivemat-progress-reporter' , [ ] ) ;
750874} , { async : true } ) ;
751875
876+ task ( "runtests-file" , [ "build-rules" , "tests" , builtLocalDirectory ] , function ( ) {
877+ runTestsAndWriteOutput ( "tests/baselines/local/testresults.tap" ) ;
878+ } , { async : true } ) ;
879+
752880desc ( "Generates code coverage data via instanbul" ) ;
753881task ( "generate-code-coverage" , [ "tests" , builtLocalDirectory ] , function ( ) {
754882 var cmd = 'istanbul cover node_modules/mocha/bin/_mocha -- -R min -t ' + testTimeout + ' ' + run ;
@@ -780,7 +908,7 @@ task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFi
780908 fs . unlinkSync ( testConfigFile ) ;
781909 }
782910 if ( tests || light ) {
783- writeTestConfigFile ( tests , light , testConfigFile ) ;
911+ writeTestConfigFile ( testConfigFile , tests , light ) ;
784912 }
785913
786914 tests = tests ? tests : '' ;
@@ -1031,3 +1159,85 @@ task("lint-server", ["build-rules"], function() {
10311159 lintWatchFile ( lintTargets [ i ] ) ;
10321160 }
10331161} ) ;
1162+
1163+ function ProgressBar ( title ) {
1164+ this . title = title ;
1165+ }
1166+ ProgressBar . prototype = {
1167+ progressChars : [ "\u0020" , "\u2591" , "\u2592" , "\u2593" , "\u2588" ] ,
1168+ colors : {
1169+ foreground : {
1170+ black : "\u001b[90m" ,
1171+ red : "\u001b[91m" ,
1172+ green : "\u001b[92m" ,
1173+ yellow : "\u001b[93m" ,
1174+ blue : "\u001b[94m" ,
1175+ magenta : "\u001b[95m" ,
1176+ cyan : "\u001b[96m" ,
1177+ white : "\u001b[97m" ,
1178+ gray : "\u001b[37m"
1179+ } ,
1180+ background : {
1181+ black : "\u001b[40m" ,
1182+ red : "\u001b[41m" ,
1183+ green : "\u001b[42m" ,
1184+ yellow : "\u001b[43m" ,
1185+ blue : "\u001b[44m" ,
1186+ magenta : "\u001b[45m" ,
1187+ cyan : "\u001b[46m" ,
1188+ white : "\u001b[47m" ,
1189+ gray : "\u001b[100m"
1190+ } ,
1191+ reset : "\u001b[0m"
1192+ } ,
1193+ update : function ( percentComplete , foregroundColor , backgroundColor ) {
1194+ var progress = "" ;
1195+ for ( var i = 0 ; i < 100 ; i += 4 ) {
1196+ progress += this . progressChars [ Math . floor ( Math . max ( 0 , Math . min ( 4 , percentComplete - i ) ) ) ] ;
1197+ }
1198+
1199+ foregroundColor = foregroundColor && this . colors . foreground [ foregroundColor ] ;
1200+ backgroundColor = backgroundColor && this . colors . background [ backgroundColor ] ;
1201+ if ( foregroundColor || backgroundColor ) {
1202+ if ( foregroundColor ) {
1203+ progress = foregroundColor + progress ;
1204+ }
1205+ if ( backgroundColor ) {
1206+ progress = backgroundColor + progress ;
1207+ }
1208+
1209+ progress += this . colors . reset ;
1210+ }
1211+
1212+ if ( this . _lastProgress !== progress || ! this . visible ) {
1213+ this . _print ( progress ) ;
1214+ }
1215+ } ,
1216+ hide : function ( ) {
1217+ if ( this . visible ) {
1218+ this . _savedProgress = this . _lastProgress ;
1219+ this . clear ( ) ;
1220+ }
1221+ } ,
1222+ show : function ( ) {
1223+ if ( this . _savedProgress && ! this . visible ) {
1224+ this . _print ( this . _savedProgress ) ;
1225+ this . _savedProgress = undefined ;
1226+ }
1227+ } ,
1228+ clear : function ( ) {
1229+ if ( this . _lastProgress ) {
1230+ readline . moveCursor ( process . stdout , - process . stdout . columns , 0 ) ;
1231+ readline . clearLine ( process . stdout , 1 ) ;
1232+ this . _lastProgress = undefined ;
1233+ this . visible = false ;
1234+ }
1235+ } ,
1236+ _print : function ( progress ) {
1237+ readline . moveCursor ( process . stdout , - process . stdout . columns , 0 ) ;
1238+ process . stdout . write ( this . title ? progress + " " + this . title : progress ) ;
1239+ readline . clearLine ( process . stdout , 1 ) ;
1240+ this . _lastProgress = progress ;
1241+ this . visible = true ;
1242+ }
1243+ } ;
0 commit comments