api-shell-spec.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const { expect } = require('chai')
  2. const fs = require('fs')
  3. const path = require('path')
  4. const os = require('os')
  5. const http = require('http')
  6. const { shell } = require('electron')
  7. describe('shell module', () => {
  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)', () => {
  19. beforeEach(function () {
  20. if (process.platform !== 'win32') this.skip()
  21. })
  22. it('throws when failed', () => {
  23. expect(() => {
  24. shell.readShortcutLink('not-exist')
  25. }).to.throw('Failed to read shortcut link')
  26. })
  27. it('reads all properties of a shortcut', () => {
  28. const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'))
  29. expect(shortcut).to.deep.equal(shortcutOptions)
  30. })
  31. })
  32. describe('shell.writeShortcutLink(shortcutPath[, operation], options)', () => {
  33. beforeEach(function () {
  34. if (process.platform !== 'win32') this.skip()
  35. })
  36. const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`)
  37. afterEach(() => {
  38. fs.unlinkSync(tmpShortcut)
  39. })
  40. it('writes the shortcut', () => {
  41. expect(shell.writeShortcutLink(tmpShortcut, { target: 'C:\\' })).to.be.true()
  42. expect(fs.existsSync(tmpShortcut)).to.be.true()
  43. })
  44. it('correctly sets the fields', () => {
  45. expect(shell.writeShortcutLink(tmpShortcut, shortcutOptions)).to.be.true()
  46. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions)
  47. })
  48. it('updates the shortcut', () => {
  49. expect(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions)).to.be.false()
  50. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true()
  51. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions)
  52. const change = { target: 'D:\\' }
  53. expect(shell.writeShortcutLink(tmpShortcut, 'update', change)).to.be.true()
  54. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(Object.assign(shortcutOptions, change))
  55. })
  56. it('replaces the shortcut', () => {
  57. expect(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions)).to.be.false()
  58. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true()
  59. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions)
  60. const change = {
  61. target: 'D:\\',
  62. description: 'description2',
  63. cwd: 'cwd2',
  64. args: 'args2',
  65. appUserModelId: 'appUserModelId2',
  66. icon: 'icon2',
  67. iconIndex: 2
  68. }
  69. expect(shell.writeShortcutLink(tmpShortcut, 'replace', change)).to.be.true()
  70. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(change)
  71. })
  72. })
  73. })