Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/stub-modules/common.js

Issue 29354864: Issue 4223 - Migrate some more of adblockplustests (Closed)
Patch Set: Addressed feedback Created Oct. 3, 2016, 1:44 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/signatures.js ('k') | test/stub-modules/io.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/stub-modules/common.js
diff --git a/test/stub-modules/common.js b/test/stub-modules/common.js
new file mode 100644
index 0000000000000000000000000000000000000000..27c4639e83ecb1223b01f15fd889b2825234d0f2
--- /dev/null
+++ b/test/stub-modules/common.js
@@ -0,0 +1,78 @@
+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;
« no previous file with comments | « test/signatures.js ('k') | test/stub-modules/io.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld