Skip to content

Commit

Permalink
depromisify things that were promisified b/t electron 5 <=> 6
Browse files Browse the repository at this point in the history
Revert "update shell.openExternal to promisified"

This reverts commit 8b6460d.

Revert "update dialog.showOpenDialog to promisified"

This reverts commit 5f178b0.

Revert "update webContents.session.setProxy to promisified"

This reverts commit 727df3a.
  • Loading branch information
flotwig committed Sep 24, 2019
1 parent 1e698b5 commit a724d4f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
40 changes: 20 additions & 20 deletions packages/server/lib/browsers/electron.coffee
Expand Up @@ -93,7 +93,7 @@ getAutomation = (win) ->
.then ->
cookieToBeCleared
when "is:automation:client:connected"
tryToCall(win, 'isDestroyed') == false
true
when "take:screenshot"
tryToCall(win, 'capturePage')
.then _.partialRight(_.invoke, 'toDataURL')
Expand Down Expand Up @@ -194,18 +194,16 @@ module.exports = {

_attachDebugger: (webContents) ->
originalSendCommand = webContents.debugger.sendCommand
webContents.debugger.sendCommand = (message, data = {}) ->
debug('debugger: sending %s %o', message, data)

originalSendCommand
.call(webContents.debugger, message, data)
.then (result) =>
debug("debugger: received response for %s: result: %o", message, result)
result
.catch (err) =>
debug("debugger: received error on %s: result: %o", message, err)
throw err

webContents.debugger.sendCommand = (message, data = {}) ->
new Promise (resolve, reject) =>
debug('debugger: sending %s %o', message, data)

originalSendCommand.call webContents.debugger, message, data, (err, result) =>
debug("debugger: received response for %s: %o", message, { err, result })
if _.isEmpty(err)
return resolve(result)
reject(err)
try
webContents.debugger.attach()
debug("debugger attached")
Expand Down Expand Up @@ -240,7 +238,8 @@ module.exports = {

_clearCache: (webContents) ->
debug("clearing cache")
webContents.session.clearCache()
Promise.fromCallback (cb) =>
webContents.session.clearCache(cb)

_setUserAgent: (webContents, userAgent) ->
debug("setting user agent to:", userAgent)
Expand All @@ -249,13 +248,14 @@ module.exports = {
webContents.session.setUserAgent(userAgent)

_setProxy: (webContents, proxyServer) ->
webContents.session.setProxy({
proxyRules: proxyServer
## this should really only be necessary when
## running Chromium versions >= 72
## https://github.com/cypress-io/cypress/issues/1872
proxyBypassRules: "<-loopback>"
})
Promise.fromCallback (cb) =>
webContents.session.setProxy({
proxyRules: proxyServer
## this should really only be necessary when
## running Chromium versions >= 72
## https://github.com/cypress-io/cypress/issues/1872
proxyBypassRules: "<-loopback>"
}, cb)

open: (browser, url, options = {}, automation) ->
{ projectRoot, isTextTerminal } = options
Expand Down
5 changes: 4 additions & 1 deletion packages/server/lib/gui/auth.js
Expand Up @@ -190,7 +190,10 @@ const _launchNativeAuth = Promise.method((loginUrl, sendMessage) => {

openExternalAttempted = true

return shell.openExternal(loginUrl)
// wrap openExternal here in case `electron.shell` is not available (during tests)
return Promise.fromCallback((cb) => {
shell.openExternal(loginUrl, {}, cb)
})
.catch((err) => {
debug('Error launching native auth: %o', { err })
warnCouldNotLaunch()
Expand Down
9 changes: 6 additions & 3 deletions packages/server/lib/gui/dialog.coffee
Expand Up @@ -15,7 +15,10 @@ module.exports = {
properties: ["openDirectory"]
}

dialog.showOpenDialog(props)
.then ({ filePaths }) ->
return filePaths[0]
new Promise (resolve, reject) ->
dialog.showOpenDialog props, (paths = []) ->
process.nextTick ->
## return the first path since there can only ever
## be a single directory selection
resolve(paths[0])
}
9 changes: 5 additions & 4 deletions packages/server/lib/gui/windows.coffee
Expand Up @@ -34,10 +34,11 @@ setWindowProxy = (win) ->
if not process.env.HTTP_PROXY
return

win.webContents.session.setProxy({
proxyRules: process.env.HTTP_PROXY
proxyBypassRules: process.env.NO_PROXY
})
return new Promise (resolve) ->
win.webContents.session.setProxy({
proxyRules: process.env.HTTP_PROXY
proxyBypassRules: process.env.NO_PROXY
}, resolve)

module.exports = {
reset: ->
Expand Down
12 changes: 5 additions & 7 deletions packages/server/test/unit/gui/auth_spec.js
Expand Up @@ -88,23 +88,21 @@ describe('lib/gui/auth', function () {
})

it('returns a promise that is fulfilled when openExternal succeeds', function () {
sinon.stub(electron.shell, 'openExternal').resolves()
const sendWarning = sinon.stub()
sinon.stub(electron.shell, 'openExternal').callsArg(2)

return auth._launchNativeAuth(REDIRECT_URL, sendWarning)
return auth._launchNativeAuth(REDIRECT_URL)
.then(() => {
expect(electron.shell.openExternal).to.be.calledWithMatch(REDIRECT_URL)
expect(sendWarning).to.not.be.called
expect(electron.shell.openExternal).to.be.calledWithMatch(REDIRECT_URL, {}, sinon.match.func)
})
})

it('is still fulfilled when openExternal fails, but sendWarning is called', function () {
sinon.stub(electron.shell, 'openExternal').rejects(new Error)
sinon.stub(electron.shell, 'openExternal').callsArgWith(2, new Error)
const sendWarning = sinon.stub()

return auth._launchNativeAuth(REDIRECT_URL, sendWarning)
.then(() => {
expect(electron.shell.openExternal).to.be.calledWithMatch(REDIRECT_URL)
expect(electron.shell.openExternal).to.be.calledWithMatch(REDIRECT_URL, {}, sinon.match.func)
expect(sendWarning).to.be.calledWithMatch('warning', 'AUTH_COULD_NOT_LAUNCH_BROWSER', REDIRECT_URL)
})
})
Expand Down
10 changes: 4 additions & 6 deletions packages/server/test/unit/gui/dialog_spec.coffee
Expand Up @@ -7,9 +7,7 @@ Windows = require("#{root}../lib/gui/windows")
describe "gui/dialog", ->
context ".show", ->
beforeEach ->
@showOpenDialog = electron.dialog.showOpenDialog = sinon.stub().resolves({
filePaths: []
})
@showOpenDialog = electron.dialog.showOpenDialog = sinon.stub()

it "calls dialog.showOpenDialog with args", ->
dialog.show()
Expand All @@ -18,13 +16,13 @@ describe "gui/dialog", ->
})

it "resolves with first path", ->
@showOpenDialog.resolves({
filePaths: ["foo", "bar"]
})
@showOpenDialog.yields(["foo", "bar"])

dialog.show().then (ret) ->
expect(ret).to.eq("foo")

it "handles null paths", ->
@showOpenDialog.yields(null)

dialog.show().then (ret) ->
expect(ret).to.eq(undefined)

0 comments on commit a724d4f

Please sign in to comment.