std/fs/walk_test.ts

274 lines
6.5 KiB
TypeScript
Raw Normal View History

const { cwd, chdir, makeTempDir, mkdir, open } = Deno;
const { remove } = Deno;
import { walk, walkSync, WalkOptions, WalkInfo } from "./walk.ts";
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
2019-02-15 16:20:59 +00:00
export async function testWalk(
2019-05-30 12:59:30 +00:00
setup: (arg0: string) => void | Promise<void>,
t: Deno.TestFunction
2019-02-15 16:20:59 +00:00
): Promise<void> {
const name = t.name;
2019-03-12 05:51:51 +00:00
async function fn(): Promise<void> {
const origCwd = cwd();
2019-02-15 16:20:59 +00:00
const d = await makeTempDir();
chdir(d);
try {
await setup(d);
await t();
} finally {
2019-03-12 05:51:51 +00:00
chdir(origCwd);
2019-02-15 16:20:59 +00:00
remove(d, { recursive: true });
}
}
Deno.test({ name, fn });
2019-02-15 16:20:59 +00:00
}
function normalize({ filename }: WalkInfo): string {
return filename.replace(/\\/g, "/");
}
export async function walkArray(
root: string,
2019-02-15 16:20:59 +00:00
options: WalkOptions = {}
2019-03-12 05:51:51 +00:00
): Promise<string[]> {
2019-02-15 16:20:59 +00:00
const arr: string[] = [];
for await (const w of walk(root, { ...options })) {
arr.push(normalize(w));
2019-02-15 16:20:59 +00: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 05:51:51 +00:00
assertEquals(arr, arrSync);
2019-02-15 16:20:59 +00:00
return arr;
}
export async function touch(path: string): Promise<void> {
2019-02-15 16:20:59 +00:00
await open(path, "w");
}
2019-03-12 05:51:51 +00:00
function assertReady(expectedLength: number): void {
const arr = Array.from(walkSync("."), normalize);
assertEquals(arr.length, expectedLength);
2019-02-15 16:20:59 +00:00
}
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await mkdir(d + "/empty");
},
2019-04-24 11:41:23 +00:00
async function emptyDir(): Promise<void> {
const arr = await walkArray(".");
assertEquals(arr, [".", "empty"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x");
},
2019-04-24 11:41:23 +00:00
async function singleFile(): Promise<void> {
const arr = await walkArray(".");
assertEquals(arr, [".", "x"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x");
},
2019-04-24 11:41:23 +00:00
async function iteratable(): Promise<void> {
2019-02-15 16:20:59 +00:00
let count = 0;
for (const _ of walkSync(".")) {
2019-02-15 16:20:59 +00:00
count += 1;
}
assertEquals(count, 2);
for await (const _ of walk(".")) {
2019-02-15 16:20:59 +00:00
count += 1;
}
assertEquals(count, 4);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await mkdir(d + "/a");
await touch(d + "/a/x");
},
2019-04-24 11:41:23 +00:00
async function nestedSingleFile(): Promise<void> {
const arr = await walkArray(".");
assertEquals(arr, [".", "a", "a/x"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
await mkdir(d + "/a/b/c/d", { recursive: true });
2019-02-15 16:20:59 +00:00
await touch(d + "/a/b/c/d/x");
},
2019-04-24 11:41:23 +00:00
async function depth(): Promise<void> {
assertReady(6);
2019-03-12 05:51:51 +00:00
const arr3 = await walkArray(".", { maxDepth: 3 });
assertEquals(arr3, [".", "a", "a/b", "a/b/c"]);
2019-03-12 05:51:51 +00:00
const arr5 = await walkArray(".", { maxDepth: 5 });
assertEquals(arr5, [".", "a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/x"]);
2019-02-15 16:20:59 +00:00
}
);
2019-09-18 15:37:37 +00: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> {
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 15:37:37 +00:00
}
);
2019-02-15 16:20:59 +00:00
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x.ts");
await touch(d + "/y.rs");
},
2019-04-24 11:41:23 +00:00
async function ext(): Promise<void> {
assertReady(3);
2019-02-15 16:20:59 +00:00
const arr = await walkArray(".", { exts: [".ts"] });
assertEquals(arr, ["x.ts"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x.ts");
await touch(d + "/y.rs");
await touch(d + "/z.py");
},
2019-04-24 11:41:23 +00:00
async function extAny(): Promise<void> {
assertReady(4);
2019-02-15 16:20:59 +00:00
const arr = await walkArray(".", { exts: [".rs", ".ts"] });
assertEquals(arr, ["x.ts", "y.rs"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x");
await touch(d + "/y");
},
2019-04-24 11:41:23 +00:00
async function match(): Promise<void> {
assertReady(3);
2019-02-15 16:20:59 +00:00
const arr = await walkArray(".", { match: [/x/] });
assertEquals(arr, ["x"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x");
await touch(d + "/y");
await touch(d + "/z");
},
2019-04-24 11:41:23 +00:00
async function matchAny(): Promise<void> {
assertReady(4);
2019-02-15 16:20:59 +00:00
const arr = await walkArray(".", { match: [/x/, /y/] });
assertEquals(arr, ["x", "y"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x");
await touch(d + "/y");
},
2019-04-24 11:41:23 +00:00
async function skip(): Promise<void> {
assertReady(3);
2019-02-15 16:20:59 +00:00
const arr = await walkArray(".", { skip: [/x/] });
assertEquals(arr, [".", "y"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00:00
await touch(d + "/x");
await touch(d + "/y");
await touch(d + "/z");
},
2019-04-24 11:41:23 +00:00
async function skipAny(): Promise<void> {
assertReady(4);
2019-02-15 16:20:59 +00:00
const arr = await walkArray(".", { skip: [/x/, /y/] });
assertEquals(arr, [".", "z"]);
2019-02-15 16:20:59 +00:00
}
);
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00: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 11:41:23 +00:00
async function subDir(): Promise<void> {
assertReady(6);
2019-02-15 16:20:59 +00:00
const arr = await walkArray("b");
assertEquals(arr, ["b", "b/z"]);
2019-02-15 16:20:59 +00:00
}
);
2019-04-24 11:41:23 +00:00
testWalk(
async (_d: string): Promise<void> => {},
async function nonexistentRoot(): Promise<void> {
await assertThrowsAsync(async () => {
await walkArray("nonexistent");
}, Deno.Err.NotFound);
2019-04-24 11:41:23 +00:00
}
);
2019-02-15 16:20:59 +00:00
/* TODO(ry) Re-enable followSymlinks
2019-02-15 16:20:59 +00:00
testWalk(
2019-04-24 11:41:23 +00:00
async (d: string): Promise<void> => {
2019-02-15 16:20:59 +00: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 11:41:23 +00:00
async function symlink(): Promise<void> {
2019-02-15 16:20:59 +00:00
// symlink is not yet implemented on Windows.
if (isWindows) {
return;
}
assertReady(6);
2019-02-15 16:20:59 +00:00
const files = await walkArray("a");
assertEquals(files.length, 2);
2019-02-15 16:20:59 +00:00
assert(!files.includes("a/bb/z"));
const arr = await walkArray("a", { followSymlinks: true });
assertEquals(arr.length, 3);
2019-04-24 11:41:23 +00:00
assert(arr.some((f): boolean => f.endsWith("/b/z")));
2019-02-15 16:20:59 +00:00
}
);
*/