2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-12 01:52:43 -04:00
|
|
|
import { assertThrows, assertThrowsAsync } from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-12 01:52:43 -04:00
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
2019-04-06 21:01:23 -04:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
2019-03-12 01:52:43 -04:00
|
|
|
|
2020-09-08 05:43:43 -04:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata");
|
2019-03-12 01:52:43 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureDirIfItNotExist", async function (): Promise<void> {
|
2019-03-12 01:52:43 -04:00
|
|
|
const baseDir = path.join(testdataDir, "ensure_dir_not_exist");
|
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
|
|
|
await ensureDir(testDir);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-11-13 13:42:34 -05:00
|
|
|
await Deno.stat(testDir).then((): void => {
|
|
|
|
throw new Error("test dir should exists.");
|
|
|
|
});
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureDirSyncIfItNotExist", function (): void {
|
2019-03-12 01:52:43 -04:00
|
|
|
const baseDir = path.join(testdataDir, "ensure_dir_sync_not_exist");
|
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
|
|
|
ensureDirSync(testDir);
|
|
|
|
|
2019-04-06 21:01:23 -04:00
|
|
|
Deno.statSync(testDir);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureDirIfItExist", async function (): Promise<void> {
|
2019-03-12 01:52:43 -04:00
|
|
|
const baseDir = path.join(testdataDir, "ensure_dir_exist");
|
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
|
|
|
// create test directory
|
2020-01-07 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await ensureDir(testDir);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-11-13 13:42:34 -05:00
|
|
|
await Deno.stat(testDir).then((): void => {
|
|
|
|
throw new Error("test dir should still exists.");
|
|
|
|
});
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureDirSyncIfItExist", function (): void {
|
2019-03-12 01:52:43 -04:00
|
|
|
const baseDir = path.join(testdataDir, "ensure_dir_sync_exist");
|
|
|
|
const testDir = path.join(baseDir, "test");
|
|
|
|
|
|
|
|
// create test directory
|
2020-01-07 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
ensureDirSync(testDir);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testDir);
|
|
|
|
throw new Error("test dir should still exists.");
|
|
|
|
});
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
});
|
2019-04-06 21:01:23 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureDirIfItAsFile", async function (): Promise<void> {
|
2019-04-06 21:01:23 -04:00
|
|
|
const baseDir = path.join(testdataDir, "ensure_dir_exist_file");
|
|
|
|
const testFile = path.join(baseDir, "test");
|
|
|
|
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-04-06 21:01:23 -04:00
|
|
|
await ensureDir(testFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Ensure path exists, expected 'dir', got 'file'`,
|
2019-04-06 21:01:23 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureDirSyncIfItAsFile", function (): void {
|
2019-04-06 21:01:23 -04:00
|
|
|
const baseDir = path.join(testdataDir, "ensure_dir_exist_file_async");
|
|
|
|
const testFile = path.join(baseDir, "test");
|
|
|
|
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-04-06 21:01:23 -04:00
|
|
|
ensureDirSync(testFile);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Ensure path exists, expected 'dir', got 'file'`,
|
2019-04-06 21:01:23 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
});
|