fix(fs/expandGlob): globstar false does not take effect (#2571) (#2779)

This commit is contained in:
Nash Kabbara 2022-10-18 09:40:00 -05:00 committed by GitHub
parent c1ec22566d
commit 2c7274741c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
.DS_Store
.idea
.vscode/settings.json
.vim
**/cov/
/crypto/_wasm_crypto/target
/encoding/varint/_wasm_varint/target

View File

@ -91,11 +91,15 @@ export async function* expandGlob(
.map((s: string): RegExp => globToRegExp(s, globOptions));
const shouldInclude = (path: string): boolean =>
!excludePatterns.some((p: RegExp): boolean => !!path.match(p));
const { segments, isAbsolute: isGlobAbsolute, hasTrailingSep, winRoot } =
split(toPathString(glob));
const {
segments,
isAbsolute: isGlobAbsolute,
hasTrailingSep,
winRoot,
} = split(toPathString(glob));
let fixedRoot = isGlobAbsolute
? (winRoot != undefined ? winRoot : "/")
? winRoot != undefined ? winRoot : "/"
: absRoot;
while (segments.length > 0 && !isGlob(segments[0])) {
const seg = segments.shift();
@ -127,7 +131,10 @@ export async function* expandGlob(
}
return;
} else if (globSegment == "**") {
return yield* walk(walkInfo.path, { skip: excludePatterns });
return yield* walk(walkInfo.path, {
skip: excludePatterns,
maxDepth: globstar ? Infinity : 1,
});
}
const globPattern = globToRegExp(globSegment, globOptions);
for await (
@ -137,7 +144,8 @@ export async function* expandGlob(
})
) {
if (
walkEntry.path != walkInfo.path && walkEntry.name.match(globPattern)
walkEntry.path != walkInfo.path &&
walkEntry.name.match(globPattern)
) {
yield walkEntry;
}
@ -149,13 +157,16 @@ export async function* expandGlob(
// Advancing the list of current matches may introduce duplicates, so we
// pass everything through this Map.
const nextMatchMap: Map<string, WalkEntry> = new Map();
await Promise.all(currentMatches.map(async (currentMatch) => {
for await (const nextMatch of advanceMatch(currentMatch, segment)) {
nextMatchMap.set(nextMatch.path, nextMatch);
}
}));
await Promise.all(
currentMatches.map(async (currentMatch) => {
for await (const nextMatch of advanceMatch(currentMatch, segment)) {
nextMatchMap.set(nextMatch.path, nextMatch);
}
}),
);
currentMatches = [...nextMatchMap.values()].sort(comparePath);
}
if (hasTrailingSep) {
currentMatches = currentMatches.filter(
(entry: WalkEntry): boolean => entry.isDirectory,
@ -199,11 +210,15 @@ export function* expandGlobSync(
.map((s: string): RegExp => globToRegExp(s, globOptions));
const shouldInclude = (path: string): boolean =>
!excludePatterns.some((p: RegExp): boolean => !!path.match(p));
const { segments, isAbsolute: isGlobAbsolute, hasTrailingSep, winRoot } =
split(toPathString(glob));
const {
segments,
isAbsolute: isGlobAbsolute,
hasTrailingSep,
winRoot,
} = split(toPathString(glob));
let fixedRoot = isGlobAbsolute
? (winRoot != undefined ? winRoot : "/")
? winRoot != undefined ? winRoot : "/"
: absRoot;
while (segments.length > 0 && !isGlob(segments[0])) {
const seg = segments.shift();
@ -235,7 +250,10 @@ export function* expandGlobSync(
}
return;
} else if (globSegment == "**") {
return yield* walkSync(walkInfo.path, { skip: excludePatterns });
return yield* walkSync(walkInfo.path, {
skip: excludePatterns,
maxDepth: globstar ? Infinity : 1,
});
}
const globPattern = globToRegExp(globSegment, globOptions);
for (
@ -245,7 +263,8 @@ export function* expandGlobSync(
})
) {
if (
walkEntry.path != walkInfo.path && walkEntry.name.match(globPattern)
walkEntry.path != walkInfo.path &&
walkEntry.name.match(globPattern)
) {
yield walkEntry;
}
@ -264,6 +283,7 @@ export function* expandGlobSync(
}
currentMatches = [...nextMatchMap.values()].sort(comparePath);
}
if (hasTrailingSep) {
currentMatches = currentMatches.filter(
(entry: WalkEntry): boolean => entry.isDirectory,

View File

@ -112,6 +112,18 @@ Deno.test("expandGlobGlobstarParent", async function () {
);
});
Deno.test("expandGlobGlobstarFalseWithGlob", async function () {
const options = { ...EG_OPTIONS, globstar: false };
assertEquals(await expandGlobArray("**", options), [
".",
"a[b]c",
"abc",
"abcdef",
"abcdefghi",
"subdir",
]);
});
Deno.test("expandGlobIncludeDirs", async function () {
const options = { ...EG_OPTIONS, includeDirs: false };
assertEquals(await expandGlobArray("subdir", options), []);
@ -120,12 +132,7 @@ Deno.test("expandGlobIncludeDirs", async function () {
Deno.test("expandGlobPermError", async function () {
const exampleUrl = new URL("testdata/expand_wildcard.js", import.meta.url);
const { code, success, stdout, stderr } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--quiet",
"--unstable",
exampleUrl.toString(),
],
args: ["run", "--quiet", "--unstable", exampleUrl.toString()],
});
const decoder = new TextDecoder();
assert(!success);