2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-12 05:52:43 +00:00
|
|
|
import { assertThrows, assertThrowsAsync } from "../testing/asserts.ts";
|
2019-10-16 18:39:33 +00:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-12 05:52:43 +00:00
|
|
|
import { ensureFile, ensureFileSync } from "./ensure_file.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureFileIfItNotExist", async function (): Promise<void> {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_1");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
2019-04-24 11:41:23 +00:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-11-13 18:42:34 +00:00
|
|
|
await Deno.stat(testFile).then((): void => {
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureFileSyncIfItNotExist", function (): void {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-03-12 05:52:43 +00:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureFileIfItExist", async function (): Promise<void> {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-03-12 05:52:43 +00:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
2019-04-24 11:41:23 +00:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-11-13 18:42:34 +00:00
|
|
|
await Deno.stat(testFile).then((): void => {
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-03-12 05:52:43 +00:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureFileSyncIfItExist", function (): void {
|
2019-03-12 05:52:43 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_4");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-03-12 05:52:43 +00:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-03-12 05:52:43 +00:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
2019-04-07 01:01:23 +00:00
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureFileIfItExistAsDir", async function (): Promise<void> {
|
2019-04-07 01:01:23 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_5");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-07 01:01:23 +00:00
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 11:41:23 +00:00
|
|
|
async (): Promise<void> => {
|
2019-04-07 01:01:23 +00:00
|
|
|
await ensureFile(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
`Ensure path exists, expected 'file', got 'dir'`,
|
2019-04-07 01:01:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureFileSyncIfItExistAsDir", function (): void {
|
2019-04-07 01:01:23 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_6");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-07 01:01:23 +00:00
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 11:41:23 +00:00
|
|
|
(): void => {
|
2019-04-07 01:01:23 +00:00
|
|
|
ensureFileSync(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 19:24:17 +00:00
|
|
|
`Ensure path exists, expected 'file', got 'dir'`,
|
2019-04-07 01:01:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|