Tests are now faster (#2165)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
diff --git a/tests/checks/extend.js b/tests/checks/extend.js
deleted file mode 100644
index b699dd3..0000000
--- a/tests/checks/extend.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const { testFunction } = require('./../helper/check-functionality');
-
-function extendTest(id, redef) {
- const details = `\nextend("${id}", ${redef})`;
-
- // type checks
- if (typeof id !== 'string') {
- throw new TypeError(`The id argument has to be a 'string'.` + details);
- }
- if (typeof redef !== 'object') {
- throw new TypeError(`The redef argument has to be an 'object'.` + details);
- }
-
-
- if (!(id in Prism.languages)) {
- throw new Error(`Cannot extend '${id}' because it is not defined in Prism.languages.`);
- }
-}
-
-testFunction('extend', Prism.languages, extendTest);
diff --git a/tests/checks/insert-before.js b/tests/checks/insert-before.js
deleted file mode 100644
index 8536345..0000000
--- a/tests/checks/insert-before.js
+++ /dev/null
@@ -1,32 +0,0 @@
-const { testFunction } = require('./../helper/check-functionality');
-
-function insertBeforeTest(inside, before, insert, root) {
- const details = `\ninsertBefore("${inside}", "${before}", ${insert}, ${root})`;
-
- // type checks
- if (typeof inside !== 'string') {
- throw new TypeError(`The inside argument has to be a 'string'.` + details);
- }
- if (typeof before !== 'string') {
- throw new TypeError(`The before argument has to be a 'string'.` + details);
- }
- if (typeof insert !== 'object') {
- throw new TypeError(`The insert argument has to be an 'object'.` + details);
- }
- if (root && typeof root !== 'object') {
- throw new TypeError(`The root argument has to be an 'object' if defined.` + details);
- }
-
-
- root = root || Prism.languages;
- var grammar = root[inside];
-
- if (typeof grammar !== 'object') {
- throw new Error(`The grammar "${inside}" has to be an 'object' not '${typeof grammar}'.`);
- }
- if (!(before in grammar)) {
- throw new Error(`"${before}" has to be a key of the grammar "${inside}".`);
- }
-}
-
-testFunction('insertBefore', Prism.languages, insertBeforeTest);
diff --git a/tests/helper/check-functionality.js b/tests/helper/check-functionality.js
deleted file mode 100644
index 0192d0c..0000000
--- a/tests/helper/check-functionality.js
+++ /dev/null
@@ -1,13 +0,0 @@
-"use strict";
-
-module.exports = {
- testFunction(name, object, tester) {
- const func = object[name];
-
- object[name] = function () {
- tester.apply(this, arguments);
- return func.apply(this, arguments);
- };
- }
-
-}
diff --git a/tests/helper/checks.js b/tests/helper/checks.js
new file mode 100644
index 0000000..f926626
--- /dev/null
+++ b/tests/helper/checks.js
@@ -0,0 +1,63 @@
+"use strict";
+
+function testFunction(name, object, tester) {
+ const func = object[name];
+
+ object[name] = function () {
+ tester.apply(this, arguments);
+ return func.apply(this, arguments);
+ };
+}
+
+module.exports = (Prism) => {
+
+ function extendTest(id, redef) {
+ const details = `\nextend("${id}", ${redef})`;
+
+ // type checks
+ if (typeof id !== 'string') {
+ throw new TypeError(`The id argument has to be a 'string'.` + details);
+ }
+ if (typeof redef !== 'object') {
+ throw new TypeError(`The redef argument has to be an 'object'.` + details);
+ }
+
+
+ if (!(id in Prism.languages)) {
+ throw new Error(`Cannot extend '${id}' because it is not defined in Prism.languages.`);
+ }
+ }
+
+ function insertBeforeTest(inside, before, insert, root) {
+ const details = `\ninsertBefore("${inside}", "${before}", ${insert}, ${root})`;
+
+ // type checks
+ if (typeof inside !== 'string') {
+ throw new TypeError(`The inside argument has to be a 'string'.` + details);
+ }
+ if (typeof before !== 'string') {
+ throw new TypeError(`The before argument has to be a 'string'.` + details);
+ }
+ if (typeof insert !== 'object') {
+ throw new TypeError(`The insert argument has to be an 'object'.` + details);
+ }
+ if (root && typeof root !== 'object') {
+ throw new TypeError(`The root argument has to be an 'object' if defined.` + details);
+ }
+
+
+ root = root || Prism.languages;
+ var grammar = root[inside];
+
+ if (typeof grammar !== 'object') {
+ throw new Error(`The grammar "${inside}" has to be an 'object' not '${typeof grammar}'.`);
+ }
+ if (!(before in grammar)) {
+ throw new Error(`"${before}" has to be a key of the grammar "${inside}".`);
+ }
+ }
+
+ testFunction('extend', Prism.languages, extendTest);
+ testFunction('insertBefore', Prism.languages, insertBeforeTest);
+
+};
diff --git a/tests/helper/prism-loader.js b/tests/helper/prism-loader.js
index 562e02d..2d70c98 100644
--- a/tests/helper/prism-loader.js
+++ b/tests/helper/prism-loader.js
@@ -1,11 +1,11 @@
"use strict";
const fs = require("fs");
-const vm = require("vm");
const { getAllFiles } = require("./test-discovery");
const components = require("../../components.json");
const getLoader = require("../../dependencies");
const languagesCatalog = components.languages;
+const coreChecks = require('./checks');
/**
@@ -14,6 +14,13 @@ const languagesCatalog = components.languages;
* @property {Set<string>} loaded A set of loaded components.
*/
+/** @type {Map<string, string>} */
+const fileSourceCache = new Map();
+/** @type {() => any} */
+let coreSupplierFunction = null;
+/** @type {Map<string, (Prism: any) => void>} */
+const languageCache = new Map();
+
module.exports = {
/**
@@ -51,9 +58,15 @@ module.exports = {
throw new Error(`Language '${id}' not found.`);
}
- // load the language itself
- const languageSource = this.loadComponentSource(id);
- context.Prism = this.runFileWithContext(languageSource, { Prism: context.Prism }).Prism;
+ // get the function which adds the language from cache
+ let languageFunction = languageCache.get(id);
+ if (languageFunction === undefined) {
+ // make a function from the code which take "Prism" as an argument, so the language grammar
+ // references the function argument
+ const func = new Function('Prism', this.loadComponentSource(id));
+ languageCache.set(id, languageFunction = (Prism) => func(Prism));
+ }
+ languageFunction(context.Prism);
context.loaded.add(id);
});
@@ -69,45 +82,26 @@ module.exports = {
* @returns {Prism}
*/
createEmptyPrism() {
- const coreSource = this.loadComponentSource("core");
- const context = this.runFileWithContext(coreSource);
-
- for (const testSource of this.getChecks().map(src => this.loadFileSource(src))) {
- context.Prism = this.runFileWithContext(testSource, {
- Prism: context.Prism,
- /**
- * A pseudo require function for the checks.
- *
- * This function will behave like the regular `require` in real modules when called form a check file.
- *
- * @param {string} id The id of relative path to require.
- */
- require(id) {
- if (id.startsWith('./')) {
- // We have to rewrite relative paths starting with './'
- return require('./../checks/' + id.substr(2));
- } else {
- // This might be an id like 'mocha' or 'fs' or a relative path starting with '../'.
- // In both cases we don't have to change anything.
- return require(id);
- }
- }
- }).Prism;
+ if (!coreSupplierFunction) {
+ const source = this.loadComponentSource("core");
+ // Core exports itself in 2 ways:
+ // 1) it uses `module.exports = Prism` which what we'll use
+ // 2) it uses `global.Prism = Prism` which we want to sabotage to prevent leaking
+ const func = new Function('module', 'global', source);
+ coreSupplierFunction = () => {
+ const module = {
+ // that's all we care about
+ exports: {}
+ };
+ func(module, {});
+ return module.exports;
+ };
}
-
- return context.Prism;
+ const Prism = coreSupplierFunction();
+ coreChecks(Prism);
+ return Prism;
},
-
- /**
- * Cached file sources, to prevent massive HDD work
- *
- * @private
- * @type {Object.<string, string>}
- */
- fileSourceCache: {},
-
-
/**
* Loads the given component's file source as string
*
@@ -127,36 +121,10 @@ module.exports = {
* @returns {string}
*/
loadFileSource(src) {
- return this.fileSourceCache[src] = this.fileSourceCache[src] || fs.readFileSync(src, "utf8");
- },
-
-
- checkCache: null,
-
- /**
- * Returns a list of files which add additional checks to Prism functions.
- *
- * @returns {ReadonlyArray<string>}
- */
- getChecks() {
- return this.checkCache = this.checkCache || getAllFiles(__dirname + "/../checks");
- },
-
-
- /**
- * Runs a VM for a given file source with the given context
- *
- * @private
- * @param {string} fileSource
- * @param {*} [context={}]
- *
- * @returns {*}
- */
- runFileWithContext(fileSource, context = {}) {
- // we don't have to pass our console but it's the only way these scripts can talk
- // not supplying console here means that all references to `console` inside them will refer to a no-op console
- context.console = console;
- vm.runInNewContext(fileSource, context);
- return context;
+ let content = fileSourceCache.get(src);
+ if (content === undefined) {
+ fileSourceCache.set(src, content = fs.readFileSync(src, "utf8"));
+ }
+ return content;
}
};