guess.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const assert = require('node:assert/strict')
  2. const { describe, test } = require('node:test')
  3. const guessEditor = require('./guess.js')
  4. const { getEditorFromMacProcesses, getEditorFromWindowsProcesses, getEditorFromLinuxProcesses } =
  5. guessEditor
  6. describe('getEditorFromMacProcesses', () => {
  7. test('maps an exact process path to its CLI command', () => {
  8. const output = [
  9. '/sbin/launchd',
  10. '/Applications/Visual Studio Code.app/Contents/MacOS/Code',
  11. '/usr/sbin/cfprefsd',
  12. ].join('\n')
  13. assert.equal(getEditorFromMacProcesses(output), 'code')
  14. })
  15. test('uses the CLI command for an install outside /Applications', () => {
  16. // `Atom` maps to the short command `atom`, so even when running from a
  17. // custom location we should return that command.
  18. const output = ['/sbin/launchd', '/Users/me/dev/Atom.app/Contents/MacOS/Atom'].join('\n')
  19. assert.equal(getEditorFromMacProcesses(output), 'atom')
  20. })
  21. test('returns the running process path when the map points at itself', () => {
  22. // `CLion` maps to its own full path, so for an install outside
  23. // /Applications we should return the actual running path.
  24. const runningPath = '/Users/me/dev/CLion.app/Contents/MacOS/clion'
  25. const output = ['/sbin/launchd', runningPath].join('\n')
  26. assert.equal(getEditorFromMacProcesses(output), runningPath)
  27. })
  28. test('returns undefined when no editor is running', () => {
  29. const output = ['/sbin/launchd', '/usr/sbin/cfprefsd'].join('\n')
  30. assert.equal(getEditorFromMacProcesses(output), undefined)
  31. })
  32. })
  33. describe('getEditorFromWindowsProcesses', () => {
  34. test('returns the full executable path of a known editor', () => {
  35. const codePath = 'C:\\Program Files\\Microsoft VS Code\\Code.exe'
  36. const output = ['C:\\Windows\\System32\\svchost.exe', codePath].join('\r\n')
  37. assert.equal(getEditorFromWindowsProcesses(output), codePath)
  38. })
  39. test('returns undefined when no editor is running', () => {
  40. const output = ['C:\\Windows\\System32\\svchost.exe', 'C:\\Windows\\explorer.exe'].join('\r\n')
  41. assert.equal(getEditorFromWindowsProcesses(output), undefined)
  42. })
  43. })
  44. describe('getEditorFromLinuxProcesses', () => {
  45. test('maps a process name to its CLI command', () => {
  46. const output = ['systemd', 'code', 'bash'].join('\n')
  47. assert.equal(getEditorFromLinuxProcesses(output), 'code')
  48. })
  49. test('maps sublime_text to subl', () => {
  50. const output = ['systemd', 'sublime_text'].join('\n')
  51. assert.equal(getEditorFromLinuxProcesses(output), 'subl')
  52. })
  53. test('matches code-insiders before code due to key order', () => {
  54. const output = ['systemd', 'code-insiders'].join('\n')
  55. assert.equal(getEditorFromLinuxProcesses(output), 'code-insiders')
  56. })
  57. test('returns undefined when no editor is running', () => {
  58. const output = ['systemd', 'bash', 'sshd'].join('\n')
  59. assert.equal(getEditorFromLinuxProcesses(output), undefined)
  60. })
  61. })