2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
|
|
|
|
import * as path from "@std/path";
|
2019-03-12 05:52:43 +00:00
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
2019-04-07 01:01:23 +00:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
2024-09-11 10:17:27 +00:00
|
|
|
import { IS_DENO_2 } from "../internal/_is_deno_2.ts";
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2020-09-08 09:43:43 +00:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
2024-04-22 11:51:39 +00:00
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata", "ensure_dir");
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureDir() creates dir if it does not exist", async function () {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "not_exist");
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
await ensureDir(testDir);
|
|
|
|
|
|
|
|
// test dir should exists.
|
|
|
|
await Deno.stat(testDir);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureDirSync() creates dir if it does not exist", function () {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "sync_not_exist");
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
ensureDirSync(testDir);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
// test dir should exists.
|
|
|
|
Deno.statSync(testDir);
|
|
|
|
} finally {
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureDir() ensures existing dir exists", async function () {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "exist");
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
// create test directory
|
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
await ensureDir(testDir);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
// test dir should still exists.
|
|
|
|
await Deno.stat(testDir);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureDirSync() ensures existing dir exists", function () {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "sync_exist");
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
// create test directory
|
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
ensureDirSync(testDir);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
// test dir should still exists.
|
2019-11-13 18:42:34 +00:00
|
|
|
Deno.statSync(testDir);
|
2023-03-08 12:27:10 +00:00
|
|
|
} finally {
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
}
|
2019-03-12 05:52:43 +00:00
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2024-04-22 11:51:39 +00:00
|
|
|
Deno.test("ensureDir() accepts links to dirs", async function () {
|
|
|
|
const ldir = path.join(testdataDir, "ldir");
|
|
|
|
|
|
|
|
await ensureDir(ldir);
|
|
|
|
|
|
|
|
// test dir should still exists.
|
|
|
|
await Deno.stat(ldir);
|
|
|
|
// ldir should be still be a symlink
|
|
|
|
const { isSymlink } = await Deno.lstat(ldir);
|
|
|
|
assertEquals(isSymlink, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("ensureDirSync() accepts links to dirs", function () {
|
|
|
|
const ldir = path.join(testdataDir, "ldir");
|
|
|
|
|
|
|
|
ensureDirSync(ldir);
|
|
|
|
|
|
|
|
// test dir should still exists.
|
|
|
|
Deno.statSync(ldir);
|
|
|
|
// ldir should be still be a symlink
|
|
|
|
const { isSymlink } = Deno.lstatSync(ldir);
|
|
|
|
assertEquals(isSymlink, true);
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureDir() rejects if input is a file", async function () {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "exist_file");
|
2019-04-07 01:01:23 +00:00
|
|
|
const testFile = path.join(baseDir, "test");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await ensureDir(testFile);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure directory exists: expected 'dir', got 'file'`,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
}
|
2019-04-07 01:01:23 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureDirSync() throws if input is a file", function () {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "exist_file_async");
|
2019-04-07 01:01:23 +00:00
|
|
|
const testFile = path.join(baseDir, "test");
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
try {
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
ensureDirSync(testFile);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure directory exists: expected 'dir', got 'file'`,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2024-04-22 11:51:39 +00:00
|
|
|
Deno.test("ensureDir() rejects links to files", async function () {
|
|
|
|
const lf = path.join(testdataDir, "lf");
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await ensureDir(lf);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure directory exists: expected 'dir', got 'file'`,
|
2024-04-22 11:51:39 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("ensureDirSync() rejects links to files", function () {
|
|
|
|
const lf = path.join(testdataDir, "lf");
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
ensureDirSync(lf);
|
|
|
|
},
|
|
|
|
Error,
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure directory exists: expected 'dir', got 'file'`,
|
2024-04-22 11:51:39 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
Deno.test({
|
2023-11-23 06:59:59 +00:00
|
|
|
name: "ensureDir() rejects permission fs write error",
|
2023-03-08 12:27:10 +00:00
|
|
|
permissions: { read: true },
|
|
|
|
async fn() {
|
2024-04-22 11:51:39 +00:00
|
|
|
const baseDir = path.join(testdataDir, "without_permission");
|
2023-03-08 12:27:10 +00:00
|
|
|
|
|
|
|
// ensureDir fails because this test doesn't have write permissions,
|
|
|
|
// but don't swallow that error.
|
|
|
|
await assertRejects(
|
|
|
|
async () => await ensureDir(baseDir),
|
2024-09-11 10:17:27 +00:00
|
|
|
IS_DENO_2
|
|
|
|
// TODO(iuioiua): Just use `Deno.errors.NotCapable` once Deno 2 is released.
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
? (Deno as any).errors.NotCapable
|
|
|
|
: Deno.errors.PermissionDenied,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2023-03-08 12:27:10 +00:00
|
|
|
Deno.test({
|
2023-11-23 06:59:59 +00:00
|
|
|
name: "ensureDirSync() throws permission fs write error",
|
2023-03-08 12:27:10 +00:00
|
|
|
permissions: { read: true },
|
|
|
|
fn() {
|
|
|
|
const baseDir = path.join(
|
|
|
|
testdataDir,
|
2024-04-22 11:51:39 +00:00
|
|
|
"sync_without_permission",
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ensureDirSync fails because this test doesn't have write permissions,
|
|
|
|
// but don't swallow that error.
|
|
|
|
assertThrows(
|
|
|
|
() => ensureDirSync(baseDir),
|
2024-09-11 10:17:27 +00:00
|
|
|
IS_DENO_2
|
|
|
|
// TODO(iuioiua): Just use `Deno.errors.NotCapable` once Deno 2 is released.
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
? (Deno as any).errors.NotCapable
|
|
|
|
: Deno.errors.PermissionDenied,
|
2023-03-08 12:27:10 +00:00
|
|
|
);
|
|
|
|
},
|
2019-04-07 01:01:23 +00:00
|
|
|
});
|
2024-06-19 10:12:54 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "ensureDir() isn't racy",
|
|
|
|
async fn() {
|
|
|
|
for (const _ of Array(100)) {
|
|
|
|
const dir = path.join(
|
|
|
|
await Deno.makeTempDir(),
|
|
|
|
"check",
|
|
|
|
"race",
|
|
|
|
);
|
|
|
|
|
|
|
|
// It doesn't throw with successive calls.
|
|
|
|
await Promise.all([
|
|
|
|
ensureDir(dir),
|
|
|
|
ensureDir(dir),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|