api-shell-spec.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const assert = require('assert')
  2. const fs = require('fs')
  3. const path = require('path')
  4. const os = require('os')
  5. const {shell} = require('electron')
  6. describe('shell module', function () {
  7. if (process.platform !== 'win32') return
  8. const fixtures = path.resolve(__dirname, 'fixtures')
  9. const shortcutOptions = {
  10. target: 'C:\\target',
  11. description: 'description',
  12. cwd: 'cwd',
  13. args: 'args',
  14. appUserModelId: 'appUserModelId',
  15. icon: 'icon',
  16. iconIndex: 1
  17. }
  18. describe('shell.readShortcutLink(shortcutPath)', function () {
  19. it('throws when failed', function () {
  20. assert.throws(function () {
  21. shell.readShortcutLink('not-exist')
  22. }, /Failed to read shortcut link/)
  23. })
  24. it('reads all properties of a shortcut', function () {
  25. const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'))
  26. assert.deepEqual(shortcut, shortcutOptions)
  27. })
  28. })
  29. describe('shell.writeShortcutLink(shortcutPath[, operation], options)', function () {
  30. const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`)
  31. afterEach(function () {
  32. fs.unlinkSync(tmpShortcut)
  33. })
  34. it('writes the shortcut', function () {
  35. assert.equal(shell.writeShortcutLink(tmpShortcut, {target: 'C:\\'}), true)
  36. assert.equal(fs.existsSync(tmpShortcut), true)
  37. })
  38. it('correctly sets the fields', function () {
  39. assert.equal(shell.writeShortcutLink(tmpShortcut, shortcutOptions), true)
  40. assert.deepEqual(shell.readShortcutLink(tmpShortcut), shortcutOptions)
  41. })
  42. it('updates the shortcut', function () {
  43. assert.equal(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions), false)
  44. assert.equal(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions), true)
  45. assert.deepEqual(shell.readShortcutLink(tmpShortcut), shortcutOptions)
  46. const change = {target: 'D:\\'}
  47. assert.equal(shell.writeShortcutLink(tmpShortcut, 'update', change), true)
  48. assert.deepEqual(shell.readShortcutLink(tmpShortcut), Object.assign(shortcutOptions, change))
  49. })
  50. it('replaces the shortcut', function () {
  51. assert.equal(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions), false)
  52. assert.equal(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions), true)
  53. assert.deepEqual(shell.readShortcutLink(tmpShortcut), shortcutOptions)
  54. const change = {
  55. target: 'D:\\',
  56. description: 'description2',
  57. cwd: 'cwd2',
  58. args: 'args2',
  59. appUserModelId: 'appUserModelId2',
  60. icon: 'icon2',
  61. iconIndex: 2
  62. }
  63. assert.equal(shell.writeShortcutLink(tmpShortcut, 'replace', change), true)
  64. assert.deepEqual(shell.readShortcutLink(tmpShortcut), change)
  65. })
  66. })
  67. })