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";
|
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";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureFileIfItNotExist(): 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-10-09 17:22:22 -04:00
|
|
|
await Deno.stat(testFile).then(
|
|
|
|
(): void => {
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
}
|
|
|
|
);
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
|
|
|
);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureFileSyncIfItNotExist(): 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-10-09 17:22:22 -04: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-24 07:41:23 -04:00
|
|
|
test(async function ensureFileIfItExist(): 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");
|
|
|
|
|
|
|
|
await Deno.mkdir(testDir, true);
|
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
await ensureFile(testFile);
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
2019-10-09 17:22:22 -04:00
|
|
|
await Deno.stat(testFile).then(
|
|
|
|
(): void => {
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
}
|
|
|
|
);
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
|
|
|
);
|
2019-03-12 01:52:43 -04:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureFileSyncIfItExist(): void {
|
2019-03-12 01:52:43 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_4");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
|
|
|
Deno.mkdirSync(testDir, true);
|
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
ensureFileSync(testFile);
|
|
|
|
|
2019-10-09 17:22:22 -04: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
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureFileIfItExistAsDir(): Promise<void> {
|
2019-04-06 21:01:23 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_5");
|
|
|
|
|
|
|
|
await Deno.mkdir(testDir, true);
|
|
|
|
|
|
|
|
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,
|
|
|
|
`Ensure path exists, expected 'file', got 'dir'`
|
|
|
|
);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureFileSyncIfItExistAsDir(): void {
|
2019-04-06 21:01:23 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_file_6");
|
|
|
|
|
|
|
|
Deno.mkdirSync(testDir, true);
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-04-06 21:01:23 -04:00
|
|
|
ensureFileSync(testDir);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
`Ensure path exists, expected 'file', got 'dir'`
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|