1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

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.
This commit is contained in:
Nathan Whitaker 2024-10-17 12:51:15 -07:00 committed by GitHub
parent eca83fc9b4
commit 9fde5cb5e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View file

@ -53,8 +53,9 @@ export function copyFile(
}, (e) => { }, (e) => {
if (e instanceof Deno.errors.NotFound) { if (e instanceof Deno.errors.NotFound) {
Deno.copyFile(srcStr, destStr).then(() => cb(null), cb); Deno.copyFile(srcStr, destStr).then(() => cb(null), cb);
} } else {
cb(e); cb(e);
}
}); });
} else { } else {
Deno.copyFile(srcStr, destStr).then(() => cb(null), cb); Deno.copyFile(srcStr, destStr).then(() => cb(null), cb);
@ -83,9 +84,10 @@ export function copyFileSync(
} catch (e) { } catch (e) {
if (e instanceof Deno.errors.NotFound) { if (e instanceof Deno.errors.NotFound) {
Deno.copyFileSync(srcStr, destStr); Deno.copyFileSync(srcStr, destStr);
} } else {
throw e; throw e;
} }
}
} else { } else {
Deno.copyFileSync(srcStr, destStr); Deno.copyFileSync(srcStr, destStr);
} }

View file

@ -1,11 +1,13 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // 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 { join } from "node:path";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import { import {
closeSync, closeSync,
constants, constants,
copyFileSync,
createWriteStream, createWriteStream,
existsSync, existsSync,
lstatSync, lstatSync,
@ -20,6 +22,7 @@ import {
} from "node:fs"; } from "node:fs";
import { import {
constants as fsPromiseConstants, constants as fsPromiseConstants,
copyFile,
cp, cp,
FileHandle, FileHandle,
open, open,
@ -212,3 +215,19 @@ Deno.test("[node/fs] readSync works", () => {
assertEquals(bytesRead, 12); assertEquals(bytesRead, 12);
closeSync(fd!); 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)
);
});