@@ -245,7 +245,7 @@ var librarySourceMap = [
245245 { target : "lib.es2015.d.ts" , sources : [ "header.d.ts" , "es2015.d.ts" ] } ,
246246 { target : "lib.es2016.d.ts" , sources : [ "header.d.ts" , "es2016.d.ts" ] } ,
247247 { target : "lib.es2017.d.ts" , sources : [ "header.d.ts" , "es2017.d.ts" ] } ,
248-
248+
249249 // JavaScript + all host library
250250 { target : "lib.d.ts" , sources : [ "header.d.ts" , "es5.d.ts" ] . concat ( hostsLibrarySources ) } ,
251251 { target : "lib.es6.d.ts" , sources : [ "header.d.ts" , "es5.d.ts" ] . concat ( es2015LibrarySources , hostsLibrarySources , "dom.iterable.d.ts" ) }
@@ -722,24 +722,32 @@ function cleanTestDirs() {
722722}
723723
724724// used to pass data from jake command line directly to run.js
725- function writeTestConfigFile ( testConfigFile , tests , light , stackTraceLimit ) {
725+ function writeTestConfigFile ( tests , light , taskConfigFolder , workerCount , stackTraceLimit ) {
726726 var testConfig ;
727727 if ( tests ) {
728- console . log ( 'Running test(s): ' + tests ) ;
729- ( testConfig || ( testConfig = { } ) ) . tests = [ tests ] ;
728+ ( testConfig || ( testConfig = { } ) ) . test = [ tests ] ;
730729 }
731730
732731 if ( light ) {
733732 ( testConfig || ( testConfig = { } ) ) . light = light ;
734733 }
735734
735+ if ( workerCount ) {
736+ ( testConfig || ( testConfig = { } ) ) . workerCount = workerCount ;
737+ }
738+
739+ if ( taskConfigFolder ) {
740+ ( testConfig || ( testConfig = { } ) ) . taskConfigFolder = taskConfigFolder ;
741+ }
742+
736743 if ( / ^ ( \d + | f u l l ) $ / . test ( stackTraceLimit ) ) {
737744 ( testConfig || ( testConfig = { } ) ) . stackTraceLimit = stackTraceLimit ;
738745 }
739746
740747 if ( testConfig ) {
741748 var testConfigContents = JSON . stringify ( testConfig ) ;
742- fs . writeFileSync ( testConfigFile , testConfigContents ) ;
749+ console . log ( 'Running tests with config: ' + testConfigContents ) ;
750+ fs . writeFileSync ( 'test.config' , testConfigContents ) ;
743751 }
744752}
745753
@@ -874,7 +882,7 @@ function runTestsAndWriteOutput(file) {
874882 } ) ;
875883}
876884
877- function runConsoleTests ( defaultReporter , defaultSubsets , dirty ) {
885+ function runConsoleTests ( defaultReporter , runInParallel , dirty ) {
878886 if ( ! dirty ) {
879887 cleanTestDirs ( ) ;
880888 }
@@ -887,9 +895,22 @@ function runConsoleTests(defaultReporter, defaultSubsets, dirty) {
887895 if ( fs . existsSync ( testConfigFile ) ) {
888896 fs . unlinkSync ( testConfigFile ) ;
889897 }
898+ var workerCount , taskConfigsFolder ;
899+ if ( runInParallel ) {
900+ // generate name to store task configuration files
901+ var prefix = os . tmpdir ( ) + "/ts-tests" ;
902+ var i = 1 ;
903+ do {
904+ taskConfigsFolder = prefix + i ;
905+ i ++ ;
906+ } while ( fs . existsSync ( taskConfigsFolder ) ) ;
907+ fs . mkdirSync ( taskConfigsFolder ) ;
908+
909+ workerCount = process . env . workerCount || os . cpus ( ) . length ;
910+ }
890911
891- if ( tests || light ) {
892- writeTestConfigFile ( testConfigFile , tests , light , stackTraceLimit ) ;
912+ if ( tests || light || taskConfigsFolder ) {
913+ writeTestConfigFile ( tests , light , taskConfigsFolder , workerCount , stackTraceLimit ) ;
893914 }
894915
895916 if ( tests && tests . toLocaleLowerCase ( ) === "rwc" ) {
@@ -903,63 +924,93 @@ function runConsoleTests(defaultReporter, defaultSubsets, dirty) {
903924
904925 // timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally
905926 // default timeout is 2sec which really should be enough, but maybe we just need a small amount longer
906- var subsetRegexes ;
907- if ( defaultSubsets . length === 0 ) {
908- subsetRegexes = [ tests ] ;
927+ if ( ! runInParallel ) {
928+ tests = tests ? ' -g "' + tests + '"' : '' ;
929+ var cmd = "mocha" + ( debug ? " --debug-brk" : "" ) + " -R " + reporter + tests + colors + ' -t ' + testTimeout + ' ' + run ;
930+ console . log ( cmd ) ;
931+ exec ( cmd , function ( ) {
932+ runLinter ( ) ;
933+ finish ( ) ;
934+ } , function ( e , status ) {
935+ finish ( status ) ;
936+ } ) ;
937+
909938 }
910939 else {
911- var subsets = tests ? tests . split ( "|" ) : defaultSubsets ;
912- subsetRegexes = subsets . map ( function ( sub ) { return "^" + sub + ".*$" ; } ) ;
913- subsetRegexes . push ( "^(?!" + subsets . join ( "|" ) + ").*$" ) ;
914- }
915- var counter = subsetRegexes . length ;
916- var errorStatus ;
917- subsetRegexes . forEach ( function ( subsetRegex , i ) {
918- tests = subsetRegex ? ' -g "' + subsetRegex + '"' : '' ;
919- var cmd = "mocha" + ( debug ? " --debug-brk" : "" ) + " -R " + reporter + tests + colors + ' -t ' + testTimeout + ' ' + run ;
940+ // run task to load all tests and partition them between workers
941+ var cmd = "mocha " + " -R min " + colors + run ;
920942 console . log ( cmd ) ;
921- function finish ( status ) {
922- counter -- ;
923- // save first error status
924- if ( status !== undefined && errorStatus === undefined ) {
925- errorStatus = status ;
926- }
943+ exec ( cmd , function ( ) {
944+ // read all configuration files and spawn a worker for every config
945+ var configFiles = fs . readdirSync ( taskConfigsFolder ) ;
946+ var counter = configFiles . length ;
947+ var firstErrorStatus ;
948+ // schedule work for chunks
949+ configFiles . forEach ( function ( f ) {
950+ var configPath = path . join ( taskConfigsFolder , f ) ;
951+ var workerCmd = "mocha" + " -t " + testTimeout + " -R " + reporter + " " + colors + " " + run + " --config='" + configPath + "'" ;
952+ console . log ( workerCmd ) ;
953+ exec ( workerCmd , finishWorker , finishWorker )
954+ } ) ;
927955
928- deleteTemporaryProjectOutput ( ) ;
929- if ( counter !== 0 || errorStatus === undefined ) {
930- // run linter when last worker is finished
931- if ( lintFlag && counter === 0 && ! dirty ) {
932- var lint = jake . Task [ 'lint' ] ;
933- lint . addListener ( 'complete' , function ( ) {
934- complete ( ) ;
935- } ) ;
936- lint . invoke ( ) ;
956+ function finishWorker ( e , errorStatus ) {
957+ counter -- ;
958+ if ( firstErrorStatus === undefined && errorStatus !== undefined ) {
959+ firstErrorStatus = errorStatus ;
937960 }
938- else {
961+ if ( counter !== 0 ) {
939962 complete ( ) ;
940963 }
964+ else {
965+ // last worker clean everything and runs linter in case if there were no errors
966+ deleteTemporaryProjectOutput ( ) ;
967+ jake . rmRf ( taskConfigsFolder ) ;
968+ if ( firstErrorStatus === undefined ) {
969+ runLinter ( ) ;
970+ complete ( ) ;
971+ }
972+ else {
973+ failWithStatus ( firstErrorStatus ) ;
974+ }
975+ }
941976 }
942- else {
943- fail ( "Process exited with code " + status ) ;
944- }
977+ } ) ;
978+ }
979+
980+ function failWithStatus ( status ) {
981+ fail ( "Process exited with code " + status ) ;
982+ }
983+
984+ function finish ( errorStatus ) {
985+ deleteTemporaryProjectOutput ( ) ;
986+ if ( errorStatus !== undefined ) {
987+ failWithStatus ( errorStatus ) ;
945988 }
946- exec ( cmd , function ( ) {
947- finish ( ) ;
948- } , function ( e , status ) {
949- finish ( status ) ;
989+ else {
990+ complete ( ) ;
991+ }
992+ }
993+ function runLinter ( ) {
994+ if ( ! lintFlag || dirty ) {
995+ return ;
996+ }
997+ var lint = jake . Task [ 'lint' ] ;
998+ lint . addListener ( 'complete' , function ( ) {
999+ complete ( ) ;
9501000 } ) ;
951- } ) ;
1001+ lint . invoke ( ) ;
1002+ }
9521003}
9531004
9541005var testTimeout = 20000 ;
9551006desc ( "Runs all the tests in parallel using the built run.js file. Optional arguments are: t[ests]=category1|category2|... d[ebug]=true." ) ;
9561007task ( "runtests-parallel" , [ "build-rules" , "tests" , builtLocalDirectory ] , function ( ) {
957- runConsoleTests ( 'min' , [ 'compiler' , 'conformance' , 'Projects' , 'fourslash' ] ) ;
1008+ runConsoleTests ( 'min' , /*runInParallel*/ true ) ;
9581009} , { async : true } ) ;
9591010
9601011desc ( "Runs the tests using the built run.js file. Optional arguments are: t[ests]=regex r[eporter]=[list|spec|json|<more>] d[ebug]=true color[s]=false lint=true." ) ;
9611012task ( "runtests" , [ "build-rules" , "tests" , builtLocalDirectory ] , function ( ) {
962- runConsoleTests ( 'mocha-fivemat-progress-reporter' , [ ] ) ;
1013+ runConsoleTests ( 'mocha-fivemat-progress-reporter' , /*runInParallel*/ false ) ;
9631014} , { async : true } ) ;
9641015
9651016task ( "runtests-file" , [ "build-rules" , "tests" , builtLocalDirectory ] , function ( ) {
@@ -1000,7 +1051,7 @@ task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFi
10001051 fs . unlinkSync ( testConfigFile ) ;
10011052 }
10021053 if ( tests || light ) {
1003- writeTestConfigFile ( testConfigFile , tests , light ) ;
1054+ writeTestConfigFile ( tests , light ) ;
10041055 }
10051056
10061057 tests = tests ? tests : '' ;
0 commit comments