summaryrefslogtreecommitdiff
path: root/cli/js/40_test_common.js
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-01-25 14:54:35 -0500
committerGitHub <noreply@github.com>2024-01-25 14:54:35 -0500
commit7038074c8583465872b16083f54f2211312f0943 (patch)
tree71cfc7164d40b493cdd4800f130681bdf363ac45 /cli/js/40_test_common.js
parente06be89143e0cfc85896ee0b89f2e4cce8ba8d44 (diff)
chore(cli): split 40_testing (#22112)
No code changes -- just splitting 40_testing into three files and removing a couple of unused lines of code.
Diffstat (limited to 'cli/js/40_test_common.js')
-rw-r--r--cli/js/40_test_common.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/cli/js/40_test_common.js b/cli/js/40_test_common.js
new file mode 100644
index 000000000..7711148f1
--- /dev/null
+++ b/cli/js/40_test_common.js
@@ -0,0 +1,60 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+import { core, primordials } from "ext:core/mod.js";
+import { serializePermissions } from "ext:runtime/10_permissions.js";
+const ops = core.ops;
+const {
+ StringPrototypeReplaceAll,
+ SafeArrayIterator,
+} = primordials;
+
+const ESCAPE_ASCII_CHARS = [
+ ["\b", "\\b"],
+ ["\f", "\\f"],
+ ["\t", "\\t"],
+ ["\n", "\\n"],
+ ["\r", "\\r"],
+ ["\v", "\\v"],
+];
+
+/**
+ * @param {string} name
+ * @returns {string}
+ */
+export function escapeName(name) {
+ // Check if we need to escape a character
+ for (let i = 0; i < name.length; i++) {
+ const ch = name.charCodeAt(i);
+ if (ch <= 13 && ch >= 8) {
+ // Slow path: We do need to escape it
+ for (const [escape, replaceWith] of ESCAPE_ASCII_CHARS) {
+ name = StringPrototypeReplaceAll(name, escape, replaceWith);
+ }
+ return name;
+ }
+ }
+
+ // We didn't need to escape anything, return original string
+ return name;
+}
+
+export function pledgePermissions(permissions) {
+ return ops.op_pledge_test_permissions(
+ serializePermissions(permissions),
+ );
+}
+
+export function restorePermissions(token) {
+ ops.op_restore_test_permissions(token);
+}
+
+export function withPermissions(fn, permissions) {
+ return async function applyPermissions(...params) {
+ const token = pledgePermissions(permissions);
+
+ try {
+ return await fn(...new SafeArrayIterator(params));
+ } finally {
+ restorePermissions(token);
+ }
+ };
+}