diff options
Diffstat (limited to 'cli/js/write_file_test.ts')
-rw-r--r-- | cli/js/write_file_test.ts | 271 |
1 files changed, 143 insertions, 128 deletions
diff --git a/cli/js/write_file_test.ts b/cli/js/write_file_test.ts index d788748f8..692290938 100644 --- a/cli/js/write_file_test.ts +++ b/cli/js/write_file_test.ts @@ -1,18 +1,21 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { testPerm, assert, assertEquals } from "./test_util.ts"; +import { unitTest, assert, assertEquals } from "./test_util.ts"; -testPerm({ read: true, write: true }, function writeFileSyncSuccess(): void { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - const filename = Deno.makeTempDirSync() + "/test.txt"; - Deno.writeFileSync(filename, data); - const dataRead = Deno.readFileSync(filename); - const dec = new TextDecoder("utf-8"); - const actual = dec.decode(dataRead); - assertEquals("Hello", actual); -}); +unitTest( + { perms: { read: true, write: true } }, + function writeFileSyncSuccess(): void { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + Deno.writeFileSync(filename, data); + const dataRead = Deno.readFileSync(filename); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEquals("Hello", actual); + } +); -testPerm({ write: true }, function writeFileSyncFail(): void { +unitTest({ perms: { write: true } }, function writeFileSyncFail(): void { const enc = new TextEncoder(); const data = enc.encode("Hello"); const filename = "/baddir/test.txt"; @@ -27,7 +30,7 @@ testPerm({ write: true }, function writeFileSyncFail(): void { assert(caughtError); }); -testPerm({ write: false }, function writeFileSyncPerm(): void { +unitTest({ perms: { write: false } }, function writeFileSyncPerm(): void { const enc = new TextEncoder(); const data = enc.encode("Hello"); const filename = "/baddir/test.txt"; @@ -42,65 +45,74 @@ testPerm({ write: false }, function writeFileSyncPerm(): void { assert(caughtError); }); -testPerm({ read: true, write: true }, function writeFileSyncUpdatePerm(): void { - if (Deno.build.os !== "win") { +unitTest( + { perms: { read: true, write: true } }, + function writeFileSyncUpdatePerm(): void { + if (Deno.build.os !== "win") { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + Deno.writeFileSync(filename, data, { perm: 0o755 }); + assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755); + Deno.writeFileSync(filename, data, { perm: 0o666 }); + assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666); + } + } +); + +unitTest( + { perms: { read: true, write: true } }, + function writeFileSyncCreate(): void { const enc = new TextEncoder(); const data = enc.encode("Hello"); const filename = Deno.makeTempDirSync() + "/test.txt"; - Deno.writeFileSync(filename, data, { perm: 0o755 }); - assertEquals(Deno.statSync(filename).mode! & 0o777, 0o755); - Deno.writeFileSync(filename, data, { perm: 0o666 }); - assertEquals(Deno.statSync(filename).mode! & 0o777, 0o666); - } -}); + let caughtError = false; + // if create turned off, the file won't be created + try { + Deno.writeFileSync(filename, data, { create: false }); + } catch (e) { + caughtError = true; + assert(e instanceof Deno.errors.NotFound); + } + assert(caughtError); -testPerm({ read: true, write: true }, function writeFileSyncCreate(): void { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - const filename = Deno.makeTempDirSync() + "/test.txt"; - let caughtError = false; - // if create turned off, the file won't be created - try { + // Turn on create, should have no error + Deno.writeFileSync(filename, data, { create: true }); Deno.writeFileSync(filename, data, { create: false }); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.NotFound); + const dataRead = Deno.readFileSync(filename); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEquals("Hello", actual); } - assert(caughtError); - - // Turn on create, should have no error - Deno.writeFileSync(filename, data, { create: true }); - Deno.writeFileSync(filename, data, { create: false }); - const dataRead = Deno.readFileSync(filename); - const dec = new TextDecoder("utf-8"); - const actual = dec.decode(dataRead); - assertEquals("Hello", actual); -}); +); -testPerm({ read: true, write: true }, function writeFileSyncAppend(): void { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - const filename = Deno.makeTempDirSync() + "/test.txt"; - Deno.writeFileSync(filename, data); - Deno.writeFileSync(filename, data, { append: true }); - let dataRead = Deno.readFileSync(filename); - const dec = new TextDecoder("utf-8"); - let actual = dec.decode(dataRead); - assertEquals("HelloHello", actual); - // Now attempt overwrite - Deno.writeFileSync(filename, data, { append: false }); - dataRead = Deno.readFileSync(filename); - actual = dec.decode(dataRead); - assertEquals("Hello", actual); - // append not set should also overwrite - Deno.writeFileSync(filename, data); - dataRead = Deno.readFileSync(filename); - actual = dec.decode(dataRead); - assertEquals("Hello", actual); -}); +unitTest( + { perms: { read: true, write: true } }, + function writeFileSyncAppend(): void { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + Deno.writeFileSync(filename, data); + Deno.writeFileSync(filename, data, { append: true }); + let dataRead = Deno.readFileSync(filename); + const dec = new TextDecoder("utf-8"); + let actual = dec.decode(dataRead); + assertEquals("HelloHello", actual); + // Now attempt overwrite + Deno.writeFileSync(filename, data, { append: false }); + dataRead = Deno.readFileSync(filename); + actual = dec.decode(dataRead); + assertEquals("Hello", actual); + // append not set should also overwrite + Deno.writeFileSync(filename, data); + dataRead = Deno.readFileSync(filename); + actual = dec.decode(dataRead); + assertEquals("Hello", actual); + } +); -testPerm( - { read: true, write: true }, +unitTest( + { perms: { read: true, write: true } }, async function writeFileSuccess(): Promise<void> { const enc = new TextEncoder(); const data = enc.encode("Hello"); @@ -113,8 +125,8 @@ testPerm( } ); -testPerm( - { read: true, write: true }, +unitTest( + { perms: { read: true, write: true } }, async function writeFileNotFound(): Promise<void> { const enc = new TextEncoder(); const data = enc.encode("Hello"); @@ -131,25 +143,26 @@ testPerm( } ); -testPerm({ read: true, write: false }, async function writeFilePerm(): Promise< - void -> { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - const filename = "/baddir/test.txt"; - // The following should fail due to no write permission - let caughtError = false; - try { - await Deno.writeFile(filename, data); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.PermissionDenied); +unitTest( + { perms: { read: true, write: false } }, + async function writeFilePerm(): Promise<void> { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = "/baddir/test.txt"; + // The following should fail due to no write permission + let caughtError = false; + try { + await Deno.writeFile(filename, data); + } catch (e) { + caughtError = true; + assert(e instanceof Deno.errors.PermissionDenied); + } + assert(caughtError); } - assert(caughtError); -}); +); -testPerm( - { read: true, write: true }, +unitTest( + { perms: { read: true, write: true } }, async function writeFileUpdatePerm(): Promise<void> { if (Deno.build.os !== "win") { const enc = new TextEncoder(); @@ -163,51 +176,53 @@ testPerm( } ); -testPerm({ read: true, write: true }, async function writeFileCreate(): Promise< - void -> { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - const filename = Deno.makeTempDirSync() + "/test.txt"; - let caughtError = false; - // if create turned off, the file won't be created - try { +unitTest( + { perms: { read: true, write: true } }, + async function writeFileCreate(): Promise<void> { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + let caughtError = false; + // if create turned off, the file won't be created + try { + await Deno.writeFile(filename, data, { create: false }); + } catch (e) { + caughtError = true; + assert(e instanceof Deno.errors.NotFound); + } + assert(caughtError); + + // Turn on create, should have no error + await Deno.writeFile(filename, data, { create: true }); await Deno.writeFile(filename, data, { create: false }); - } catch (e) { - caughtError = true; - assert(e instanceof Deno.errors.NotFound); + const dataRead = Deno.readFileSync(filename); + const dec = new TextDecoder("utf-8"); + const actual = dec.decode(dataRead); + assertEquals("Hello", actual); } - assert(caughtError); - - // Turn on create, should have no error - await Deno.writeFile(filename, data, { create: true }); - await Deno.writeFile(filename, data, { create: false }); - const dataRead = Deno.readFileSync(filename); - const dec = new TextDecoder("utf-8"); - const actual = dec.decode(dataRead); - assertEquals("Hello", actual); -}); +); -testPerm({ read: true, write: true }, async function writeFileAppend(): Promise< - void -> { - const enc = new TextEncoder(); - const data = enc.encode("Hello"); - const filename = Deno.makeTempDirSync() + "/test.txt"; - await Deno.writeFile(filename, data); - await Deno.writeFile(filename, data, { append: true }); - let dataRead = Deno.readFileSync(filename); - const dec = new TextDecoder("utf-8"); - let actual = dec.decode(dataRead); - assertEquals("HelloHello", actual); - // Now attempt overwrite - await Deno.writeFile(filename, data, { append: false }); - dataRead = Deno.readFileSync(filename); - actual = dec.decode(dataRead); - assertEquals("Hello", actual); - // append not set should also overwrite - await Deno.writeFile(filename, data); - dataRead = Deno.readFileSync(filename); - actual = dec.decode(dataRead); - assertEquals("Hello", actual); -}); +unitTest( + { perms: { read: true, write: true } }, + async function writeFileAppend(): Promise<void> { + const enc = new TextEncoder(); + const data = enc.encode("Hello"); + const filename = Deno.makeTempDirSync() + "/test.txt"; + await Deno.writeFile(filename, data); + await Deno.writeFile(filename, data, { append: true }); + let dataRead = Deno.readFileSync(filename); + const dec = new TextDecoder("utf-8"); + let actual = dec.decode(dataRead); + assertEquals("HelloHello", actual); + // Now attempt overwrite + await Deno.writeFile(filename, data, { append: false }); + dataRead = Deno.readFileSync(filename); + actual = dec.decode(dataRead); + assertEquals("Hello", actual); + // append not set should also overwrite + await Deno.writeFile(filename, data); + dataRead = Deno.readFileSync(filename); + actual = dec.decode(dataRead); + assertEquals("Hello", actual); + } +); |