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: Get filterListener tests passing reliably Created Sept. 29, 2016, 3:57 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..8871003a84ed2ff79137f920b710a2a62d010b41
--- /dev/null
+++ b/test/stub-modules/common.js
@@ -0,0 +1,55 @@
+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 bodgeExports(exports)
+{
+ return src => src + "\n" + exports.
+ map(name => "Object.defineProperty(exports, '" + name + "'," +
+ "{get: () => " + name + "});").join("\n");
+}
+
+function libPath(moduleId)
+{
+ return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js");
+}
+
+exports.createSandbox = function(extraExports)
+{
+ // Shared require cache for this sandbox
+ let cache = {};
+
+ // For any modules which we need extra exports we must transform them now.
+ for (let moduleId in extraExports)
+ cache[libPath(moduleId)] = SandboxedModule.require(moduleId, {
+ cache: cache,
+ globals: globals,
+ sourceTransformers: [bodgeExports(extraExports[moduleId])],
+ sourceTransformersSingleOnly: true,
+ singleOnly: false
+ });
+
+ return (moduleId, exports) =>
+ {
+ let key = libPath(moduleId);
+
+ // Only load modules which aren't already cached for this sandbox
+ if (!(key in cache))
+ cache[key] = SandboxedModule.require(moduleId, {
+ cache: cache,
+ globals: globals
+ });
+
+ return cache[key];
+ };
+};
« 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