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

Side by Side Diff: test/_common.js

Issue 29354864: Issue 4223 - Migrate some more of adblockplustests (Closed)
Patch Set: Addressed final nit Created Oct. 4, 2016, 12:16 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « package.json ('k') | test/cssRules.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 let fs = require("fs");
21 let path = require("path");
22 let SandboxedModule = require("sandboxed-module");
23
24 let globals = {
25 atob: data => new Buffer(data, "base64").toString("binary"),
26 btoa: data => new Buffer(data, "binary").toString("base64"),
27 Ci: {
28 },
29 Cu: {
30 import: () =>
31 {
32 }
33 },
34 navigator: {
35 },
36 onShutdown: {
37 add: () =>
38 {
39 }
40 },
41 Services: {
42 obs: {
43 addObserver: () =>
44 {
45 }
46 }
47 },
48 XPCOMUtils: {
49 generateQI: () =>
50 {
51 }
52 }
53 };
54
55 let knownModules = new Map();
56 for (let dir of [path.join(__dirname, "stub-modules"),
57 path.join(__dirname, "..", "lib")])
58 for (let file of fs.readdirSync(path.resolve(dir)))
59 if (path.extname(file) == ".js")
60 knownModules[path.basename(file, ".js")] = path.resolve(dir, file);
61
62 function addExports(exports)
63 {
64 return function(source)
65 {
66 let extraExports = exports[path.basename(this.filename, ".js")];
67 if (extraExports)
68 for (let name of extraExports)
69 source += `
70 Object.defineProperty(exports, "${name}", {get: () => ${name}});`;
71 return source;
72 };
73 }
74
75 function rewriteRequires(source)
76 {
77 function escapeString(str)
78 {
79 return str.replace(/(["'\\])/g, "\\$1");
80 }
81
82 return source.replace(/(\brequire\(["'])([^"']+)/g, (match, prefix, request) = >
83 {
84 if (request in knownModules)
85 return prefix + escapeString(knownModules[request]);
86 return match;
87 });
88 }
89
90 exports.createSandbox = function(extraExports)
91 {
92 let sourceTransformers = [rewriteRequires];
93 if (extraExports)
94 sourceTransformers.push(addExports(extraExports));
95
96 // This module loads itself into a sandbox, keeping track of the require
97 // function which can be used to load further modules into the sandbox.
98 return SandboxedModule.require("./_common", {
99 globals: globals,
100 sourceTransformers: sourceTransformers
101 }).require;
102 };
103
104 exports.require = require;
OLDNEW
« no previous file with comments | « package.json ('k') | test/cssRules.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld