From feeda28a554eaba6d616de5bdeb41a65257363f0 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sat, 22 Dec 2018 08:57:59 -0700 Subject: [PATCH] test: add new openExternal specs for linux --- default_app/menu.js | 8 +++--- default_app/renderer.js | 2 +- docs/api/menu.md | 2 +- docs/api/promisification.md | 6 ++-- docs/api/shell.md | 6 ++-- docs/api/webview-tag.md | 2 +- docs/tutorial/security.md | 2 +- script/prepare-release.js | 4 ++- spec/api-remote-spec.js | 2 +- spec/api-shell-spec.js | 55 +++++++++++++++++++++++++++++++++---- 10 files changed, 68 insertions(+), 21 deletions(-) diff --git a/default_app/menu.js b/default_app/menu.js index 6dd37b2d32875..6fa94852984bb 100644 --- a/default_app/menu.js +++ b/default_app/menu.js @@ -77,13 +77,13 @@ const setDefaultApplicationMenu = () => { { label: 'Learn More', click () { - shell.openExternal('https://electronjs.org') + shell.openExternalSync('https://electronjs.org') } }, { label: 'Documentation', click () { - shell.openExternal( + shell.openExternalSync( `https://github.com/electron/electron/tree/v${process.versions.electron}/docs#readme` ) } @@ -91,13 +91,13 @@ const setDefaultApplicationMenu = () => { { label: 'Community Discussions', click () { - shell.openExternal('https://discuss.atom.io/c/electron') + shell.openExternalSync('https://discuss.atom.io/c/electron') } }, { label: 'Search Issues', click () { - shell.openExternal('https://github.com/electron/electron/issues') + shell.openExternalSync('https://github.com/electron/electron/issues') } } ] diff --git a/default_app/renderer.js b/default_app/renderer.js index b971f9fb16e6f..1783fcf067362 100644 --- a/default_app/renderer.js +++ b/default_app/renderer.js @@ -19,7 +19,7 @@ function initialize () { const openLinkExternally = (e) => { e.preventDefault() - shell.openExternal(url) + shell.openExternalSync(url) } link.addEventListener('click', openLinkExternally) diff --git a/docs/api/menu.md b/docs/api/menu.md index 0363ffdb49506..b096d59ad38b3 100644 --- a/docs/api/menu.md +++ b/docs/api/menu.md @@ -198,7 +198,7 @@ const template = [ submenu: [ { label: 'Learn More', - click () { require('electron').shell.openExternal('https://electronjs.org') } + click () { require('electron').shell.openExternalSync('https://electronjs.org') } } ] } diff --git a/docs/api/promisification.md b/docs/api/promisification.md index 921f8e0f393c6..c1794a7e54459 100644 --- a/docs/api/promisification.md +++ b/docs/api/promisification.md @@ -8,7 +8,6 @@ When a majority of affected functions are migrated, this flag will be enabled by ### Candidate Functions -- [ ] [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon) - [ ] [app.importCertificate(options, callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#importCertificate) - [ ] [request.write(chunk[, encoding][, callback])](https://github.com/electron/electron/blob/master/docs/api/client-request.md#write) - [ ] [request.end([chunk][, encoding][, callback])](https://github.com/electron/electron/blob/master/docs/api/client-request.md#end) @@ -41,7 +40,6 @@ When a majority of affected functions are migrated, this flag will be enabled by - [ ] [ses.clearHostResolverCache([callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearHostResolverCache) - [ ] [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData) - [ ] [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache) -- [ ] [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal) - [ ] [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript) - [ ] [contents.getZoomFactor(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#getZoomFactor) - [ ] [contents.getZoomLevel(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#getZoomLevel) @@ -61,4 +59,6 @@ When a majority of affected functions are migrated, this flag will be enabled by - [ ] [win.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/browser-window.md#capturePage) - [ ] [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage) -- [ ] [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage) \ No newline at end of file +- [ ] [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage) +- [ ] [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon) +- [ ] [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal) \ No newline at end of file diff --git a/docs/api/shell.md b/docs/api/shell.md index cf3fbefb59299..e2c6781830af7 100644 --- a/docs/api/shell.md +++ b/docs/api/shell.md @@ -36,7 +36,7 @@ Open the given file in the desktop's default manner. ### `shell.openExternalSync(url[, options])` -* `url` String - Max 2081 characters on windows, or the function returns false. +* `url` String - Max 2081 characters on Windows, or the function returns false. * `options` Object (optional) * `activate` Boolean (optional) - `true` to bring the opened application to the foreground. The default is `true`. _macOS_ @@ -48,13 +48,13 @@ Open the given external protocol URL in the desktop's default manner. (For examp ### `shell.openExternal(url[, options])` -* `url` String - Max 2081 characters on windows, or the function returns false. +* `url` String - Max 2081 characters on windows. * `options` Object (optional) * `activate` Boolean (optional) - `true` to bring the opened application to the foreground. The default is `true`. _macOS_ * `workingDirectory` String (optional) - The working directory. _Windows_ -Returns `Promise` +Returns `Promise` Open the given external protocol URL in the desktop's default manner. (For example, mailto: URLs in the user's default mail agent). diff --git a/docs/api/webview-tag.md b/docs/api/webview-tag.md index ad0afaa27f7d7..bab79f4086b41 100644 --- a/docs/api/webview-tag.md +++ b/docs/api/webview-tag.md @@ -782,7 +782,7 @@ const webview = document.querySelector('webview') webview.addEventListener('new-window', (e) => { const protocol = require('url').parse(e.url).protocol if (protocol === 'http:' || protocol === 'https:') { - shell.openExternal(e.url) + shell.openExternalSync(e.url) } }) ``` diff --git a/docs/tutorial/security.md b/docs/tutorial/security.md index b9e4a0fd28d4c..4a3300ee7f243 100644 --- a/docs/tutorial/security.md +++ b/docs/tutorial/security.md @@ -639,7 +639,7 @@ app.on('web-contents-created', (event, contents) => { // to open this event's url in the default browser. event.preventDefault() - shell.openExternal(navigationUrl) + shell.openExternalSync(navigationUrl) }) }) ``` diff --git a/script/prepare-release.js b/script/prepare-release.js index 5692f6ae26bd2..13aa6e588fec6 100755 --- a/script/prepare-release.js +++ b/script/prepare-release.js @@ -60,7 +60,9 @@ async function getReleaseNotes (currentBranch) { } console.log(`Generating release notes for ${currentBranch}.`) const releaseNotes = await releaseNotesGenerator(currentBranch) - if (releaseNotes.warning) console.warn(releaseNotes.warning) + if (releaseNotes.warning) { + console.warn(releaseNotes.warning) + } return releaseNotes } diff --git a/spec/api-remote-spec.js b/spec/api-remote-spec.js index 77956cf7d18e5..bb87bd72cdbba 100644 --- a/spec/api-remote-spec.js +++ b/spec/api-remote-spec.js @@ -184,7 +184,7 @@ describe('remote module', () => { assert.strictEqual(typeof remote.app.getPath, 'function') assert.strictEqual(typeof remote.webContents.getFocusedWebContents, 'function') assert.strictEqual(typeof remote.clipboard.readText, 'function') - assert.strictEqual(typeof remote.shell.openExternal, 'function') + assert.strictEqual(typeof remote.shell.openExternalSync, 'function') }) it('returns toString() of original function via toString()', () => { diff --git a/spec/api-shell-spec.js b/spec/api-shell-spec.js index 720b75da4a9de..9c01866c80fea 100644 --- a/spec/api-shell-spec.js +++ b/spec/api-shell-spec.js @@ -16,14 +16,55 @@ describe('shell module', () => { iconIndex: 1 } - // (alexeykuzmin): `.skip()` in `before` doesn't work for nested `describe`s. - beforeEach(function () { - if (process.platform !== 'win32') { - this.skip() - } + describe('shell.openExternal()', () => { + let envVars = {} + + beforeEach(() => { + // keep original process.env values to prevent side effect + envVars = { + display: process.env.DISPLAY, + de: process.env.DE, + browser: process.env.BROWSER + } + }) + + afterEach(() => { + process.env.BROWSER = envVars.browser + process.env.DE = envVars.de + process.env.DISPLAY = envVars.display + }) + + it('opens an external link asynchronously', function (done) { + if (process.platform !== 'linux') this.skip() + + // set env vars for xdg-open + const url = 'http://www.example.com' + process.env.DE = 'generic' + process.env.BROWSER = '/bin/true' + process.env.DISPLAY = '' + + shell.openExternal(url).then(() => done()) + }) + + it('opens an external link synchronously', function () { + if (process.platform !== 'linux') this.skip() + + // set env vars for xdg-open + const url = 'http://www.example.com' + process.env.DE = 'generic' + process.env.BROWSER = '/bin/true' + process.env.DISPLAY = '' + + const success = shell.openExternalSync(url) + assert.strictEqual(true, success) + }) }) describe('shell.readShortcutLink(shortcutPath)', () => { + beforeEach(function () { + if (process.platform !== 'win32') this.skip() + }) + it('throws when failed', () => { assert.throws(() => { shell.readShortcutLink('not-exist') @@ -37,6 +78,10 @@ describe('shell module', () => { }) describe('shell.writeShortcutLink(shortcutPath[, operation], options)', () => { + beforeEach(function () { + if (process.platform !== 'win32') this.skip() + }) + const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`) afterEach(() => {