Skip to content

Commit

Permalink
fix: PathRef - walk - resolve path before walking (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Mar 15, 2023
1 parent 6c1def7 commit 9374eab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Deno.test("expandGlob", async () => {

Deno.test("walk", async () => {
await withTempDir(async () => {
const dir = createPathRef("rootDir").mkdirSync().resolve();
const dir = createPathRef("rootDir").mkdirSync();
dir.join("file1").writeTextSync("");
dir.join("file2").writeTextSync("");
const subDir = dir.join("dir").join("subDir");
Expand All @@ -398,6 +398,7 @@ Deno.test("walk", async () => {
}
const entries2 = Array.from(dir.walkSync()).map((e) => e.path);

assertEquals(entries1[0].toString(), dir.resolve().toString());
assertEquals(entries1, entries2);
assertEquals(entries1.length, 6);
const entryNames = entries1.map((e) => e.basename());
Expand All @@ -413,6 +414,7 @@ Deno.test("walk", async () => {
const subDir2 = dir.join("dir2");
subDir2.join("other.txt").writeTextSync("");
const entries3 = Array.from(subDir2.walkSync()).map((e) => e.path);
assertEquals(entries3[0].toString(), subDir2.resolve().toString());
assertEquals(entries3.length, 2);
assertEquals(entries3[0].basename(), "dir2");
assertEquals(entries3[1].basename(), "other.txt");
Expand Down
10 changes: 6 additions & 4 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class PathRef {
/** Expands the glob using the current path as the root. */
async *expandGlob(glob: string | URL, options?: Omit<fs.ExpandGlobOptions, "root">) {
const entries = fs.expandGlob(glob, {
root: this.#path,
root: this.resolve().toString(),
...options,
});
for await (const entry of entries) {
Expand All @@ -301,7 +301,7 @@ export class PathRef {
/** Synchronously expands the glob using the current path as the root. */
*expandGlobSync(glob: string | URL, options?: Omit<fs.ExpandGlobOptions, "root">) {
const entries = fs.expandGlobSync(glob, {
root: this.#path,
root: this.resolve().toString(),
...options,
});
for (const entry of entries) {
Expand All @@ -312,15 +312,17 @@ export class PathRef {
/** Walks the file tree rooted at the current path, yielding each file or
* directory in the tree filtered according to the given options. */
async *walk(options?: fs.WalkOptions): AsyncIterableIterator<WalkEntry> {
for await (const entry of fs.walk(this.toString(), options)) {
// Resolve the path before walking so that these paths always point to
// absolute paths in the case that someone changes the cwd after walking.
for await (const entry of fs.walk(this.resolve().toString(), options)) {
yield this.#stdWalkEntryToDax(entry);
}
}

/** Synchronously walks the file tree rooted at the current path, yielding each
* file or directory in the tree filtered according to the given options. */
*walkSync(options?: fs.WalkOptions): Iterable<WalkEntry> {
for (const entry of fs.walkSync(this.toString(), options)) {
for (const entry of fs.walkSync(this.resolve().toString(), options)) {
yield this.#stdWalkEntryToDax(entry);
}
}
Expand Down

0 comments on commit 9374eab

Please sign in to comment.