diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index f0de08481d8ec..08f08f9ea7b39 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -6,6 +6,7 @@ const https = require('https') const net = require('net') const fs = require('fs') const path = require('path') +const cp = require('child_process') const { ipcRenderer, remote } = require('electron') const { emittedOnce } = require('./events-helpers') const { closeWindow } = require('./window-helpers') @@ -1093,29 +1094,57 @@ describe('app module', () => { }) describe('commandLine.hasSwitch', () => { - it('returns true when present', () => { + it('returns true when present', async () => { app.commandLine.appendSwitch('foobar1') expect(app.commandLine.hasSwitch('foobar1')).to.be.true() + + const { hasSwitch } = await runCommandLineTestApp('--foobar') + expect(hasSwitch).to.be.true() }) - it('returns false when not present', () => { + it('returns false when not present', async () => { expect(app.commandLine.hasSwitch('foobar2')).to.be.false() + + const { hasSwitch } = await runCommandLineTestApp() + expect(hasSwitch).to.be.false() }) }) describe('commandLine.getSwitchValue', () => { - it('returns the value when present', () => { + it('returns the value when present', async () => { app.commandLine.appendSwitch('foobar', 'test') expect(app.commandLine.getSwitchValue('foobar')).to.equal('test') + + const { getSwitchValue } = await runCommandLineTestApp('--foobar=test') + expect(getSwitchValue).to.equal('test') }) - it('returns an empty string when present without value', () => { + it('returns an empty string when present without value', async () => { app.commandLine.appendSwitch('foobar1') expect(app.commandLine.getSwitchValue('foobar1')).to.equal('') + + const { getSwitchValue } = await runCommandLineTestApp('--foobar') + expect(getSwitchValue).to.equal('') }) - it('returns an empty string when not present', () => { + it('returns an empty string when not present', async () => { expect(app.commandLine.getSwitchValue('foobar2')).to.equal('') + + const { getSwitchValue } = await runCommandLineTestApp() + expect(getSwitchValue).to.equal('') }) }) + + async function runCommandLineTestApp (...args) { + const appPath = path.join(__dirname, 'fixtures', 'api', 'command-line') + const electronPath = remote.getGlobal('process').execPath + const appProcess = cp.spawn(electronPath, [appPath, ...args]) + + let output = '' + appProcess.stdout.on('data', (data) => { output += data }) + + await emittedOnce(appProcess.stdout, 'end') + + return JSON.parse(output) + } }) diff --git a/spec/fixtures/api/command-line/main.js b/spec/fixtures/api/command-line/main.js new file mode 100644 index 0000000000000..39e62cafbbc87 --- /dev/null +++ b/spec/fixtures/api/command-line/main.js @@ -0,0 +1,15 @@ +const { app } = require('electron') + +app.on('ready', () => { + const payload = { + hasSwitch: app.commandLine.hasSwitch('foobar'), + getSwitchValue: app.commandLine.getSwitchValue('foobar') + } + + process.stdout.write(JSON.stringify(payload)) + process.stdout.end() + + setImmediate(() => { + app.quit() + }) +}) diff --git a/spec/fixtures/api/command-line/package.json b/spec/fixtures/api/command-line/package.json new file mode 100644 index 0000000000000..bbe8102015d01 --- /dev/null +++ b/spec/fixtures/api/command-line/package.json @@ -0,0 +1,4 @@ +{ + "name": "command-line", + "main": "main.js" +}