2019-05-14 17:14:08 -04:00
|
|
|
const { mkdir } = Deno;
|
2019-03-07 19:25:16 -05:00
|
|
|
type FileInfo = Deno.FileInfo;
|
2019-05-14 17:14:08 -04:00
|
|
|
import { test, runIfMain } from "../testing/mod.ts";
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-03-02 14:56:19 -05:00
|
|
|
import { glob } from "./glob.ts";
|
|
|
|
import { join } from "./path.ts";
|
|
|
|
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-03-06 19:42:24 -05:00
|
|
|
assertEquals(glob("unicorn.*") instanceof RegExp, true);
|
|
|
|
assertEquals(glob("unicorn.*").test("poney.ts"), false);
|
|
|
|
assertEquals(glob("unicorn.*").test("unicorn.py"), true);
|
|
|
|
assertEquals(glob("*.ts").test("poney.ts"), true);
|
|
|
|
assertEquals(glob("*.ts").test("unicorn.js"), false);
|
|
|
|
assertEquals(
|
2019-03-02 14:56:19 -05:00
|
|
|
glob(join("unicorn", "**", "cathedral.ts")).test(
|
|
|
|
join("unicorn", "in", "the", "cathedral.ts")
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-02 14:56:19 -05:00
|
|
|
glob(join("unicorn", "**", "cathedral.ts")).test(
|
|
|
|
join("unicorn", "in", "the", "kitchen.ts")
|
|
|
|
),
|
|
|
|
false
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-02 14:56:19 -05:00
|
|
|
glob(join("unicorn", "**", "bathroom.*")).test(
|
|
|
|
join("unicorn", "sleeping", "in", "bathroom.py")
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-02 14:56:19 -05:00
|
|
|
glob(join("unicorn", "!(sleeping)", "bathroom.ts"), {
|
|
|
|
extended: true
|
|
|
|
}).test(join("unicorn", "flying", "bathroom.ts")),
|
|
|
|
true
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-02 14:56:19 -05:00
|
|
|
glob(join("unicorn", "(!sleeping)", "bathroom.ts"), {
|
|
|
|
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-03-02 14:56:19 -05:00
|
|
|
const arr = await walkArray(".", { match: [glob("*.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-03-02 14:56:19 -05:00
|
|
|
const arr = await walkArray(".", { match: [glob("*.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: [
|
|
|
|
glob(join("a", "**", "*.ts"), {
|
|
|
|
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: [
|
|
|
|
glob(join("a", "+(raptor|deno)", "*.ts"), {
|
|
|
|
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-03-04 13:06:05 -05:00
|
|
|
match: [glob("x.*", { flags: "g", globstar: true })]
|
2019-03-02 14:56:19 -05:00
|
|
|
});
|
|
|
|
console.log(arr);
|
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
|
|
|
|
|
|
|
runIfMain(import.meta);
|