2019-03-17 12:34:55 -04:00
|
|
|
// Copyright the Browserify authors. MIT License.
|
|
|
|
|
|
|
|
import { test } from "../testing/mod.ts";
|
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-04-06 21:01:23 -04:00
|
|
|
import { isSubdir, getFileInfoType, PathType } from "./utils.ts";
|
2019-03-17 12:34:55 -04:00
|
|
|
import * as path from "./path/mod.ts";
|
2019-04-06 21:01:23 -04:00
|
|
|
import { ensureFileSync } from "./ensure_file.ts";
|
|
|
|
import { ensureDirSync } from "./ensure_dir.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
2019-03-17 12:34:55 -04:00
|
|
|
|
|
|
|
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}'`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-04-06 21:01:23 -04:00
|
|
|
|
|
|
|
test(function _getFileInfoType() {
|
|
|
|
const pairs = [
|
2019-04-23 17:42:02 -04:00
|
|
|
[path.join(testdataDir, "file_type_1"), "file"],
|
|
|
|
[path.join(testdataDir, "file_type_dir_1"), "dir"]
|
2019-04-06 21:01:23 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
pairs.forEach(function(p) {
|
|
|
|
const filePath = p[0] as string;
|
|
|
|
const type = p[1] as PathType;
|
|
|
|
switch (type) {
|
2019-04-23 17:42:02 -04:00
|
|
|
case "file":
|
2019-04-06 21:01:23 -04:00
|
|
|
ensureFileSync(filePath);
|
|
|
|
break;
|
2019-04-23 17:42:02 -04:00
|
|
|
case "dir":
|
2019-04-06 21:01:23 -04:00
|
|
|
ensureDirSync(filePath);
|
|
|
|
break;
|
2019-04-23 17:42:02 -04:00
|
|
|
case "symlink":
|
2019-04-06 21:01:23 -04:00
|
|
|
// TODO(axetroy): test symlink
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stat = Deno.statSync(filePath);
|
|
|
|
|
|
|
|
Deno.removeSync(filePath, { recursive: true });
|
|
|
|
|
|
|
|
assertEquals(getFileInfoType(stat), type);
|
|
|
|
});
|
|
|
|
});
|