2019-03-12 01:52:43 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { test } from "../testing/mod.ts";
|
|
|
|
import { assertThrows, assertThrowsAsync } from "../testing/asserts.ts";
|
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
|
|
|
import * as path from "./path/mod.ts";
|
2019-04-06 21:01:23 -04:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureDirIfItNotExist(): 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-09-11 23:31:23 -04:00
|
|
|
await Deno.stat(testDir).then((): void => {
|
|
|
|
throw new Error("test dir should exists.");
|
|
|
|
});
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
|
|
|
);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureDirSyncIfItNotExist(): 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 });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureDirIfItExist(): 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
|
|
|
|
await Deno.mkdir(testDir, true);
|
|
|
|
|
|
|
|
await ensureDir(testDir);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-09-11 23:31:23 -04:00
|
|
|
await Deno.stat(testDir).then((): void => {
|
|
|
|
throw new Error("test dir should still exists.");
|
|
|
|
});
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
|
|
|
);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureDirSyncIfItExist(): 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
|
|
|
|
Deno.mkdirSync(testDir, true);
|
|
|
|
|
|
|
|
ensureDirSync(testDir);
|
|
|
|
|
2019-09-11 23:31:23 -04: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
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureDirIfItAsFile(): 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,
|
|
|
|
`Ensure path exists, expected 'dir', got 'file'`
|
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(baseDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureDirSyncIfItAsFile(): 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,
|
|
|
|
`Ensure path exists, expected 'dir', got 'file'`
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(baseDir, { recursive: true });
|
|
|
|
});
|