| Index: test/common.js |
| diff --git a/test/common.js b/test/common.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..60765627fad4d9410902ecca883fdce5475c309c |
| --- /dev/null |
| +++ b/test/common.js |
| @@ -0,0 +1,97 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-2016 Eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +"use strict"; |
| + |
| +let path = require("path"); |
| +let SandboxedModule = require("sandboxed-module"); |
| + |
| +let globals = { |
| + atob: data => new Buffer(data, "base64").toString("binary"), |
| + btoa: data => new Buffer(data, "binary").toString("base64"), |
| + Ci: { |
| + }, |
| + Cu: { |
| + import: () => |
| + { |
| + } |
| + }, |
| + navigator: { |
| + }, |
| + onShutdown: { |
| + add: () => |
| + { |
| + } |
| + }, |
| + Services: { |
| + obs: { |
| + addObserver: () => |
| + { |
| + } |
| + } |
| + }, |
| + XPCOMUtils: { |
| + generateQI: () => |
| + { |
| + } |
| + } |
| +}; |
| + |
| +function libPath(moduleId) |
| +{ |
| + return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js"); |
| +} |
| + |
| +function addExports(exports) |
| +{ |
| + return function(source) |
| + { |
| + let extraExports = exports[path.basename(this.filename, ".js")]; |
| + if (extraExports) |
| + for (let name of extraExports) |
| + source += ` |
| + Object.defineProperty(exports, "${name}", {get: () => ${name}});`; |
| + return source; |
| + }; |
| +} |
| + |
| +function rewriteRequires(source) |
| +{ |
| + let modules = new Set(["events", "io"]); |
| + return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) => |
| + { |
| + if (modules.has(request)) |
| + return prefix + request + ".js"; |
| + return match; |
| + }); |
| +} |
| + |
| +exports.createSandbox = function(extraExports) |
| +{ |
| + let sourceTransformers = [rewriteRequires]; |
| + if (extraExports) |
| + sourceTransformers.push(addExports(extraExports)); |
| + |
| + // This module loads itself in a sandbox, then we return its require function |
| + // which our tests can then use to require further modules in the sandbox. |
| + return SandboxedModule.require("./common", { |
| + globals: globals, |
| + sourceTransformers: sourceTransformers |
| + }).require; |
| +}; |
| + |
| +exports.require = require; |