diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-10-17 12:51:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-17 19:51:15 +0000 |
commit | 9fde5cb5e045551fe344b3f60370744eea30ccb4 (patch) | |
tree | f407e3358a710927c2e6967fd9023ddd26581bfe | |
parent | eca83fc9b45ab1e5a73bd7b13b05ee42ab1a4dcc (diff) |
fix(node/fs): copyFile with `COPYFILE_EXCL` should not throw if the destination doesn't exist (#26360)
Fixes #26313.
We were checking for the NotFound error, but still calling the callback
with the error / throwing.
-rw-r--r-- | ext/node/polyfills/_fs/_fs_copy.ts | 6 | ||||
-rw-r--r-- | tests/unit_node/fs_test.ts | 21 |
2 files changed, 24 insertions, 3 deletions
diff --git a/ext/node/polyfills/_fs/_fs_copy.ts b/ext/node/polyfills/_fs/_fs_copy.ts index 2f8ddf4fc..0434bff4d 100644 --- a/ext/node/polyfills/_fs/_fs_copy.ts +++ b/ext/node/polyfills/_fs/_fs_copy.ts @@ -53,8 +53,9 @@ export function copyFile( }, (e) => { if (e instanceof Deno.errors.NotFound) { Deno.copyFile(srcStr, destStr).then(() => cb(null), cb); + } else { + cb(e); } - cb(e); }); } else { Deno.copyFile(srcStr, destStr).then(() => cb(null), cb); @@ -83,8 +84,9 @@ export function copyFileSync( } catch (e) { if (e instanceof Deno.errors.NotFound) { Deno.copyFileSync(srcStr, destStr); + } else { + throw e; } - throw e; } } else { Deno.copyFileSync(srcStr, destStr); diff --git a/tests/unit_node/fs_test.ts b/tests/unit_node/fs_test.ts index 2d1465aec..17da45dcf 100644 --- a/tests/unit_node/fs_test.ts +++ b/tests/unit_node/fs_test.ts @@ -1,11 +1,13 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals, assertThrows } from "@std/assert"; +/// <reference lib="deno.ns" /> +import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert"; import { join } from "node:path"; import { tmpdir } from "node:os"; import { closeSync, constants, + copyFileSync, createWriteStream, existsSync, lstatSync, @@ -20,6 +22,7 @@ import { } from "node:fs"; import { constants as fsPromiseConstants, + copyFile, cp, FileHandle, open, @@ -212,3 +215,19 @@ Deno.test("[node/fs] readSync works", () => { assertEquals(bytesRead, 12); closeSync(fd!); }); + +Deno.test("[node/fs] copyFile COPYFILE_EXCL works", async () => { + const dir = mkdtempSync(join(tmpdir(), "foo-")); + const src = join(dir, "src.txt"); + const dest = join(dir, "dest.txt"); + await writeFile(src, ""); + await copyFile(src, dest, fsPromiseConstants.COPYFILE_EXCL); + assert(existsSync(dest)); + assertRejects(() => copyFile(src, dest, fsPromiseConstants.COPYFILE_EXCL)); + const dest2 = join(dir, "dest2.txt"); + copyFileSync(src, dest2, fsPromiseConstants.COPYFILE_EXCL); + assert(existsSync(dest2)); + assertThrows(() => + copyFileSync(src, dest2, fsPromiseConstants.COPYFILE_EXCL) + ); +}); |