@@ -29,53 +29,106 @@ const MAX_FILENAME_LENGTH = 128;
2929 * test file. If a user provides a path to a directory, it will find all test
3030 * files within that directory.
3131 *
32+ * A pattern that matches nothing under the project source root is retried from the
33+ * project root, which is what the builder's schema documents patterns to be relative
34+ * to. A pattern that already matches keeps matching exactly what it matches today, and
35+ * a project rooted at the workspace root is never retried, so a search never widens to
36+ * the whole workspace.
37+ *
3238 * @param include Glob patterns of files to include.
3339 * @param exclude Glob patterns of files to exclude.
3440 * @param workspaceRoot The absolute path to the workspace root.
3541 * @param projectSourceRoot The absolute path to the project's source root.
42+ * @param projectRoot The absolute path to the project root. Defaults to the source root.
3643 * @returns A unique set of absolute paths to all test files.
3744 */
3845export async function findTests (
3946 include : string [ ] ,
4047 exclude : string [ ] ,
4148 workspaceRoot : string ,
4249 projectSourceRoot : string ,
50+ projectRoot = projectSourceRoot ,
4351) : Promise < string [ ] > {
4452 await initializeHash ( ) ;
4553 const resolvedTestFiles = new Set < string > ( ) ;
46- const dynamicPatterns : string [ ] = [ ] ;
4754
48- const projectRootPrefix = toPosixPath ( relative ( workspaceRoot , projectSourceRoot ) + '/' ) ;
49- const normalizedExcludes = exclude . map ( ( p ) => normalizePattern ( p , projectRootPrefix ) ) ;
55+ const unmatched = await collectTests (
56+ projectSourceRoot ,
57+ include ,
58+ exclude ,
59+ workspaceRoot ,
60+ resolvedTestFiles ,
61+ ) ;
62+
63+ // Retrying from the workspace root would search every project, so it is skipped.
64+ if ( unmatched . length > 0 && projectRoot !== projectSourceRoot && projectRoot !== workspaceRoot ) {
65+ await collectTests ( projectRoot , unmatched , exclude , workspaceRoot , resolvedTestFiles ) ;
66+ }
67+
68+ return [ ...resolvedTestFiles ] ;
69+ }
70+
71+ /**
72+ * Resolves include patterns against a single root, adding every match to `found`.
73+ *
74+ * @param searchRoot The absolute path the patterns are resolved against.
75+ * @param include Glob patterns of files to include.
76+ * @param exclude Glob patterns of files to exclude.
77+ * @param workspaceRoot The absolute path to the workspace root.
78+ * @param found Accumulates the absolute path of every matched file.
79+ * @returns The patterns that matched nothing, so a caller can retry them elsewhere.
80+ */
81+ async function collectTests (
82+ searchRoot : string ,
83+ include : string [ ] ,
84+ exclude : string [ ] ,
85+ workspaceRoot : string ,
86+ found : Set < string > ,
87+ ) : Promise < string [ ] > {
88+ const relativeRoot = relative ( workspaceRoot , searchRoot ) ;
89+ const searchRootPrefix = relativeRoot ? toPosixPath ( relativeRoot + '/' ) : '' ;
90+ const normalizedExcludes = exclude . map ( ( p ) => normalizePattern ( p , searchRootPrefix ) ) ;
91+ const unmatched : string [ ] = [ ] ;
5092
51- // 1. Separate static and dynamic patterns
5293 for ( const pattern of include ) {
53- const normalized = normalizePattern ( pattern , projectRootPrefix ) ;
94+ const normalized = normalizePattern ( pattern , searchRootPrefix ) ;
95+ const dynamicPatterns : string [ ] = [ ] ;
96+
97+ // 1. Separate static and dynamic patterns
5498 if ( isDynamicPattern ( pattern ) ) {
5599 dynamicPatterns . push ( normalized ) ;
56100 } else {
57- const { resolved, unresolved } = await resolveStaticPattern ( normalized , projectSourceRoot ) ;
58- resolved . forEach ( ( file ) => resolvedTestFiles . add ( file ) ) ;
59- unresolved . forEach ( ( p ) => dynamicPatterns . push ( p ) ) ;
101+ const { resolved, unresolved } = await resolveStaticPattern ( normalized , searchRoot ) ;
102+ if ( resolved . length > 0 ) {
103+ resolved . forEach ( ( file ) => found . add ( file ) ) ;
104+ continue ;
105+ }
106+ dynamicPatterns . push ( ...unresolved ) ;
60107 }
61- }
62108
63- // 2. Execute a single glob for all dynamic patterns
64- if ( dynamicPatterns . length > 0 ) {
65- const globMatches = await glob ( dynamicPatterns , {
66- cwd : projectSourceRoot ,
67- absolute : true ,
68- expandDirectories : false ,
69- ignore : [ '**/node_modules/**' , ...normalizedExcludes ] ,
70- } ) ;
109+ // 2. Execute a glob for the pattern, so that a pattern which matches nothing here
110+ // can be told apart from one that does and retried against another root.
111+ const globMatches = dynamicPatterns . length
112+ ? await glob ( dynamicPatterns , {
113+ cwd : searchRoot ,
114+ absolute : true ,
115+ expandDirectories : false ,
116+ ignore : [ '**/node_modules/**' , ...normalizedExcludes ] ,
117+ } )
118+ : [ ] ;
119+
120+ if ( globMatches . length === 0 ) {
121+ unmatched . push ( pattern ) ;
122+ continue ;
123+ }
71124
125+ // 3. Combine and de-duplicate results
72126 for ( const match of globMatches ) {
73- resolvedTestFiles . add ( toPosixPath ( match ) ) ;
127+ found . add ( toPosixPath ( match ) ) ;
74128 }
75129 }
76130
77- // 3. Combine and de-duplicate results
78- return [ ...resolvedTestFiles ] ;
131+ return unmatched ;
79132}
80133
81134interface TestEntrypointsOptions {
@@ -255,7 +308,7 @@ function removeRoots(path: string, roots: string[]): string {
255308 * slashes, and making it relative to the project source root.
256309 *
257310 * @param pattern The glob pattern to normalize.
258- * @param projectRootPrefix The POSIX-formatted prefix of the project's source root relative to the workspace root.
311+ * @param projectRootPrefix The POSIX-formatted prefix of the search root relative to the workspace root.
259312 * @returns A normalized glob pattern.
260313 */
261314function normalizePattern ( pattern : string , projectRootPrefix : string ) : string {
@@ -266,8 +319,8 @@ function normalizePattern(pattern: string, projectRootPrefix: string): string {
266319 return posixPattern ;
267320 }
268321
269- // For relative paths, ensure they are correctly relative to the project source root.
270- // This involves removing the project root prefix if the user provided a workspace-relative path.
322+ // For relative paths, ensure they are correctly relative to the search root.
323+ // This involves removing the root prefix if the user provided a workspace-relative path.
271324 const normalizedRelative = removePrefix ( posixPattern , projectRootPrefix ) ;
272325
273326 return normalizedRelative ;
0 commit comments