2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-06-25 06:57:08 +08:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 21:21:11 +08:00
|
|
|
assertRejects,
|
2020-06-25 06:57:08 +08:00
|
|
|
assertThrows,
|
2020-10-27 23:21:32 +11:00
|
|
|
pathToAbsoluteFileUrl,
|
2020-06-25 06:57:08 +08:00
|
|
|
} from "./test_util.ts";
|
2020-04-02 14:54:33 -04:00
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
function assertDirectory(path: string, mode?: number) {
|
2020-04-02 14:54:33 -04:00
|
|
|
const info = Deno.lstatSync(path);
|
2020-04-16 06:40:30 +01:00
|
|
|
assert(info.isDirectory);
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows" && mode !== undefined) {
|
2020-04-02 14:54:33 -04:00
|
|
|
assertEquals(info.mode! & 0o777, mode & ~Deno.umask());
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 14:18:36 +04:30
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncSuccess() {
|
2020-03-04 17:31:14 +01:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir";
|
|
|
|
Deno.mkdirSync(path);
|
2020-04-02 14:54:33 -04:00
|
|
|
assertDirectory(path);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
);
|
2018-09-10 14:18:36 +04:30
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncMode() {
|
2020-03-04 17:31:14 +01:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir";
|
2020-03-11 16:14:23 -04:00
|
|
|
Deno.mkdirSync(path, { mode: 0o737 });
|
2020-04-02 14:54:33 -04:00
|
|
|
assertDirectory(path, 0o737);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
);
|
2018-09-14 12:30:43 -07:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test({ permissions: { write: false } }, function mkdirSyncPerm() {
|
2020-06-25 06:57:08 +08:00
|
|
|
assertThrows(() => {
|
2019-02-13 02:08:56 +11:00
|
|
|
Deno.mkdirSync("/baddir");
|
2020-06-25 06:57:08 +08:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-09-10 14:18:36 +04:30
|
|
|
});
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function mkdirSuccess() {
|
2020-03-04 17:31:14 +01:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir";
|
|
|
|
await Deno.mkdir(path);
|
2020-04-02 14:54:33 -04:00
|
|
|
assertDirectory(path);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
);
|
2019-01-17 23:39:06 -05:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function mkdirMode() {
|
2020-03-11 16:14:23 -04:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir";
|
|
|
|
await Deno.mkdir(path, { mode: 0o737 });
|
2020-04-02 14:54:33 -04:00
|
|
|
assertDirectory(path, 0o737);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-11 16:14:23 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test({ permissions: { write: true } }, function mkdirErrSyncIfExists() {
|
2021-10-11 21:21:18 +08:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.mkdirSync(".");
|
|
|
|
},
|
|
|
|
Deno.errors.AlreadyExists,
|
|
|
|
`mkdir '.'`,
|
|
|
|
);
|
2019-01-17 23:39:06 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test({ permissions: { write: true } }, async function mkdirErrIfExists() {
|
2021-10-11 21:21:18 +08:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.mkdir(".");
|
|
|
|
},
|
|
|
|
Deno.errors.AlreadyExists,
|
|
|
|
`mkdir '.'`,
|
|
|
|
);
|
2020-04-02 14:54:33 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncRecursive() {
|
2020-03-04 17:31:14 +01:00
|
|
|
const path = Deno.makeTempDirSync() + "/nested/directory";
|
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
2020-04-02 14:54:33 -04:00
|
|
|
assertDirectory(path);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
);
|
2019-01-17 23:39:06 -05:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function mkdirRecursive() {
|
2020-03-04 17:31:14 +01:00
|
|
|
const path = Deno.makeTempDirSync() + "/nested/directory";
|
|
|
|
await Deno.mkdir(path, { recursive: true });
|
2020-04-02 14:54:33 -04:00
|
|
|
assertDirectory(path);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-02 14:54:33 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncRecursiveMode() {
|
2020-04-02 14:54:33 -04:00
|
|
|
const nested = Deno.makeTempDirSync() + "/nested";
|
|
|
|
const path = nested + "/dir";
|
|
|
|
Deno.mkdirSync(path, { mode: 0o737, recursive: true });
|
|
|
|
assertDirectory(path, 0o737);
|
|
|
|
assertDirectory(nested, 0o737);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-02 14:54:33 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function mkdirRecursiveMode() {
|
2020-04-02 14:54:33 -04:00
|
|
|
const nested = Deno.makeTempDirSync() + "/nested";
|
|
|
|
const path = nested + "/dir";
|
|
|
|
await Deno.mkdir(path, { mode: 0o737, recursive: true });
|
|
|
|
assertDirectory(path, 0o737);
|
|
|
|
assertDirectory(nested, 0o737);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-02 14:54:33 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncRecursiveIfExists() {
|
2020-04-02 14:54:33 -04:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir";
|
|
|
|
Deno.mkdirSync(path, { mode: 0o737 });
|
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
|
|
|
Deno.mkdirSync(path, { recursive: true, mode: 0o731 });
|
|
|
|
assertDirectory(path, 0o737);
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-04-02 14:54:33 -04:00
|
|
|
const pathLink = path + "Link";
|
|
|
|
Deno.symlinkSync(path, pathLink);
|
|
|
|
Deno.mkdirSync(pathLink, { recursive: true });
|
|
|
|
Deno.mkdirSync(pathLink, { recursive: true, mode: 0o731 });
|
|
|
|
assertDirectory(path, 0o737);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-02 14:54:33 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function mkdirRecursiveIfExists() {
|
2020-04-02 14:54:33 -04:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir";
|
|
|
|
await Deno.mkdir(path, { mode: 0o737 });
|
|
|
|
await Deno.mkdir(path, { recursive: true });
|
|
|
|
await Deno.mkdir(path, { recursive: true, mode: 0o731 });
|
|
|
|
assertDirectory(path, 0o737);
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-04-02 14:54:33 -04:00
|
|
|
const pathLink = path + "Link";
|
|
|
|
Deno.symlinkSync(path, pathLink);
|
|
|
|
await Deno.mkdir(pathLink, { recursive: true });
|
|
|
|
await Deno.mkdir(pathLink, { recursive: true, mode: 0o731 });
|
|
|
|
assertDirectory(path, 0o737);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-04-02 14:54:33 -04:00
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncErrors() {
|
2020-04-02 14:54:33 -04:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const emptydir = testDir + "/empty";
|
|
|
|
const fulldir = testDir + "/dir";
|
|
|
|
const file = fulldir + "/file";
|
|
|
|
Deno.mkdirSync(emptydir);
|
|
|
|
Deno.mkdirSync(fulldir);
|
|
|
|
Deno.createSync(file).close();
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(emptydir, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(fulldir, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(file, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(file, { recursive: true });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
|
|
|
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2020-04-02 14:54:33 -04:00
|
|
|
const fileLink = testDir + "/fileLink";
|
|
|
|
const dirLink = testDir + "/dirLink";
|
|
|
|
const danglingLink = testDir + "/danglingLink";
|
|
|
|
Deno.symlinkSync(file, fileLink);
|
|
|
|
Deno.symlinkSync(emptydir, dirLink);
|
|
|
|
Deno.symlinkSync(testDir + "/nonexistent", danglingLink);
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(dirLink, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(fileLink, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(fileLink, { recursive: true });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(danglingLink, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 13:08:58 +02:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(danglingLink, { recursive: true });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
);
|
2020-10-27 23:21:32 +11:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
function mkdirSyncRelativeUrlPath() {
|
2020-10-27 23:21:32 +11:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const nestedDir = testDir + "/nested";
|
|
|
|
// Add trailing slash so base path is treated as a directory. pathToAbsoluteFileUrl removes trailing slashes.
|
|
|
|
const path = new URL("../dir", pathToAbsoluteFileUrl(nestedDir) + "/");
|
|
|
|
|
|
|
|
Deno.mkdirSync(nestedDir);
|
|
|
|
Deno.mkdirSync(path);
|
|
|
|
|
|
|
|
assertDirectory(testDir + "/dir");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function mkdirRelativeUrlPath() {
|
2020-10-27 23:21:32 +11:00
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const nestedDir = testDir + "/nested";
|
|
|
|
// Add trailing slash so base path is treated as a directory. pathToAbsoluteFileUrl removes trailing slashes.
|
|
|
|
const path = new URL("../dir", pathToAbsoluteFileUrl(nestedDir) + "/");
|
|
|
|
|
|
|
|
await Deno.mkdir(nestedDir);
|
|
|
|
await Deno.mkdir(path);
|
|
|
|
|
|
|
|
assertDirectory(testDir + "/dir");
|
|
|
|
},
|
|
|
|
);
|