Skip to content

Commit 31f0286

Browse files
akhilbijualan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): normalize exclude path
fix(@angular-devkit/build-angular): normalize exclude path
1 parent fc40bb5 commit 31f0286

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

packages/angular_devkit/build_angular/src/builders/karma/find-tests-plugin.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ async function findTests(
9090

9191
const normalizePath = (path: string): string => path.replace(/\\/g, '/');
9292

93+
const removeLeadingSlash = (pattern: string): string => {
94+
if (pattern.charAt(0) === '/') {
95+
return pattern.substring(1);
96+
}
97+
98+
return pattern;
99+
};
100+
101+
const removeRelativeRoot = (path: string, root: string): string => {
102+
if (path.startsWith(root)) {
103+
return path.substring(root.length);
104+
}
105+
106+
return path;
107+
};
108+
93109
async function findMatchingTests(
94110
pattern: string,
95111
ignore: string[],
@@ -98,17 +114,13 @@ async function findMatchingTests(
98114
): Promise<string[]> {
99115
// normalize pattern, glob lib only accepts forward slashes
100116
let normalizedPattern = normalizePath(pattern);
101-
if (normalizedPattern.charAt(0) === '/') {
102-
normalizedPattern = normalizedPattern.substring(1);
103-
}
117+
normalizedPattern = removeLeadingSlash(normalizedPattern);
104118

105119
const relativeProjectRoot = normalizePath(relative(workspaceRoot, projectSourceRoot) + '/');
106120

107121
// remove relativeProjectRoot to support relative paths from root
108122
// such paths are easy to get when running scripts via IDEs
109-
if (normalizedPattern.startsWith(relativeProjectRoot)) {
110-
normalizedPattern = normalizedPattern.substring(relativeProjectRoot.length);
111-
}
123+
normalizedPattern = removeRelativeRoot(normalizedPattern, relativeProjectRoot);
112124

113125
// special logic when pattern does not look like a glob
114126
if (!isDynamicPattern(normalizedPattern)) {
@@ -130,10 +142,15 @@ async function findMatchingTests(
130142
}
131143
}
132144

145+
// normalize the patterns in the ignore list
146+
const normalizedIgnorePatternList = ignore.map((pattern: string) =>
147+
removeRelativeRoot(removeLeadingSlash(normalizePath(pattern)), relativeProjectRoot),
148+
);
149+
133150
return glob(normalizedPattern, {
134151
cwd: projectSourceRoot,
135152
absolute: true,
136-
ignore: ['**/node_modules/**', ...ignore],
153+
ignore: ['**/node_modules/**', ...normalizedIgnorePatternList],
137154
});
138155
}
139156

packages/angular_devkit/build_angular/src/builders/karma/tests/options/exclude_spec.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,30 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
3131
expect(result?.success).toBeFalse();
3232
});
3333

34-
it(`should exclude spec that matches the 'exclude' pattern`, async () => {
34+
it(`should exclude spec that matches the 'exclude' glob pattern`, async () => {
3535
harness.useTarget('test', {
3636
...BASE_OPTIONS,
3737
exclude: ['**/error.spec.ts'],
3838
});
39+
const { result } = await harness.executeOnce();
40+
expect(result?.success).toBeTrue();
41+
});
42+
43+
it(`should exclude spec that matches the 'exclude' pattern with a relative project root`, async () => {
44+
harness.useTarget('test', {
45+
...BASE_OPTIONS,
46+
exclude: ['src/app/error.spec.ts'],
47+
});
48+
49+
const { result } = await harness.executeOnce();
50+
expect(result?.success).toBeTrue();
51+
});
52+
53+
it(`should exclude spec that matches the 'exclude' pattern prefixed with a slash`, async () => {
54+
harness.useTarget('test', {
55+
...BASE_OPTIONS,
56+
exclude: ['/src/app/error.spec.ts'],
57+
});
3958

4059
const { result } = await harness.executeOnce();
4160
expect(result?.success).toBeTrue();

0 commit comments

Comments
 (0)