Skip to content

Commit

Permalink
fs/walk.ts: Make includeDirs true by default
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Oct 2, 2019
1 parent b7af9a0 commit 3b1b189
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 64 deletions.
4 changes: 0 additions & 4 deletions fs/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,11 @@ export async function* expandGlob(
} else if (globSegment == "**") {
return yield* walk(walkInfo.filename, {
includeFiles: false,
includeDirs: true,
skip: excludePatterns
});
}
yield* walk(walkInfo.filename, {
maxDepth: 1,
includeDirs: true,
match: [
globToRegExp(
joinGlobs([walkInfo.filename, globSegment], globOptions),
Expand Down Expand Up @@ -317,13 +315,11 @@ export function* expandGlobSync(
} else if (globSegment == "**") {
return yield* walkSync(walkInfo.filename, {
includeFiles: false,
includeDirs: true,
skip: excludePatterns
});
}
yield* walkSync(walkInfo.filename, {
maxDepth: 1,
includeDirs: true,
match: [
globToRegExp(
joinGlobs([walkInfo.filename, globSegment], globOptions),
Expand Down
42 changes: 29 additions & 13 deletions fs/walk.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Documentation and interface for walk were adapted from Go
// https://golang.org/pkg/path/filepath/#Walk
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
const { readDir, readDirSync, stat, statSync } = Deno;
type FileInfo = Deno.FileInfo;
import { unimplemented } from "../testing/asserts.ts";
import { join } from "./path/mod.ts";
const { readDir, readDirSync, stat, statSync } = Deno;
type FileInfo = Deno.FileInfo;

export interface WalkOptions {
maxDepth?: number;
includeFiles?: boolean;
includeDirs?: boolean;
followSymlinks?: boolean;
exts?: string[];
match?: RegExp[];
skip?: RegExp[];
onError?: (err: Error) => void;
followSymlinks?: boolean;
}

function patternTest(patterns: RegExp[], path: string): boolean {
Expand Down Expand Up @@ -55,14 +55,14 @@ export interface WalkInfo {
* directories walk() can be inefficient.
*
* Options:
* - maxDepth?: number;
* - includeFiles?: boolean;
* - includeDirs?: boolean;
* - maxDepth?: number = Infinity;
* - includeFiles?: boolean = true;
* - includeDirs?: boolean = true;
* - followSymlinks?: boolean = false;
* - exts?: string[];
* - match?: RegExp[];
* - skip?: RegExp[];
* - onError?: (err: Error) => void;
* - followSymlinks?: boolean;
*
* for await (const { filename, info } of walk(".")) {
* console.log(filename);
Expand All @@ -77,9 +77,17 @@ export async function* walk(
if (maxDepth < 0) {
return;
}
if (options.includeDirs && include(root, options)) {
const rootInfo = await stat(root);
yield { filename: root, info: rootInfo };
if (options.includeDirs != false && include(root, options)) {
let rootInfo: FileInfo;
try {
rootInfo = await stat(root);
} catch (err) {
if (options.onError) {
options.onError(err);
return;
}
}
yield { filename: root, info: rootInfo! };
}
if (maxDepth < 1 || patternTest(options.skip || [], root)) {
return;
Expand Down Expand Up @@ -123,9 +131,17 @@ export function* walkSync(
if (maxDepth < 0) {
return;
}
if (options.includeDirs && include(root, options)) {
const rootInfo = statSync(root);
yield { filename: root, info: rootInfo };
if (options.includeDirs != false && include(root, options)) {
let rootInfo: FileInfo;
try {
rootInfo = statSync(root);
} catch (err) {
if (options.onError) {
options.onError(err);
return;
}
}
yield { filename: root, info: rootInfo! };
}
if (maxDepth < 1 || patternTest(options.skip || [], root)) {
return;
Expand Down
77 changes: 30 additions & 47 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ testWalk(
},
async function emptyDir(): Promise<void> {
const arr = await walkArray(".");
assertEquals(arr.length, 0);
assertEquals(arr, [".", "empty"]);
}
);

Expand All @@ -68,8 +68,7 @@ testWalk(
},
async function singleFile(): Promise<void> {
const arr = await walkArray(".");
assertEquals(arr.length, 1);
assertEquals(arr[0], "x");
assertEquals(arr, [".", "x"]);
}
);

Expand All @@ -82,11 +81,11 @@ testWalk(
for (const _ of walkSync(".")) {
count += 1;
}
assertEquals(count, 1);
assertEquals(count, 2);
for await (const _ of walk(".")) {
count += 1;
}
assertEquals(count, 2);
assertEquals(count, 4);
}
);

Expand All @@ -97,8 +96,7 @@ testWalk(
},
async function nestedSingleFile(): Promise<void> {
const arr = await walkArray(".");
assertEquals(arr.length, 1);
assertEquals(arr[0], "a/x");
assertEquals(arr, [".", "a", "a/x"]);
}
);

Expand All @@ -108,12 +106,11 @@ testWalk(
await touch(d + "/a/b/c/d/x");
},
async function depth(): Promise<void> {
assertReady(1);
assertReady(6);
const arr3 = await walkArray(".", { maxDepth: 3 });
assertEquals(arr3.length, 0);
assertEquals(arr3, [".", "a", "a/b", "a/b/c"]);
const arr5 = await walkArray(".", { maxDepth: 5 });
assertEquals(arr5.length, 1);
assertEquals(arr5[0], "a/b/c/d/x");
assertEquals(arr5, [".", "a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/x"]);
}
);

Expand All @@ -124,10 +121,9 @@ testWalk(
await touch(d + "/b/c");
},
async function includeDirs(): Promise<void> {
assertReady(2);
const arr = await walkArray(".", { includeDirs: true });
assertEquals(arr.length, 4);
assertEquals(arr, [".", "a", "b", "b/c"]);
assertReady(4);
const arr = await walkArray(".", { includeDirs: false });
assertEquals(arr, ["a", "b/c"]);
}
);

Expand All @@ -138,12 +134,8 @@ testWalk(
await touch(d + "/b/c");
},
async function includeFiles(): Promise<void> {
assertReady(2);
const arr = await walkArray(".", {
includeDirs: true,
includeFiles: false
});
assertEquals(arr.length, 2);
assertReady(4);
const arr = await walkArray(".", { includeFiles: false });
assertEquals(arr, [".", "b"]);
}
);
Expand All @@ -154,10 +146,9 @@ testWalk(
await touch(d + "/y.rs");
},
async function ext(): Promise<void> {
assertReady(2);
assertReady(3);
const arr = await walkArray(".", { exts: [".ts"] });
assertEquals(arr.length, 1);
assertEquals(arr[0], "x.ts");
assertEquals(arr, ["x.ts"]);
}
);

Expand All @@ -168,11 +159,9 @@ testWalk(
await touch(d + "/z.py");
},
async function extAny(): Promise<void> {
assertReady(3);
assertReady(4);
const arr = await walkArray(".", { exts: [".rs", ".ts"] });
assertEquals(arr.length, 2);
assertEquals(arr[0], "x.ts");
assertEquals(arr[1], "y.rs");
assertEquals(arr, ["x.ts", "y.rs"]);
}
);

Expand All @@ -182,10 +171,9 @@ testWalk(
await touch(d + "/y");
},
async function match(): Promise<void> {
assertReady(2);
assertReady(3);
const arr = await walkArray(".", { match: [/x/] });
assertEquals(arr.length, 1);
assertEquals(arr[0], "x");
assertEquals(arr, ["x"]);
}
);

Expand All @@ -196,11 +184,9 @@ testWalk(
await touch(d + "/z");
},
async function matchAny(): Promise<void> {
assertReady(3);
assertReady(4);
const arr = await walkArray(".", { match: [/x/, /y/] });
assertEquals(arr.length, 2);
assertEquals(arr[0], "x");
assertEquals(arr[1], "y");
assertEquals(arr, ["x", "y"]);
}
);

Expand All @@ -210,10 +196,9 @@ testWalk(
await touch(d + "/y");
},
async function skip(): Promise<void> {
assertReady(2);
assertReady(3);
const arr = await walkArray(".", { skip: [/x/] });
assertEquals(arr.length, 1);
assertEquals(arr[0], "y");
assertEquals(arr, [".", "y"]);
}
);

Expand All @@ -224,10 +209,9 @@ testWalk(
await touch(d + "/z");
},
async function skipAny(): Promise<void> {
assertReady(3);
assertReady(4);
const arr = await walkArray(".", { skip: [/x/, /y/] });
assertEquals(arr.length, 1);
assertEquals(arr[0], "z");
assertEquals(arr, [".", "z"]);
}
);

Expand All @@ -240,19 +224,18 @@ testWalk(
await touch(d + "/b/z");
},
async function subDir(): Promise<void> {
assertReady(3);
assertReady(6);
const arr = await walkArray("b");
assertEquals(arr.length, 1);
assertEquals(arr[0], "b/z");
assertEquals(arr, ["b", "b/z"]);
}
);

testWalk(
async (_d: string): Promise<void> => {},
async function onError(): Promise<void> {
assertReady(0);
assertReady(1);
const ignored = await walkArray("missing");
assertEquals(ignored.length, 0);
assertEquals(ignored, ["missing"]);
let errors = 0;
await walkArray("missing", { onError: (_e): number => (errors += 1) });
// It's 2 since walkArray iterates over both sync and async.
Expand Down Expand Up @@ -281,7 +264,7 @@ testWalk(
return;
}
assertReady(3);
assertReady(6);
const files = await walkArray("a");
assertEquals(files.length, 2);
assert(!files.includes("a/bb/z"));
Expand Down

0 comments on commit 3b1b189

Please sign in to comment.