From de0153a044f81b7eefd16eb6224f05e1be27a381 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 11 Apr 2022 17:44:38 -0400 Subject: [PATCH] fix: handle directory being passed to `Deno.remove` (#104) --- .../src/deno/stable/functions/createSync.ts | 7 ++++- .../src/deno/stable/functions/open.ts | 9 ++++-- .../src/deno/stable/functions/openSync.ts | 9 ++++-- .../src/deno/stable/functions/read.ts | 7 +++++ .../src/deno/stable/functions/remove.ts | 17 +++++++++-- .../src/deno/stable/functions/removeSync.ts | 16 +++++++++-- packages/shim-deno/tools/run_tests.mjs | 28 +++++++++++++++++++ .../shim-deno/tools/working_test_files.txt | 1 + 8 files changed, 84 insertions(+), 10 deletions(-) diff --git a/packages/shim-deno/src/deno/stable/functions/createSync.ts b/packages/shim-deno/src/deno/stable/functions/createSync.ts index 00ae016..a4b8a70 100644 --- a/packages/shim-deno/src/deno/stable/functions/createSync.ts +++ b/packages/shim-deno/src/deno/stable/functions/createSync.ts @@ -3,5 +3,10 @@ import { openSync } from "./openSync.js"; export const createSync: typeof Deno.createSync = function createSync(path) { - return openSync(path, { create: true, truncate: true }); + return openSync(path, { + create: true, + truncate: true, + read: true, + write: true, + }); }; diff --git a/packages/shim-deno/src/deno/stable/functions/open.ts b/packages/shim-deno/src/deno/stable/functions/open.ts index 6aa77d2..d3c92c0 100644 --- a/packages/shim-deno/src/deno/stable/functions/open.ts +++ b/packages/shim-deno/src/deno/stable/functions/open.ts @@ -5,6 +5,7 @@ import { promisify } from "util"; import { File } from "../classes/FsFile.js"; import { getFsFlag } from "../../internal/fs_flags.js"; +import mapError from "../../internal/errorMap.js"; const nodeOpen = promisify(_open); @@ -22,6 +23,10 @@ export const open: typeof Deno.open = async function open( create, createNew, }); - const fd = await nodeOpen(path, flagMode, mode); - return new File(fd); + try { + const fd = await nodeOpen(path, flagMode, mode); + return new File(fd); + } catch (err) { + throw mapError(err); + } }; diff --git a/packages/shim-deno/src/deno/stable/functions/openSync.ts b/packages/shim-deno/src/deno/stable/functions/openSync.ts index e5bdcd3..1a3d091 100644 --- a/packages/shim-deno/src/deno/stable/functions/openSync.ts +++ b/packages/shim-deno/src/deno/stable/functions/openSync.ts @@ -4,6 +4,7 @@ import { openSync as nodeOpenSync } from "fs"; import { File } from "../classes/FsFile.js"; import { getFsFlag } from "../../internal/fs_flags.js"; +import mapError from "../../internal/errorMap.js"; export const openSync: typeof Deno.openSync = function openSync( path, @@ -19,6 +20,10 @@ export const openSync: typeof Deno.openSync = function openSync( create, createNew, }); - const fd = nodeOpenSync(path, flagMode, mode); - return new File(fd); + try { + const fd = nodeOpenSync(path, flagMode, mode); + return new File(fd); + } catch (err) { + throw mapError(err); + } }; diff --git a/packages/shim-deno/src/deno/stable/functions/read.ts b/packages/shim-deno/src/deno/stable/functions/read.ts index 44865cc..f638fe8 100644 --- a/packages/shim-deno/src/deno/stable/functions/read.ts +++ b/packages/shim-deno/src/deno/stable/functions/read.ts @@ -6,6 +6,13 @@ import { read as nodeRead } from "fs"; const _read = promisify(nodeRead); export const read: typeof Deno.read = async function read(rid, buffer) { + if (buffer == null) { + throw new TypeError("Buffer must not be null."); + } + if (buffer.length === 0) { + return 0; + } + const { bytesRead } = await _read(rid, buffer, 0, buffer.length, null); // node returns 0 on EOF, Deno expects null return bytesRead === 0 ? null : bytesRead; diff --git a/packages/shim-deno/src/deno/stable/functions/remove.ts b/packages/shim-deno/src/deno/stable/functions/remove.ts index 9704319..131baa9 100644 --- a/packages/shim-deno/src/deno/stable/functions/remove.ts +++ b/packages/shim-deno/src/deno/stable/functions/remove.ts @@ -1,10 +1,21 @@ /// -import { rm } from "fs/promises"; +import { rm, rmdir } from "fs/promises"; -export const remove: typeof Deno.remove = function remove( +export const remove: typeof Deno.remove = async function remove( path, options = {}, ) { - return rm(path, options.recursive ? { recursive: true, force: true } : {}); + const innerOptions = options.recursive + ? { recursive: true, force: true } + : {}; + try { + return await rm(path, innerOptions); + } catch (err) { + if ((err as any).code === "ERR_FS_EISDIR") { + return await rmdir(path, innerOptions); + } else { + throw err; + } + } }; diff --git a/packages/shim-deno/src/deno/stable/functions/removeSync.ts b/packages/shim-deno/src/deno/stable/functions/removeSync.ts index 523c21b..dd9580e 100644 --- a/packages/shim-deno/src/deno/stable/functions/removeSync.ts +++ b/packages/shim-deno/src/deno/stable/functions/removeSync.ts @@ -2,5 +2,17 @@ import * as fs from "fs"; -export const removeSync: typeof Deno.removeSync = (path, options = {}) => - fs.rmSync(path, options.recursive ? { recursive: true, force: true } : {}); +export const removeSync: typeof Deno.removeSync = (path, options = {}) => { + const innerOptions = options.recursive + ? { recursive: true, force: true } + : {}; + try { + fs.rmSync(path, innerOptions); + } catch (err) { + if ((err as any).code === "ERR_FS_EISDIR") { + fs.rmdirSync(path, innerOptions); + } else { + throw err; + } + } +}; diff --git a/packages/shim-deno/tools/run_tests.mjs b/packages/shim-deno/tools/run_tests.mjs index 6864c50..4e75e68 100644 --- a/packages/shim-deno/tools/run_tests.mjs +++ b/packages/shim-deno/tools/run_tests.mjs @@ -25,6 +25,34 @@ const testsToSkip = new Set([ // event_target_test "eventTargetThisShouldDefaultToWindow", // window + // files_test + "filesIter", // deprecated + "filesIterCustomBufSize", // deprecated + "filesIterSync", // deprecated + "filesIterSyncCustomBufSize", // deprecated + "readerIter", // deprecated + "readerIterSync", // deprecated + "writePermFailure", // permissions + "openOptions", // todo + "openMode", // depends on umask + "openSyncMode", // depends on umask + "openSyncUrl", // depends on umask + "openUrl", // depends on umask + "readPermFailure", // permissions + "readWritePermFailure", // permissions + "openNotFound", // todo + "openModeWriteRead", // not implemented + "seekStart", // not implemented + "seekSyncStart", // not implemented + "seekCurrent", // not implemented + "seekSyncCurrent", // not implemented + "seekEnd", // not implemented + "seekSyncEnd", // not implemented + "seekMode", // not implemented + "readableStream", // not implemented + "readableStreamTextEncoderPipe", // not implemented + "writableStream", // not implemented + // mkdir_test "mkdirMode", // depends on Deno.umask "mkdirRecursiveIfExists", // depends on Deno.umask diff --git a/packages/shim-deno/tools/working_test_files.txt b/packages/shim-deno/tools/working_test_files.txt index 5532d74..b3e970d 100644 --- a/packages/shim-deno/tools/working_test_files.txt +++ b/packages/shim-deno/tools/working_test_files.txt @@ -8,6 +8,7 @@ third_party/deno/cli/tests/unit/copy_file_test.ts third_party/deno/cli/tests/unit/dir_test.ts third_party/deno/cli/tests/unit/error_stack_test.ts third_party/deno/cli/tests/unit/event_target_test.ts +third_party/deno/cli/tests/unit/files_test.ts third_party/deno/cli/tests/unit/get_random_values_test.ts third_party/deno/cli/tests/unit/io_test.ts third_party/deno/cli/tests/unit/message_channel_test.ts