Skip to content

Commit

Permalink
test(fs): walk tests write to temp dirs (denoland#5358)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbronder committed Dec 4, 2024
1 parent d6c34d7 commit 4186412
Showing 1 changed file with 161 additions and 52 deletions.
213 changes: 161 additions & 52 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import {
assertRejects,
assertThrows,
} from "@std/assert";
import { copy, copySync } from "./copy.ts";
import { fromFileUrl, resolve } from "@std/path";

const testdataDir = resolve(fromFileUrl(import.meta.url), "../testdata/walk");

async function assertWalkPaths(
parentDir: string,
rootPath: string,
expectedPaths: string[],
options?: WalkOptions,
) {
const root = resolve(testdataDir, rootPath);
const root = resolve(parentDir, rootPath);
const entries = await Array.fromAsync(walk(root, options));

const expected = expectedPaths.map((path) => resolve(root, path));
Expand All @@ -24,11 +26,12 @@ async function assertWalkPaths(
}

function assertWalkSyncPaths(
parentDir: string,
rootPath: string,
expectedPaths: string[],
options?: WalkOptions,
) {
const root = resolve(testdataDir, rootPath);
const root = resolve(parentDir, rootPath);
const entriesSync = Array.from(walkSync(root, options));

const expected = expectedPaths.map((path) => resolve(root, path));
Expand All @@ -37,126 +40,196 @@ function assertWalkSyncPaths(
}

Deno.test("walk() returns current dir for empty dir", async () => {
const emptyDir = resolve(testdataDir, "empty_dir");
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_walk_",
});
const emptyDir = resolve(tempDirPath, "empty_dir");
await Deno.mkdir(emptyDir);
await assertWalkPaths("empty_dir", ["."]);
await Deno.remove(emptyDir);
await assertWalkPaths(tempDirPath, "empty_dir", ["."]);
await Deno.remove(tempDirPath, { recursive: true });
});

Deno.test("walkSync() returns current dir for empty dir", async () => {
const emptyDir = resolve(testdataDir, "empty_dir");
await Deno.mkdir(emptyDir);
assertWalkSyncPaths("empty_dir", ["."]);
await Deno.remove(emptyDir);
Deno.test("walkSync() returns current dir for empty dir", () => {
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_walk_sync_",
});
const emptyDir = resolve(tempDirPath, "empty_dir");
Deno.mkdirSync(emptyDir);
assertWalkSyncPaths(tempDirPath, "empty_dir", ["."]);
Deno.removeSync(tempDirPath, { recursive: true });
});

Deno.test("walk() returns current dir and single file", async () =>
await assertWalkPaths("single_file", [".", "x"]));
await assertWalkPaths(testdataDir, "single_file", [".", "x"]));

Deno.test("walkSync() returns current dir and single file", () =>
assertWalkSyncPaths("single_file", [".", "x"]));
assertWalkSyncPaths(testdataDir, "single_file", [".", "x"]));

Deno.test("walk() returns current dir, subdir, and nested file", async () =>
await assertWalkPaths("nested_single_file", [".", "a", "a/x"]));
await assertWalkPaths(testdataDir, "nested_single_file", [".", "a", "a/x"]));

Deno.test("walkSync() returns current dir, subdir, and nested file", () =>
assertWalkSyncPaths("nested_single_file", [".", "a", "a/x"]));
assertWalkSyncPaths(testdataDir, "nested_single_file", [".", "a", "a/x"]));

Deno.test("walk() accepts maxDepth option", async () =>
await assertWalkPaths("depth", [".", "a", "a/b", "a/b/c"], { maxDepth: 3 }));
await assertWalkPaths(testdataDir, "depth", [".", "a", "a/b", "a/b/c"], {
maxDepth: 3,
}));

Deno.test("walkSync() accepts maxDepth option", () =>
assertWalkSyncPaths("depth", [".", "a", "a/b", "a/b/c"], { maxDepth: 3 }));
assertWalkSyncPaths(testdataDir, "depth", [".", "a", "a/b", "a/b/c"], {
maxDepth: 3,
}));

Deno.test("walk() accepts includeDirs option set to false", async () =>
await assertWalkPaths("depth", ["a/b/c/d/x"], { includeDirs: false }));
await assertWalkPaths(testdataDir, "depth", ["a/b/c/d/x"], {
includeDirs: false,
}));

Deno.test("walkSync() accepts includeDirs option set to false", () =>
assertWalkSyncPaths("depth", ["a/b/c/d/x"], { includeDirs: false }));
assertWalkSyncPaths(testdataDir, "depth", ["a/b/c/d/x"], {
includeDirs: false,
}));

Deno.test("walk() accepts includeFiles option set to false", async () =>
await assertWalkPaths("depth", [".", "a", "a/b", "a/b/c", "a/b/c/d"], {
await assertWalkPaths(testdataDir, "depth", [
".",
"a",
"a/b",
"a/b/c",
"a/b/c/d",
], {
includeFiles: false,
}));

Deno.test("walkSync() accepts includeFiles option set to false", () =>
assertWalkSyncPaths("depth", [".", "a", "a/b", "a/b/c", "a/b/c/d"], {
assertWalkSyncPaths(testdataDir, "depth", [
".",
"a",
"a/b",
"a/b/c",
"a/b/c/d",
], {
includeFiles: false,
}));

Deno.test("walk() accepts ext option as strings", async () =>
await assertWalkPaths("ext", ["y.rs", "x.ts"], {
await assertWalkPaths(testdataDir, "ext", ["y.rs", "x.ts"], {
exts: [".rs", ".ts"],
}));

Deno.test("walk() accepts ext option as strings (excluding period prefix)", async () =>
await assertWalkPaths("ext", ["y.rs", "x.ts"], {
await assertWalkPaths(testdataDir, "ext", ["y.rs", "x.ts"], {
exts: ["rs", "ts"],
}));

Deno.test("walkSync() accepts ext option as strings", () =>
assertWalkSyncPaths("ext", ["y.rs", "x.ts"], {
assertWalkSyncPaths(testdataDir, "ext", ["y.rs", "x.ts"], {
exts: [".rs", ".ts"],
}));

Deno.test("walkSync() accepts ext option as strings (excluding period prefix)", () =>
assertWalkSyncPaths("ext", ["y.rs", "x.ts"], {
assertWalkSyncPaths(testdataDir, "ext", ["y.rs", "x.ts"], {
exts: [".rs", ".ts"],
}));

Deno.test("walk() accepts ext option as regExps", async () =>
await assertWalkPaths("match", ["x", "y"], {
await assertWalkPaths(testdataDir, "match", ["x", "y"], {
match: [/x/, /y/],
}));

Deno.test("walkSync() accepts ext option as regExps", () =>
assertWalkSyncPaths("match", ["x", "y"], {
assertWalkSyncPaths(testdataDir, "match", ["x", "y"], {
match: [/x/, /y/],
}));

Deno.test("walk() accepts skip option as regExps", async () =>
await assertWalkPaths("match", [".", "z"], {
await assertWalkPaths(testdataDir, "match", [".", "z"], {
skip: [/x/, /y/],
}));

Deno.test("walkSync() accepts skip option as regExps", () =>
assertWalkSyncPaths("match", [".", "z"], {
assertWalkSyncPaths(testdataDir, "match", [".", "z"], {
skip: [/x/, /y/],
}));

// https://github.com/denoland/deno_std/issues/1358
Deno.test("walk() accepts followSymlinks option set to true", async () =>
await assertWalkPaths("symlink", [".", "a", "a/z", "a", "a/z", "x", "x"], {
await assertWalkPaths(testdataDir, "symlink", [
".",
"a",
"a/z",
"a",
"a/z",
"x",
"x",
], {
followSymlinks: true,
}));

Deno.test("walkSync() accepts followSymlinks option set to true", () =>
assertWalkSyncPaths("symlink", [".", "a", "a/z", "a", "a/z", "x", "x"], {
assertWalkSyncPaths(testdataDir, "symlink", [
".",
"a",
"a/z",
"a",
"a/z",
"x",
"x",
], {
followSymlinks: true,
}));

Deno.test("walk() accepts followSymlinks option set to true with canonicalize option set to false", async () =>
await assertWalkPaths("symlink", [".", "a", "a/z", "b", "b/z", "x", "y"], {
await assertWalkPaths(testdataDir, "symlink", [
".",
"a",
"a/z",
"b",
"b/z",
"x",
"y",
], {
followSymlinks: true,
canonicalize: false,
}));

Deno.test("walkSync() accepts followSymlinks option set to true with canonicalize option set to false", () =>
assertWalkSyncPaths("symlink", [".", "a", "a/z", "b", "b/z", "x", "y"], {
assertWalkSyncPaths(testdataDir, "symlink", [
".",
"a",
"a/z",
"b",
"b/z",
"x",
"y",
], {
followSymlinks: true,
canonicalize: false,
}));

Deno.test("walk() accepts followSymlinks option set to false", async () => {
await assertWalkPaths("symlink", [".", "a", "a/z", "b", "x", "y"], {
await assertWalkPaths(testdataDir, "symlink", [
".",
"a",
"a/z",
"b",
"x",
"y",
], {
followSymlinks: false,
});
});

Deno.test("walkSync() accepts followSymlinks option set to false", () => {
assertWalkSyncPaths("symlink", [".", "a", "a/z", "b", "x", "y"], {
followSymlinks: false,
});
assertWalkSyncPaths(
testdataDir,
"symlink",
[".", "a", "a/z", "b", "x", "y"],
{
followSymlinks: false,
},
);
});

Deno.test("walk() rejects Deno.errors.NotFound for non-existent root", async () => {
Expand All @@ -177,14 +250,23 @@ Deno.test({
name: "walk() walks unix socket",
ignore: Deno.build.os === "windows",
async fn() {
const path = resolve(testdataDir, "socket", "a.sock");
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_walk_",
});
// Copy contents from "walk/socket" into temporary directory.
await copy(resolve(testdataDir, "socket"), resolve(tempDirPath, "socket"));
const path = resolve(tempDirPath, "socket", "a.sock");
try {
using _listener = Deno.listen({ path, transport: "unix" });
await assertWalkPaths("socket", [".", "a.sock", ".gitignore"], {
await assertWalkPaths(tempDirPath, "socket", [
".",
"a.sock",
".gitignore",
], {
followSymlinks: true,
});
} finally {
await Deno.remove(path);
await Deno.remove(tempDirPath, { recursive: true });
}
},
});
Expand All @@ -193,15 +275,25 @@ Deno.test({
Deno.test({
name: "walkSync() walks unix socket",
ignore: Deno.build.os === "windows",
async fn() {
const path = resolve(testdataDir, "socket", "a.sock");
fn() {
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_walk_sync_",
});
// Copy contents from "walk/socket" into temporary directory.
copySync(resolve(testdataDir, "socket"), resolve(tempDirPath, "socket"));
const path = resolve(tempDirPath, "socket", "a.sock");
try {
using _listener = Deno.listen({ path, transport: "unix" });
assertWalkSyncPaths("socket", [".", "a.sock", ".gitignore"], {
followSymlinks: true,
});
assertWalkSyncPaths(
tempDirPath,
"socket",
[".", "a.sock", ".gitignore"],
{
followSymlinks: true,
},
);
} finally {
await Deno.remove(path);
Deno.removeSync(tempDirPath, { recursive: true });
}
},
});
Expand All @@ -210,32 +302,47 @@ Deno.test({
name: "walk() walks fifo files on unix",
ignore: Deno.build.os === "windows",
async fn() {
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_walk_",
});
// Copy contents from "walk/fifo" into temporary directory.
await copy(resolve(testdataDir, "fifo"), resolve(tempDirPath, "fifo"));
const command = new Deno.Command("mkfifo", {
args: [resolve(testdataDir, "fifo", "fifo")],
args: [resolve(tempDirPath, "fifo", "fifo")],
});
await command.output();
await assertWalkPaths("fifo", [".", "fifo", ".gitignore"], {
await assertWalkPaths(tempDirPath, "fifo", [".", "fifo", ".gitignore"], {
followSymlinks: true,
});
await Deno.remove(tempDirPath, { recursive: true });
},
});

Deno.test({
name: "walkSync() walks fifo files on unix",
ignore: Deno.build.os === "windows",
async fn() {
fn() {
const tempDirPath = Deno.makeTempDirSync({
prefix: "deno_std_walk_sync_",
});
// Copy contents from "walk/fifo" into temporary directory.
copySync(resolve(testdataDir, "fifo"), resolve(tempDirPath, "fifo"));
const command = new Deno.Command("mkfifo", {
args: [resolve(testdataDir, "fifo", "fifo")],
args: [resolve(tempDirPath, "fifo", "fifo")],
});
await command.output();
assertWalkSyncPaths("fifo", [".", "fifo", ".gitignore"], {
command.outputSync();
assertWalkSyncPaths(tempDirPath, "fifo", [".", "fifo", ".gitignore"], {
followSymlinks: true,
});
Deno.removeSync(tempDirPath, { recursive: true });
},
});

Deno.test("walk() rejects with `Deno.errors.NotFound` when root is removed during execution", async () => {
const root = resolve(testdataDir, "error");
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_walk_",
});
const root = resolve(tempDirPath, "error");
await Deno.mkdir(root);
try {
await assertRejects(
Expand All @@ -250,5 +357,7 @@ Deno.test("walk() rejects with `Deno.errors.NotFound` when root is removed durin
} catch (err) {
await Deno.remove(root, { recursive: true });
throw err;
} finally {
await Deno.remove(tempDirPath, { recursive: true });
}
});

0 comments on commit 4186412

Please sign in to comment.