2019-09-28 09:33:17 -04:00
|
|
|
const { cwd, mkdir } = Deno;
|
2019-05-14 17:14:08 -04:00
|
|
|
import { test, runIfMain } from "../testing/mod.ts";
|
2019-05-23 03:23:31 -04:00
|
|
|
import { assert, assertEquals } from "../testing/asserts.ts";
|
2019-10-02 13:59:27 -04:00
|
|
|
import { SEP, isWindows } from "./path/constants.ts";
|
2019-09-28 09:33:17 -04:00
|
|
|
import {
|
|
|
|
ExpandGlobOptions,
|
|
|
|
expandGlob,
|
2019-10-02 13:59:27 -04:00
|
|
|
expandGlobSync,
|
|
|
|
globToRegExp,
|
2019-09-28 09:33:17 -04:00
|
|
|
isGlob,
|
2019-10-02 13:59:27 -04:00
|
|
|
joinGlobs,
|
|
|
|
normalizeGlob
|
2019-09-28 09:33:17 -04:00
|
|
|
} from "./glob.ts";
|
|
|
|
import { join, normalize, relative } from "./path.ts";
|
2019-03-02 14:56:19 -05:00
|
|
|
import { testWalk } from "./walk_test.ts";
|
2019-05-14 17:14:08 -04:00
|
|
|
import { touch, walkArray } from "./walk_test.ts";
|
2019-03-02 14:56:19 -05:00
|
|
|
|
|
|
|
test({
|
|
|
|
name: "glob: glob to regex",
|
2019-04-24 07:41:23 -04:00
|
|
|
fn(): void {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(globToRegExp("unicorn.*") instanceof RegExp, true);
|
|
|
|
assertEquals(globToRegExp("unicorn.*").test("poney.ts"), false);
|
|
|
|
assertEquals(globToRegExp("unicorn.*").test("unicorn.py"), true);
|
|
|
|
assertEquals(globToRegExp("*.ts").test("poney.ts"), true);
|
|
|
|
assertEquals(globToRegExp("*.ts").test("unicorn.js"), false);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("unicorn", "**", "cathedral.ts")).test(
|
2019-03-02 14:56:19 -05:00
|
|
|
join("unicorn", "in", "the", "cathedral.ts")
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("unicorn", "**", "cathedral.ts")).test(
|
2019-03-02 14:56:19 -05:00
|
|
|
join("unicorn", "in", "the", "kitchen.ts")
|
|
|
|
),
|
|
|
|
false
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("unicorn", "**", "bathroom.*")).test(
|
2019-03-02 14:56:19 -05:00
|
|
|
join("unicorn", "sleeping", "in", "bathroom.py")
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("unicorn", "!(sleeping)", "bathroom.ts"), {
|
2019-03-02 14:56:19 -05:00
|
|
|
extended: true
|
|
|
|
}).test(join("unicorn", "flying", "bathroom.ts")),
|
|
|
|
true
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("unicorn", "(!sleeping)", "bathroom.ts"), {
|
2019-03-02 14:56:19 -05:00
|
|
|
extended: true
|
|
|
|
}).test(join("unicorn", "sleeping", "bathroom.ts")),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-03-02 14:56:19 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await touch(d + "/a/x.ts");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function globInWalk(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const arr = await walkArray(".", { match: [globToRegExp("*.ts")] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
2019-05-14 17:14:08 -04:00
|
|
|
assertEquals(arr[0], "a/x.ts");
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-03-02 14:56:19 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await mkdir(d + "/b");
|
|
|
|
await touch(d + "/a/x.ts");
|
|
|
|
await touch(d + "/b/z.ts");
|
|
|
|
await touch(d + "/b/z.js");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function globInWalkWildcardFiles(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const arr = await walkArray(".", { match: [globToRegExp("*.ts")] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 2);
|
2019-05-14 17:14:08 -04:00
|
|
|
assertEquals(arr[0], "a/x.ts");
|
|
|
|
assertEquals(arr[1], "b/z.ts");
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-03-02 14:56:19 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await mkdir(d + "/a/yo");
|
|
|
|
await touch(d + "/a/yo/x.ts");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function globInWalkFolderWildcard(): Promise<void> {
|
2019-03-02 14:56:19 -05:00
|
|
|
const arr = await walkArray(".", {
|
|
|
|
match: [
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("a", "**", "*.ts"), {
|
2019-03-02 14:56:19 -05:00
|
|
|
flags: "g",
|
|
|
|
globstar: true
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
2019-05-14 17:14:08 -04:00
|
|
|
assertEquals(arr[0], "a/yo/x.ts");
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-04 13:06:05 -05:00
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-03-04 13:06:05 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await mkdir(d + "/a/unicorn");
|
|
|
|
await mkdir(d + "/a/deno");
|
|
|
|
await mkdir(d + "/a/raptor");
|
|
|
|
await touch(d + "/a/raptor/x.ts");
|
|
|
|
await touch(d + "/a/deno/x.ts");
|
|
|
|
await touch(d + "/a/unicorn/x.ts");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function globInWalkFolderExtended(): Promise<void> {
|
2019-03-04 13:06:05 -05:00
|
|
|
const arr = await walkArray(".", {
|
|
|
|
match: [
|
2019-10-02 13:59:27 -04:00
|
|
|
globToRegExp(join("a", "+(raptor|deno)", "*.ts"), {
|
2019-03-04 13:06:05 -05:00
|
|
|
flags: "g",
|
|
|
|
extended: true
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 2);
|
2019-05-14 17:14:08 -04:00
|
|
|
assertEquals(arr[0], "a/deno/x.ts");
|
|
|
|
assertEquals(arr[1], "a/raptor/x.ts");
|
2019-03-04 13:06:05 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-02 14:56:19 -05:00
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-03-02 14:56:19 -05:00
|
|
|
await touch(d + "/x.ts");
|
|
|
|
await touch(d + "/x.js");
|
|
|
|
await touch(d + "/b.js");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function globInWalkWildcardExtension(): Promise<void> {
|
2019-03-02 14:56:19 -05:00
|
|
|
const arr = await walkArray(".", {
|
2019-10-02 13:59:27 -04:00
|
|
|
match: [globToRegExp("x.*", { flags: "g", globstar: true })]
|
2019-03-02 14:56:19 -05:00
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 2);
|
2019-05-14 17:14:08 -04:00
|
|
|
assertEquals(arr[0], "x.js");
|
|
|
|
assertEquals(arr[1], "x.ts");
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2019-05-23 03:23:31 -04:00
|
|
|
test({
|
|
|
|
name: "isGlob: pattern to test",
|
|
|
|
fn(): void {
|
|
|
|
// should be true if valid glob pattern
|
|
|
|
assert(isGlob("!foo.js"));
|
|
|
|
assert(isGlob("*.js"));
|
|
|
|
assert(isGlob("!*.js"));
|
|
|
|
assert(isGlob("!foo"));
|
|
|
|
assert(isGlob("!foo.js"));
|
|
|
|
assert(isGlob("**/abc.js"));
|
|
|
|
assert(isGlob("abc/*.js"));
|
|
|
|
assert(isGlob("@.(?:abc)"));
|
|
|
|
assert(isGlob("@.(?!abc)"));
|
|
|
|
|
|
|
|
// should be false if invalid glob pattern
|
|
|
|
assert(!isGlob(""));
|
|
|
|
assert(!isGlob("~/abc"));
|
|
|
|
assert(!isGlob("~/abc"));
|
|
|
|
assert(!isGlob("~/(abc)"));
|
|
|
|
assert(!isGlob("+~(abc)"));
|
|
|
|
assert(!isGlob("."));
|
|
|
|
assert(!isGlob("@.(abc)"));
|
|
|
|
assert(!isGlob("aa"));
|
|
|
|
assert(!isGlob("who?"));
|
|
|
|
assert(!isGlob("why!?"));
|
|
|
|
assert(!isGlob("where???"));
|
|
|
|
assert(!isGlob("abc!/def/!ghi.js"));
|
|
|
|
assert(!isGlob("abc.js"));
|
|
|
|
assert(!isGlob("abc/def/!ghi.js"));
|
|
|
|
assert(!isGlob("abc/def/ghi.js"));
|
|
|
|
|
|
|
|
// Should be true if path has regex capture group
|
|
|
|
assert(isGlob("abc/(?!foo).js"));
|
|
|
|
assert(isGlob("abc/(?:foo).js"));
|
|
|
|
assert(isGlob("abc/(?=foo).js"));
|
|
|
|
assert(isGlob("abc/(a|b).js"));
|
|
|
|
assert(isGlob("abc/(a|b|c).js"));
|
|
|
|
assert(isGlob("abc/(foo bar)/*.js"));
|
|
|
|
|
|
|
|
// Should be false if the path has parens but is not a valid capture group
|
|
|
|
assert(!isGlob("abc/(?foo).js"));
|
|
|
|
assert(!isGlob("abc/(a b c).js"));
|
|
|
|
assert(!isGlob("abc/(ab).js"));
|
|
|
|
assert(!isGlob("abc/(abc).js"));
|
|
|
|
assert(!isGlob("abc/(foo bar).js"));
|
|
|
|
|
|
|
|
// should be false if the capture group is imbalanced
|
|
|
|
assert(!isGlob("abc/(?ab.js"));
|
|
|
|
assert(!isGlob("abc/(ab.js"));
|
|
|
|
assert(!isGlob("abc/(a|b.js"));
|
|
|
|
assert(!isGlob("abc/(a|b|c.js"));
|
|
|
|
|
|
|
|
// should be true if the path has a regex character class
|
|
|
|
assert(isGlob("abc/[abc].js"));
|
|
|
|
assert(isGlob("abc/[^abc].js"));
|
|
|
|
assert(isGlob("abc/[1-3].js"));
|
|
|
|
|
|
|
|
// should be false if the character class is not balanced
|
|
|
|
assert(!isGlob("abc/[abc.js"));
|
|
|
|
assert(!isGlob("abc/[^abc.js"));
|
|
|
|
assert(!isGlob("abc/[1-3.js"));
|
|
|
|
|
|
|
|
// should be false if the character class is escaped
|
|
|
|
assert(!isGlob("abc/\\[abc].js"));
|
|
|
|
assert(!isGlob("abc/\\[^abc].js"));
|
|
|
|
assert(!isGlob("abc/\\[1-3].js"));
|
|
|
|
|
|
|
|
// should be true if the path has brace characters
|
|
|
|
assert(isGlob("abc/{a,b}.js"));
|
|
|
|
assert(isGlob("abc/{a..z}.js"));
|
|
|
|
assert(isGlob("abc/{a..z..2}.js"));
|
|
|
|
|
|
|
|
// should be false if (basic) braces are not balanced
|
|
|
|
assert(!isGlob("abc/\\{a,b}.js"));
|
|
|
|
assert(!isGlob("abc/\\{a..z}.js"));
|
|
|
|
assert(!isGlob("abc/\\{a..z..2}.js"));
|
|
|
|
|
|
|
|
// should be true if the path has regex characters
|
|
|
|
assert(isGlob("!&(abc)"));
|
|
|
|
assert(isGlob("!*.js"));
|
|
|
|
assert(isGlob("!foo"));
|
|
|
|
assert(isGlob("!foo.js"));
|
|
|
|
assert(isGlob("**/abc.js"));
|
|
|
|
assert(isGlob("*.js"));
|
|
|
|
assert(isGlob("*z(abc)"));
|
|
|
|
assert(isGlob("[1-10].js"));
|
|
|
|
assert(isGlob("[^abc].js"));
|
|
|
|
assert(isGlob("[a-j]*[^c]b/c"));
|
|
|
|
assert(isGlob("[abc].js"));
|
|
|
|
assert(isGlob("a/b/c/[a-z].js"));
|
|
|
|
assert(isGlob("abc/(aaa|bbb).js"));
|
|
|
|
assert(isGlob("abc/*.js"));
|
|
|
|
assert(isGlob("abc/{a,b}.js"));
|
|
|
|
assert(isGlob("abc/{a..z..2}.js"));
|
|
|
|
assert(isGlob("abc/{a..z}.js"));
|
|
|
|
|
|
|
|
assert(!isGlob("$(abc)"));
|
|
|
|
assert(!isGlob("&(abc)"));
|
|
|
|
assert(!isGlob("Who?.js"));
|
|
|
|
assert(!isGlob("? (abc)"));
|
|
|
|
assert(!isGlob("?.js"));
|
|
|
|
assert(!isGlob("abc/?.js"));
|
|
|
|
|
|
|
|
// should be false if regex characters are escaped
|
|
|
|
assert(!isGlob("\\?.js"));
|
|
|
|
assert(!isGlob("\\[1-10\\].js"));
|
|
|
|
assert(!isGlob("\\[^abc\\].js"));
|
|
|
|
assert(!isGlob("\\[a-j\\]\\*\\[^c\\]b/c"));
|
|
|
|
assert(!isGlob("\\[abc\\].js"));
|
|
|
|
assert(!isGlob("\\a/b/c/\\[a-z\\].js"));
|
|
|
|
assert(!isGlob("abc/\\(aaa|bbb).js"));
|
|
|
|
assert(!isGlob("abc/\\?.js"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-02 13:59:27 -04:00
|
|
|
test(function normalizeGlobGlobstar(): void {
|
|
|
|
assertEquals(normalizeGlob(`**${SEP}..`, { globstar: true }), `**${SEP}..`);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function joinGlobsGlobstar(): void {
|
|
|
|
assertEquals(joinGlobs(["**", ".."], { globstar: true }), `**${SEP}..`);
|
|
|
|
});
|
|
|
|
|
2019-09-28 09:33:17 -04:00
|
|
|
async function expandGlobArray(
|
|
|
|
globString: string,
|
|
|
|
options: ExpandGlobOptions
|
|
|
|
): Promise<string[]> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const paths: string[] = [];
|
|
|
|
for await (const { filename } of expandGlob(globString, options)) {
|
|
|
|
paths.push(filename);
|
2019-09-28 09:33:17 -04:00
|
|
|
}
|
2019-10-02 13:59:27 -04:00
|
|
|
paths.sort();
|
|
|
|
const pathsSync = [...expandGlobSync(globString, options)].map(
|
|
|
|
({ filename }): string => filename
|
|
|
|
);
|
|
|
|
pathsSync.sort();
|
|
|
|
assertEquals(paths, pathsSync);
|
2019-09-28 09:33:17 -04:00
|
|
|
const root = normalize(options.root || cwd());
|
|
|
|
for (const path of paths) {
|
|
|
|
assert(path.startsWith(root));
|
|
|
|
}
|
2019-10-02 13:59:27 -04:00
|
|
|
const relativePaths = paths.map(
|
|
|
|
(path: string): string => relative(root, path) || "."
|
2019-09-28 09:33:17 -04:00
|
|
|
);
|
|
|
|
relativePaths.sort();
|
|
|
|
return relativePaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
function urlToFilePath(url: URL): string {
|
|
|
|
// Since `new URL('file:///C:/a').pathname` is `/C:/a`, remove leading slash.
|
|
|
|
return url.pathname.slice(url.protocol == "file:" && isWindows ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:59:27 -04:00
|
|
|
const EG_OPTIONS: ExpandGlobOptions = {
|
2019-09-28 09:33:17 -04:00
|
|
|
root: urlToFilePath(new URL(join("testdata", "glob"), import.meta.url)),
|
|
|
|
includeDirs: true,
|
|
|
|
extended: false,
|
2019-10-02 13:59:27 -04:00
|
|
|
globstar: false
|
2019-09-28 09:33:17 -04:00
|
|
|
};
|
|
|
|
|
2019-10-02 13:59:27 -04:00
|
|
|
test(async function expandGlobWildcard(): Promise<void> {
|
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("*", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
|
|
|
"subdir"
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(async function expandGlobTrailingSeparator(): Promise<void> {
|
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("*/", options), ["subdir"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(async function expandGlobParent(): Promise<void> {
|
|
|
|
const options = EG_OPTIONS;
|
|
|
|
assertEquals(await expandGlobArray("subdir/../*", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi",
|
|
|
|
"subdir"
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2019-09-28 09:33:17 -04:00
|
|
|
test(async function expandGlobExt(): Promise<void> {
|
|
|
|
const options = { ...EG_OPTIONS, extended: true };
|
|
|
|
assertEquals(await expandGlobArray("abc?(def|ghi)", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef"
|
|
|
|
]);
|
|
|
|
assertEquals(await expandGlobArray("abc*(def|ghi)", options), [
|
|
|
|
"abc",
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi"
|
|
|
|
]);
|
|
|
|
assertEquals(await expandGlobArray("abc+(def|ghi)", options), [
|
|
|
|
"abcdef",
|
|
|
|
"abcdefghi"
|
|
|
|
]);
|
|
|
|
assertEquals(await expandGlobArray("abc@(def|ghi)", options), ["abcdef"]);
|
|
|
|
assertEquals(await expandGlobArray("abc{def,ghi}", options), ["abcdef"]);
|
|
|
|
assertEquals(await expandGlobArray("abc!(def|ghi)", options), ["abc"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(async function expandGlobGlobstar(): Promise<void> {
|
|
|
|
const options = { ...EG_OPTIONS, globstar: true };
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(
|
|
|
|
await expandGlobArray(joinGlobs(["**", "abc"], options), options),
|
|
|
|
["abc", join("subdir", "abc")]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(async function expandGlobGlobstarParent(): Promise<void> {
|
|
|
|
const options = { ...EG_OPTIONS, globstar: true };
|
|
|
|
assertEquals(
|
|
|
|
await expandGlobArray(joinGlobs(["subdir", "**", ".."], options), options),
|
|
|
|
["."]
|
|
|
|
);
|
2019-09-28 09:33:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(async function expandGlobIncludeDirs(): Promise<void> {
|
|
|
|
const options = { ...EG_OPTIONS, includeDirs: false };
|
|
|
|
assertEquals(await expandGlobArray("subdir", options), []);
|
|
|
|
});
|
|
|
|
|
2019-05-14 17:14:08 -04:00
|
|
|
runIfMain(import.meta);
|