Skip to content

Commit 2e0f931

Browse files
committed
fix(@angular/build): support parent directory globs in unit-test discovery
1 parent 6737a20 commit 2e0f931

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎packages/angular/build/src/builders/unit-test/test-discovery.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { type PathLike, constants, promises as fs } from 'node:fs';
1010
import os from 'node:os';
11-
import { basename, dirname, extname, isAbsolute, join, relative } from 'node:path';
11+
import { basename, dirname, extname, isAbsolute, join, posix, relative } from 'node:path';
1212
import { glob, isDynamicPattern } from 'tinyglobby';
1313
import { calculateHash, initializeHash } from '../../utils/hash';
1414
import { toPosixPath } from '../../utils/path';
@@ -62,11 +62,39 @@ export async function findTests(
6262

6363
// 2. Execute a single glob for all dynamic patterns
6464
if (dynamicPatterns.length > 0) {
65-
const globMatches = await glob(dynamicPatterns, {
66-
cwd: projectSourceRoot,
65+
let maxParentPrefix = '';
66+
for (const pattern of [...dynamicPatterns, ...normalizedExcludes]) {
67+
if (isAbsolute(pattern)) {
68+
continue;
69+
}
70+
71+
const parentMatch = /^(?:\.\.\/)+/.exec(pattern);
72+
if (parentMatch && parentMatch[0].length > maxParentPrefix.length) {
73+
maxParentPrefix = parentMatch[0];
74+
}
75+
}
76+
77+
let globCwd = projectSourceRoot;
78+
let patternsToGlob = dynamicPatterns;
79+
let excludesToGlob = normalizedExcludes;
80+
if (maxParentPrefix) {
81+
globCwd = join(projectSourceRoot, maxParentPrefix.slice(0, -1));
82+
const relativeSourceRoot = toPosixPath(relative(globCwd, projectSourceRoot));
83+
if (relativeSourceRoot) {
84+
patternsToGlob = dynamicPatterns.map((p) =>
85+
isAbsolute(p) ? p : posix.join(relativeSourceRoot, p),
86+
);
87+
excludesToGlob = normalizedExcludes.map((p) =>
88+
isAbsolute(p) || p.startsWith('**/') ? p : posix.join(relativeSourceRoot, p),
89+
);
90+
}
91+
}
92+
93+
const globMatches = await glob(patternsToGlob, {
94+
cwd: globCwd,
6795
absolute: true,
6896
expandDirectories: false,
69-
ignore: ['**/node_modules/**', ...normalizedExcludes],
97+
ignore: ['**/node_modules/**', ...excludesToGlob],
7098
});
7199

72100
for (const match of globMatches) {

‎packages/angular/build/src/builders/unit-test/test-discovery_spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ describe('findTests', () => {
3939
const expected = files.map((file) => toPosixPath(join(projectSourceRoot, file))).sort();
4040
expect(result).toEqual(expected);
4141
});
42+
43+
it('should match files both inside and outside projectSourceRoot when using parent directory globs', async () => {
44+
const srcRoot = join(projectSourceRoot, 'src');
45+
const files = ['src/lib/my-lib.spec.ts', 'secondary/src/secondary.spec.ts'];
46+
for (const file of files) {
47+
const path = join(projectSourceRoot, file);
48+
await fs.mkdir(join(path, '..'), { recursive: true });
49+
await fs.writeFile(path, '');
50+
}
51+
52+
const result = await findTests(['../**/*.spec.ts'], [], projectSourceRoot, srcRoot);
53+
54+
const expected = files.map((file) => toPosixPath(join(projectSourceRoot, file))).sort();
55+
expect(result).toEqual(expected);
56+
});
4257
});
4358

4459
describe('getTestEntrypoints', () => {

0 commit comments

Comments
 (0)