2020-04-24 18:45:55 -04:00
|
|
|
const { cwd, chdir, makeTempDir, mkdir, create, symlink } = Deno;
|
2019-11-14 22:22:33 -05:00
|
|
|
const { remove } = Deno;
|
2020-04-16 01:40:30 -04:00
|
|
|
import { walk, walkSync, WalkOptions, WalkEntry } from "./walk.ts";
|
2020-03-15 08:03:25 -04:00
|
|
|
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
export function testWalk(
|
2019-05-30 08:59:30 -04:00
|
|
|
setup: (arg0: string) => void | Promise<void>,
|
2020-04-01 04:47:23 -04:00
|
|
|
t: () => void | Promise<void>,
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore = false
|
2020-03-20 09:38:34 -04:00
|
|
|
): void {
|
2019-02-15 11:20:59 -05:00
|
|
|
const name = t.name;
|
2019-03-12 01:51:51 -04:00
|
|
|
async function fn(): Promise<void> {
|
|
|
|
const origCwd = cwd();
|
2019-02-15 11:20:59 -05:00
|
|
|
const d = await makeTempDir();
|
|
|
|
chdir(d);
|
|
|
|
try {
|
|
|
|
await setup(d);
|
|
|
|
await t();
|
|
|
|
} finally {
|
2019-03-12 01:51:51 -04:00
|
|
|
chdir(origCwd);
|
2020-03-18 19:25:55 -04:00
|
|
|
await remove(d, { recursive: true });
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 05:58:12 -04:00
|
|
|
Deno.test({ ignore, name: `[walk] ${name}`, fn });
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
function normalize({ path }: WalkEntry): string {
|
|
|
|
return path.replace(/\\/g, "/");
|
2019-05-14 17:14:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function walkArray(
|
2019-10-02 13:59:27 -04:00
|
|
|
root: string,
|
2019-02-15 11:20:59 -05:00
|
|
|
options: WalkOptions = {}
|
2019-03-12 01:51:51 -04:00
|
|
|
): Promise<string[]> {
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr: string[] = [];
|
2019-05-14 17:14:08 -04:00
|
|
|
for await (const w of walk(root, { ...options })) {
|
|
|
|
arr.push(normalize(w));
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
arr.sort(); // TODO(ry) Remove sort. The order should be deterministic.
|
|
|
|
const arrSync = Array.from(walkSync(root, options), normalize);
|
|
|
|
arrSync.sort(); // TODO(ry) Remove sort. The order should be deterministic.
|
2019-03-12 01:51:51 -04:00
|
|
|
assertEquals(arr, arrSync);
|
2019-02-15 11:20:59 -05:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2019-05-14 17:14:08 -04:00
|
|
|
export async function touch(path: string): Promise<void> {
|
2020-04-24 18:45:55 -04:00
|
|
|
const f = await create(path);
|
2020-03-18 19:25:55 -04:00
|
|
|
f.close();
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2019-03-12 01:51:51 -04:00
|
|
|
function assertReady(expectedLength: number): void {
|
2019-10-02 13:59:27 -04:00
|
|
|
const arr = Array.from(walkSync("."), normalize);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, expectedLength);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await mkdir(d + "/empty");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function emptyDir(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const arr = await walkArray(".");
|
|
|
|
assertEquals(arr, [".", "empty"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function singleFile(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const arr = await walkArray(".");
|
|
|
|
assertEquals(arr, [".", "x"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function iteratable(): Promise<void> {
|
2019-02-15 11:20:59 -05:00
|
|
|
let count = 0;
|
2019-05-14 17:14:08 -04:00
|
|
|
for (const _ of walkSync(".")) {
|
2019-02-15 11:20:59 -05:00
|
|
|
count += 1;
|
|
|
|
}
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(count, 2);
|
2019-05-14 17:14:08 -04:00
|
|
|
for await (const _ of walk(".")) {
|
2019-02-15 11:20:59 -05:00
|
|
|
count += 1;
|
|
|
|
}
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(count, 4);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await touch(d + "/a/x");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function nestedSingleFile(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const arr = await walkArray(".");
|
|
|
|
assertEquals(arr, [".", "a", "a/x"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2020-01-07 14:14:33 -05:00
|
|
|
await mkdir(d + "/a/b/c/d", { recursive: true });
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/a/b/c/d/x");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function depth(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(6);
|
2019-03-12 01:51:51 -04:00
|
|
|
const arr3 = await walkArray(".", { maxDepth: 3 });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr3, [".", "a", "a/b", "a/b/c"]);
|
2019-03-12 01:51:51 -04:00
|
|
|
const arr5 = await walkArray(".", { maxDepth: 5 });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr5, [".", "a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/x"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-09-18 11:37:37 -04:00
|
|
|
testWalk(
|
|
|
|
async (d: string): Promise<void> => {
|
|
|
|
await touch(d + "/a");
|
|
|
|
await mkdir(d + "/b");
|
|
|
|
await touch(d + "/b/c");
|
|
|
|
},
|
|
|
|
async function includeDirs(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(4);
|
|
|
|
const arr = await walkArray(".", { includeDirs: false });
|
|
|
|
assertEquals(arr, ["a", "b/c"]);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
|
|
|
async (d: string): Promise<void> => {
|
|
|
|
await touch(d + "/a");
|
|
|
|
await mkdir(d + "/b");
|
|
|
|
await touch(d + "/b/c");
|
|
|
|
},
|
|
|
|
async function includeFiles(): Promise<void> {
|
|
|
|
assertReady(4);
|
|
|
|
const arr = await walkArray(".", { includeFiles: false });
|
|
|
|
assertEquals(arr, [".", "b"]);
|
2019-09-18 11:37:37 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-02-15 11:20:59 -05:00
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x.ts");
|
|
|
|
await touch(d + "/y.rs");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function ext(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(3);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray(".", { exts: [".ts"] });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, ["x.ts"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x.ts");
|
|
|
|
await touch(d + "/y.rs");
|
|
|
|
await touch(d + "/z.py");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function extAny(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(4);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray(".", { exts: [".rs", ".ts"] });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, ["x.ts", "y.rs"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x");
|
|
|
|
await touch(d + "/y");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function match(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(3);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray(".", { match: [/x/] });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, ["x"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x");
|
|
|
|
await touch(d + "/y");
|
|
|
|
await touch(d + "/z");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function matchAny(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(4);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray(".", { match: [/x/, /y/] });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, ["x", "y"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x");
|
|
|
|
await touch(d + "/y");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function skip(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(3);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray(".", { skip: [/x/] });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, [".", "y"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await touch(d + "/x");
|
|
|
|
await touch(d + "/y");
|
|
|
|
await touch(d + "/z");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function skipAny(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(4);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray(".", { skip: [/x/, /y/] });
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, [".", "z"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await mkdir(d + "/b");
|
|
|
|
await touch(d + "/a/x");
|
|
|
|
await touch(d + "/a/y");
|
|
|
|
await touch(d + "/b/z");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function subDir(): Promise<void> {
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(6);
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray("b");
|
2019-10-02 13:59:27 -04:00
|
|
|
assertEquals(arr, ["b", "b/z"]);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
testWalk(
|
|
|
|
async (_d: string): Promise<void> => {},
|
2019-11-14 22:22:33 -05:00
|
|
|
async function nonexistentRoot(): Promise<void> {
|
2020-02-21 10:36:13 -05:00
|
|
|
await assertThrowsAsync(async () => {
|
2019-11-14 22:22:33 -05:00
|
|
|
await walkArray("nonexistent");
|
2020-02-24 15:48:35 -05:00
|
|
|
}, Deno.errors.NotFound);
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
|
|
|
);
|
2019-02-15 11:20:59 -05:00
|
|
|
|
2020-03-15 08:03:25 -04:00
|
|
|
// TODO(ry) Re-enable followSymlinks
|
2019-02-15 11:20:59 -05:00
|
|
|
testWalk(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (d: string): Promise<void> => {
|
2019-02-15 11:20:59 -05:00
|
|
|
await mkdir(d + "/a");
|
|
|
|
await mkdir(d + "/b");
|
|
|
|
await touch(d + "/a/x");
|
|
|
|
await touch(d + "/a/y");
|
|
|
|
await touch(d + "/b/z");
|
|
|
|
try {
|
|
|
|
await symlink(d + "/b", d + "/a/bb");
|
|
|
|
} catch (err) {
|
2020-04-28 12:35:23 -04:00
|
|
|
assert(Deno.build.os == "windows");
|
2020-04-03 13:49:05 -04:00
|
|
|
assertEquals(err.message, "Not implemented");
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function symlink(): Promise<void> {
|
2019-02-15 11:20:59 -05:00
|
|
|
// symlink is not yet implemented on Windows.
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os == "windows") {
|
2019-02-15 11:20:59 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:59:27 -04:00
|
|
|
assertReady(6);
|
2019-02-15 11:20:59 -05:00
|
|
|
const files = await walkArray("a");
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(files.length, 2);
|
2019-02-15 11:20:59 -05:00
|
|
|
assert(!files.includes("a/bb/z"));
|
|
|
|
|
|
|
|
const arr = await walkArray("a", { followSymlinks: true });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 3);
|
2019-04-24 07:41:23 -04:00
|
|
|
assert(arr.some((f): boolean => f.endsWith("/b/z")));
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
true
|
2019-02-15 11:20:59 -05:00
|
|
|
);
|