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

feat: support createNew in Deno.writeFile (#17023)

This commit is contained in:
Leo Kettmeir 2022-12-13 05:12:49 +01:00 committed by GitHub
parent a2ba573e77
commit 5d9bb8b4b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View file

@ -96,6 +96,20 @@ Deno.test(
},
);
Deno.test(
{ permissions: { read: true, write: true } },
function writeFileSyncCreateNew() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
Deno.writeFileSync(filename, data, { createNew: true });
assertThrows(() => {
Deno.writeFileSync(filename, data, { createNew: true });
}, Deno.errors.AlreadyExists);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
function writeFileSyncAppend() {
@ -216,6 +230,19 @@ Deno.test(
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function writeFileCreateNew() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = Deno.makeTempDirSync() + "/test.txt";
await Deno.writeFile(filename, data, { createNew: true });
await assertRejects(async () => {
await Deno.writeFile(filename, data, { createNew: true });
}, Deno.errors.AlreadyExists);
},
);
Deno.test(
{ permissions: { read: true, write: true } },
async function writeFileAppend() {

View file

@ -3296,6 +3296,10 @@ declare namespace Deno {
/** Sets the option to allow creating a new file, if one doesn't already
* exist at the specified path (defaults to `true`). */
create?: boolean;
/** Defaults to `false`. If set to `true`, no file, directory, or symlink is
* allowed to exist at the target location. When createNew is set to `true`,
* `create` is ignored. */
createNew?: boolean;
/** Permissions always applied to file. */
mode?: number;
/** An abort signal to allow cancellation of the file write operation.

View file

@ -17,6 +17,7 @@
options.mode,
options.append ?? false,
options.create ?? true,
options.createNew ?? false,
data,
);
}
@ -41,6 +42,7 @@
options.mode,
options.append ?? false,
options.create ?? true,
options.createNew ?? false,
data,
cancelRid,
);

View file

@ -222,14 +222,18 @@ async fn op_open_async(
}
#[inline]
fn write_open_options(create: bool, append: bool) -> OpenOptions {
fn write_open_options(
create: bool,
append: bool,
create_new: bool,
) -> OpenOptions {
OpenOptions {
read: false,
write: true,
create,
truncate: !append,
append,
create_new: false,
create_new,
}
}
@ -240,13 +244,14 @@ fn op_write_file_sync(
mode: Option<u32>,
append: bool,
create: bool,
create_new: bool,
data: ZeroCopyBuf,
) -> Result<(), AnyError> {
let (path, open_options) = open_helper(
state,
&path,
mode,
Some(&write_open_options(create, append)),
Some(&write_open_options(create, append, create_new)),
"Deno.writeFileSync()",
)?;
write_file(&path, open_options, mode, data)
@ -259,6 +264,7 @@ async fn op_write_file_async(
mode: Option<u32>,
append: bool,
create: bool,
create_new: bool,
data: ZeroCopyBuf,
cancel_rid: Option<ResourceId>,
) -> Result<(), AnyError> {
@ -274,7 +280,7 @@ async fn op_write_file_async(
&mut state.borrow_mut(),
&path,
mode,
Some(&write_open_options(create, append)),
Some(&write_open_options(create, append, create_new)),
"Deno.writeFile()",
)?;
let write_future = tokio::task::spawn_blocking(move || {