From e65a3e348a9ba386032263cb4af5934e28071b67 Mon Sep 17 00:00:00 2001 From: Stokes Player Date: Fri, 30 Jun 2023 19:01:12 +0000 Subject: [PATCH] fix: do not override electron debug port if previously set (#27169) * fix: do not override electron debug port if previously set * Add changelog * Update cli/CHANGELOG.md Co-authored-by: Mike Plummer * Removed unneeded return value --------- Co-authored-by: Mike Plummer --- cli/CHANGELOG.md | 3 +- packages/server/lib/util/electron-app.js | 9 ++++- .../test/support/helpers/electron_stub.js | 1 + .../test/unit/util/electron-app_spec.js | 36 +++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 packages/server/test/unit/util/electron-app_spec.js diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index d544e8fbb393..7e6b421f30e5 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -14,10 +14,11 @@ _Released 07/05/2023 (PENDING)_ - Fixed a race condition that was causing a GraphQL error to appear on the [Debug page](https://docs.cypress.io/guides/cloud/runs#Debug) when viewing a running Cypress Cloud build. Fixed in [#27134](https://github.com/cypress-io/cypress/pull/27134). - Fixed a race condition in electron where the test window exiting prematurely during the browser launch process was causing the whole test run to fail. Addressed in [#27167](https://github.com/cypress-io/cypress/pull/27167). - Fixed minor issues with Typescript types in the CLI. Fixes [#24110](https://github.com/cypress-io/cypress/issues/24110). +- Fixed an issue where a value for the Electron debug port would not be respected if defined using the `ELECTRON_EXTRA_LAUNCH_ARGS` environment variable. Fixes [#26711](https://github.com/cypress-io/cypress/issues/26711). **Dependency Updates:** - - Update dependency semver to ^7.5.3. Addressed in [#27151](https://github.com/cypress-io/cypress/pull/27151). +- Update dependency semver to ^7.5.3. Addressed in [#27151](https://github.com/cypress-io/cypress/pull/27151). ## 12.16.0 diff --git a/packages/server/lib/util/electron-app.js b/packages/server/lib/util/electron-app.js index 2f9cbaa5c2a8..1d9c2a6e3acc 100644 --- a/packages/server/lib/util/electron-app.js +++ b/packages/server/lib/util/electron-app.js @@ -24,9 +24,16 @@ const getRemoteDebuggingPort = () => { const setRemoteDebuggingPort = async () => { try { - const port = await getPort() const { app } = require('electron') + // if port was already set via passing from environment variable ELECTRON_EXTRA_LAUNCH_ARGS, + // then just keep the supplied value + if (app.commandLine.getSwitchValue('remote-debugging-port')) { + return + } + + const port = await getPort() + // set up remote debugging port app.commandLine.appendSwitch('remote-debugging-port', String(port)) } catch (err) { diff --git a/packages/server/test/support/helpers/electron_stub.js b/packages/server/test/support/helpers/electron_stub.js index 454edbdbda7f..4ad522b7f171 100644 --- a/packages/server/test/support/helpers/electron_stub.js +++ b/packages/server/test/support/helpers/electron_stub.js @@ -17,6 +17,7 @@ module.exports = { exit () {}, commandLine: { appendSwitch () {}, + getSwitchValue () {}, appendArgument () {}, }, disableHardwareAcceleration () {}, diff --git a/packages/server/test/unit/util/electron-app_spec.js b/packages/server/test/unit/util/electron-app_spec.js new file mode 100644 index 000000000000..54186b4c4090 --- /dev/null +++ b/packages/server/test/unit/util/electron-app_spec.js @@ -0,0 +1,36 @@ +const electronApp = require('../../../lib/util/electron-app') + +describe('/lib/util/electron-app', () => { + context('remote debugging port', () => { + beforeEach(() => { + sinon.restore() + }) + + it('should not override port if previously set', async () => { + const { app } = require('electron') + + sinon.stub(app.commandLine, 'appendSwitch') + sinon.stub(app.commandLine, 'getSwitchValue').callsFake((args) => { + return '4567' + }) + + await electronApp.setRemoteDebuggingPort() + + expect(app.commandLine.appendSwitch).to.not.have.been.called + }) + + it('should assign random port if not previously set', async () => { + const { app } = require('electron') + + sinon.stub(app.commandLine, 'appendSwitch') + + sinon.stub(app.commandLine, 'getSwitchValue').callsFake((args) => { + return undefined + }) + + await electronApp.setRemoteDebuggingPort() + + expect(app.commandLine.appendSwitch).to.have.been.calledWith('remote-debugging-port', sinon.match.string) + }) + }) +})