test: keep extension directory filters covered

This commit is contained in:
Peter Steinberger
2026-04-22 23:06:26 +01:00
parent eb689f3535
commit 1a90893e90
2 changed files with 37 additions and 3 deletions

View File

@@ -131,6 +131,17 @@ describe("createScopedVitestConfig", () => {
expect(config.test?.include).toEqual(["slack/**/*.test.*"]);
});
it("keeps broad scoped cli directory filters aligned with repo-root include patterns", () => {
const config = createScopedVitestConfig([BUNDLED_PLUGIN_TEST_GLOB], {
argv: ["vitest", "run", "extensions/speech-core"],
dir: "extensions",
env: {},
passWithNoTests: true,
});
expect(config.test?.include).toEqual(["speech-core/**/*.test.*"]);
});
it("relativizes scoped include and exclude patterns to the configured dir", () => {
const config = createScopedVitestConfig([BUNDLED_PLUGIN_TEST_GLOB], {
dir: "extensions",

View File

@@ -26,6 +26,31 @@ function looksLikeCliIncludePattern(value: string): boolean {
);
}
function literalPrefixForGlobPattern(value: string): string {
const normalized = value.replaceAll("\\", "/");
const globIndex = normalized.search(/[?*[\]{}]/u);
if (globIndex === -1) {
return normalized;
}
const slashIndex = normalized.lastIndexOf("/", globIndex);
return slashIndex === -1 ? "" : normalized.slice(0, slashIndex + 1);
}
function patternsCouldOverlap(value: string, pattern: string): boolean {
if (path.matchesGlob(value, pattern) || path.matchesGlob(pattern, value)) {
return true;
}
const valuePrefix = literalPrefixForGlobPattern(value);
const patternPrefix = literalPrefixForGlobPattern(pattern);
return (
patternPrefix === "" ||
valuePrefix === "" ||
valuePrefix.startsWith(patternPrefix) ||
patternPrefix.startsWith(valuePrefix)
);
}
export function loadPatternListFile(filePath: string, label: string): string[] {
const parsed = JSON.parse(fs.readFileSync(filePath, "utf8")) as unknown;
if (!Array.isArray(parsed)) {
@@ -99,9 +124,7 @@ export function narrowIncludePatternsForCli(
}
const matched = cliPatterns.filter((value) =>
includePatterns.some(
(pattern) => path.matchesGlob(value, pattern) || path.matchesGlob(pattern, value),
),
includePatterns.some((pattern) => patternsCouldOverlap(value, pattern)),
);
return [...new Set(matched)];