Browse Source

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
Jeremy Apthorp 6 years ago
parent
commit
ea6a926494
3 changed files with 34 additions and 2 deletions
  1. 28 0
      spec/api-app-spec.js
  2. 5 2
      spec/fixtures/api/singleton/main.js
  3. 1 0
      spec/package.json

+ 28 - 0
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', () => {

+ 5 - 2
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) {

+ 1 - 0
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",