From 472c06b92ebec396559a494502c9438678a49cac Mon Sep 17 00:00:00 2001 From: Sanskar Gauchan <62867497+Sanskar531@users.noreply.github.com> Date: Sun, 19 Mar 2023 21:37:00 +1100 Subject: [PATCH] test(ext/node): add _fs_access_test.ts and _fs_watch_test.ts (#18249) part of https://github.com/denoland/deno/issues/17840 --- cli/tests/unit_node/_fs/_fs_access_test.ts | 70 ++++++++++++++++++++++ cli/tests/unit_node/_fs/_fs_watch_test.ts | 27 +++++++++ 2 files changed, 97 insertions(+) create mode 100644 cli/tests/unit_node/_fs/_fs_access_test.ts create mode 100644 cli/tests/unit_node/_fs/_fs_watch_test.ts diff --git a/cli/tests/unit_node/_fs/_fs_access_test.ts b/cli/tests/unit_node/_fs/_fs_access_test.ts new file mode 100644 index 0000000000..c3d54f10e5 --- /dev/null +++ b/cli/tests/unit_node/_fs/_fs_access_test.ts @@ -0,0 +1,70 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import * as fs from "node:fs"; +import { + assertRejects, + assertThrows, +} from "../../../../test_util/std/testing/asserts.ts"; + +Deno.test( + "[node/fs.access] Uses the owner permission when the user is the owner", + { ignore: Deno.build.os === "windows" }, + async () => { + const file = await Deno.makeTempFile(); + try { + Deno.chmod(file, 0o600); + await fs.promises.access(file, fs.constants.R_OK); + await fs.promises.access(file, fs.constants.W_OK); + await assertRejects(async () => { + await fs.promises.access(file, fs.constants.X_OK); + }); + } finally { + await Deno.remove(file); + } + }, +); + +Deno.test( + "[node/fs.access] doesn't reject on windows", + { ignore: Deno.build.os !== "windows" }, + async () => { + const file = await Deno.makeTempFile(); + try { + await fs.promises.access(file, fs.constants.R_OK); + await fs.promises.access(file, fs.constants.W_OK); + } finally { + await Deno.remove(file); + } + }, +); + +Deno.test( + "[node/fs.accessSync] Uses the owner permission when the user is the owner", + { ignore: Deno.build.os === "windows" }, + () => { + const file = Deno.makeTempFileSync(); + try { + Deno.chmod(file, 0o600); + fs.accessSync(file, fs.constants.R_OK); + fs.accessSync(file, fs.constants.W_OK); + assertThrows(() => { + fs.accessSync(file, fs.constants.X_OK); + }); + } finally { + Deno.removeSync(file); + } + }, +); + +Deno.test( + "[node/fs.accessSync] doesn't throw on windows", + { ignore: Deno.build.os !== "windows" }, + () => { + const file = Deno.makeTempFileSync(); + try { + fs.accessSync(file, fs.constants.R_OK); + fs.accessSync(file, fs.constants.W_OK); + } finally { + Deno.removeSync(file); + } + }, +); diff --git a/cli/tests/unit_node/_fs/_fs_watch_test.ts b/cli/tests/unit_node/_fs/_fs_watch_test.ts new file mode 100644 index 0000000000..2316b2db3d --- /dev/null +++ b/cli/tests/unit_node/_fs/_fs_watch_test.ts @@ -0,0 +1,27 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { watch } from "node:fs"; +import { assertEquals } from "../../../../test_util/std/testing/asserts.ts"; + +function wait(time: number) { + return new Promise((resolve) => { + setTimeout(resolve, time); + }); +} + +Deno.test({ + name: "watching a file", + async fn() { + const file = Deno.makeTempFileSync(); + const result: Array<[string, string]> = []; + const watcher = watch( + file, + (eventType, filename) => result.push([eventType, filename]), + ); + await wait(100); + Deno.writeTextFileSync(file, "something"); + await wait(100); + watcher.close(); + await wait(100); + assertEquals(result.length >= 1, true); + }, +});