From f7dce3987640e2b934e43239bd6cb42a8909fb48 Mon Sep 17 00:00:00 2001 From: mirobo <78788210+mirobo@users.noreply.github.com> Date: Tue, 16 Aug 2022 02:26:54 +0200 Subject: [PATCH] fix: escape backslashes in downloadFolder for Firefox on Windows #17896 (#23006) * fix: escape backslashes in downloadFolder for Firefox on Windows * fix import of doubleEscape in packages/server/lib/browsers/firefox.ts Co-authored-by: Bill Glesias * don't run ls command on Windows * Revert "don't run ls command on Windows" This reverts commit deec721b06d2582a66c4ea3b802e2f88fdc0bd79. * chore: add unit test to verify double escape behavior Co-authored-by: Bill Glesias Co-authored-by: Rachel --- packages/server/lib/browsers/firefox.ts | 3 ++- .../server/test/unit/browsers/firefox_spec.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/server/lib/browsers/firefox.ts b/packages/server/lib/browsers/firefox.ts index c58fc2b61434..cb454643c837 100644 --- a/packages/server/lib/browsers/firefox.ts +++ b/packages/server/lib/browsers/firefox.ts @@ -6,6 +6,7 @@ import getPort from 'get-port' import path from 'path' import urlUtil from 'url' import { debug as launcherDebug, launch, LaunchedBrowser } from '@packages/launcher/lib/browsers' +import { doubleEscape } from '@packages/launcher/lib/windows' import FirefoxProfile from 'firefox-profile' import * as errors from '../errors' import firefoxUtil from './firefox-util' @@ -427,7 +428,7 @@ export async function open (browser: Browser, url, options: any = {}, automation 'network.proxy.http_port': +port, 'network.proxy.ssl_port': +port, 'network.proxy.no_proxies_on': '', - 'browser.download.dir': options.downloadsFolder, + 'browser.download.dir': os.platform() === 'win32' ? doubleEscape(options.downloadsFolder) : options.downloadsFolder, }) } diff --git a/packages/server/test/unit/browsers/firefox_spec.ts b/packages/server/test/unit/browsers/firefox_spec.ts index c6ebb7e151a1..4baa95596cc0 100644 --- a/packages/server/test/unit/browsers/firefox_spec.ts +++ b/packages/server/test/unit/browsers/firefox_spec.ts @@ -303,6 +303,23 @@ describe('lib/browsers/firefox', () => { }) }) + // @see https://github.com/cypress-io/cypress/issues/17896 + it('escapes the downloadsFolders path correctly when running on Windows OS', function () { + this.options.proxyServer = 'http://proxy-server:1234' + this.options.downloadsFolder = 'C:/Users/test/Downloads/My_Test_Downloads_Folder' + sinon.stub(os, 'platform').returns('win32') + const executeBeforeBrowserLaunchSpy = sinon.spy(utils, 'executeBeforeBrowserLaunch') + + return firefox.open(this.browser, 'http://', this.options, this.automation).then(() => { + expect(executeBeforeBrowserLaunchSpy).to.have.been.calledWith(this.browser, sinon.match({ + preferences: { + // NOTE: sinon.match treats the string itself as a regular expression. The backslashes need to be escaped. + 'browser.download.dir': 'C:\\\\Users\\\\test\\\\Downloads\\\\My_Test_Downloads_Folder', + }, + }), this.options) + }) + }) + it('updates the preferences', function () { return firefox.open(this.browser, 'http://', this.options, this.automation).then(() => { expect(FirefoxProfile.prototype.updatePreferences).to.be.called