From a13b0e27279e54205adb53cdaf2415c825e77714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 28 Feb 2020 15:47:54 +0100 Subject: [PATCH] rewrite permission revoke test as integration test (#4164) --- cli/js/permissions_test.ts | 31 +----------------------- cli/js/unit_tests.ts | 6 +---- cli/tests/057_revoke_permissions.out | 10 ++++++++ cli/tests/057_revoke_permissions.ts | 36 ++++++++++++++++++++++++++++ cli/tests/integration_tests.rs | 5 ++++ 5 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 cli/tests/057_revoke_permissions.out create mode 100644 cli/tests/057_revoke_permissions.ts diff --git a/cli/js/permissions_test.ts b/cli/js/permissions_test.ts index 6d79cfec0f..7a36ee901e 100644 --- a/cli/js/permissions_test.ts +++ b/cli/js/permissions_test.ts @@ -1,34 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { test, testPerm, assert, assertEquals } from "./test_util.ts"; - -const knownPermissions: Deno.PermissionName[] = [ - "run", - "read", - "write", - "net", - "env", - "plugin", - "hrtime" -]; - -function genFunc(grant: Deno.PermissionName): () => Promise { - const gen: () => Promise = async function Granted(): Promise { - const status0 = await Deno.permissions.query({ name: grant }); - assert(status0 != null); - assertEquals(status0.state, "granted"); - - const status1 = await Deno.permissions.revoke({ name: grant }); - assert(status1 != null); - assertEquals(status1.state, "prompt"); - }; - // Properly name these generated functions. - Object.defineProperty(gen, "name", { value: grant + "Granted" }); - return gen; -} - -for (const grant of knownPermissions) { - testPerm({ [grant]: true }, genFunc(grant)); -} +import { test, assert } from "./test_util.ts"; test(async function permissionInvalidName(): Promise { let thrown = false; diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts index 2495c938bf..f20e2f137c 100644 --- a/cli/js/unit_tests.ts +++ b/cli/js/unit_tests.ts @@ -37,6 +37,7 @@ import "./mixins/dom_iterable_test.ts"; import "./mkdir_test.ts"; import "./net_test.ts"; import "./os_test.ts"; +import "./permissions_test.ts"; import "./process_test.ts"; import "./realpath_test.ts"; import "./read_dir_test.ts"; @@ -63,11 +64,6 @@ import "./performance_test.ts"; import "./version_test.ts"; import "./workers_test.ts"; -// FIXME(bartlomieju): -// This test file revokes permissions, it must be run last, -// otherwise it might revoke permission for tests that need them. -import "./permissions_test.ts"; - if (import.meta.main) { await Deno.runTests(); } diff --git a/cli/tests/057_revoke_permissions.out b/cli/tests/057_revoke_permissions.out new file mode 100644 index 0000000000..1f12d3b93a --- /dev/null +++ b/cli/tests/057_revoke_permissions.out @@ -0,0 +1,10 @@ +running 7 tests +OK runGranted [WILDCARD] +OK readGranted [WILDCARD] +OK writeGranted [WILDCARD] +OK netGranted [WILDCARD] +OK envGranted [WILDCARD] +OK pluginGranted [WILDCARD] +OK hrtimeGranted [WILDCARD] + +test result: OK 7 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out [WILDCARD] \ No newline at end of file diff --git a/cli/tests/057_revoke_permissions.ts b/cli/tests/057_revoke_permissions.ts new file mode 100644 index 0000000000..4481dbfd91 --- /dev/null +++ b/cli/tests/057_revoke_permissions.ts @@ -0,0 +1,36 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +const knownPermissions: Deno.PermissionName[] = [ + "run", + "read", + "write", + "net", + "env", + "plugin", + "hrtime" +]; + +export function assert(cond: unknown): asserts cond { + if (!cond) { + throw Error("Assertion failed"); + } +} + +function genFunc(grant: Deno.PermissionName): () => Promise { + const gen: () => Promise = async function Granted(): Promise { + const status0 = await Deno.permissions.query({ name: grant }); + assert(status0 != null); + assert(status0.state === "granted"); + + const status1 = await Deno.permissions.revoke({ name: grant }); + assert(status1 != null); + assert(status1.state === "prompt"); + }; + // Properly name these generated functions. + Object.defineProperty(gen, "name", { value: grant + "Granted" }); + return gen; +} + +for (const grant of knownPermissions) { + Deno.test(genFunc(grant)); +} diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 843f56a7fc..434a55c089 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -936,6 +936,11 @@ itest!(_056_make_temp_file_write_perm { output: "056_make_temp_file_write_perm.out", }); +itest!(_057_revoke_permissions { + args: "test -A 057_revoke_permissions.ts", + output: "057_revoke_permissions.out", +}); + itest!(js_import_detect { args: "run --reload js_import_detect.ts", output: "js_import_detect.ts.out",