feat(cli): Add WriteFileOptions to writeTextFile & writeTextFileSync (#6280)

This commit is contained in:
Marcos Casagrande 2020-07-08 15:38:22 +02:00 committed by GitHub
parent 82aabb657a
commit 231899695d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 129 additions and 20 deletions

View file

@ -1524,7 +1524,11 @@ declare namespace Deno {
* *
* Requires `allow-write` permission, and `allow-read` if `options.create` is `false`. * Requires `allow-write` permission, and `allow-read` if `options.create` is `false`.
*/ */
export function writeTextFileSync(path: string | URL, data: string): void; export function writeTextFileSync(
path: string | URL,
data: string,
options?: WriteFileOptions
): void;
/** Asynchronously write string `data` to the given `path`, by default creating a new file if needed, /** Asynchronously write string `data` to the given `path`, by default creating a new file if needed,
* else overwriting. * else overwriting.
@ -1537,7 +1541,8 @@ declare namespace Deno {
*/ */
export function writeTextFile( export function writeTextFile(
path: string | URL, path: string | URL,
data: string data: string,
options?: WriteFileOptions
): Promise<void>; ): Promise<void>;
/** Synchronously truncates or extends the specified file, to reach the /** Synchronously truncates or extends the specified file, to reach the

View file

@ -1,23 +1,20 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts";
import { open, openSync } from "./files.ts"; export function writeTextFileSync(
import { writeAll, writeAllSync } from "./buffer.ts";
export function writeTextFileSync(path: string | URL, data: string): void {
const file = openSync(path, { write: true, create: true, truncate: true });
const encoder = new TextEncoder();
const contents = encoder.encode(data);
writeAllSync(file, contents);
file.close();
}
export async function writeTextFile(
path: string | URL, path: string | URL,
data: string data: string,
): Promise<void> { options: WriteFileOptions = {}
const file = await open(path, { write: true, create: true, truncate: true }); ): void {
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const contents = encoder.encode(data); return writeFileSync(path, encoder.encode(data), options);
await writeAll(file, contents); }
file.close();
export function writeTextFile(
path: string | URL,
data: string,
options: WriteFileOptions = {}
): Promise<void> {
const encoder = new TextEncoder();
return writeFile(path, encoder.encode(data), options);
} }

View file

@ -1,5 +1,6 @@
import { import {
unitTest, unitTest,
assert,
assertEquals, assertEquals,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
@ -46,6 +47,59 @@ unitTest({ perms: { write: false } }, function writeTextFileSyncPerm(): void {
}, Deno.errors.PermissionDenied); }, Deno.errors.PermissionDenied);
}); });
unitTest(
{ perms: { read: true, write: true } },
function writeTextFileSyncUpdateMode(): void {
if (Deno.build.os !== "windows") {
const data = "Hello";
const filename = Deno.makeTempDirSync() + "/test.txt";
Deno.writeTextFileSync(filename, data, { mode: 0o755 });
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
Deno.writeTextFileSync(filename, data, { mode: 0o666 });
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
}
}
);
unitTest(
{ perms: { read: true, write: true } },
function writeTextFileSyncCreate(): void {
const data = "Hello";
const filename = Deno.makeTempDirSync() + "/test.txt";
let caughtError = false;
// if create turned off, the file won't be created
try {
Deno.writeTextFileSync(filename, data, { create: false });
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
// Turn on create, should have no error
Deno.writeTextFileSync(filename, data, { create: true });
Deno.writeTextFileSync(filename, data, { create: false });
assertEquals("Hello", Deno.readTextFileSync(filename));
}
);
unitTest(
{ perms: { read: true, write: true } },
function writeTextFileSyncAppend(): void {
const data = "Hello";
const filename = Deno.makeTempDirSync() + "/test.txt";
Deno.writeTextFileSync(filename, data);
Deno.writeTextFileSync(filename, data, { append: true });
assertEquals("HelloHello", Deno.readTextFileSync(filename));
// Now attempt overwrite
Deno.writeTextFileSync(filename, data, { append: false });
assertEquals("Hello", Deno.readTextFileSync(filename));
// append not set should also overwrite
Deno.writeTextFileSync(filename, data);
assertEquals("Hello", Deno.readTextFileSync(filename));
}
);
unitTest( unitTest(
{ perms: { read: true, write: true } }, { perms: { read: true, write: true } },
async function writeTextFileSuccess(): Promise<void> { async function writeTextFileSuccess(): Promise<void> {
@ -92,3 +146,56 @@ unitTest(
}, Deno.errors.PermissionDenied); }, Deno.errors.PermissionDenied);
} }
); );
unitTest(
{ perms: { read: true, write: true } },
async function writeTextFileUpdateMode(): Promise<void> {
if (Deno.build.os !== "windows") {
const data = "Hello";
const filename = Deno.makeTempDirSync() + "/test.txt";
await Deno.writeTextFile(filename, data, { mode: 0o755 });
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755);
await Deno.writeTextFile(filename, data, { mode: 0o666 });
assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666);
}
}
);
unitTest(
{ perms: { read: true, write: true } },
async function writeTextFileCreate(): Promise<void> {
const data = "Hello";
const filename = Deno.makeTempDirSync() + "/test.txt";
let caughtError = false;
// if create turned off, the file won't be created
try {
await Deno.writeTextFile(filename, data, { create: false });
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.NotFound);
}
assert(caughtError);
// Turn on create, should have no error
await Deno.writeTextFile(filename, data, { create: true });
await Deno.writeTextFile(filename, data, { create: false });
assertEquals("Hello", Deno.readTextFileSync(filename));
}
);
unitTest(
{ perms: { read: true, write: true } },
async function writeTextFileAppend(): Promise<void> {
const data = "Hello";
const filename = Deno.makeTempDirSync() + "/test.txt";
await Deno.writeTextFile(filename, data);
await Deno.writeTextFile(filename, data, { append: true });
assertEquals("HelloHello", Deno.readTextFileSync(filename));
// Now attempt overwrite
await Deno.writeTextFile(filename, data, { append: false });
assertEquals("Hello", Deno.readTextFileSync(filename));
// append not set should also overwrite
await Deno.writeTextFile(filename, data);
assertEquals("Hello", Deno.readTextFileSync(filename));
}
);