test: move module tests to main process (#20419)

This commit is contained in:
Jeremy Apthorp 2019-10-09 16:33:15 -07:00 committed by GitHub
parent df1d3156a0
commit 8de925c4c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 58 additions and 76 deletions

2
.gitignore vendored
View file

@ -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

View file

@ -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 () => {

View file

@ -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')

View file

@ -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')

View file

@ -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"
}
}

11
spec-main/yarn.lock Normal file
View file

@ -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=

View file

@ -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)

View file

@ -2,7 +2,7 @@
<body>
<script type="text/javascript" charset="utf-8">
var path = require('path');
console.log(typeof require(path.join(__dirname, '..', '..', 'node_modules', 'echo')));
console.log(typeof require(path.join(__dirname, '..', '..', '..', 'spec-main', 'node_modules', 'echo')));
</script>
</body>
</html>

View file

@ -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",

View file

@ -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"