test: move module tests to main process (#20419)
This commit is contained in:
parent
df1d3156a0
commit
8de925c4c2
21 changed files with 58 additions and 76 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -58,7 +58,7 @@ spec/.hash
|
||||||
.eslintcache
|
.eslintcache
|
||||||
|
|
||||||
# Generated native addon files
|
# 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
|
# If someone runs tsc this is where stuff will end up
|
||||||
ts-gen
|
ts-gen
|
||||||
|
|
|
@ -60,7 +60,8 @@ async function main () {
|
||||||
(lastSpecInstallHash !== currentSpecInstallHash)
|
(lastSpecInstallHash !== currentSpecInstallHash)
|
||||||
|
|
||||||
if (somethingChanged) {
|
if (somethingChanged) {
|
||||||
await installSpecModules()
|
await installSpecModules(path.resolve(__dirname, '..', 'spec'))
|
||||||
|
await installSpecModules(path.resolve(__dirname, '..', 'spec-main'))
|
||||||
await getSpecHash().then(saveSpecHash)
|
await getSpecHash().then(saveSpecHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,7 +215,7 @@ async function runMainProcessElectronTests () {
|
||||||
console.log(`${pass} Electron main process tests passed.`)
|
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 nodeDir = path.resolve(BASE, `out/${utils.getOutDir(true)}/gen/node_headers`)
|
||||||
const env = Object.assign({}, process.env, {
|
const env = Object.assign({}, process.env, {
|
||||||
npm_config_nodedir: nodeDir,
|
npm_config_nodedir: nodeDir,
|
||||||
|
@ -222,11 +223,11 @@ async function installSpecModules () {
|
||||||
})
|
})
|
||||||
const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
|
const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
|
||||||
env,
|
env,
|
||||||
cwd: path.resolve(__dirname, '../spec'),
|
cwd: dir,
|
||||||
stdio: 'inherit'
|
stdio: 'inherit'
|
||||||
})
|
})
|
||||||
if (status !== 0 && !process.env.IGNORE_YARN_INSTALL_ERROR) {
|
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)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,7 +237,9 @@ function getSpecHash () {
|
||||||
(async () => {
|
(async () => {
|
||||||
const hasher = crypto.createHash('SHA256')
|
const hasher = crypto.createHash('SHA256')
|
||||||
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
|
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/yarn.lock')))
|
||||||
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/yarn.lock')))
|
||||||
return hasher.digest('hex')
|
return hasher.digest('hex')
|
||||||
})(),
|
})(),
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
|
@ -2210,7 +2210,7 @@ describe('BrowserWindow module', () => {
|
||||||
w.loadFile(path.join(fixtures, 'api', 'native-window-open-iframe.html'))
|
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 () => {
|
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')
|
const [, content] = await emittedOnce(ipcMain, 'answer')
|
||||||
expect(content).to.equal('function')
|
expect(content).to.equal('function')
|
||||||
|
|
|
@ -1,64 +1,55 @@
|
||||||
const chai = require('chai')
|
import { expect } from 'chai'
|
||||||
const dirtyChai = require('dirty-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 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 features = process.electronBinding('features')
|
||||||
|
const nativeModulesEnabled = !process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS
|
||||||
const { expect } = chai
|
|
||||||
chai.use(dirtyChai)
|
|
||||||
|
|
||||||
const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
|
|
||||||
|
|
||||||
describe('modules support', () => {
|
describe('modules support', () => {
|
||||||
const fixtures = path.join(__dirname, 'fixtures')
|
const fixtures = path.join(__dirname, 'fixtures')
|
||||||
|
|
||||||
describe('third-party module', () => {
|
describe('third-party module', () => {
|
||||||
(nativeModulesEnabled ? describe : describe.skip)('echo', () => {
|
ifdescribe(nativeModulesEnabled)('echo', () => {
|
||||||
it('can be required in renderer', () => {
|
afterEach(closeAllWindows)
|
||||||
require('echo')
|
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) {
|
ifit(features.isRunAsNodeEnabled())('can be required in node binary', function (done) {
|
||||||
if (!features.isRunAsNodeEnabled()) {
|
const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js'))
|
||||||
this.skip()
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
|
|
||||||
const echo = path.join(fixtures, 'module', 'echo.js')
|
|
||||||
const child = require('child_process').fork(echo)
|
|
||||||
child.on('message', (msg) => {
|
child.on('message', (msg) => {
|
||||||
expect(msg).to.equal('ok')
|
expect(msg).to.equal('ok')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
if (process.platform === 'win32') {
|
ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
|
||||||
it('can be required if electron.exe is renamed', () => {
|
const testExecPath = path.join(path.dirname(process.execPath), 'test.exe')
|
||||||
const { execPath } = remote.process
|
fs.copyFileSync(process.execPath, testExecPath)
|
||||||
const testExecPath = path.join(path.dirname(execPath), 'test.exe')
|
try {
|
||||||
fs.copyFileSync(execPath, testExecPath)
|
const fixture = path.join(fixtures, 'module', 'echo-renamed.js')
|
||||||
try {
|
expect(fs.existsSync(fixture)).to.be.true()
|
||||||
const fixture = path.join(fixtures, 'module', 'echo-renamed.js')
|
const child = childProcess.spawnSync(testExecPath, [fixture])
|
||||||
expect(fs.existsSync(fixture)).to.be.true()
|
expect(child.status).to.equal(0)
|
||||||
const child = require('child_process').spawnSync(testExecPath, [fixture])
|
} finally {
|
||||||
expect(child.status).to.equal(0)
|
fs.unlinkSync(testExecPath)
|
||||||
} finally {
|
}
|
||||||
fs.unlinkSync(testExecPath)
|
})
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('q', () => {
|
describe('q', () => {
|
||||||
const Q = require('q')
|
const Q = require('q')
|
||||||
describe('Q.when', () => {
|
describe('Q.when', () => {
|
||||||
it('emits the fullfil callback', (done) => {
|
it('emits the fullfil callback', (done) => {
|
||||||
Q(true).then((val) => {
|
Q(true).then((val: boolean) => {
|
||||||
expect(val).to.be.true()
|
expect(val).to.be.true()
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
@ -150,23 +141,9 @@ describe('modules support', () => {
|
||||||
|
|
||||||
describe('require', () => {
|
describe('require', () => {
|
||||||
describe('when loaded URL is not file: protocol', () => {
|
describe('when loaded URL is not file: protocol', () => {
|
||||||
let w
|
afterEach(closeAllWindows)
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
w = new BrowserWindow({
|
|
||||||
show: false,
|
|
||||||
webPreferences: {
|
|
||||||
nodeIntegration: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await closeWindow(w)
|
|
||||||
w = null
|
|
||||||
})
|
|
||||||
|
|
||||||
it('searches for module under app directory', async () => {
|
it('searches for module under app directory', async () => {
|
||||||
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
|
||||||
w.loadURL('about:blank')
|
w.loadURL('about:blank')
|
||||||
const result = await w.webContents.executeJavaScript('typeof require("q").when')
|
const result = await w.webContents.executeJavaScript('typeof require("q").when')
|
||||||
expect(result).to.equal('function')
|
expect(result).to.equal('function')
|
|
@ -2,5 +2,9 @@
|
||||||
"name": "electron-test-main",
|
"name": "electron-test-main",
|
||||||
"productName": "Electron Test Main",
|
"productName": "Electron Test Main",
|
||||||
"main": "index.js",
|
"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
11
spec-main/yarn.lock
Normal 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=
|
|
@ -7,9 +7,6 @@ const os = require('os')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const { shell } = require('electron')
|
const { shell } = require('electron')
|
||||||
|
|
||||||
const { closeWindow } = require('./window-helpers')
|
|
||||||
const { emittedOnce } = require('./events-helpers')
|
|
||||||
|
|
||||||
const { expect } = chai
|
const { expect } = chai
|
||||||
chai.use(dirtyChai)
|
chai.use(dirtyChai)
|
||||||
|
|
||||||
|
|
2
spec/fixtures/pages/native-module.html
vendored
2
spec/fixtures/pages/native-module.html
vendored
|
@ -2,7 +2,7 @@
|
||||||
<body>
|
<body>
|
||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
var path = require('path');
|
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>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
"coffeescript": "^2.4.1",
|
"coffeescript": "^2.4.1",
|
||||||
"dbus-native": "github:nornagon/dbus-native#master",
|
"dbus-native": "github:nornagon/dbus-native#master",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"echo": "file:fixtures/native-addon/echo",
|
|
||||||
"graceful-fs": "^4.1.15",
|
"graceful-fs": "^4.1.15",
|
||||||
"is-valid-window": "0.0.5",
|
"is-valid-window": "0.0.5",
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^0.5.1",
|
||||||
|
@ -21,7 +20,6 @@
|
||||||
"mocha-junit-reporter": "^1.18.0",
|
"mocha-junit-reporter": "^1.18.0",
|
||||||
"mocha-multi-reporters": "^1.1.7",
|
"mocha-multi-reporters": "^1.1.7",
|
||||||
"multiparty": "^4.2.1",
|
"multiparty": "^4.2.1",
|
||||||
"q": "^1.5.1",
|
|
||||||
"send": "^0.16.2",
|
"send": "^0.16.2",
|
||||||
"split": "^1.0.1",
|
"split": "^1.0.1",
|
||||||
"temp": "^0.9.0",
|
"temp": "^0.9.0",
|
||||||
|
|
|
@ -377,9 +377,6 @@ ecc-jsbn@~0.1.1:
|
||||||
jsbn "~0.1.0"
|
jsbn "~0.1.0"
|
||||||
safer-buffer "^2.1.0"
|
safer-buffer "^2.1.0"
|
||||||
|
|
||||||
"echo@file:fixtures/native-addon/echo":
|
|
||||||
version "0.0.1"
|
|
||||||
|
|
||||||
ee-first@1.1.1:
|
ee-first@1.1.1:
|
||||||
version "1.1.1"
|
version "1.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
|
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"
|
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
|
||||||
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
|
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:
|
qs@~6.5.2:
|
||||||
version "6.5.2"
|
version "6.5.2"
|
||||||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
|
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
|
||||||
|
|
Loading…
Reference in a new issue