2019-05-20 17:04:18 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
|
2016-07-27 10:43:02 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const os = require('os')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { shell } = require('electron')
|
2016-07-27 10:43:02 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
const { expect } = chai
|
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('shell module', () => {
|
2016-07-27 10:43:02 +00:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
|
const shortcutOptions = {
|
|
|
|
target: 'C:\\target',
|
|
|
|
description: 'description',
|
|
|
|
cwd: 'cwd',
|
|
|
|
args: 'args',
|
|
|
|
appUserModelId: 'appUserModelId',
|
|
|
|
icon: 'icon',
|
|
|
|
iconIndex: 1
|
|
|
|
}
|
|
|
|
|
2019-01-15 04:35:21 +00:00
|
|
|
describe('shell.openExternal()', () => {
|
|
|
|
let envVars = {}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
envVars = {
|
|
|
|
display: process.env.DISPLAY,
|
|
|
|
de: process.env.DE,
|
|
|
|
browser: process.env.BROWSER
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// reset env vars to prevent side effects
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
process.env.DE = envVars.de
|
|
|
|
process.env.BROWSER = envVars.browser
|
|
|
|
process.env.DISPLAY = envVars.display
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-03 20:53:45 +00:00
|
|
|
it('opens an external link', done => {
|
2019-01-15 04:35:21 +00:00
|
|
|
const url = 'http://www.example.com'
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
process.env.BROWSER = '/bin/true'
|
|
|
|
process.env.DE = 'generic'
|
|
|
|
process.env.DISPLAY = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
shell.openExternal(url).then(() => done())
|
|
|
|
})
|
2017-11-15 21:05:46 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('shell.readShortcutLink(shortcutPath)', () => {
|
2019-01-15 04:35:21 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
if (process.platform !== 'win32') this.skip()
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('throws when failed', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2016-07-27 10:43:02 +00:00
|
|
|
shell.readShortcutLink('not-exist')
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw('Failed to read shortcut link')
|
2016-07-27 10:43:02 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('reads all properties of a shortcut', () => {
|
2016-07-27 10:43:02 +00:00
|
|
|
const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'))
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shortcut).to.deep.equal(shortcutOptions)
|
2016-07-27 10:43:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('shell.writeShortcutLink(shortcutPath[, operation], options)', () => {
|
2019-01-15 04:35:21 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
if (process.platform !== 'win32') this.skip()
|
|
|
|
})
|
|
|
|
|
2016-07-27 10:43:02 +00:00
|
|
|
const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`)
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => {
|
2016-07-27 10:43:02 +00:00
|
|
|
fs.unlinkSync(tmpShortcut)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('writes the shortcut', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, { target: 'C:\\' })).to.be.true()
|
|
|
|
expect(fs.existsSync(tmpShortcut)).to.be.true()
|
2016-07-27 10:43:02 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('correctly sets the fields', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, shortcutOptions)).to.be.true()
|
|
|
|
expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions)
|
2016-07-27 10:43:02 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('updates the shortcut', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions)).to.be.false()
|
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true()
|
|
|
|
expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions)
|
2018-09-13 16:10:51 +00:00
|
|
|
const change = { target: 'D:\\' }
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, 'update', change)).to.be.true()
|
|
|
|
expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(Object.assign(shortcutOptions, change))
|
2016-07-27 10:43:02 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('replaces the shortcut', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions)).to.be.false()
|
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true()
|
|
|
|
expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions)
|
2016-07-27 10:43:02 +00:00
|
|
|
const change = {
|
|
|
|
target: 'D:\\',
|
|
|
|
description: 'description2',
|
|
|
|
cwd: 'cwd2',
|
|
|
|
args: 'args2',
|
|
|
|
appUserModelId: 'appUserModelId2',
|
|
|
|
icon: 'icon2',
|
|
|
|
iconIndex: 2
|
|
|
|
}
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(shell.writeShortcutLink(tmpShortcut, 'replace', change)).to.be.true()
|
|
|
|
expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(change)
|
2016-07-27 10:43:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|