|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { readFileSync, promises: fs } = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const vm = require("vm"); |
| 6 | + |
| 7 | +process.on("unhandledRejection", e => { |
| 8 | + throw e; |
| 9 | +}); |
| 10 | + |
| 11 | +function createSandbox(setupScripts) { |
| 12 | + const sandbox = vm.createContext(); |
| 13 | + sandbox.self = vm.runInContext("this", sandbox); |
| 14 | + for (const script of setupScripts) { |
| 15 | + script.runInContext(sandbox); |
| 16 | + } |
| 17 | + sandbox.require = p => { |
| 18 | + if (p[0] !== ".") { |
| 19 | + // eslint-disable-next-line global-require |
| 20 | + return require(p); |
| 21 | + } |
| 22 | + |
| 23 | + const resolved = path.resolve(__dirname, "../output", p); |
| 24 | + const src = readFileSync(resolved, { encoding: "utf8" }); |
| 25 | + return sandbox.self.eval( |
| 26 | + `(() => { |
| 27 | + let exports = {}; |
| 28 | + const module = { exports }; |
| 29 | + (() => {${src}})(); |
| 30 | + return module.exports; |
| 31 | + })();` |
| 32 | + ); |
| 33 | + }; |
| 34 | + return sandbox; |
| 35 | +} |
| 36 | + |
| 37 | +async function testInterface(name, setupScripts) { |
| 38 | + const idlToTest = await fs.readFile( |
| 39 | + path.join(__dirname, `../cases/${name}.webidl`), |
| 40 | + { |
| 41 | + encoding: "utf8" |
| 42 | + } |
| 43 | + ); |
| 44 | + |
| 45 | + const sandbox = createSandbox(setupScripts); |
| 46 | + vm.runInContext( |
| 47 | + ` |
| 48 | + Object.defineProperty(self, '${name}', { |
| 49 | + value: require("./${name}.js").interface, |
| 50 | + enumerable: false, |
| 51 | + writable:true, |
| 52 | + configurable:true |
| 53 | + })`, |
| 54 | + sandbox |
| 55 | + ); |
| 56 | + |
| 57 | + const prom = new Promise((resolve, reject) => { |
| 58 | + const errors = []; |
| 59 | + |
| 60 | + sandbox.add_result_callback(test => { |
| 61 | + if (test.status === 1) { |
| 62 | + errors.push( |
| 63 | + `Failed in "${test.name}": \n${test.message}\n\n${test.stack}` |
| 64 | + ); |
| 65 | + } else if (test.status === 2) { |
| 66 | + errors.push( |
| 67 | + `Timeout in "${test.name}": \n${test.message}\n\n${test.stack}` |
| 68 | + ); |
| 69 | + } else if (test.status === 3) { |
| 70 | + errors.push( |
| 71 | + `Uncompleted test "${test.name}": \n${test.message}\n\n${test.stack}` |
| 72 | + ); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + sandbox.add_completion_callback((tests, harnessStatus) => { |
| 77 | + if (harnessStatus.status === 2) { |
| 78 | + errors.push(new Error(`test harness should not timeout`)); |
| 79 | + } |
| 80 | + |
| 81 | + if (errors.length === 1) { |
| 82 | + reject(new Error(errors[0])); |
| 83 | + } else if (errors.length) { |
| 84 | + reject( |
| 85 | + new Error(`${errors.length} errors in test:\n\n${errors.join("\n")}`) |
| 86 | + ); |
| 87 | + } else { |
| 88 | + resolve(); |
| 89 | + } |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + const idlArray = new sandbox.IdlArray(); |
| 94 | + idlArray.add_idls(idlToTest); |
| 95 | + |
| 96 | + idlArray.test(); |
| 97 | + await prom; |
| 98 | +} |
| 99 | + |
| 100 | +describe("IDL Harness", () => { |
| 101 | + let setupScripts; |
| 102 | + |
| 103 | + beforeAll(async () => { |
| 104 | + const files = await Promise.all( |
| 105 | + ["testharness.js", "webidl2.js", "idlharness.js"].map(async p => ({ |
| 106 | + path: path.join(__dirname, "/vendor/", p), |
| 107 | + contents: await fs.readFile(path.join(__dirname, "/vendor/", p), { |
| 108 | + encoding: "utf8" |
| 109 | + }) |
| 110 | + })) |
| 111 | + ); |
| 112 | + setupScripts = files.map( |
| 113 | + file => new vm.Script(file.contents, { filename: file.path }) |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + test("URL.js", async () => { |
| 118 | + await testInterface("URL", setupScripts); |
| 119 | + }); |
| 120 | +}); |
0 commit comments