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:
parent
48a95f9677
commit
ea6a926494
3 changed files with 34 additions and 2 deletions
|
@ -7,6 +7,7 @@ const net = require('net')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const cp = require('child_process')
|
const cp = require('child_process')
|
||||||
|
const split = require('split')
|
||||||
const { ipcRenderer, remote } = require('electron')
|
const { ipcRenderer, remote } = require('electron')
|
||||||
const { emittedOnce } = require('./events-helpers')
|
const { emittedOnce } = require('./events-helpers')
|
||||||
const { closeWindow } = require('./window-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', () => {
|
describe('app.relaunch', () => {
|
||||||
|
|
7
spec/fixtures/api/singleton/main.js
vendored
7
spec/fixtures/api/singleton/main.js
vendored
|
@ -6,8 +6,11 @@ app.once('ready', () => {
|
||||||
|
|
||||||
const gotTheLock = app.requestSingleInstanceLock()
|
const gotTheLock = app.requestSingleInstanceLock()
|
||||||
|
|
||||||
app.on('second-instance', () => {
|
app.on('second-instance', (event, args) => {
|
||||||
setImmediate(() => app.exit(0))
|
setImmediate(() => {
|
||||||
|
console.log(JSON.stringify(args))
|
||||||
|
app.exit(0)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!gotTheLock) {
|
if (!gotTheLock) {
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
"multiparty": "^4.2.1",
|
"multiparty": "^4.2.1",
|
||||||
"q": "^1.5.1",
|
"q": "^1.5.1",
|
||||||
"send": "^0.16.2",
|
"send": "^0.16.2",
|
||||||
|
"split": "^1.0.1",
|
||||||
"temp": "^0.9.0",
|
"temp": "^0.9.0",
|
||||||
"walkdir": "^0.3.2",
|
"walkdir": "^0.3.2",
|
||||||
"winreg": "^1.2.4",
|
"winreg": "^1.2.4",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue