Skip to content

Commit

Permalink
fix(fs): no root path in walk error
Browse files Browse the repository at this point in the history
This adds the root path to error message.

Fix #863
  • Loading branch information
Nautigsam committed Apr 27, 2021
1 parent 7498d9d commit c285075
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 45 deletions.
104 changes: 60 additions & 44 deletions fs/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,41 @@ export async function* walk(
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
return;
}
for await (const entry of Deno.readDir(root)) {
assert(entry.name != null);
let path = join(root, entry.name);
try {
for await (const entry of Deno.readDir(root)) {
assert(entry.name != null);
let path = join(root, entry.name);

if (entry.isSymlink) {
if (followSymlinks) {
path = await Deno.realPath(path);
} else {
continue;
if (entry.isSymlink) {
if (followSymlinks) {
path = await Deno.realPath(path);
} else {
continue;
}
}
}

if (entry.isFile) {
if (includeFiles && include(path, exts, match, skip)) {
yield { path, ...entry };
if (entry.isFile) {
if (includeFiles && include(path, exts, match, skip)) {
yield { path, ...entry };
}
} else {
yield* walk(path, {
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
followSymlinks,
exts,
match,
skip,
});
}
} else {
yield* walk(path, {
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
followSymlinks,
exts,
match,
skip,
});
}
} catch (err) {
if (!err.root) {
err.root = root;
err.message = `${err.message} for path "${normalize(root)}"`
}
throw err;
}
}

Expand All @@ -157,32 +165,40 @@ export function* walkSync(
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
return;
}
for (const entry of Deno.readDirSync(root)) {
assert(entry.name != null);
let path = join(root, entry.name);
try {
for (const entry of Deno.readDirSync(root)) {
assert(entry.name != null);
let path = join(root, entry.name);

if (entry.isSymlink) {
if (followSymlinks) {
path = Deno.realPathSync(path);
} else {
continue;
if (entry.isSymlink) {
if (followSymlinks) {
path = Deno.realPathSync(path);
} else {
continue;
}
}
}

if (entry.isFile) {
if (includeFiles && include(path, exts, match, skip)) {
yield { path, ...entry };
if (entry.isFile) {
if (includeFiles && include(path, exts, match, skip)) {
yield { path, ...entry };
}
} else {
yield* walkSync(path, {
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
followSymlinks,
exts,
match,
skip,
});
}
} else {
yield* walkSync(path, {
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
followSymlinks,
exts,
match,
skip,
});
}
} catch (err) {
if (!err.root) {
err.root = root;
err.message = `${err.message} for path "${normalize(root)}"`
}
throw err;
}
}
45 changes: 44 additions & 1 deletion fs/walk_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { walk, WalkEntry, WalkOptions, walkSync } from "./walk.ts";
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import {
assert,
assertEquals,
assertThrows,
assertThrowsAsync,
} from "../testing/asserts.ts";

export function testWalk(
setup: (arg0: string) => void | Promise<void>,
Expand Down Expand Up @@ -260,3 +265,41 @@ testWalk(
assert(arr.some((f): boolean => f.endsWith("/b/z")));
},
);

testWalk(
async (d: string) => {
await Deno.mkdir(d + "/a/b", { recursive: true });
await Deno.chmod(d + "/a/b", 0o000);
},
async function subDirNoPermissionAsync() {
try {
await assertThrowsAsync(
async () => {
await walkArray("a");
},
Deno.errors.PermissionDenied, 'for path "a/b"'
);
} finally {
await Deno.chmod("a/b", 0o755);
}
},
);

testWalk(
async (d: string) => {
await Deno.mkdir(d + "/a/b", { recursive: true });
await Deno.chmod(d + "/a/b", 0o000);
},
async function subDirNoPermissionSync() {
try {
assertThrows(
() => {
return [...walkSync("a")];
},
Deno.errors.PermissionDenied, 'for path "a/b"'
);
} finally {
await Deno.chmod("a/b", 0o755);
}
},
);

0 comments on commit c285075

Please sign in to comment.