diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-10-10 05:31:23 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-10 05:31:23 -0400 |
commit | e7562eed8c816cd0d97aab6b818d7c8453dbaa2b (patch) | |
tree | c5a9f536e79d2c8d2d02897511a9138acaf35394 /std/fs/ensure_file_test.ts | |
parent | 3882c9d19a641e0c919f1350d87c6d7ee280cf78 (diff) | |
parent | 93f7f00c956c14620ef031626f124b57397ca867 (diff) |
Merge deno_std in main repo (#3091)
The history of deno_std is persevered but rewritten to update links to issues and PRs
Fixes denoland/deno_std#603
Diffstat (limited to 'std/fs/ensure_file_test.ts')
m--------- | std | 0 | ||||
-rw-r--r-- | std/fs/ensure_file_test.ts | 115 |
2 files changed, 115 insertions, 0 deletions
diff --git a/std b/std deleted file mode 160000 -Subproject 43aafbf33285753e7b42230f0eb7969b300f71c diff --git a/std/fs/ensure_file_test.ts b/std/fs/ensure_file_test.ts new file mode 100644 index 000000000..fa27133ab --- /dev/null +++ b/std/fs/ensure_file_test.ts @@ -0,0 +1,115 @@ +// 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 { ensureFile, ensureFileSync } from "./ensure_file.ts"; +import * as path from "./path/mod.ts"; + +const testdataDir = path.resolve("fs", "testdata"); + +test(async function ensureFileIfItNotExist(): Promise<void> { + const testDir = path.join(testdataDir, "ensure_file_1"); + const testFile = path.join(testDir, "test.txt"); + + await ensureFile(testFile); + + await assertThrowsAsync( + async (): Promise<void> => { + await Deno.stat(testFile).then( + (): void => { + throw new Error("test file should exists."); + } + ); + } + ); + + await Deno.remove(testDir, { recursive: true }); +}); + +test(function ensureFileSyncIfItNotExist(): void { + const testDir = path.join(testdataDir, "ensure_file_2"); + const testFile = path.join(testDir, "test.txt"); + + ensureFileSync(testFile); + + assertThrows( + (): void => { + Deno.statSync(testFile); + throw new Error("test file should exists."); + } + ); + + Deno.removeSync(testDir, { recursive: true }); +}); + +test(async function ensureFileIfItExist(): Promise<void> { + 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); + + await assertThrowsAsync( + async (): Promise<void> => { + await Deno.stat(testFile).then( + (): void => { + throw new Error("test file should exists."); + } + ); + } + ); + + await Deno.remove(testDir, { recursive: true }); +}); + +test(function ensureFileSyncIfItExist(): void { + 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); + + assertThrows( + (): void => { + Deno.statSync(testFile); + throw new Error("test file should exists."); + } + ); + + Deno.removeSync(testDir, { recursive: true }); +}); + +test(async function ensureFileIfItExistAsDir(): Promise<void> { + const testDir = path.join(testdataDir, "ensure_file_5"); + + await Deno.mkdir(testDir, true); + + await assertThrowsAsync( + async (): Promise<void> => { + await ensureFile(testDir); + }, + Error, + `Ensure path exists, expected 'file', got 'dir'` + ); + + await Deno.remove(testDir, { recursive: true }); +}); + +test(function ensureFileSyncIfItExistAsDir(): void { + const testDir = path.join(testdataDir, "ensure_file_6"); + + Deno.mkdirSync(testDir, true); + + assertThrows( + (): void => { + ensureFileSync(testDir); + }, + Error, + `Ensure path exists, expected 'file', got 'dir'` + ); + + Deno.removeSync(testDir, { recursive: true }); +}); |