From 8de925c4c2add87888d058e78279f61264ab2292 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 9 Oct 2019 16:33:15 -0700 Subject: [PATCH] test: move module tests to main process (#20419) --- .gitignore | 2 +- script/spec-runner.js | 11 ++- spec-main/api-browser-window-spec.ts | 2 +- .../api/native-window-open-native-addon.html | 0 .../fixtures/module/declare-buffer.js | 0 .../fixtures/module/declare-global.js | 0 .../fixtures/module/declare-process.js | 0 .../fixtures/module/echo-renamed.js | 0 {spec => spec-main}/fixtures/module/echo.js | 0 .../fixtures/module/test.coffee | 0 .../fixtures/native-addon/echo/binding.cc | 0 .../fixtures/native-addon/echo/binding.gyp | 0 .../fixtures/native-addon/echo/lib/echo.js | 0 .../fixtures/native-addon/echo/package.json | 0 .../modules-spec.ts | 87 +++++++------------ spec-main/package.json | 6 +- spec-main/yarn.lock | 11 +++ spec/api-shell-spec.js | 3 - spec/fixtures/pages/native-module.html | 2 +- spec/package.json | 2 - spec/yarn.lock | 8 -- 21 files changed, 58 insertions(+), 76 deletions(-) rename {spec => spec-main}/fixtures/api/native-window-open-native-addon.html (100%) rename {spec => spec-main}/fixtures/module/declare-buffer.js (100%) rename {spec => spec-main}/fixtures/module/declare-global.js (100%) rename {spec => spec-main}/fixtures/module/declare-process.js (100%) rename {spec => spec-main}/fixtures/module/echo-renamed.js (100%) rename {spec => spec-main}/fixtures/module/echo.js (100%) rename {spec => spec-main}/fixtures/module/test.coffee (100%) rename {spec => spec-main}/fixtures/native-addon/echo/binding.cc (100%) rename {spec => spec-main}/fixtures/native-addon/echo/binding.gyp (100%) rename {spec => spec-main}/fixtures/native-addon/echo/lib/echo.js (100%) rename {spec => spec-main}/fixtures/native-addon/echo/package.json (100%) rename spec/modules-spec.js => spec-main/modules-spec.ts (71%) create mode 100644 spec-main/yarn.lock diff --git a/.gitignore b/.gitignore index 218dd25f4dd0..d3c7ca924ca3 100644 --- a/.gitignore +++ b/.gitignore @@ -58,7 +58,7 @@ spec/.hash .eslintcache # Generated native addon files -/spec/fixtures/native-addon/echo/build/ +/spec-main/fixtures/native-addon/echo/build/ # If someone runs tsc this is where stuff will end up ts-gen diff --git a/script/spec-runner.js b/script/spec-runner.js index e4bcde4a9e58..5d9c1e278db6 100755 --- a/script/spec-runner.js +++ b/script/spec-runner.js @@ -60,7 +60,8 @@ async function main () { (lastSpecInstallHash !== currentSpecInstallHash) if (somethingChanged) { - await installSpecModules() + await installSpecModules(path.resolve(__dirname, '..', 'spec')) + await installSpecModules(path.resolve(__dirname, '..', 'spec-main')) await getSpecHash().then(saveSpecHash) } @@ -214,7 +215,7 @@ async function runMainProcessElectronTests () { console.log(`${pass} Electron main process tests passed.`) } -async function installSpecModules () { +async function installSpecModules (dir) { const nodeDir = path.resolve(BASE, `out/${utils.getOutDir(true)}/gen/node_headers`) const env = Object.assign({}, process.env, { npm_config_nodedir: nodeDir, @@ -222,11 +223,11 @@ async function installSpecModules () { }) const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], { env, - cwd: path.resolve(__dirname, '../spec'), + cwd: dir, stdio: 'inherit' }) if (status !== 0 && !process.env.IGNORE_YARN_INSTALL_ERROR) { - console.log(`${fail} Failed to yarn install in the spec folder`) + console.log(`${fail} Failed to yarn install in '${dir}'`) process.exit(1) } } @@ -236,7 +237,9 @@ function getSpecHash () { (async () => { const hasher = crypto.createHash('SHA256') hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json'))) + hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/package.json'))) hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/yarn.lock'))) + hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/yarn.lock'))) return hasher.digest('hex') })(), (async () => { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index e4416676cd7b..e845622b3cce 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -2210,7 +2210,7 @@ describe('BrowserWindow module', () => { w.loadFile(path.join(fixtures, 'api', 'native-window-open-iframe.html')) }); ifit(!process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS)('loads native addons correctly after reload', async () => { - w.loadFile(path.join(fixtures, 'api', 'native-window-open-native-addon.html')) + w.loadFile(path.join(__dirname, 'fixtures', 'api', 'native-window-open-native-addon.html')) { const [, content] = await emittedOnce(ipcMain, 'answer') expect(content).to.equal('function') diff --git a/spec/fixtures/api/native-window-open-native-addon.html b/spec-main/fixtures/api/native-window-open-native-addon.html similarity index 100% rename from spec/fixtures/api/native-window-open-native-addon.html rename to spec-main/fixtures/api/native-window-open-native-addon.html diff --git a/spec/fixtures/module/declare-buffer.js b/spec-main/fixtures/module/declare-buffer.js similarity index 100% rename from spec/fixtures/module/declare-buffer.js rename to spec-main/fixtures/module/declare-buffer.js diff --git a/spec/fixtures/module/declare-global.js b/spec-main/fixtures/module/declare-global.js similarity index 100% rename from spec/fixtures/module/declare-global.js rename to spec-main/fixtures/module/declare-global.js diff --git a/spec/fixtures/module/declare-process.js b/spec-main/fixtures/module/declare-process.js similarity index 100% rename from spec/fixtures/module/declare-process.js rename to spec-main/fixtures/module/declare-process.js diff --git a/spec/fixtures/module/echo-renamed.js b/spec-main/fixtures/module/echo-renamed.js similarity index 100% rename from spec/fixtures/module/echo-renamed.js rename to spec-main/fixtures/module/echo-renamed.js diff --git a/spec/fixtures/module/echo.js b/spec-main/fixtures/module/echo.js similarity index 100% rename from spec/fixtures/module/echo.js rename to spec-main/fixtures/module/echo.js diff --git a/spec/fixtures/module/test.coffee b/spec-main/fixtures/module/test.coffee similarity index 100% rename from spec/fixtures/module/test.coffee rename to spec-main/fixtures/module/test.coffee diff --git a/spec/fixtures/native-addon/echo/binding.cc b/spec-main/fixtures/native-addon/echo/binding.cc similarity index 100% rename from spec/fixtures/native-addon/echo/binding.cc rename to spec-main/fixtures/native-addon/echo/binding.cc diff --git a/spec/fixtures/native-addon/echo/binding.gyp b/spec-main/fixtures/native-addon/echo/binding.gyp similarity index 100% rename from spec/fixtures/native-addon/echo/binding.gyp rename to spec-main/fixtures/native-addon/echo/binding.gyp diff --git a/spec/fixtures/native-addon/echo/lib/echo.js b/spec-main/fixtures/native-addon/echo/lib/echo.js similarity index 100% rename from spec/fixtures/native-addon/echo/lib/echo.js rename to spec-main/fixtures/native-addon/echo/lib/echo.js diff --git a/spec/fixtures/native-addon/echo/package.json b/spec-main/fixtures/native-addon/echo/package.json similarity index 100% rename from spec/fixtures/native-addon/echo/package.json rename to spec-main/fixtures/native-addon/echo/package.json diff --git a/spec/modules-spec.js b/spec-main/modules-spec.ts similarity index 71% rename from spec/modules-spec.js rename to spec-main/modules-spec.ts index ac63c0165115..4beb99220b7b 100644 --- a/spec/modules-spec.js +++ b/spec-main/modules-spec.ts @@ -1,64 +1,55 @@ -const chai = require('chai') -const dirtyChai = require('dirty-chai') +import { expect } from 'chai' +import * as path from 'path' +import * as fs from 'fs' +import { BrowserWindow } from 'electron' +import { ifdescribe, ifit } from './spec-helpers' +import { closeAllWindows } from './window-helpers' +import * as childProcess from 'child_process' const Module = require('module') -const path = require('path') -const fs = require('fs') -const { remote } = require('electron') -const { BrowserWindow } = remote -const { closeWindow } = require('./window-helpers') + const features = process.electronBinding('features') - -const { expect } = chai -chai.use(dirtyChai) - -const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled') +const nativeModulesEnabled = !process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS describe('modules support', () => { const fixtures = path.join(__dirname, 'fixtures') describe('third-party module', () => { - (nativeModulesEnabled ? describe : describe.skip)('echo', () => { - it('can be required in renderer', () => { - require('echo') + ifdescribe(nativeModulesEnabled)('echo', () => { + afterEach(closeAllWindows) + it('can be required in renderer', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) + w.loadURL('about:blank') + await expect(w.webContents.executeJavaScript(`{ require('echo') }`)).to.be.fulfilled() }) - it('can be required in node binary', function (done) { - if (!features.isRunAsNodeEnabled()) { - this.skip() - done() - } - - const echo = path.join(fixtures, 'module', 'echo.js') - const child = require('child_process').fork(echo) + ifit(features.isRunAsNodeEnabled())('can be required in node binary', function (done) { + const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js')) child.on('message', (msg) => { expect(msg).to.equal('ok') done() }) }) - if (process.platform === 'win32') { - it('can be required if electron.exe is renamed', () => { - const { execPath } = remote.process - const testExecPath = path.join(path.dirname(execPath), 'test.exe') - fs.copyFileSync(execPath, testExecPath) - try { - const fixture = path.join(fixtures, 'module', 'echo-renamed.js') - expect(fs.existsSync(fixture)).to.be.true() - const child = require('child_process').spawnSync(testExecPath, [fixture]) - expect(child.status).to.equal(0) - } finally { - fs.unlinkSync(testExecPath) - } - }) - } + ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => { + const testExecPath = path.join(path.dirname(process.execPath), 'test.exe') + fs.copyFileSync(process.execPath, testExecPath) + try { + const fixture = path.join(fixtures, 'module', 'echo-renamed.js') + expect(fs.existsSync(fixture)).to.be.true() + const child = childProcess.spawnSync(testExecPath, [fixture]) + expect(child.status).to.equal(0) + } finally { + fs.unlinkSync(testExecPath) + } + }) }) describe('q', () => { const Q = require('q') describe('Q.when', () => { it('emits the fullfil callback', (done) => { - Q(true).then((val) => { + Q(true).then((val: boolean) => { expect(val).to.be.true() done() }) @@ -150,23 +141,9 @@ describe('modules support', () => { describe('require', () => { describe('when loaded URL is not file: protocol', () => { - let w - - beforeEach(() => { - w = new BrowserWindow({ - show: false, - webPreferences: { - nodeIntegration: true - } - }) - }) - - afterEach(async () => { - await closeWindow(w) - w = null - }) - + afterEach(closeAllWindows) it('searches for module under app directory', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) w.loadURL('about:blank') const result = await w.webContents.executeJavaScript('typeof require("q").when') expect(result).to.equal('function') diff --git a/spec-main/package.json b/spec-main/package.json index 8a802f6bfdf2..eb5a7e949a10 100644 --- a/spec-main/package.json +++ b/spec-main/package.json @@ -2,5 +2,9 @@ "name": "electron-test-main", "productName": "Electron Test Main", "main": "index.js", - "version": "0.1.0" + "version": "0.1.0", + "devDependencies": { + "echo": "file:fixtures/native-addon/echo", + "q": "^1.5.1" + } } diff --git a/spec-main/yarn.lock b/spec-main/yarn.lock new file mode 100644 index 000000000000..79ec47695e19 --- /dev/null +++ b/spec-main/yarn.lock @@ -0,0 +1,11 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"echo@file:fixtures/native-addon/echo": + version "0.0.1" + +q@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7" + integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc= diff --git a/spec/api-shell-spec.js b/spec/api-shell-spec.js index 7d0b27c445a7..a3b3f28a103a 100644 --- a/spec/api-shell-spec.js +++ b/spec/api-shell-spec.js @@ -7,9 +7,6 @@ const os = require('os') const http = require('http') const { shell } = require('electron') -const { closeWindow } = require('./window-helpers') -const { emittedOnce } = require('./events-helpers') - const { expect } = chai chai.use(dirtyChai) diff --git a/spec/fixtures/pages/native-module.html b/spec/fixtures/pages/native-module.html index 6d6bb821b302..78d928f2366c 100644 --- a/spec/fixtures/pages/native-module.html +++ b/spec/fixtures/pages/native-module.html @@ -2,7 +2,7 @@ diff --git a/spec/package.json b/spec/package.json index 87f58e63c5d8..eada112c61f0 100644 --- a/spec/package.json +++ b/spec/package.json @@ -13,7 +13,6 @@ "coffeescript": "^2.4.1", "dbus-native": "github:nornagon/dbus-native#master", "dirty-chai": "^2.0.1", - "echo": "file:fixtures/native-addon/echo", "graceful-fs": "^4.1.15", "is-valid-window": "0.0.5", "mkdirp": "^0.5.1", @@ -21,7 +20,6 @@ "mocha-junit-reporter": "^1.18.0", "mocha-multi-reporters": "^1.1.7", "multiparty": "^4.2.1", - "q": "^1.5.1", "send": "^0.16.2", "split": "^1.0.1", "temp": "^0.9.0", diff --git a/spec/yarn.lock b/spec/yarn.lock index 71922b21c508..a649b60d80c1 100644 --- a/spec/yarn.lock +++ b/spec/yarn.lock @@ -377,9 +377,6 @@ ecc-jsbn@~0.1.1: jsbn "~0.1.0" safer-buffer "^2.1.0" -"echo@file:fixtures/native-addon/echo": - version "0.0.1" - ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" @@ -1163,11 +1160,6 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== -q@^1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7" - integrity sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc= - qs@~6.5.2: version "6.5.2" resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"