summaryrefslogtreecommitdiff
path: root/cli/tests/unit_node/_fs/_fs_dir_test.ts
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-10 13:22:13 -0700
committerGitHub <noreply@github.com>2024-02-10 20:22:13 +0000
commitf5e46c9bf2f50d66a953fa133161fc829cecff06 (patch)
tree8faf2f5831c1c7b11d842cd9908d141082c869a5 /cli/tests/unit_node/_fs/_fs_dir_test.ts
parentd2477f780630a812bfd65e3987b70c0d309385bb (diff)
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests -> tests, and updates of relative paths for files. This is the first step towards aggregate all of the integration test files under tests/, which will lead to a set of integration tests that can run without the CLI binary being built. While we could leave these tests under `cli`, it would require us to keep a more complex directory structure for the various test runners. In addition, we have a lot of complexity to ignore various test files in the `cli` project itself (cargo publish exclusion rules, autotests = false, etc). And finally, the `tests/` folder will eventually house the `test_ffi`, `test_napi` and other testing code, reducing the size of the root repo directory. For easier review, the extremely large and noisy "move" is in the first commit (with no changes -- just a move), while the remainder of the changes to actual files is in the second commit.
Diffstat (limited to 'cli/tests/unit_node/_fs/_fs_dir_test.ts')
-rw-r--r--cli/tests/unit_node/_fs/_fs_dir_test.ts205
1 files changed, 0 insertions, 205 deletions
diff --git a/cli/tests/unit_node/_fs/_fs_dir_test.ts b/cli/tests/unit_node/_fs/_fs_dir_test.ts
deleted file mode 100644
index 697929fee..000000000
--- a/cli/tests/unit_node/_fs/_fs_dir_test.ts
+++ /dev/null
@@ -1,205 +0,0 @@
-// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-import { assert, assertEquals, fail } from "@test_util/std/assert/mod.ts";
-import { assertCallbackErrorUncaught } from "../_test_utils.ts";
-import { Dir as DirOrig, type Dirent } from "node:fs";
-
-// deno-lint-ignore no-explicit-any
-const Dir = DirOrig as any;
-
-Deno.test({
- name: "Closing current directory with callback is successful",
- fn() {
- let calledBack = false;
- // deno-lint-ignore no-explicit-any
- new Dir(".").close((valOrErr: any) => {
- assert(!valOrErr);
- calledBack = true;
- });
- assert(calledBack);
- },
-});
-
-Deno.test({
- name: "Closing current directory without callback returns void Promise",
- async fn() {
- await new Dir(".").close();
- },
-});
-
-Deno.test({
- name: "Closing current directory synchronously works",
- fn() {
- new Dir(".").closeSync();
- },
-});
-
-Deno.test({
- name: "Path is correctly returned",
- fn() {
- assertEquals(new Dir("std/node").path, "std/node");
-
- const enc: Uint8Array = new TextEncoder().encode("std/node");
- assertEquals(new Dir(enc).path, "std/node");
- },
-});
-
-Deno.test({
- name: "read returns null for empty directory",
- async fn() {
- const testDir: string = Deno.makeTempDirSync();
- try {
- const file: Dirent | null = await new Dir(testDir).read();
- assert(file === null);
-
- let calledBack = false;
- const fileFromCallback: Dirent | null = await new Dir(
- testDir,
- // deno-lint-ignore no-explicit-any
- ).read((err: any, res: Dirent) => {
- assert(res === null);
- assert(err === null);
- calledBack = true;
- });
- assert(fileFromCallback === null);
- assert(calledBack);
-
- assertEquals(new Dir(testDir).readSync(), null);
- } finally {
- Deno.removeSync(testDir);
- }
- },
-});
-
-Deno.test({
- name: "Async read returns one file at a time",
- async fn() {
- const testDir: string = Deno.makeTempDirSync();
- const f1 = Deno.createSync(testDir + "/foo.txt");
- f1.close();
- const f2 = Deno.createSync(testDir + "/bar.txt");
- f2.close();
-
- try {
- let secondCallback = false;
- const dir = new Dir(testDir);
- const firstRead: Dirent | null = await dir.read();
- const secondRead: Dirent | null = await dir.read(
- // deno-lint-ignore no-explicit-any
- (_err: any, secondResult: Dirent) => {
- assert(
- secondResult.name === "bar.txt" || secondResult.name === "foo.txt",
- );
- secondCallback = true;
- },
- );
- const thirdRead: Dirent | null = await dir.read();
- const fourthRead: Dirent | null = await dir.read();
-
- if (firstRead?.name === "foo.txt") {
- assertEquals(secondRead?.name, "bar.txt");
- } else if (firstRead?.name === "bar.txt") {
- assertEquals(secondRead?.name, "foo.txt");
- } else {
- fail("File not found during read");
- }
- assert(secondCallback);
- assert(thirdRead === null);
- assert(fourthRead === null);
- } finally {
- Deno.removeSync(testDir, { recursive: true });
- }
- },
-});
-
-Deno.test({
- name: "Sync read returns one file at a time",
- fn() {
- const testDir: string = Deno.makeTempDirSync();
- const f1 = Deno.createSync(testDir + "/foo.txt");
- f1.close();
- const f2 = Deno.createSync(testDir + "/bar.txt");
- f2.close();
-
- try {
- const dir = new Dir(testDir);
- const firstRead: Dirent | null = dir.readSync();
- const secondRead: Dirent | null = dir.readSync();
- const thirdRead: Dirent | null = dir.readSync();
- const fourthRead: Dirent | null = dir.readSync();
-
- if (firstRead?.name === "foo.txt") {
- assertEquals(secondRead?.name, "bar.txt");
- } else if (firstRead?.name === "bar.txt") {
- assertEquals(secondRead?.name, "foo.txt");
- } else {
- fail("File not found during read");
- }
- assert(thirdRead === null);
- assert(fourthRead === null);
- } finally {
- Deno.removeSync(testDir, { recursive: true });
- }
- },
-});
-
-Deno.test({
- name: "Async iteration over existing directory",
- async fn() {
- const testDir: string = Deno.makeTempDirSync();
- const f1 = Deno.createSync(testDir + "/foo.txt");
- f1.close();
- const f2 = Deno.createSync(testDir + "/bar.txt");
- f2.close();
-
- try {
- const dir = new Dir(testDir);
- const results: Array<string | null> = [];
-
- for await (const file of dir[Symbol.asyncIterator]()) {
- results.push(file.name);
- }
-
- assert(results.length === 2);
- assert(results.includes("foo.txt"));
- assert(results.includes("bar.txt"));
- } finally {
- Deno.removeSync(testDir, { recursive: true });
- }
- },
-});
-
-Deno.test(
- "[std/node/fs] Dir.close callback isn't called twice if error is thrown",
- async () => {
- const tempDir = await Deno.makeTempDir();
- await assertCallbackErrorUncaught({
- prelude: `
- import { Dir } from "node:fs";
-
- const dir = new Dir(${JSON.stringify(tempDir)});
- `,
- invocation: "dir.close(",
- async cleanup() {
- await Deno.remove(tempDir);
- },
- });
- },
-);
-
-Deno.test(
- "[std/node/fs] Dir.read callback isn't called twice if error is thrown",
- async () => {
- const tempDir = await Deno.makeTempDir();
- await assertCallbackErrorUncaught({
- prelude: `
- import { Dir } from "node:fs";
-
- const dir = new Dir(${JSON.stringify(tempDir)});
- `,
- invocation: "dir.read(",
- async cleanup() {
- await Deno.remove(tempDir);
- },
- });
- },
-);