From 57dbf284c176d6a66167fc2fbe9b4714fe65fb32 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 27 Jul 2016 19:43:02 +0900 Subject: [PATCH] spec: shell.writeShortcutLink/readShortcutLink --- spec/api-shell-spec.js | 77 ++++++++++++++++++++++++++++++ spec/fixtures/assets/shortcut.lnk | Bin 0 -> 402 bytes 2 files changed, 77 insertions(+) create mode 100644 spec/api-shell-spec.js create mode 100755 spec/fixtures/assets/shortcut.lnk diff --git a/spec/api-shell-spec.js b/spec/api-shell-spec.js new file mode 100644 index 000000000000..0dc86cee9131 --- /dev/null +++ b/spec/api-shell-spec.js @@ -0,0 +1,77 @@ +const assert = require('assert') +const fs = require('fs') +const path = require('path') +const os = require('os') +const {shell} = require('electron') + +describe('shell module', function() { + if (process.platform !== 'win32') return + + const fixtures = path.resolve(__dirname, 'fixtures') + const shortcutOptions = { + target: 'C:\\target', + description: 'description', + cwd: 'cwd', + args: 'args', + appUserModelId: 'appUserModelId', + icon: 'icon', + iconIndex: 1 + } + + describe('shell.readShortcutLink(shortcutPath)', function () { + it('throws when failed', function () { + assert.throws(function () { + shell.readShortcutLink('not-exist') + }, /Failed to read shortcut link/) + }) + + it('reads all properties of a shortcut', function () { + const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk')) + assert.deepEqual(shortcut, shortcutOptions) + }) + }) + + describe('shell.writeShortcutLink(shortcutPath[, operation], options)', function () { + const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`) + + afterEach(function() { + fs.unlinkSync(tmpShortcut) + }) + + it('writes the shortcut', function () { + assert.equal(shell.writeShortcutLink(tmpShortcut, {target: 'C:\\'}), true) + assert.equal(fs.existsSync(tmpShortcut), true) + }) + + it('correctly sets the fields', function () { + assert.equal(shell.writeShortcutLink(tmpShortcut, shortcutOptions), true) + assert.deepEqual(shell.readShortcutLink(tmpShortcut), shortcutOptions) + }) + + it('updates the shortcut', function () { + assert.equal(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions), false) + assert.equal(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions), true) + assert.deepEqual(shell.readShortcutLink(tmpShortcut), shortcutOptions) + const change = {target: 'D:\\'} + assert.equal(shell.writeShortcutLink(tmpShortcut, 'update', change), true) + assert.deepEqual(shell.readShortcutLink(tmpShortcut), Object.assign(shortcutOptions, change)) + }) + + it('replaces the shortcut', function () { + assert.equal(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions), false) + assert.equal(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions), true) + assert.deepEqual(shell.readShortcutLink(tmpShortcut), shortcutOptions) + const change = { + target: 'D:\\', + description: 'description2', + cwd: 'cwd2', + args: 'args2', + appUserModelId: 'appUserModelId2', + icon: 'icon2', + iconIndex: 2 + } + assert.equal(shell.writeShortcutLink(tmpShortcut, 'replace', change), true) + assert.deepEqual(shell.readShortcutLink(tmpShortcut), change) + }) + }) +}) diff --git a/spec/fixtures/assets/shortcut.lnk b/spec/fixtures/assets/shortcut.lnk new file mode 100755 index 0000000000000000000000000000000000000000..5f325ca733ea337dd986b4544cda6570430e083d GIT binary patch literal 402 zcmeZaU|?VrVFHp23dBqILK=Dk50-$&%Lq09Rwhskq`nkr7AFJ4 z0vRA}7#tA%s4K#4kB^1(?mxnl)n??L0}3xQD i3gm&9ABYoy4k=&=1v)Dg=qz8L1t3S}Fn9uKbN~S9)j9nD literal 0 HcmV?d00001