api-shell-spec.js 2.8 KB

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