diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-03-04 17:31:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-04 17:31:14 +0100 |
commit | 8d96dffa410a149d0fff6115bd97f41fc1fe7459 (patch) | |
tree | b00dc7a78e5030b68741de8bf9dde83b9fa07364 /cli/js/chown_test.ts | |
parent | 30682cf74fa039d3493c74101dca2dbb3a8d49b6 (diff) |
refactor: rewrite testPerm into unitTest (#4231)
Rewrite "testPerm" helper function used for testing of internal
runtime code. It's been renamed to "unitTest" and provides API that
is extensible in the future by accepting optional "UnitTestOptions"
argument. "test" helper was also removed and replaced by
overloaded version of "unitTest" that takes only function argument.
"UnitTestOptions" currently supports "perms" and "skip"
options, where former works exactly as first argument to "testPerm"
did, while the latter allows to conditionally skip tests.
Diffstat (limited to 'cli/js/chown_test.ts')
-rw-r--r-- | cli/js/chown_test.ts | 107 |
1 files changed, 56 insertions, 51 deletions
diff --git a/cli/js/chown_test.ts b/cli/js/chown_test.ts index af3c04d9f..50dcd6dc1 100644 --- a/cli/js/chown_test.ts +++ b/cli/js/chown_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { testPerm, assertEquals, assert } from "./test_util.ts"; +import { unitTest, assertEquals, assert } from "./test_util.ts"; // chown on Windows is noop for now, so ignore its testing on Windows if (Deno.build.os !== "win") { @@ -28,7 +28,7 @@ if (Deno.build.os !== "win") { return { uid, gid }; } - testPerm({}, async function chownNoWritePermission(): Promise<void> { + unitTest(async function chownNoWritePermission(): Promise<void> { const filePath = "chown_test_file.txt"; try { await Deno.chown(filePath, 1000, 1000); @@ -37,8 +37,8 @@ if (Deno.build.os !== "win") { } }); - testPerm( - { run: true, write: true }, + unitTest( + { perms: { run: true, write: true } }, async function chownSyncFileNotExist(): Promise<void> { const { uid, gid } = await getUidAndGid(); const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt"; @@ -51,8 +51,8 @@ if (Deno.build.os !== "win") { } ); - testPerm( - { run: true, write: true }, + unitTest( + { perms: { run: true, write: true } }, async function chownFileNotExist(): Promise<void> { const { uid, gid } = await getUidAndGid(); const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt"; @@ -65,42 +65,46 @@ if (Deno.build.os !== "win") { } ); - testPerm({ write: true }, function chownSyncPermissionDenied(): void { - const enc = new TextEncoder(); - const dirPath = Deno.makeTempDirSync(); - const filePath = dirPath + "/chown_test_file.txt"; - const fileData = enc.encode("Hello"); - Deno.writeFileSync(filePath, fileData); + unitTest( + { perms: { write: true } }, + function chownSyncPermissionDenied(): void { + const enc = new TextEncoder(); + const dirPath = Deno.makeTempDirSync(); + const filePath = dirPath + "/chown_test_file.txt"; + const fileData = enc.encode("Hello"); + Deno.writeFileSync(filePath, fileData); - try { - // try changing the file's owner to root - Deno.chownSync(filePath, 0, 0); - } catch (e) { - assert(e instanceof Deno.errors.PermissionDenied); + try { + // try changing the file's owner to root + Deno.chownSync(filePath, 0, 0); + } catch (e) { + assert(e instanceof Deno.errors.PermissionDenied); + } + Deno.removeSync(dirPath, { recursive: true }); } - Deno.removeSync(dirPath, { recursive: true }); - }); + ); - testPerm({ write: true }, async function chownPermissionDenied(): Promise< - void - > { - const enc = new TextEncoder(); - const dirPath = await Deno.makeTempDir(); - const filePath = dirPath + "/chown_test_file.txt"; - const fileData = enc.encode("Hello"); - await Deno.writeFile(filePath, fileData); + unitTest( + { perms: { write: true } }, + async function chownPermissionDenied(): Promise<void> { + const enc = new TextEncoder(); + const dirPath = await Deno.makeTempDir(); + const filePath = dirPath + "/chown_test_file.txt"; + const fileData = enc.encode("Hello"); + await Deno.writeFile(filePath, fileData); - try { - // try changing the file's owner to root - await Deno.chown(filePath, 0, 0); - } catch (e) { - assert(e instanceof Deno.errors.PermissionDenied); + try { + // try changing the file's owner to root + await Deno.chown(filePath, 0, 0); + } catch (e) { + assert(e instanceof Deno.errors.PermissionDenied); + } + await Deno.remove(dirPath, { recursive: true }); } - await Deno.remove(dirPath, { recursive: true }); - }); + ); - testPerm( - { run: true, write: true }, + unitTest( + { perms: { run: true, write: true } }, async function chownSyncSucceed(): Promise<void> { // TODO: when a file's owner is actually being changed, // chown only succeeds if run under priviledged user (root) @@ -121,22 +125,23 @@ if (Deno.build.os !== "win") { } ); - testPerm({ run: true, write: true }, async function chownSucceed(): Promise< - void - > { - // TODO: same as chownSyncSucceed - const { uid, gid } = await getUidAndGid(); + unitTest( + { perms: { run: true, write: true } }, + async function chownSucceed(): Promise<void> { + // TODO: same as chownSyncSucceed + const { uid, gid } = await getUidAndGid(); - const enc = new TextEncoder(); - const dirPath = await Deno.makeTempDir(); - const filePath = dirPath + "/chown_test_file.txt"; - const fileData = enc.encode("Hello"); - await Deno.writeFile(filePath, fileData); + const enc = new TextEncoder(); + const dirPath = await Deno.makeTempDir(); + const filePath = dirPath + "/chown_test_file.txt"; + const fileData = enc.encode("Hello"); + await Deno.writeFile(filePath, fileData); - // the test script creates this file with the same uid and gid, - // here chown is a noop so it succeeds under non-priviledged user - await Deno.chown(filePath, uid, gid); + // the test script creates this file with the same uid and gid, + // here chown is a noop so it succeeds under non-priviledged user + await Deno.chown(filePath, uid, gid); - Deno.removeSync(dirPath, { recursive: true }); - }); + Deno.removeSync(dirPath, { recursive: true }); + } + ); } |