mirror of
https://github.com/denoland/deno.git
synced 2024-10-30 09:08:00 -04:00
extract internal method isSubdir to fs/utils.ts (denoland/deno_std#285)
Original: da4abcd9a3
This commit is contained in:
parent
8acdecd72d
commit
942df0be0d
4 changed files with 58 additions and 11 deletions
14
fs/move.ts
14
fs/move.ts
|
@ -1,20 +1,12 @@
|
|||
// 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";
|
||||
|
||||
interface MoveOptions {
|
||||
overwrite?: boolean;
|
||||
}
|
||||
|
||||
function isSrcSubdir(src: string, dest: string): boolean {
|
||||
const srcArray = src.split(path.sep);
|
||||
const destArray = dest.split(path.sep);
|
||||
|
||||
return srcArray.reduce((acc, current, i) => {
|
||||
return acc && destArray[i] === current;
|
||||
}, true);
|
||||
}
|
||||
|
||||
/** Moves a file or directory */
|
||||
export async function move(
|
||||
src: string,
|
||||
|
@ -26,7 +18,7 @@ export async function move(
|
|||
|
||||
const srcStat = await Deno.stat(src);
|
||||
|
||||
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
|
||||
if (srcStat.isDirectory() && isSubdir(src, dest)) {
|
||||
throw new Error(
|
||||
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`
|
||||
);
|
||||
|
@ -56,7 +48,7 @@ export function moveSync(
|
|||
|
||||
const srcStat = Deno.statSync(src);
|
||||
|
||||
if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
|
||||
if (srcStat.isDirectory() && isSubdir(src, dest)) {
|
||||
throw new Error(
|
||||
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`
|
||||
);
|
||||
|
|
23
fs/utils.ts
Normal file
23
fs/utils.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import * as path from "./path/mod.ts";
|
||||
|
||||
/**
|
||||
* Test whether or not `dest` is a sub-directory of `src`
|
||||
* @param src src file path
|
||||
* @param dest dest file path
|
||||
* @param sep path separator
|
||||
*/
|
||||
export function isSubdir(
|
||||
src: string,
|
||||
dest: string,
|
||||
sep: string = path.sep
|
||||
): boolean {
|
||||
if (src === dest) {
|
||||
return false;
|
||||
}
|
||||
const srcArray = src.split(sep);
|
||||
const destArray = dest.split(sep);
|
||||
|
||||
return srcArray.reduce((acc, current, i) => {
|
||||
return acc && destArray[i] === current;
|
||||
}, true);
|
||||
}
|
31
fs/utils_test.ts
Normal file
31
fs/utils_test.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Copyright the Browserify authors. MIT License.
|
||||
|
||||
import { test } from "../testing/mod.ts";
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { isSubdir } from "./utils.ts";
|
||||
import * as path from "./path/mod.ts";
|
||||
|
||||
test(function _isSubdir() {
|
||||
const pairs = [
|
||||
["", "", false, path.posix.sep],
|
||||
["/first/second", "/first", false, path.posix.sep],
|
||||
["/first", "/first", false, path.posix.sep],
|
||||
["/first", "/first/second", true, path.posix.sep],
|
||||
["first", "first/second", true, path.posix.sep],
|
||||
["../first", "../first/second", true, path.posix.sep],
|
||||
["c:\\first", "c:\\first", false, path.win32.sep],
|
||||
["c:\\first", "c:\\first\\second", true, path.win32.sep]
|
||||
];
|
||||
|
||||
pairs.forEach(function(p) {
|
||||
const src = p[0] as string;
|
||||
const dest = p[1] as string;
|
||||
const expected = p[2] as boolean;
|
||||
const sep = p[3] as string;
|
||||
assertEquals(
|
||||
isSubdir(src, dest, sep),
|
||||
expected,
|
||||
`'${src}' should ${expected ? "" : "not"} be parent dir of '${dest}'`
|
||||
);
|
||||
});
|
||||
});
|
1
test.ts
1
test.ts
|
@ -20,6 +20,7 @@ import "./fs/ensure_file_test.ts";
|
|||
import "./fs/move_test.ts";
|
||||
import "./fs/read_json_test.ts";
|
||||
import "./fs/write_json_test.ts";
|
||||
import "./fs/utils_test.ts";
|
||||
import "./io/test.ts";
|
||||
import "./http/server_test.ts";
|
||||
import "./http/file_server_test.ts";
|
||||
|
|
Loading…
Reference in a new issue