@@ -28,48 +28,62 @@ var __importStar = (this && this.__importStar) || function (mod) {
2828Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2929exports . findTests = void 0 ;
3030const fs_1 = require ( "fs" ) ;
31- const glob = __importStar ( require ( "glob" ) ) ;
31+ const glob_1 = __importStar ( require ( "glob" ) ) ;
3232const path_1 = require ( "path" ) ;
33- const is_directory_1 = require ( "../../utils/is-directory" ) ;
33+ const util_1 = require ( "util" ) ;
34+ const globPromise = ( 0 , util_1 . promisify ) ( glob_1 . default ) ;
3435// go through all patterns and find unique list of files
35- function findTests ( patterns , cwd , workspaceRoot ) {
36- return patterns . reduce ( ( files , pattern ) => {
37- const relativePathToMain = cwd . replace ( workspaceRoot , '' ) . substr ( 1 ) ; // remove leading slash
38- const tests = findMatchingTests ( pattern , cwd , relativePathToMain ) ;
39- tests . forEach ( ( file ) => {
40- if ( ! files . includes ( file ) ) {
41- files . push ( file ) ;
42- }
43- } ) ;
44- return files ;
45- } , [ ] ) ;
36+ async function findTests ( patterns , workspaceRoot , projectSourceRoot ) {
37+ const matchingTestsPromises = patterns . map ( ( pattern ) => findMatchingTests ( pattern , workspaceRoot , projectSourceRoot ) ) ;
38+ const files = await Promise . all ( matchingTestsPromises ) ;
39+ // Unique file names
40+ return [ ...new Set ( files . flat ( ) ) ] ;
4641}
4742exports . findTests = findTests ;
48- function findMatchingTests ( pattern , cwd , relativePathToMain ) {
43+ const normalizePath = ( path ) => path . replace ( / \\ / g, '/' ) ;
44+ async function findMatchingTests ( pattern , workspaceRoot , projectSourceRoot ) {
4945 // normalize pattern, glob lib only accepts forward slashes
50- pattern = pattern . replace ( / \\ / g , '/' ) ;
51- relativePathToMain = relativePathToMain . replace ( / \\ / g , '/' ) ;
52- // remove relativePathToMain to support relative paths from root
46+ let normalizedPattern = normalizePath ( pattern ) ;
47+ const relativeProjectRoot = normalizePath ( ( 0 , path_1 . relative ) ( workspaceRoot , projectSourceRoot ) + '/' ) ;
48+ // remove relativeProjectRoot to support relative paths from root
5349 // such paths are easy to get when running scripts via IDEs
54- if ( pattern . startsWith ( relativePathToMain + '/' ) ) {
55- pattern = pattern . substr ( relativePathToMain . length + 1 ) ; // +1 to include slash
50+ if ( normalizedPattern . startsWith ( relativeProjectRoot ) ) {
51+ normalizedPattern = normalizedPattern . substring ( relativeProjectRoot . length ) ;
5652 }
5753 // special logic when pattern does not look like a glob
58- if ( ! glob . hasMagic ( pattern ) ) {
59- if ( ( 0 , is_directory_1 . isDirectory ) ( ( 0 , path_1 . join ) ( cwd , pattern ) ) ) {
60- pattern = `${ pattern } /**/*.spec.@(ts|tsx)` ;
54+ if ( ! ( 0 , glob_1 . hasMagic ) ( normalizedPattern ) ) {
55+ if ( await isDirectory ( ( 0 , path_1 . join ) ( projectSourceRoot , normalizedPattern ) ) ) {
56+ normalizedPattern = `${ normalizedPattern } /**/*.spec.@(ts|tsx)` ;
6157 }
6258 else {
6359 // see if matching spec file exists
64- const extension = ( 0 , path_1 . extname ) ( pattern ) ;
65- const matchingSpec = `${ ( 0 , path_1 . basename ) ( pattern , extension ) } .spec${ extension } ` ;
66- if ( ( 0 , fs_1 . existsSync ) ( ( 0 , path_1 . join ) ( cwd , ( 0 , path_1 . dirname ) ( pattern ) , matchingSpec ) ) ) {
67- pattern = ( 0 , path_1 . join ) ( ( 0 , path_1 . dirname ) ( pattern ) , matchingSpec ) . replace ( / \\ / g, '/' ) ;
60+ const fileExt = ( 0 , path_1 . extname ) ( normalizedPattern ) ;
61+ // Replace extension to `.spec.ext`. Example: `src/app/app.component.ts`-> `src/app/app.component.spec.ts`
62+ const potentialSpec = ( 0 , path_1 . join ) ( ( 0 , path_1 . dirname ) ( normalizedPattern ) , `${ ( 0 , path_1 . basename ) ( normalizedPattern , fileExt ) } .spec${ fileExt } ` ) ;
63+ if ( await exists ( ( 0 , path_1 . join ) ( projectSourceRoot , potentialSpec ) ) ) {
64+ return [ normalizePath ( potentialSpec ) ] ;
6865 }
6966 }
7067 }
71- const files = glob . sync ( pattern , {
72- cwd,
68+ return globPromise ( normalizedPattern , {
69+ cwd : projectSourceRoot ,
7370 } ) ;
74- return files ;
71+ }
72+ async function isDirectory ( path ) {
73+ try {
74+ const stats = await fs_1 . promises . stat ( path ) ;
75+ return stats . isDirectory ( ) ;
76+ }
77+ catch {
78+ return false ;
79+ }
80+ }
81+ async function exists ( path ) {
82+ try {
83+ await fs_1 . promises . access ( path , fs_1 . constants . F_OK ) ;
84+ return true ;
85+ }
86+ catch {
87+ return false ;
88+ }
7589}
0 commit comments