diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 2f9014c7cd13..999f2eca3f5b 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -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', () => { diff --git a/spec/fixtures/api/singleton/main.js b/spec/fixtures/api/singleton/main.js index fda3ed8cc4ff..9c1b0a17e4cf 100644 --- a/spec/fixtures/api/singleton/main.js +++ b/spec/fixtures/api/singleton/main.js @@ -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) { diff --git a/spec/package.json b/spec/package.json index aa9a4a5bccdb..38f6f328994f 100644 --- a/spec/package.json +++ b/spec/package.json @@ -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",