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