From a24d3e8763bc48b69936db9231efb76766914303 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 4 Dec 2023 16:05:40 -0500 Subject: perf(node/fs): faster `existsSync` when not exists (#21458) --- cli/tests/unit_node/fs_test.ts | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'cli/tests') diff --git a/cli/tests/unit_node/fs_test.ts b/cli/tests/unit_node/fs_test.ts index 310113233..d9fe3af76 100644 --- a/cli/tests/unit_node/fs_test.ts +++ b/cli/tests/unit_node/fs_test.ts @@ -3,7 +3,8 @@ import { assert, assertThrows } from "../../../test_util/std/assert/mod.ts"; import { join } from "node:path"; import { tmpdir } from "node:os"; -import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { pathToAbsoluteFileUrl } from "../unit/test_util.ts"; Deno.test( "[node/fs writeFileSync] write file without option", @@ -43,3 +44,39 @@ Deno.test( ); }, ); + +Deno.test( + "[node/fs existsSync] path", + { permissions: { read: true } }, + () => { + assert(existsSync("cli/tests/testdata/assets/fixture.json")); + }, +); + +Deno.test( + "[node/fs existsSync] url", + { permissions: { read: true } }, + () => { + assert(existsSync( + pathToAbsoluteFileUrl("cli/tests/testdata/assets/fixture.json"), + )); + }, +); + +Deno.test( + "[node/fs existsSync] no permission", + { permissions: { read: false } }, + () => { + assertThrows(() => { + existsSync("cli/tests/testdata/assets/fixture.json"); + }, Deno.errors.PermissionDenied); + }, +); + +Deno.test( + "[node/fs existsSync] not exists", + { permissions: { read: true } }, + () => { + assert(!existsSync("bad_filename")); + }, +); -- cgit v1.2.3