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: Migrate filterStorage tests Created Sept. 29, 2016, 7:24 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
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..b1fc2b8b9b28c7306fad73e17f15ee4cd31a6ff7
--- /dev/null
+++ b/test/stub-modules/common.js
@@ -0,0 +1,68 @@
+let path = require("path");
Wladimir Palant 2016/09/30 09:37:54 I realized that this module doesn't belong into st
kzar 2016/10/03 13:46:16 You're right but nodeunit considers all JS files i
kzar 2016/10/03 16:43:14 Well I've had a go at this, I moved common.js and
+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: () => { }},
Wladimir Palant 2016/09/30 09:37:54 I don't think that eslint will accept this. Accord
kzar 2016/10/03 13:46:17 Done.
+ navigator: {},
+ onShutdown: {add: () => { }},
+ Services: {obs: {addObserver: () => { }}},
+ XPCOMUtils: {generateQI: () => { }}
+};
+
+function addExports(exports)
+{
+ // Unfortunately there's not a way for a source transformer to know the name
+ // of the module being transformed. This means we must add all extra exports
Wladimir Palant 2016/09/30 09:37:54 Well, source transformers are being bound to the S
kzar 2016/10/03 13:46:15 Oh cool, Done.
+ // to all modules, not ideal!
+ let code = "if (!('EXPORT' in exports))\n" +
+ " Object.defineProperty(exports, 'EXPORT', {get: () => EXPORT});";
Wladimir Palant 2016/09/30 09:37:55 Template literals allow multi-line input and are e
kzar 2016/10/03 13:46:16 Done.
+
+ return src => src + "\n" + exports.
+ map(name => code.replace(/EXPORT/g, name)).join("\n");
+}
+
+function rewriteRequires(source)
+{
+ // Taken from EasyPasswords
+ // https://github.com/palant/easypasswords/blob/master/gulp-utils.js
Wladimir Palant 2016/09/30 09:37:55 I'm donating this code to the Adblock Plus project
kzar 2016/10/03 13:46:15 Acknowledged.
+ let modules = new Set(["events", "io"]);
Wladimir Palant 2016/09/30 09:37:55 Why only these two modules? I'd suggest that you g
kzar 2016/10/03 13:46:17 Still thinking about this one. (There's also the p
kzar 2016/10/03 17:47:43 Done.
+ return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) =>
+ {
+ if (modules.has(request))
+ return prefix + request + ".js";
+ return match;
+ });
+}
+
+function libPath(moduleId)
+{
+ return path.resolve(__dirname, "..", "..", "lib", moduleId + ".js");
+}
+
+exports.createSandbox = function(extraExports)
+{
+ // Shared require cache for this sandbox
+ let cache = {};
+
+ let options = {
+ cache: cache,
+ globals: globals,
+ sourceTransformers: [rewriteRequires]
+ };
+ if (extraExports)
+ options.sourceTransformers.push(addExports(extraExports));
+
+ return (moduleId, exports) =>
Wladimir Palant 2016/09/30 09:37:55 exports parameter is unused.
kzar 2016/10/03 13:46:16 Done.
+ {
+ let key = libPath(moduleId);
+
+ // Only load modules which aren't already cached for this sandbox
+ if (!(key in cache))
+ cache[key] = SandboxedModule.require(moduleId, options);
+
+ return cache[key];
Wladimir Palant 2016/09/30 09:37:54 Ok, if you need to cache modules manually then thi
kzar 2016/10/03 13:46:16 Ah really cool idea. We don't even need that pull
+ };
+};
« test/filterListener.js ('K') | « test/signatures.js ('k') | test/stub-modules/io.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld