0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-30 09:08:00 -04:00

refactor(runtime): apply permissions as a hook during registration (#11347)

This commit is contained in:
Casper Beyer 2021-07-14 06:11:02 +08:00 committed by GitHub
parent 9dc3390720
commit 9cb48bd8fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -105,6 +105,29 @@ finishing test case.`;
}; };
} }
function withPermissions(fn, permissions) {
function pledgePermissions(permissions) {
return core.opSync(
"op_pledge_test_permissions",
parsePermissions(permissions),
);
}
function restorePermissions(token) {
core.opSync("op_restore_test_permissions", token);
}
return async function applyPermissions() {
const token = pledgePermissions(permissions);
try {
await fn();
} finally {
restorePermissions(token);
}
};
}
const tests = []; const tests = [];
// Main test function provided by Deno, as you can see it merely // Main test function provided by Deno, as you can see it merely
@ -153,6 +176,13 @@ finishing test case.`;
testDef.fn = assertExit(testDef.fn); testDef.fn = assertExit(testDef.fn);
} }
if (testDef.permissions) {
testDef.fn = withPermissions(
testDef.fn,
parsePermissions(testDef.permissions),
);
}
ArrayPrototypePush(tests, testDef); ArrayPrototypePush(tests, testDef);
} }
@ -180,37 +210,16 @@ finishing test case.`;
}; };
} }
function pledgeTestPermissions(permissions) { async function runTest({ ignore, fn }) {
return core.opSync(
"op_pledge_test_permissions",
parsePermissions(permissions),
);
}
function restoreTestPermissions(token) {
core.opSync("op_restore_test_permissions", token);
}
async function runTest({ ignore, fn, permissions }) {
if (ignore) { if (ignore) {
return "ignored"; return "ignored";
} }
let token = null;
try { try {
if (permissions) {
token = pledgeTestPermissions(permissions);
}
await fn(); await fn();
return "ok"; return "ok";
} catch (error) { } catch (error) {
return { "failed": inspectArgs([error]) }; return { "failed": inspectArgs([error]) };
} finally {
if (token) {
restoreTestPermissions(token);
}
} }
} }