Skip to content

Commit

Permalink
test: add readDirSync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbronder committed Feb 5, 2025
1 parent 462b683 commit 49c01ca
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions fs/unstable_read_dir_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { assert, assertEquals, assertRejects } from "@std/assert";
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
import { fromFileUrl, join, resolve } from "@std/path";
import { readDir } from "./unstable_read_dir.ts";
import { readDir, readDirSync } from "./unstable_read_dir.ts";
import { NotFound } from "./unstable_errors.js";

const testdataDir = resolve(fromFileUrl(import.meta.url), "../testdata");
Expand Down Expand Up @@ -36,3 +36,33 @@ Deno.test("readDir() rejects when the directory does not exist", async () => {
await readDir("non_existent_dir")[Symbol.asyncIterator]().next();
}, NotFound);
});

Deno.test("readDirSync() reads from the directory and its subdirectories", () => {
const files = [];
for (const e of readDirSync(testdataDir)) {
files.push(e);
}

let counter = 0;
for (const f of files) {
if (f.name === "walk") {
assert(f.isDirectory);
counter++;
}
}

assertEquals(counter, 1);
});

Deno.test("readDirSync() throws with Error when the path is not a directory", () => {
assertThrows(() => {
const testFile = join(testdataDir, "0.ts");
readDirSync(testFile)[Symbol.iterator]().next();
}, Error);
});

Deno.test("readDirSync() throws with NotFound when a directory does not exist", () => {
assertThrows(() => {
readDirSync("non_existent_dir")[Symbol.iterator]().next();
}, NotFound);
});

0 comments on commit 49c01ca

Please sign in to comment.