From adb19cbae366288a152dd3ab8293e5aea2edc80b Mon Sep 17 00:00:00 2001 From: Axetroy Date: Wed, 20 Mar 2019 01:23:22 +0800 Subject: [PATCH] remove unnecessary path.resolve in move/readJson/writeJson (denoland/deno_std#292) Original: https://github.com/denoland/deno_std/commit/a00c51b05b6172d250b04a3bb6c244e5276c2704 --- fs/move.ts | 7 ------- fs/read_json.ts | 3 --- fs/read_json_test.ts | 20 ++++++++++++++++---- fs/write_json.ts | 5 ----- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/fs/move.ts b/fs/move.ts index 007e4520bd..190f886093 100644 --- a/fs/move.ts +++ b/fs/move.ts @@ -1,5 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as path from "./path/mod.ts"; import { exists, existsSync } from "./exists.ts"; import { isSubdir } from "./utils.ts"; @@ -13,9 +12,6 @@ export async function move( dest: string, options?: MoveOptions ): Promise { - src = path.resolve(src); - dest = path.resolve(dest); - const srcStat = await Deno.stat(src); if (srcStat.isDirectory() && isSubdir(src, dest)) { @@ -43,9 +39,6 @@ export function moveSync( dest: string, options?: MoveOptions ): void { - src = path.resolve(src); - dest = path.resolve(dest); - const srcStat = Deno.statSync(src); if (srcStat.isDirectory() && isSubdir(src, dest)) { diff --git a/fs/read_json.ts b/fs/read_json.ts index 1bed750782..8979c24e8c 100644 --- a/fs/read_json.ts +++ b/fs/read_json.ts @@ -1,9 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import * as path from "./path/mod.ts"; /** Reads a JSON file and then parses it into an object */ export async function readJson(filePath: string): Promise { - filePath = path.resolve(filePath); const decoder = new TextDecoder("utf-8"); const content = decoder.decode(await Deno.readFile(filePath)); @@ -18,7 +16,6 @@ export async function readJson(filePath: string): Promise { /** Reads a JSON file and then parses it into an object */ export function readJsonSync(filePath: string): any { - filePath = path.resolve(filePath); const decoder = new TextDecoder("utf-8"); const content = decoder.decode(Deno.readFileSync(filePath)); diff --git a/fs/read_json_test.ts b/fs/read_json_test.ts index 46ce1ec08b..ca32ee0539 100644 --- a/fs/read_json_test.ts +++ b/fs/read_json_test.ts @@ -34,7 +34,7 @@ test(async function readInvalidJsonFile() { }); }); -test(async function readValidJsonFile() { +test(async function readValidArrayJsonFile() { const invalidJsonFile = path.join(testdataDir, "json_valid_array.json"); const json = await readJson(invalidJsonFile); @@ -42,7 +42,7 @@ test(async function readValidJsonFile() { assertEquals(json, ["1", "2", "3"]); }); -test(async function readValidJsonFile() { +test(async function readValidObjJsonFile() { const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json"); const json = await readJson(invalidJsonFile); @@ -50,6 +50,12 @@ test(async function readValidJsonFile() { assertEquals(json, { key1: "value1", key2: "value2" }); }); +test(async function readValidObjJsonFileWithRelativePath() { + const json = await readJson("./fs/testdata/json_valid_obj.json"); + + assertEquals(json, { key1: "value1", key2: "value2" }); +}); + test(function readJsonFileNotExistsSync() { const emptyJsonFile = path.join(testdataDir, "json_not_exists.json"); @@ -74,7 +80,7 @@ test(function readInvalidJsonFile() { }); }); -test(function readValidJsonFile() { +test(function readValidArrayJsonFileSync() { const invalidJsonFile = path.join(testdataDir, "json_valid_array.json"); const json = readJsonSync(invalidJsonFile); @@ -82,10 +88,16 @@ test(function readValidJsonFile() { assertEquals(json, ["1", "2", "3"]); }); -test(function readValidJsonFile() { +test(function readValidObjJsonFileSync() { const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json"); const json = readJsonSync(invalidJsonFile); assertEquals(json, { key1: "value1", key2: "value2" }); }); + +test(function readValidObjJsonFileSyncWithRelativePath() { + const json = readJsonSync("./fs/testdata/json_valid_obj.json"); + + assertEquals(json, { key1: "value1", key2: "value2" }); +}); diff --git a/fs/write_json.ts b/fs/write_json.ts index f94489095e..c5936d3f80 100644 --- a/fs/write_json.ts +++ b/fs/write_json.ts @@ -1,6 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. /* eslint-disable @typescript-eslint/no-explicit-any */ -import * as path from "./path/mod.ts"; type Replacer = (key: string, value: any) => any; export interface WriteJsonOptions { @@ -14,8 +13,6 @@ export async function writeJson( object: any, options: WriteJsonOptions = {} ): Promise { - filePath = path.resolve(filePath); - let contentRaw = ""; try { @@ -38,8 +35,6 @@ export function writeJsonSync( object: any, options: WriteJsonOptions = {} ): void { - filePath = path.resolve(filePath); - let contentRaw = ""; try {