2019-03-07 09:07:12 -05:00
|
|
|
const { cwd, chdir, makeTempDir, mkdir, open, build, remove, symlink } = Deno;
|
2019-03-07 19:25:16 -05:00
|
|
|
type FileInfo = Deno.FileInfo;
|
2019-02-15 11:20:59 -05:00
|
|
|
import { walk, walkSync, WalkOptions } from "./walk.ts";
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test, TestFunction } from "../testing/mod.ts";
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assert, assertEquals } from "../testing/asserts.ts";
|
2019-02-15 11:20:59 -05:00
|
|
|
|
2019-03-07 09:07:12 -05:00
|
|
|
const isWindows = build.os === "win";
|
2019-02-15 11:20:59 -05:00
|
|
|
|
2019-03-02 14:56:19 -05:00
|
|
|
export async function testWalk(
|
2019-02-15 11:20:59 -05:00
|
|
|
setup: (string) => void | Promise<void>,
|
|
|
|
t: TestFunction
|
|
|
|
): Promise<void> {
|
|
|
|
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);
|
2019-02-15 11:20:59 -05:00
|
|
|
remove(d, { recursive: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test({ name, fn });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function walkArray(
|
|
|
|
dirname: string = ".",
|
|
|
|
options: WalkOptions = {}
|
2019-03-12 01:51:51 -04:00
|
|
|
): Promise<string[]> {
|
2019-02-15 11:20:59 -05:00
|
|
|
const arr: string[] = [];
|
|
|
|
for await (const f of walk(dirname, { ...options })) {
|
|
|
|
arr.push(f.path.replace(/\\/g, "/"));
|
|
|
|
}
|
|
|
|
arr.sort();
|
2019-04-24 07:41:23 -04:00
|
|
|
const arrSync = Array.from(
|
|
|
|
walkSync(dirname, options),
|
|
|
|
(f: FileInfo): string => f.path.replace(/\\/g, "/")
|
2019-02-15 11:20:59 -05:00
|
|
|
).sort();
|
2019-03-12 01:51:51 -04:00
|
|
|
assertEquals(arr, arrSync);
|
2019-02-15 11:20:59 -05:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function touch(path: string): Promise<void> {
|
|
|
|
await open(path, "w");
|
|
|
|
}
|
2019-03-12 01:51:51 -04:00
|
|
|
function assertReady(expectedLength: number): void {
|
2019-04-24 07:41:23 -04:00
|
|
|
const arr = Array.from(walkSync(), (f: FileInfo): string => f.path);
|
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-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray();
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 0);
|
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-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray();
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "./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-03-12 01:51:51 -04:00
|
|
|
for (const _ of walkSync()) {
|
2019-02-15 11:20:59 -05:00
|
|
|
count += 1;
|
|
|
|
}
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(count, 1);
|
2019-03-12 01:51:51 -04:00
|
|
|
for await (const _ of walk()) {
|
2019-02-15 11:20:59 -05:00
|
|
|
count += 1;
|
|
|
|
}
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(count, 2);
|
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-02-15 11:20:59 -05:00
|
|
|
const arr = await walkArray();
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "./a/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 mkdir(d + "/a/b/c/d", true);
|
|
|
|
await touch(d + "/a/b/c/d/x");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function depth(): Promise<void> {
|
2019-02-15 11:20:59 -05:00
|
|
|
assertReady(1);
|
2019-03-12 01:51:51 -04:00
|
|
|
const arr3 = await walkArray(".", { maxDepth: 3 });
|
|
|
|
assertEquals(arr3.length, 0);
|
|
|
|
const arr5 = await walkArray(".", { maxDepth: 5 });
|
|
|
|
assertEquals(arr5.length, 1);
|
|
|
|
assertEquals(arr5[0], "./a/b/c/d/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.ts");
|
|
|
|
await touch(d + "/y.rs");
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
async function ext(): Promise<void> {
|
2019-02-15 11:20:59 -05:00
|
|
|
assertReady(2);
|
|
|
|
const arr = await walkArray(".", { exts: [".ts"] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "./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-02-15 11:20:59 -05:00
|
|
|
assertReady(3);
|
|
|
|
const arr = await walkArray(".", { exts: [".rs", ".ts"] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 2);
|
|
|
|
assertEquals(arr[0], "./x.ts");
|
|
|
|
assertEquals(arr[1], "./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-02-15 11:20:59 -05:00
|
|
|
assertReady(2);
|
|
|
|
const arr = await walkArray(".", { match: [/x/] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "./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-02-15 11:20:59 -05:00
|
|
|
assertReady(3);
|
|
|
|
const arr = await walkArray(".", { match: [/x/, /y/] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 2);
|
|
|
|
assertEquals(arr[0], "./x");
|
|
|
|
assertEquals(arr[1], "./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-02-15 11:20:59 -05:00
|
|
|
assertReady(2);
|
|
|
|
const arr = await walkArray(".", { skip: [/x/] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "./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-02-15 11:20:59 -05:00
|
|
|
assertReady(3);
|
|
|
|
const arr = await walkArray(".", { skip: [/x/, /y/] });
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "./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-02-15 11:20:59 -05:00
|
|
|
assertReady(3);
|
|
|
|
const arr = await walkArray("b");
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr.length, 1);
|
|
|
|
assertEquals(arr[0], "b/z");
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
testWalk(
|
|
|
|
async (_d: string): Promise<void> => {},
|
|
|
|
async function onError(): Promise<void> {
|
|
|
|
assertReady(0);
|
|
|
|
const ignored = await walkArray("missing");
|
|
|
|
assertEquals(ignored.length, 0);
|
|
|
|
let errors = 0;
|
|
|
|
await walkArray("missing", { onError: (_e): number => (errors += 1) });
|
|
|
|
// It's 2 since walkArray iterates over both sync and async.
|
|
|
|
assertEquals(errors, 2);
|
|
|
|
}
|
|
|
|
);
|
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) {
|
|
|
|
assert(isWindows);
|
|
|
|
assert(err.message, "Not implemented");
|
|
|
|
}
|
|
|
|
},
|
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.
|
|
|
|
if (isWindows) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertReady(3);
|
|
|
|
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")));
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
);
|