Skip to content

Commit

Permalink
std/node: fs.writeFile/sync path can now be an URL (denoland#5652)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed May 23, 2020
1 parent 28b651c commit 7f81f02
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
9 changes: 7 additions & 2 deletions std/node/_fs/_fs_writeFile.ts
@@ -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,
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions std/node/_fs/_fs_writeFile_test.ts
Expand Up @@ -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() {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion std/node/_fs/promises/_fs_writeFile.ts
Expand Up @@ -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> {
Expand Down

0 comments on commit 7f81f02

Please sign in to comment.