2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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 { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
|
|
|
|
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("ensureFileIfItNotExist", async function (): Promise<void> {
|
2019-03-12 01:52:43 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_1");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-11-13 13:42:34 -05:00
|
|
|
await Deno.stat(testFile).then((): void => {
|
|
|
|
throw new Error("test file 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(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureFileSyncIfItNotExist", function (): void {
|
2019-03-12 01:52:43 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureFileIfItExist", async function (): Promise<void> {
|
2019-03-12 01:52:43 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-03-12 01:52:43 -04:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-11-13 13:42:34 -05:00
|
|
|
await Deno.stat(testFile).then((): void => {
|
|
|
|
throw new Error("test file 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(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureFileSyncIfItExist", function (): void {
|
2019-03-12 01:52:43 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_4");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-03-12 01:52:43 -04:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
2019-04-06 21:01:23 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureFileIfItExistAsDir", async function (): Promise<void> {
|
2019-04-06 21:01:23 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_5");
|
|
|
|
|
2020-01-07 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-06 21:01:23 -04:00
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-04-06 21:01:23 -04:00
|
|
|
await ensureFile(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Ensure path exists, expected 'file', got 'dir'`,
|
2019-04-06 21:01:23 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureFileSyncIfItExistAsDir", function (): void {
|
2019-04-06 21:01:23 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_6");
|
|
|
|
|
2020-01-07 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-06 21:01:23 -04:00
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-04-06 21:01:23 -04:00
|
|
|
ensureFileSync(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
`Ensure path exists, expected 'file', got 'dir'`,
|
2019-04-06 21:01:23 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|