mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
std/node: fs.writeFile/sync path can now be an URL (#5652)
This commit is contained in:
parent
28b651c2e2
commit
7f81f02ce7
3 changed files with 48 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { notImplemented } from "../_utils.ts";
|
||||
import { fromFileUrl } from "../path.ts";
|
||||
|
||||
import {
|
||||
WriteFileOptions,
|
||||
|
@ -10,7 +11,7 @@ import {
|
|||
} from "./_fs_common.ts";
|
||||
|
||||
export function writeFile(
|
||||
pathOrRid: string | number,
|
||||
pathOrRid: string | number | URL,
|
||||
data: string | Uint8Array,
|
||||
optOrCallback: string | CallbackWithError | WriteFileOptions | undefined,
|
||||
callback?: CallbackWithError
|
||||
|
@ -24,6 +25,8 @@ export function writeFile(
|
|||
throw new TypeError("Callback must be a function.");
|
||||
}
|
||||
|
||||
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
|
||||
|
||||
const flag: string | undefined = isFileOptions(options)
|
||||
? options.flag
|
||||
: undefined;
|
||||
|
@ -65,10 +68,12 @@ export function writeFile(
|
|||
}
|
||||
|
||||
export function writeFileSync(
|
||||
pathOrRid: string | number,
|
||||
pathOrRid: string | number | URL,
|
||||
data: string | Uint8Array,
|
||||
options?: string | WriteFileOptions
|
||||
): void {
|
||||
pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
|
||||
|
||||
const flag: string | undefined = isFileOptions(options)
|
||||
? options.flag
|
||||
: undefined;
|
||||
|
|
|
@ -8,7 +8,9 @@ import {
|
|||
assertThrows,
|
||||
} from "../../testing/asserts.ts";
|
||||
import { writeFile, writeFileSync } from "./_fs_writeFile.ts";
|
||||
import * as path from "../../path/mod.ts";
|
||||
|
||||
const testDataDir = path.resolve(path.join("node", "_fs", "testdata"));
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
|
||||
test("Callback must be a function error", function fn() {
|
||||
|
@ -139,6 +141,27 @@ test("Data is written to correct file", async function testCorrectWriteUsingPath
|
|||
assertEquals(decoder.decode(data), "hello world");
|
||||
});
|
||||
|
||||
test("Path can be an URL", async function testCorrectWriteUsingURL() {
|
||||
const url = new URL(
|
||||
Deno.build.os === "windows"
|
||||
? "file:///" +
|
||||
path
|
||||
.join(testDataDir, "_fs_writeFile_test_file_url.txt")
|
||||
.replace(/\\/g, "/")
|
||||
: "file://" + path.join(testDataDir, "_fs_writeFile_test_file_url.txt")
|
||||
);
|
||||
const filePath = path.fromFileUrl(url);
|
||||
const res = await new Promise((resolve) => {
|
||||
writeFile(url, "hello world", resolve);
|
||||
});
|
||||
assert(res === null);
|
||||
|
||||
const data = await Deno.readFile(filePath);
|
||||
await Deno.remove(filePath);
|
||||
assertEquals(res, null);
|
||||
assertEquals(decoder.decode(data), "hello world");
|
||||
});
|
||||
|
||||
test("Mode is correctly set", async function testCorrectFileMode() {
|
||||
if (Deno.build.os === "windows") return;
|
||||
const filename = "_fs_writeFile_test_file.txt";
|
||||
|
@ -204,6 +227,23 @@ test("Data is written synchronously to correct file", function testCorrectWriteS
|
|||
assertEquals(decoder.decode(data), "hello world");
|
||||
});
|
||||
|
||||
test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() {
|
||||
const filePath = path.join(
|
||||
testDataDir,
|
||||
"_fs_writeFileSync_test_file_url.txt"
|
||||
);
|
||||
const url = new URL(
|
||||
Deno.build.os === "windows"
|
||||
? "file:///" + filePath.replace(/\\/g, "/")
|
||||
: "file://" + filePath
|
||||
);
|
||||
writeFileSync(url, "hello world");
|
||||
|
||||
const data = Deno.readFileSync(filePath);
|
||||
Deno.removeSync(filePath);
|
||||
assertEquals(decoder.decode(data), "hello world");
|
||||
});
|
||||
|
||||
test("Mode is correctly set when writing synchronously", function testCorrectFileModeSync() {
|
||||
if (Deno.build.os === "windows") return;
|
||||
const filename = "_fs_writeFileSync_test_file.txt";
|
||||
|
|
|
@ -4,7 +4,7 @@ import { WriteFileOptions } from "../_fs_common.ts";
|
|||
import { writeFile as writeFileCallback } from "../_fs_writeFile.ts";
|
||||
|
||||
export function writeFile(
|
||||
pathOrRid: string | number,
|
||||
pathOrRid: string | number | URL,
|
||||
data: string | Uint8Array,
|
||||
options?: string | WriteFileOptions
|
||||
): Promise<void> {
|
||||
|
|
Loading…
Add table
Reference in a new issue