import * as path from 'path' ; import { globP , forkP } from './utils' ; export interface Options { includeGlobs : string [ ] ; excludeGlobs ? : string [ ] ; rootDir : string ; coverage ? : boolean ; timeout ? : number ; } export async function runTests ( options ) { const { includeGlobs , excludeGlobs , rootDir , coverage , timeout } = options ; function nodule ( nodule ) { return ( '.' + path . sep + path . relative ( rootDir , path . join ( 'node_modules' , nodule ) ) ) ; } let testNum = 0 ; const excludedFiles = ( [ ] as string [ ] ) . concat ( ... ( await Promise . all ( ( excludeGlobs || [ ] ) . map ( glob => globP ( glob ) ) ) ) ) ; const includedFiles = ( [ ] as string [ ] ) . concat ( ... ( await Promise . all ( includeGlobs . map ( glob => globP ( glob ) ) ) ) ) ; const files = includedFiles . filter ( i => excludedFiles . indexOf ( i ) < 0 ) ; for ( const file of files ) { const moduleAndArgs = [ ... ( coverage ? [ nodule ( path . join ( '.bin' , 'c8' ) ) , '--report-dir' , path . join ( '.' , '.coverage' , ( testNum ++ ) . toString ( ) ) , '--exclude' , path . join ( 'build' , 'test' , 'fixtures' , '**' ) , '--exclude' , path . join ( 'build' , 'test' , 'plugins' , 'fixtures' , '**' ) , ] : [ ] ) , nodule ( path . join ( 'mocha' , 'bin' , '_mocha' ) ) , '--require' , 'source-map-support/register' , path . relative ( rootDir , file ) , ... ( timeout ? [ '--timeout' , `${ timeout }` ] : [ '--no-timeouts' ] ) , ] ; await forkP ( moduleAndArgs [ 0 ] , moduleAndArgs . slice ( 1 ) , { cwd : rootDir } ) ; } }