Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): normalize exclude path
Browse files Browse the repository at this point in the history
fix(@angular-devkit/build-angular): normalize exclude path

(cherry picked from commit 31f0286)
  • Loading branch information
akhilbiju authored and alan-agius4 committed Nov 7, 2023
1 parent 64e263e commit bab3672
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ async function findTests(

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

const removeLeadingSlash = (pattern: string): string => {
if (pattern.charAt(0) === '/') {
return pattern.substring(1);
}

return pattern;
};

const removeRelativeRoot = (path: string, root: string): string => {
if (path.startsWith(root)) {
return path.substring(root.length);
}

return path;
};

async function findMatchingTests(
pattern: string,
ignore: string[],
Expand All @@ -98,17 +114,13 @@ async function findMatchingTests(
): Promise<string[]> {
// normalize pattern, glob lib only accepts forward slashes
let normalizedPattern = normalizePath(pattern);
if (normalizedPattern.charAt(0) === '/') {
normalizedPattern = normalizedPattern.substring(1);
}
normalizedPattern = removeLeadingSlash(normalizedPattern);

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

// remove relativeProjectRoot to support relative paths from root
// such paths are easy to get when running scripts via IDEs
if (normalizedPattern.startsWith(relativeProjectRoot)) {
normalizedPattern = normalizedPattern.substring(relativeProjectRoot.length);
}
normalizedPattern = removeRelativeRoot(normalizedPattern, relativeProjectRoot);

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

// normalize the patterns in the ignore list
const normalizedIgnorePatternList = ignore.map((pattern: string) =>
removeRelativeRoot(removeLeadingSlash(normalizePath(pattern)), relativeProjectRoot),
);

return glob(normalizedPattern, {
cwd: projectSourceRoot,
absolute: true,
ignore: ['**/node_modules/**', ...ignore],
ignore: ['**/node_modules/**', ...normalizedIgnorePatternList],
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,30 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
expect(result?.success).toBeFalse();
});

it(`should exclude spec that matches the 'exclude' pattern`, async () => {
it(`should exclude spec that matches the 'exclude' glob pattern`, async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
exclude: ['**/error.spec.ts'],
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});

it(`should exclude spec that matches the 'exclude' pattern with a relative project root`, async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
exclude: ['src/app/error.spec.ts'],
});

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

it(`should exclude spec that matches the 'exclude' pattern prefixed with a slash`, async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
exclude: ['/src/app/error.spec.ts'],
});

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

0 comments on commit bab3672

Please sign in to comment.