@@ -9,34 +9,73 @@ import { getIncludeFiles } from "../utils/pathResolver.js";
9
9
import { SourceFunctionInfo , UnittestPackage } from "../interface.js" ;
10
10
import { projectRoot } from "../utils/projectRoot.js" ;
11
11
12
- const sourceFunctions = new Map < string , SourceFunctionInfo [ ] > ( ) ;
13
12
export async function precompile (
14
13
includes : string [ ] ,
15
14
excludes : string [ ] ,
16
15
testcases : string [ ] | undefined ,
17
- flags : string ,
18
- transformFunction = join ( projectRoot , "transform" , "listFunctions.mjs" )
16
+ testNamePattern : string | undefined ,
17
+ flags : string
19
18
) : Promise < UnittestPackage > {
20
19
// if specify testcases, use testcases for unittest
21
20
// otherwise, get testcases(*.test.ts) in includes directory
22
21
const testCodePaths = testcases ?? getRelatedFiles ( includes , excludes , ( path : string ) => path . endsWith ( ".test.ts" ) ) ;
23
22
24
- const sourceCodePaths = getRelatedFiles ( includes , excludes , ( path : string ) => ! path . endsWith ( ".test.ts" ) ) ;
23
+ const matchedTestNames : string [ ] = [ ] ;
24
+ if ( testNamePattern ) {
25
+ const testNameInfos = new Map < string , string [ ] > ( ) ;
26
+ const testNameTransformFunction = join ( projectRoot , "transform" , "listTestNames.mjs" ) ;
27
+ for ( const testCodePath of testCodePaths ) {
28
+ await transform ( testNameTransformFunction , testCodePath , flags , ( ) => {
29
+ testNameInfos . set ( testCodePath , testNames ) ;
30
+ } ) ;
31
+ }
32
+ const regexPattern = new RegExp ( testNamePattern ) ;
33
+ for ( const [ fileName , testNames ] of testNameInfos ) {
34
+ for ( const testName of testNames ) {
35
+ if ( regexPattern . test ( testName ) ) {
36
+ matchedTestNames . push ( testName ) ;
37
+ }
38
+ }
39
+ }
40
+ }
25
41
42
+ const sourceCodePaths = getRelatedFiles ( includes , excludes , ( path : string ) => ! path . endsWith ( ".test.ts" ) ) ;
43
+ const sourceFunctions = new Map < string , SourceFunctionInfo [ ] > ( ) ;
44
+ const sourceTransformFunction = join ( projectRoot , "transform" , "listFunctions.mjs" ) ;
26
45
// The batchSize = 2 is empirical data after benchmarking
27
46
const batchSize = 2 ;
28
47
for ( let i = 0 ; i < sourceCodePaths . length ; i += batchSize ) {
29
48
await Promise . all (
30
- sourceCodePaths . slice ( i , i + batchSize ) . map ( ( sourcePath ) => transform ( sourcePath , transformFunction , flags ) )
49
+ sourceCodePaths . slice ( i , i + batchSize ) . map ( ( sourcePath ) =>
50
+ transform ( sourceTransformFunction , sourcePath , flags , ( ) => {
51
+ sourceFunctions . set ( sourcePath , functionInfos ) ;
52
+ } )
53
+ )
31
54
) ;
32
55
}
33
56
34
57
return {
35
58
testCodePaths,
59
+ matchedTestNames,
36
60
sourceFunctions,
37
61
} ;
38
62
}
39
63
64
+ async function transform ( transformFunction : string , codePath : string , flags : string , collectCallback : ( ) => void ) {
65
+ let ascArgv = [ codePath , "--noEmit" , "--disableWarning" , "--transform" , transformFunction , "-O0" ] ;
66
+ if ( flags ) {
67
+ const argv = flags . split ( " " ) ;
68
+ ascArgv = ascArgv . concat ( argv ) ;
69
+ }
70
+ const { error, stderr } = await main ( ascArgv ) ;
71
+ if ( error ) {
72
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
73
+ console . error ( stderr . toString ( ) ) ;
74
+ throw error ;
75
+ }
76
+ collectCallback ( ) ;
77
+ }
78
+
40
79
// a. include in config
41
80
// b. exclude in config
42
81
export function getRelatedFiles ( includes : string [ ] , excludes : string [ ] , filter : ( path : string ) => boolean ) {
@@ -58,18 +97,3 @@ export function getRelatedFiles(includes: string[], excludes: string[], filter:
58
97
}
59
98
return result ;
60
99
}
61
-
62
- async function transform ( sourceCodePath : string , transformFunction : string , flags : string ) {
63
- let ascArgv = [ sourceCodePath , "--noEmit" , "--disableWarning" , "--transform" , transformFunction , "-O0" ] ;
64
- if ( flags ) {
65
- const argv = flags . split ( " " ) ;
66
- ascArgv = ascArgv . concat ( argv ) ;
67
- }
68
- const { error, stderr } = await main ( ascArgv ) ;
69
- if ( error ) {
70
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
71
- console . error ( stderr . toString ( ) ) ;
72
- throw error ;
73
- }
74
- sourceFunctions . set ( sourceCodePath , functionInfos ) ;
75
- }
0 commit comments