test: add test for second-instance event parameter (#16798)

* test: add test for second-instance event parameter

* robustify getting data from child process

* fix test on windows

* fix lint

* Update api-app-spec.js

* fix package-lock.json
This commit is contained in:
Jeremy Apthorp 2019-03-12 08:56:28 -07:00 committed by John Kleinschmidt
parent 48a95f9677
commit ea6a926494
3 changed files with 34 additions and 2 deletions

View file

@ -7,6 +7,7 @@ const net = require('net')
const fs = require('fs')
const path = require('path')
const cp = require('child_process')
const split = require('split')
const { ipcRenderer, remote } = require('electron')
const { emittedOnce } = require('./events-helpers')
const { closeWindow } = require('./window-helpers')
@ -226,6 +227,33 @@ describe('app module', () => {
})
})
})
it('passes arguments to the second-instance event', async () => {
const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
const first = ChildProcess.spawn(remote.process.execPath, [appPath])
const firstExited = emittedOnce(first, 'exit')
// Wait for the first app to boot.
const firstStdoutLines = first.stdout.pipe(split())
while ((await emittedOnce(firstStdoutLines, 'data')).toString() !== 'started') {
// wait.
}
const data2Promise = emittedOnce(firstStdoutLines, 'data')
const secondInstanceArgs = [remote.process.execPath, appPath, '--some-switch', 'some-arg']
const second = ChildProcess.spawn(secondInstanceArgs[0], secondInstanceArgs.slice(1))
const [code2] = await emittedOnce(second, 'exit')
expect(code2).to.equal(1)
const [code1] = await firstExited
expect(code1).to.equal(0)
const data2 = (await data2Promise).toString('ascii')
const secondInstanceArgsReceived = JSON.parse(data2.toString('ascii'))
const expected = process.platform === 'win32'
? [remote.process.execPath, '--some-switch', '--allow-file-access-from-files', secondInstanceArgsReceived.find(x => x.includes('original-process-start-time')), appPath, 'some-arg']
: secondInstanceArgs
expect(secondInstanceArgsReceived).to.eql(expected,
`expected ${JSON.stringify(expected)} but got ${data2.toString('ascii')}`)
})
})
describe('app.relaunch', () => {

View file

@ -6,8 +6,11 @@ app.once('ready', () => {
const gotTheLock = app.requestSingleInstanceLock()
app.on('second-instance', () => {
setImmediate(() => app.exit(0))
app.on('second-instance', (event, args) => {
setImmediate(() => {
console.log(JSON.stringify(args))
app.exit(0)
})
})
if (!gotTheLock) {

View file

@ -25,6 +25,7 @@
"multiparty": "^4.2.1",
"q": "^1.5.1",
"send": "^0.16.2",
"split": "^1.0.1",
"temp": "^0.9.0",
"walkdir": "^0.3.2",
"winreg": "^1.2.4",