2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows,
|
2020-10-27 08:21:32 -04:00
|
|
|
pathToAbsoluteFileUrl,
|
2020-06-24 18:57:08 -04:00
|
|
|
} from "./test_util.ts";
|
2020-04-02 14:54:33 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
function assertDirectory(path: string, mode?: number) {
|
2020-04-02 14:54:33 -04:00
|
|
|
const info = Deno.lstatSync(path);
|
2020-04-16 01:40:30 -04: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 05:48:36 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function mkdirSyncSuccess() {
|
2020-03-04 11:31:14 -05: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 11:31:14 -05:00
|
|
|
);
|
2018-09-10 05:48:36 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function mkdirSyncMode() {
|
2020-03-04 11:31:14 -05: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 11:31:14 -05:00
|
|
|
);
|
2018-09-14 15:30:43 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { write: false } }, function mkdirSyncPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync("/baddir");
|
2024-09-10 14:12:24 -04:00
|
|
|
}, Deno.errors.NotCapable);
|
2018-09-10 05:48:36 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function mkdirSuccess() {
|
2020-03-04 11:31:14 -05: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 11:31:14 -05:00
|
|
|
);
|
2019-01-17 23:39:06 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04: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 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { write: true } }, function mkdirErrSyncIfExists() {
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.mkdirSync(".");
|
|
|
|
},
|
|
|
|
Deno.errors.AlreadyExists,
|
|
|
|
`mkdir '.'`,
|
|
|
|
);
|
2019-01-17 23:39:06 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { write: true } }, async function mkdirErrIfExists() {
|
2021-10-11 09:21:18 -04:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.mkdir(".");
|
|
|
|
},
|
|
|
|
Deno.errors.AlreadyExists,
|
|
|
|
`mkdir '.'`,
|
|
|
|
);
|
2020-04-02 14:54:33 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function mkdirSyncRecursive() {
|
2020-03-04 11:31:14 -05: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 11:31:14 -05:00
|
|
|
);
|
2019-01-17 23:39:06 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function mkdirRecursive() {
|
2020-03-04 11:31:14 -05: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 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04: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 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04: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 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04: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 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04: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 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04: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 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(emptydir, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(fulldir, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(file, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04: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 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(dirLink, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(fileLink, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(fileLink, { recursive: true });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04:00
|
|
|
assertThrows(() => {
|
2020-04-02 14:54:33 -04:00
|
|
|
Deno.mkdirSync(danglingLink, { recursive: false });
|
|
|
|
}, Deno.errors.AlreadyExists);
|
2021-08-05 07:08:58 -04: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 11:31:14 -05:00
|
|
|
);
|
2020-10-27 08:21:32 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function mkdirSyncRelativeUrlPath() {
|
2020-10-27 08:21:32 -04: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 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function mkdirRelativeUrlPath() {
|
2020-10-27 08:21:32 -04: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");
|
|
|
|
},
|
|
|
|
);
|