package-manager-spec.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. const path = require('path');
  2. const url = require('url');
  3. const Package = require('../src/package');
  4. const PackageManager = require('../src/package-manager');
  5. const temp = require('temp').track();
  6. const fs = require('fs-plus');
  7. const { Disposable } = require('atom');
  8. const { buildKeydownEvent } = require('../src/keymap-extensions');
  9. const { mockLocalStorage } = require('./spec-helper');
  10. const ModuleCache = require('../src/module-cache');
  11. describe('PackageManager', () => {
  12. function createTestElement(className) {
  13. const element = document.createElement('div');
  14. element.className = className;
  15. return element;
  16. }
  17. beforeEach(() => {
  18. spyOn(ModuleCache, 'add');
  19. });
  20. describe('initialize', () => {
  21. it('adds regular package path', () => {
  22. const packageManger = new PackageManager({});
  23. const configDirPath = path.join('~', 'someConfig');
  24. packageManger.initialize({ configDirPath });
  25. expect(packageManger.packageDirPaths.length).toBe(1);
  26. expect(packageManger.packageDirPaths[0]).toBe(
  27. path.join(configDirPath, 'packages')
  28. );
  29. });
  30. it('adds regular package path, dev package path, and Atom repo package path in dev mode and dev resource path is set', () => {
  31. const packageManger = new PackageManager({});
  32. const configDirPath = path.join('~', 'someConfig');
  33. const resourcePath = path.join('~', '/atom');
  34. packageManger.initialize({ configDirPath, resourcePath, devMode: true });
  35. expect(packageManger.packageDirPaths.length).toBe(3);
  36. expect(packageManger.packageDirPaths).toContain(
  37. path.join(configDirPath, 'packages')
  38. );
  39. expect(packageManger.packageDirPaths).toContain(
  40. path.join(configDirPath, 'dev', 'packages')
  41. );
  42. expect(packageManger.packageDirPaths).toContain(
  43. path.join(resourcePath, 'packages')
  44. );
  45. });
  46. });
  47. describe('::getApmPath()', () => {
  48. /**
  49. * TODO: FAILING TEST - This test fails with the following output:
  50. * Expected '/home/runner/work/pulsar/pulsar/apm/node_modules/ppm/bin/apm' to be
  51. * '/home/runner/work/pulsar/pulsar/node_modules/electron/dist/resources/app/apm/bin/apm'
  52. */
  53. xit('returns the path to the apm command', () => {
  54. let apmPath = path.join(
  55. process.resourcesPath,
  56. 'app',
  57. 'apm',
  58. 'bin',
  59. 'apm'
  60. );
  61. if (process.platform === 'win32') {
  62. apmPath += '.cmd';
  63. }
  64. expect(atom.packages.getApmPath()).toBe(apmPath);
  65. });
  66. describe('when the core.apmPath setting is set', () => {
  67. beforeEach(() => atom.config.set('core.apmPath', '/path/to/apm'));
  68. it('returns the value of the core.apmPath config setting', () => {
  69. expect(atom.packages.getApmPath()).toBe('/path/to/apm');
  70. });
  71. });
  72. });
  73. describe('::loadPackages()', () => {
  74. beforeEach(() => spyOn(atom.packages, 'loadAvailablePackage'));
  75. afterEach(async () => {
  76. await atom.packages.deactivatePackages();
  77. atom.packages.unloadPackages();
  78. });
  79. it('sets hasLoadedInitialPackages', () => {
  80. expect(atom.packages.hasLoadedInitialPackages()).toBe(false);
  81. atom.packages.loadPackages();
  82. expect(atom.packages.hasLoadedInitialPackages()).toBe(true);
  83. });
  84. });
  85. describe('::loadPackage(name)', () => {
  86. beforeEach(() => atom.config.set('core.disabledPackages', []));
  87. it('returns the package', () => {
  88. const pack = atom.packages.loadPackage('package-with-index');
  89. expect(pack instanceof Package).toBe(true);
  90. expect(pack.metadata.name).toBe('package-with-index');
  91. });
  92. it('returns the package if it has an invalid keymap', () => {
  93. spyOn(atom, 'inSpecMode').andReturn(false);
  94. const pack = atom.packages.loadPackage('package-with-broken-keymap');
  95. expect(pack instanceof Package).toBe(true);
  96. expect(pack.metadata.name).toBe('package-with-broken-keymap');
  97. });
  98. it('returns the package if it has an invalid stylesheet', () => {
  99. spyOn(atom, 'inSpecMode').andReturn(false);
  100. const pack = atom.packages.loadPackage('package-with-invalid-styles');
  101. expect(pack instanceof Package).toBe(true);
  102. expect(pack.metadata.name).toBe('package-with-invalid-styles');
  103. expect(pack.stylesheets.length).toBe(0);
  104. const addErrorHandler = jasmine.createSpy();
  105. atom.notifications.onDidAddNotification(addErrorHandler);
  106. expect(() => pack.reloadStylesheets()).not.toThrow();
  107. expect(addErrorHandler.callCount).toBe(2);
  108. expect(addErrorHandler.argsForCall[1][0].message).toContain(
  109. 'Failed to reload the package-with-invalid-styles package stylesheets'
  110. );
  111. expect(addErrorHandler.argsForCall[1][0].options.packageName).toEqual(
  112. 'package-with-invalid-styles'
  113. );
  114. });
  115. it('returns null if the package has an invalid package.json', () => {
  116. spyOn(atom, 'inSpecMode').andReturn(false);
  117. const addErrorHandler = jasmine.createSpy();
  118. atom.notifications.onDidAddNotification(addErrorHandler);
  119. expect(
  120. atom.packages.loadPackage('package-with-broken-package-json')
  121. ).toBeNull();
  122. expect(addErrorHandler.callCount).toBe(1);
  123. expect(addErrorHandler.argsForCall[0][0].message).toContain(
  124. 'Failed to load the package-with-broken-package-json package'
  125. );
  126. expect(addErrorHandler.argsForCall[0][0].options.packageName).toEqual(
  127. 'package-with-broken-package-json'
  128. );
  129. });
  130. it('returns null if the package name or path starts with a dot', () => {
  131. expect(
  132. atom.packages.loadPackage('/Users/user/.atom/packages/.git')
  133. ).toBeNull();
  134. });
  135. it('normalizes short repository urls in package.json', () => {
  136. let { metadata } = atom.packages.loadPackage(
  137. 'package-with-short-url-package-json'
  138. );
  139. expect(metadata.repository.type).toBe('git');
  140. expect(metadata.repository.url).toBe('https://github.com/example/repo');
  141. ({ metadata } = atom.packages.loadPackage(
  142. 'package-with-invalid-url-package-json'
  143. ));
  144. expect(metadata.repository.type).toBe('git');
  145. expect(metadata.repository.url).toBe('foo');
  146. });
  147. it('trims git+ from the beginning and .git from the end of repository URLs, even if npm already normalized them ', () => {
  148. const { metadata } = atom.packages.loadPackage(
  149. 'package-with-prefixed-and-suffixed-repo-url'
  150. );
  151. expect(metadata.repository.type).toBe('git');
  152. expect(metadata.repository.url).toBe('https://github.com/example/repo');
  153. });
  154. it('returns null if the package is not found in any package directory', () => {
  155. spyOn(console, 'warn');
  156. expect(
  157. atom.packages.loadPackage('this-package-cannot-be-found')
  158. ).toBeNull();
  159. expect(console.warn.callCount).toBe(1);
  160. expect(console.warn.argsForCall[0][0]).toContain('Could not resolve');
  161. });
  162. it('invokes ::onDidLoadPackage listeners with the loaded package', () => {
  163. let loadedPackage = null;
  164. atom.packages.onDidLoadPackage(pack => {
  165. loadedPackage = pack;
  166. });
  167. atom.packages.loadPackage('package-with-main');
  168. expect(loadedPackage.name).toBe('package-with-main');
  169. });
  170. it("registers any deserializers specified in the package's package.json", () => {
  171. atom.packages.loadPackage('package-with-deserializers');
  172. const state1 = { deserializer: 'Deserializer1', a: 'b' };
  173. expect(atom.deserializers.deserialize(state1)).toEqual({
  174. wasDeserializedBy: 'deserializeMethod1',
  175. state: state1
  176. });
  177. const state2 = { deserializer: 'Deserializer2', c: 'd' };
  178. expect(atom.deserializers.deserialize(state2)).toEqual({
  179. wasDeserializedBy: 'deserializeMethod2',
  180. state: state2
  181. });
  182. });
  183. it('early-activates any atom.directory-provider or atom.repository-provider services that the package provide', () => {
  184. jasmine.useRealClock();
  185. const providers = [];
  186. atom.packages.serviceHub.consume(
  187. 'atom.directory-provider',
  188. '^0.1.0',
  189. provider => providers.push(provider)
  190. );
  191. atom.packages.loadPackage('package-with-directory-provider');
  192. expect(providers.map(p => p.name)).toEqual([
  193. 'directory provider from package-with-directory-provider'
  194. ]);
  195. });
  196. describe("when there are view providers specified in the package's package.json", () => {
  197. const model1 = { worksWithViewProvider1: true };
  198. const model2 = { worksWithViewProvider2: true };
  199. afterEach(async () => {
  200. await atom.packages.deactivatePackage('package-with-view-providers');
  201. atom.packages.unloadPackage('package-with-view-providers');
  202. });
  203. it('does not load the view providers immediately', () => {
  204. const pack = atom.packages.loadPackage('package-with-view-providers');
  205. expect(pack.mainModule).toBeNull();
  206. expect(() => atom.views.getView(model1)).toThrow();
  207. expect(() => atom.views.getView(model2)).toThrow();
  208. });
  209. it('registers the view providers when the package is activated', async () => {
  210. atom.packages.loadPackage('package-with-view-providers');
  211. await atom.packages.activatePackage('package-with-view-providers');
  212. const element1 = atom.views.getView(model1);
  213. expect(element1 instanceof HTMLDivElement).toBe(true);
  214. expect(element1.dataset.createdBy).toBe('view-provider-1');
  215. const element2 = atom.views.getView(model2);
  216. expect(element2 instanceof HTMLDivElement).toBe(true);
  217. expect(element2.dataset.createdBy).toBe('view-provider-2');
  218. });
  219. it("registers the view providers when any of the package's deserializers are used", () => {
  220. atom.packages.loadPackage('package-with-view-providers');
  221. spyOn(atom.views, 'addViewProvider').andCallThrough();
  222. atom.deserializers.deserialize({
  223. deserializer: 'DeserializerFromPackageWithViewProviders',
  224. a: 'b'
  225. });
  226. expect(atom.views.addViewProvider.callCount).toBe(2);
  227. atom.deserializers.deserialize({
  228. deserializer: 'DeserializerFromPackageWithViewProviders',
  229. a: 'b'
  230. });
  231. expect(atom.views.addViewProvider.callCount).toBe(2);
  232. const element1 = atom.views.getView(model1);
  233. expect(element1 instanceof HTMLDivElement).toBe(true);
  234. expect(element1.dataset.createdBy).toBe('view-provider-1');
  235. const element2 = atom.views.getView(model2);
  236. expect(element2 instanceof HTMLDivElement).toBe(true);
  237. expect(element2.dataset.createdBy).toBe('view-provider-2');
  238. });
  239. });
  240. it("registers the config schema in the package's metadata, if present", () => {
  241. let pack = atom.packages.loadPackage('package-with-json-config-schema');
  242. expect(atom.config.getSchema('package-with-json-config-schema')).toEqual({
  243. type: 'object',
  244. properties: {
  245. a: { type: 'number', default: 5 },
  246. b: { type: 'string', default: 'five' }
  247. }
  248. });
  249. expect(pack.mainModule).toBeNull();
  250. atom.packages.unloadPackage('package-with-json-config-schema');
  251. atom.config.clear();
  252. pack = atom.packages.loadPackage('package-with-json-config-schema');
  253. expect(atom.config.getSchema('package-with-json-config-schema')).toEqual({
  254. type: 'object',
  255. properties: {
  256. a: { type: 'number', default: 5 },
  257. b: { type: 'string', default: 'five' }
  258. }
  259. });
  260. });
  261. describe('when a package does not have deserializers, view providers or a config schema in its package.json', () => {
  262. beforeEach(() => mockLocalStorage());
  263. it("defers loading the package's main module if the package previously used no Atom APIs when its main module was required", () => {
  264. const pack1 = atom.packages.loadPackage('package-with-main');
  265. expect(pack1.mainModule).toBeDefined();
  266. atom.packages.unloadPackage('package-with-main');
  267. const pack2 = atom.packages.loadPackage('package-with-main');
  268. expect(pack2.mainModule).toBeNull();
  269. });
  270. it("does not defer loading the package's main module if the package previously used Atom APIs when its main module was required", () => {
  271. const pack1 = atom.packages.loadPackage(
  272. 'package-with-eval-time-api-calls'
  273. );
  274. expect(pack1.mainModule).toBeDefined();
  275. atom.packages.unloadPackage('package-with-eval-time-api-calls');
  276. const pack2 = atom.packages.loadPackage(
  277. 'package-with-eval-time-api-calls'
  278. );
  279. expect(pack2.mainModule).not.toBeNull();
  280. });
  281. });
  282. });
  283. describe('::loadAvailablePackage(availablePackage)', () => {
  284. describe('if the package was preloaded', () => {
  285. it('adds the package path to the module cache', () => {
  286. const availablePackage = atom.packages
  287. .getAvailablePackages()
  288. .find(p => p.name === 'spell-check');
  289. availablePackage.isBundled = true;
  290. expect(
  291. atom.packages.preloadedPackages[availablePackage.name]
  292. ).toBeUndefined();
  293. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(
  294. false
  295. );
  296. const metadata = atom.packages.loadPackageMetadata(availablePackage);
  297. atom.packages.preloadPackage(availablePackage.name, {
  298. rootDirPath: path.relative(
  299. atom.packages.resourcePath,
  300. availablePackage.path
  301. ),
  302. metadata
  303. });
  304. atom.packages.loadAvailablePackage(availablePackage);
  305. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(true);
  306. expect(ModuleCache.add).toHaveBeenCalledWith(
  307. availablePackage.path,
  308. metadata
  309. );
  310. });
  311. it('deactivates it if it had been disabled', () => {
  312. const availablePackage = atom.packages
  313. .getAvailablePackages()
  314. .find(p => p.name === 'spell-check');
  315. availablePackage.isBundled = true;
  316. expect(
  317. atom.packages.preloadedPackages[availablePackage.name]
  318. ).toBeUndefined();
  319. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(
  320. false
  321. );
  322. const metadata = atom.packages.loadPackageMetadata(availablePackage);
  323. const preloadedPackage = atom.packages.preloadPackage(
  324. availablePackage.name,
  325. {
  326. rootDirPath: path.relative(
  327. atom.packages.resourcePath,
  328. availablePackage.path
  329. ),
  330. metadata
  331. }
  332. );
  333. expect(preloadedPackage.keymapActivated).toBe(true);
  334. expect(preloadedPackage.settingsActivated).toBe(true);
  335. expect(preloadedPackage.menusActivated).toBe(true);
  336. atom.packages.loadAvailablePackage(
  337. availablePackage,
  338. new Set([availablePackage.name])
  339. );
  340. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(
  341. false
  342. );
  343. expect(preloadedPackage.keymapActivated).toBe(false);
  344. expect(preloadedPackage.settingsActivated).toBe(false);
  345. expect(preloadedPackage.menusActivated).toBe(false);
  346. });
  347. it('deactivates it and reloads the new one if trying to load the same package outside of the bundle', () => {
  348. const availablePackage = atom.packages
  349. .getAvailablePackages()
  350. .find(p => p.name === 'spell-check');
  351. availablePackage.isBundled = true;
  352. expect(
  353. atom.packages.preloadedPackages[availablePackage.name]
  354. ).toBeUndefined();
  355. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(
  356. false
  357. );
  358. const metadata = atom.packages.loadPackageMetadata(availablePackage);
  359. const preloadedPackage = atom.packages.preloadPackage(
  360. availablePackage.name,
  361. {
  362. rootDirPath: path.relative(
  363. atom.packages.resourcePath,
  364. availablePackage.path
  365. ),
  366. metadata
  367. }
  368. );
  369. expect(preloadedPackage.keymapActivated).toBe(true);
  370. expect(preloadedPackage.settingsActivated).toBe(true);
  371. expect(preloadedPackage.menusActivated).toBe(true);
  372. availablePackage.isBundled = false;
  373. atom.packages.loadAvailablePackage(availablePackage);
  374. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(true);
  375. expect(preloadedPackage.keymapActivated).toBe(false);
  376. expect(preloadedPackage.settingsActivated).toBe(false);
  377. expect(preloadedPackage.menusActivated).toBe(false);
  378. });
  379. });
  380. describe('if the package was not preloaded', () => {
  381. it('adds the package path to the module cache', () => {
  382. const availablePackage = atom.packages
  383. .getAvailablePackages()
  384. .find(p => p.name === 'spell-check');
  385. availablePackage.isBundled = true;
  386. const metadata = atom.packages.loadPackageMetadata(availablePackage);
  387. atom.packages.loadAvailablePackage(availablePackage);
  388. expect(ModuleCache.add).toHaveBeenCalledWith(
  389. availablePackage.path,
  390. metadata
  391. );
  392. });
  393. });
  394. });
  395. describe('preloading', () => {
  396. it('requires the main module, loads the config schema and activates keymaps, menus and settings without reactivating them during package activation', () => {
  397. const availablePackage = atom.packages
  398. .getAvailablePackages()
  399. .find(p => p.name === 'spell-check');
  400. availablePackage.isBundled = true;
  401. const metadata = atom.packages.loadPackageMetadata(availablePackage);
  402. expect(
  403. atom.packages.preloadedPackages[availablePackage.name]
  404. ).toBeUndefined();
  405. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(false);
  406. atom.packages.packagesCache = {};
  407. atom.packages.packagesCache[availablePackage.name] = {
  408. main: path.join(availablePackage.path, metadata.main),
  409. grammarPaths: []
  410. };
  411. const preloadedPackage = atom.packages.preloadPackage(
  412. availablePackage.name,
  413. {
  414. rootDirPath: path.relative(
  415. atom.packages.resourcePath,
  416. availablePackage.path
  417. ),
  418. metadata
  419. }
  420. );
  421. expect(preloadedPackage.keymapActivated).toBe(true);
  422. expect(preloadedPackage.settingsActivated).toBe(true);
  423. expect(preloadedPackage.menusActivated).toBe(true);
  424. expect(preloadedPackage.mainModule).toBeTruthy();
  425. expect(preloadedPackage.configSchemaRegisteredOnLoad).toBeTruthy();
  426. spyOn(atom.keymaps, 'add');
  427. spyOn(atom.menu, 'add');
  428. spyOn(atom.contextMenu, 'add');
  429. spyOn(atom.config, 'setSchema');
  430. atom.packages.loadAvailablePackage(availablePackage);
  431. expect(preloadedPackage.getMainModulePath()).toBe(
  432. path.join(availablePackage.path, metadata.main)
  433. );
  434. atom.packages.activatePackage(availablePackage.name);
  435. expect(atom.keymaps.add).not.toHaveBeenCalled();
  436. expect(atom.menu.add).not.toHaveBeenCalled();
  437. expect(atom.contextMenu.add).not.toHaveBeenCalled();
  438. expect(atom.config.setSchema).not.toHaveBeenCalled();
  439. expect(preloadedPackage.keymapActivated).toBe(true);
  440. expect(preloadedPackage.settingsActivated).toBe(true);
  441. expect(preloadedPackage.menusActivated).toBe(true);
  442. expect(preloadedPackage.mainModule).toBeTruthy();
  443. expect(preloadedPackage.configSchemaRegisteredOnLoad).toBeTruthy();
  444. });
  445. it('deactivates disabled keymaps during package activation', () => {
  446. const availablePackage = atom.packages
  447. .getAvailablePackages()
  448. .find(p => p.name === 'spell-check');
  449. availablePackage.isBundled = true;
  450. const metadata = atom.packages.loadPackageMetadata(availablePackage);
  451. expect(
  452. atom.packages.preloadedPackages[availablePackage.name]
  453. ).toBeUndefined();
  454. expect(atom.packages.isPackageLoaded(availablePackage.name)).toBe(false);
  455. atom.packages.packagesCache = {};
  456. atom.packages.packagesCache[availablePackage.name] = {
  457. main: path.join(availablePackage.path, metadata.main),
  458. grammarPaths: []
  459. };
  460. const preloadedPackage = atom.packages.preloadPackage(
  461. availablePackage.name,
  462. {
  463. rootDirPath: path.relative(
  464. atom.packages.resourcePath,
  465. availablePackage.path
  466. ),
  467. metadata
  468. }
  469. );
  470. expect(preloadedPackage.keymapActivated).toBe(true);
  471. expect(preloadedPackage.settingsActivated).toBe(true);
  472. expect(preloadedPackage.menusActivated).toBe(true);
  473. atom.packages.loadAvailablePackage(availablePackage);
  474. atom.config.set('core.packagesWithKeymapsDisabled', [
  475. availablePackage.name
  476. ]);
  477. atom.packages.activatePackage(availablePackage.name);
  478. expect(preloadedPackage.keymapActivated).toBe(false);
  479. expect(preloadedPackage.settingsActivated).toBe(true);
  480. expect(preloadedPackage.menusActivated).toBe(true);
  481. });
  482. });
  483. describe('::unloadPackage(name)', () => {
  484. describe('when the package is active', () => {
  485. it('throws an error', async () => {
  486. const pack = await atom.packages.activatePackage('package-with-main');
  487. expect(atom.packages.isPackageLoaded(pack.name)).toBeTruthy();
  488. expect(atom.packages.isPackageActive(pack.name)).toBeTruthy();
  489. expect(() => atom.packages.unloadPackage(pack.name)).toThrow();
  490. expect(atom.packages.isPackageLoaded(pack.name)).toBeTruthy();
  491. expect(atom.packages.isPackageActive(pack.name)).toBeTruthy();
  492. });
  493. });
  494. describe('when the package is not loaded', () => {
  495. it('throws an error', () => {
  496. expect(atom.packages.isPackageLoaded('unloaded')).toBeFalsy();
  497. expect(() => atom.packages.unloadPackage('unloaded')).toThrow();
  498. expect(atom.packages.isPackageLoaded('unloaded')).toBeFalsy();
  499. });
  500. });
  501. describe('when the package is loaded', () => {
  502. it('no longers reports it as being loaded', () => {
  503. const pack = atom.packages.loadPackage('package-with-main');
  504. expect(atom.packages.isPackageLoaded(pack.name)).toBeTruthy();
  505. atom.packages.unloadPackage(pack.name);
  506. expect(atom.packages.isPackageLoaded(pack.name)).toBeFalsy();
  507. });
  508. });
  509. it('invokes ::onDidUnloadPackage listeners with the unloaded package', () => {
  510. atom.packages.loadPackage('package-with-main');
  511. let unloadedPackage;
  512. atom.packages.onDidUnloadPackage(pack => {
  513. unloadedPackage = pack;
  514. });
  515. atom.packages.unloadPackage('package-with-main');
  516. expect(unloadedPackage.name).toBe('package-with-main');
  517. });
  518. });
  519. describe('::activatePackage(id)', () => {
  520. describe('when called multiple times', () => {
  521. it('it only calls activate on the package once', async () => {
  522. spyOn(Package.prototype, 'activateNow').andCallThrough();
  523. await atom.packages.activatePackage('package-with-index');
  524. await atom.packages.activatePackage('package-with-index');
  525. await atom.packages.activatePackage('package-with-index');
  526. expect(Package.prototype.activateNow.callCount).toBe(1);
  527. });
  528. });
  529. describe('when the package has a main module', () => {
  530. beforeEach(() => {
  531. spyOn(Package.prototype, 'requireMainModule').andCallThrough();
  532. });
  533. describe('when the metadata specifies a main module path˜', () => {
  534. it('requires the module at the specified path', async () => {
  535. const mainModule = require('./fixtures/packages/package-with-main/main-module');
  536. spyOn(mainModule, 'activate');
  537. const pack = await atom.packages.activatePackage('package-with-main');
  538. expect(mainModule.activate).toHaveBeenCalled();
  539. expect(pack.mainModule).toBe(mainModule);
  540. });
  541. });
  542. describe('when the metadata does not specify a main module', () => {
  543. it('requires index.coffee', async () => {
  544. const indexModule = require('./fixtures/packages/package-with-index/index');
  545. spyOn(indexModule, 'activate');
  546. const pack = await atom.packages.activatePackage(
  547. 'package-with-index'
  548. );
  549. expect(indexModule.activate).toHaveBeenCalled();
  550. expect(pack.mainModule).toBe(indexModule);
  551. });
  552. });
  553. it('assigns config schema, including defaults when package contains a schema', async () => {
  554. expect(
  555. atom.config.get('package-with-config-schema.numbers.one')
  556. ).toBeUndefined();
  557. await atom.packages.activatePackage('package-with-config-schema');
  558. expect(atom.config.get('package-with-config-schema.numbers.one')).toBe(
  559. 1
  560. );
  561. expect(atom.config.get('package-with-config-schema.numbers.two')).toBe(
  562. 2
  563. );
  564. expect(
  565. atom.config.set('package-with-config-schema.numbers.one', 'nope')
  566. ).toBe(false);
  567. expect(
  568. atom.config.set('package-with-config-schema.numbers.one', '10')
  569. ).toBe(true);
  570. expect(atom.config.get('package-with-config-schema.numbers.one')).toBe(
  571. 10
  572. );
  573. });
  574. describe('when the package metadata includes `activationCommands`', () => {
  575. let mainModule, promise, workspaceCommandListener, registration;
  576. beforeEach(() => {
  577. jasmine.attachToDOM(atom.workspace.getElement());
  578. mainModule = require('./fixtures/packages/package-with-activation-commands/index');
  579. mainModule.activationCommandCallCount = 0;
  580. spyOn(mainModule, 'activate').andCallThrough();
  581. workspaceCommandListener = jasmine.createSpy(
  582. 'workspaceCommandListener'
  583. );
  584. registration = atom.commands.add(
  585. 'atom-workspace',
  586. 'activation-command',
  587. workspaceCommandListener
  588. );
  589. promise = atom.packages.activatePackage(
  590. 'package-with-activation-commands'
  591. );
  592. });
  593. afterEach(() => {
  594. if (registration) {
  595. registration.dispose();
  596. }
  597. mainModule = null;
  598. });
  599. it('defers requiring/activating the main module until an activation event bubbles to the root view', async () => {
  600. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  601. atom.workspace
  602. .getElement()
  603. .dispatchEvent(
  604. new CustomEvent('activation-command', { bubbles: true })
  605. );
  606. await promise;
  607. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  608. });
  609. it('triggers the activation event on all handlers registered during activation', async () => {
  610. await atom.workspace.open();
  611. const editorElement = atom.workspace
  612. .getActiveTextEditor()
  613. .getElement();
  614. const editorCommandListener = jasmine.createSpy(
  615. 'editorCommandListener'
  616. );
  617. atom.commands.add(
  618. 'atom-text-editor',
  619. 'activation-command',
  620. editorCommandListener
  621. );
  622. atom.commands.dispatch(editorElement, 'activation-command');
  623. expect(mainModule.activate.callCount).toBe(1);
  624. expect(mainModule.activationCommandCallCount).toBe(1);
  625. expect(editorCommandListener.callCount).toBe(1);
  626. expect(workspaceCommandListener.callCount).toBe(1);
  627. atom.commands.dispatch(editorElement, 'activation-command');
  628. expect(mainModule.activationCommandCallCount).toBe(2);
  629. expect(editorCommandListener.callCount).toBe(2);
  630. expect(workspaceCommandListener.callCount).toBe(2);
  631. expect(mainModule.activate.callCount).toBe(1);
  632. });
  633. it('activates the package immediately when the events are empty', async () => {
  634. mainModule = require('./fixtures/packages/package-with-empty-activation-commands/index');
  635. spyOn(mainModule, 'activate').andCallThrough();
  636. atom.packages.activatePackage(
  637. 'package-with-empty-activation-commands'
  638. );
  639. expect(mainModule.activate.callCount).toBe(1);
  640. });
  641. it('adds a notification when the activation commands are invalid', () => {
  642. spyOn(atom, 'inSpecMode').andReturn(false);
  643. const addErrorHandler = jasmine.createSpy();
  644. atom.notifications.onDidAddNotification(addErrorHandler);
  645. expect(() =>
  646. atom.packages.activatePackage(
  647. 'package-with-invalid-activation-commands'
  648. )
  649. ).not.toThrow();
  650. expect(addErrorHandler.callCount).toBe(1);
  651. expect(addErrorHandler.argsForCall[0][0].message).toContain(
  652. 'Failed to activate the package-with-invalid-activation-commands package'
  653. );
  654. expect(addErrorHandler.argsForCall[0][0].options.packageName).toEqual(
  655. 'package-with-invalid-activation-commands'
  656. );
  657. });
  658. it('adds a notification when the context menu is invalid', () => {
  659. spyOn(atom, 'inSpecMode').andReturn(false);
  660. const addErrorHandler = jasmine.createSpy();
  661. atom.notifications.onDidAddNotification(addErrorHandler);
  662. expect(() =>
  663. atom.packages.activatePackage('package-with-invalid-context-menu')
  664. ).not.toThrow();
  665. expect(addErrorHandler.callCount).toBe(1);
  666. expect(addErrorHandler.argsForCall[0][0].message).toContain(
  667. 'Failed to activate the package-with-invalid-context-menu package'
  668. );
  669. expect(addErrorHandler.argsForCall[0][0].options.packageName).toEqual(
  670. 'package-with-invalid-context-menu'
  671. );
  672. });
  673. it('adds a notification when the grammar is invalid', async () => {
  674. let notificationEvent;
  675. await new Promise(resolve => {
  676. const subscription = atom.notifications.onDidAddNotification(
  677. event => {
  678. notificationEvent = event;
  679. subscription.dispose();
  680. resolve();
  681. }
  682. );
  683. atom.packages.activatePackage('package-with-invalid-grammar');
  684. });
  685. expect(notificationEvent.message).toContain(
  686. 'Failed to load a package-with-invalid-grammar package grammar'
  687. );
  688. expect(notificationEvent.options.packageName).toEqual(
  689. 'package-with-invalid-grammar'
  690. );
  691. });
  692. it('adds a notification when the settings are invalid', async () => {
  693. let notificationEvent;
  694. await new Promise(resolve => {
  695. const subscription = atom.notifications.onDidAddNotification(
  696. event => {
  697. notificationEvent = event;
  698. subscription.dispose();
  699. resolve();
  700. }
  701. );
  702. atom.packages.activatePackage('package-with-invalid-settings');
  703. });
  704. expect(notificationEvent.message).toContain(
  705. 'Failed to load the package-with-invalid-settings package settings'
  706. );
  707. expect(notificationEvent.options.packageName).toEqual(
  708. 'package-with-invalid-settings'
  709. );
  710. });
  711. });
  712. describe('when the package metadata includes both activation commands and deserializers', () => {
  713. let mainModule, promise, workspaceCommandListener, registration;
  714. beforeEach(() => {
  715. jasmine.attachToDOM(atom.workspace.getElement());
  716. spyOn(atom.packages, 'hasActivatedInitialPackages').andReturn(true);
  717. mainModule = require('./fixtures/packages/package-with-activation-commands-and-deserializers/index');
  718. mainModule.activationCommandCallCount = 0;
  719. spyOn(mainModule, 'activate').andCallThrough();
  720. workspaceCommandListener = jasmine.createSpy(
  721. 'workspaceCommandListener'
  722. );
  723. registration = atom.commands.add(
  724. '.workspace',
  725. 'activation-command-2',
  726. workspaceCommandListener
  727. );
  728. promise = atom.packages.activatePackage(
  729. 'package-with-activation-commands-and-deserializers'
  730. );
  731. });
  732. afterEach(() => {
  733. if (registration) {
  734. registration.dispose();
  735. }
  736. mainModule = null;
  737. });
  738. it('activates the package when a deserializer is called', async () => {
  739. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  740. const state1 = { deserializer: 'Deserializer1', a: 'b' };
  741. expect(atom.deserializers.deserialize(state1, atom)).toEqual({
  742. wasDeserializedBy: 'deserializeMethod1',
  743. state: state1
  744. });
  745. await promise;
  746. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  747. });
  748. it('defers requiring/activating the main module until an activation event bubbles to the root view', async () => {
  749. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  750. atom.workspace
  751. .getElement()
  752. .dispatchEvent(
  753. new CustomEvent('activation-command-2', { bubbles: true })
  754. );
  755. await promise;
  756. expect(mainModule.activate.callCount).toBe(1);
  757. expect(mainModule.activationCommandCallCount).toBe(1);
  758. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  759. });
  760. });
  761. describe('when the package metadata includes `activationHooks`', () => {
  762. let mainModule, promise;
  763. beforeEach(() => {
  764. mainModule = require('./fixtures/packages/package-with-activation-hooks/index');
  765. spyOn(mainModule, 'activate').andCallThrough();
  766. });
  767. it('defers requiring/activating the main module until an triggering of an activation hook occurs', async () => {
  768. promise = atom.packages.activatePackage(
  769. 'package-with-activation-hooks'
  770. );
  771. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  772. atom.packages.triggerActivationHook(
  773. 'language-fictitious:grammar-used'
  774. );
  775. atom.packages.triggerDeferredActivationHooks();
  776. await promise;
  777. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  778. });
  779. it('does not double register activation hooks when deactivating and reactivating', async () => {
  780. promise = atom.packages.activatePackage(
  781. 'package-with-activation-hooks'
  782. );
  783. expect(mainModule.activate.callCount).toBe(0);
  784. atom.packages.triggerActivationHook(
  785. 'language-fictitious:grammar-used'
  786. );
  787. atom.packages.triggerDeferredActivationHooks();
  788. await promise;
  789. expect(mainModule.activate.callCount).toBe(1);
  790. await atom.packages.deactivatePackage(
  791. 'package-with-activation-hooks'
  792. );
  793. promise = atom.packages.activatePackage(
  794. 'package-with-activation-hooks'
  795. );
  796. atom.packages.triggerActivationHook(
  797. 'language-fictitious:grammar-used'
  798. );
  799. atom.packages.triggerDeferredActivationHooks();
  800. await promise;
  801. expect(mainModule.activate.callCount).toBe(2);
  802. });
  803. it('activates the package immediately when activationHooks is empty', async () => {
  804. mainModule = require('./fixtures/packages/package-with-empty-activation-hooks/index');
  805. spyOn(mainModule, 'activate').andCallThrough();
  806. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  807. await atom.packages.activatePackage(
  808. 'package-with-empty-activation-hooks'
  809. );
  810. expect(mainModule.activate.callCount).toBe(1);
  811. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  812. });
  813. it('activates the package immediately if the activation hook had already been triggered', async () => {
  814. atom.packages.triggerActivationHook(
  815. 'language-fictitious:grammar-used'
  816. );
  817. atom.packages.triggerDeferredActivationHooks();
  818. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  819. await atom.packages.activatePackage('package-with-activation-hooks');
  820. expect(mainModule.activate.callCount).toBe(1);
  821. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  822. });
  823. });
  824. describe('when the package metadata includes `workspaceOpeners`', () => {
  825. let mainModule, promise;
  826. beforeEach(() => {
  827. mainModule = require('./fixtures/packages/package-with-workspace-openers/index');
  828. spyOn(mainModule, 'activate').andCallThrough();
  829. });
  830. it('defers requiring/activating the main module until a registered opener is called', async () => {
  831. promise = atom.packages.activatePackage(
  832. 'package-with-workspace-openers'
  833. );
  834. expect(Package.prototype.requireMainModule.callCount).toBe(0);
  835. atom.workspace.open('atom://fictitious');
  836. await promise;
  837. expect(Package.prototype.requireMainModule.callCount).toBe(1);
  838. expect(mainModule.openerCount).toBe(1);
  839. });
  840. it('activates the package immediately when the events are empty', async () => {
  841. mainModule = require('./fixtures/packages/package-with-empty-workspace-openers/index');
  842. spyOn(mainModule, 'activate').andCallThrough();
  843. atom.packages.activatePackage('package-with-empty-workspace-openers');
  844. expect(mainModule.activate.callCount).toBe(1);
  845. });
  846. });
  847. });
  848. describe('when the package has no main module', () => {
  849. it('does not throw an exception', () => {
  850. spyOn(console, 'error');
  851. spyOn(console, 'warn').andCallThrough();
  852. expect(() =>
  853. atom.packages.activatePackage('package-without-module')
  854. ).not.toThrow();
  855. expect(console.error).not.toHaveBeenCalled();
  856. expect(console.warn).not.toHaveBeenCalled();
  857. });
  858. });
  859. describe('when the package does not export an activate function', () => {
  860. it('activates the package and does not throw an exception or log a warning', async () => {
  861. spyOn(console, 'warn');
  862. await atom.packages.activatePackage('package-with-no-activate');
  863. expect(console.warn).not.toHaveBeenCalled();
  864. });
  865. });
  866. it("passes the activate method the package's previously serialized state if it exists", async () => {
  867. const pack = await atom.packages.activatePackage(
  868. 'package-with-serialization'
  869. );
  870. expect(pack.mainModule.someNumber).not.toBe(77);
  871. pack.mainModule.someNumber = 77;
  872. atom.packages.serializePackage('package-with-serialization');
  873. await atom.packages.deactivatePackage('package-with-serialization');
  874. spyOn(pack.mainModule, 'activate').andCallThrough();
  875. await atom.packages.activatePackage('package-with-serialization');
  876. expect(pack.mainModule.activate).toHaveBeenCalledWith({ someNumber: 77 });
  877. });
  878. it('invokes ::onDidActivatePackage listeners with the activated package', async () => {
  879. let activatedPackage;
  880. atom.packages.onDidActivatePackage(pack => {
  881. activatedPackage = pack;
  882. });
  883. await atom.packages.activatePackage('package-with-main');
  884. expect(activatedPackage.name).toBe('package-with-main');
  885. });
  886. describe("when the package's main module throws an error on load", () => {
  887. it('adds a notification instead of throwing an exception', () => {
  888. spyOn(atom, 'inSpecMode').andReturn(false);
  889. atom.config.set('core.disabledPackages', []);
  890. const addErrorHandler = jasmine.createSpy();
  891. atom.notifications.onDidAddNotification(addErrorHandler);
  892. expect(() =>
  893. atom.packages.activatePackage('package-that-throws-an-exception')
  894. ).not.toThrow();
  895. expect(addErrorHandler.callCount).toBe(1);
  896. expect(addErrorHandler.argsForCall[0][0].message).toContain(
  897. 'Failed to load the package-that-throws-an-exception package'
  898. );
  899. expect(addErrorHandler.argsForCall[0][0].options.packageName).toEqual(
  900. 'package-that-throws-an-exception'
  901. );
  902. });
  903. it('re-throws the exception in test mode', () => {
  904. atom.config.set('core.disabledPackages', []);
  905. expect(() =>
  906. atom.packages.activatePackage('package-that-throws-an-exception')
  907. ).toThrow('This package throws an exception');
  908. });
  909. });
  910. describe('when the package is not found', () => {
  911. it('rejects the promise', async () => {
  912. spyOn(console, 'warn');
  913. atom.config.set('core.disabledPackages', []);
  914. try {
  915. await atom.packages.activatePackage('this-doesnt-exist');
  916. expect('Error to be thrown').toBe('');
  917. } catch (error) {
  918. expect(console.warn.callCount).toBe(1);
  919. expect(error.message).toContain(
  920. "Failed to load package 'this-doesnt-exist'"
  921. );
  922. }
  923. });
  924. });
  925. describe('keymap loading', () => {
  926. describe("when the metadata does not contain a 'keymaps' manifest", () => {
  927. it('loads all the .cson/.json files in the keymaps directory', async () => {
  928. const element1 = createTestElement('test-1');
  929. const element2 = createTestElement('test-2');
  930. const element3 = createTestElement('test-3');
  931. expect(
  932. atom.keymaps.findKeyBindings({
  933. keystrokes: 'ctrl-z',
  934. target: element1
  935. })
  936. ).toHaveLength(0);
  937. expect(
  938. atom.keymaps.findKeyBindings({
  939. keystrokes: 'ctrl-z',
  940. target: element2
  941. })
  942. ).toHaveLength(0);
  943. expect(
  944. atom.keymaps.findKeyBindings({
  945. keystrokes: 'ctrl-z',
  946. target: element3
  947. })
  948. ).toHaveLength(0);
  949. await atom.packages.activatePackage('package-with-keymaps');
  950. expect(
  951. atom.keymaps.findKeyBindings({
  952. keystrokes: 'ctrl-z',
  953. target: element1
  954. })[0].command
  955. ).toBe('test-1');
  956. expect(
  957. atom.keymaps.findKeyBindings({
  958. keystrokes: 'ctrl-z',
  959. target: element2
  960. })[0].command
  961. ).toBe('test-2');
  962. expect(
  963. atom.keymaps.findKeyBindings({
  964. keystrokes: 'ctrl-z',
  965. target: element3
  966. })
  967. ).toHaveLength(0);
  968. });
  969. });
  970. describe("when the metadata contains a 'keymaps' manifest", () => {
  971. it('loads only the keymaps specified by the manifest, in the specified order', async () => {
  972. const element1 = createTestElement('test-1');
  973. const element3 = createTestElement('test-3');
  974. expect(
  975. atom.keymaps.findKeyBindings({
  976. keystrokes: 'ctrl-z',
  977. target: element1
  978. })
  979. ).toHaveLength(0);
  980. await atom.packages.activatePackage('package-with-keymaps-manifest');
  981. expect(
  982. atom.keymaps.findKeyBindings({
  983. keystrokes: 'ctrl-z',
  984. target: element1
  985. })[0].command
  986. ).toBe('keymap-1');
  987. expect(
  988. atom.keymaps.findKeyBindings({
  989. keystrokes: 'ctrl-n',
  990. target: element1
  991. })[0].command
  992. ).toBe('keymap-2');
  993. expect(
  994. atom.keymaps.findKeyBindings({
  995. keystrokes: 'ctrl-y',
  996. target: element3
  997. })
  998. ).toHaveLength(0);
  999. });
  1000. });
  1001. describe('when the keymap file is empty', () => {
  1002. it('does not throw an error on activation', async () => {
  1003. await atom.packages.activatePackage('package-with-empty-keymap');
  1004. expect(
  1005. atom.packages.isPackageActive('package-with-empty-keymap')
  1006. ).toBe(true);
  1007. });
  1008. });
  1009. describe("when the package's keymaps have been disabled", () => {
  1010. it('does not add the keymaps', async () => {
  1011. const element1 = createTestElement('test-1');
  1012. expect(
  1013. atom.keymaps.findKeyBindings({
  1014. keystrokes: 'ctrl-z',
  1015. target: element1
  1016. })
  1017. ).toHaveLength(0);
  1018. atom.config.set('core.packagesWithKeymapsDisabled', [
  1019. 'package-with-keymaps-manifest'
  1020. ]);
  1021. await atom.packages.activatePackage('package-with-keymaps-manifest');
  1022. expect(
  1023. atom.keymaps.findKeyBindings({
  1024. keystrokes: 'ctrl-z',
  1025. target: element1
  1026. })
  1027. ).toHaveLength(0);
  1028. });
  1029. });
  1030. describe('when setting core.packagesWithKeymapsDisabled', () => {
  1031. it("ignores package names in the array that aren't loaded", () => {
  1032. atom.packages.observePackagesWithKeymapsDisabled();
  1033. expect(() =>
  1034. atom.config.set('core.packagesWithKeymapsDisabled', [
  1035. 'package-does-not-exist'
  1036. ])
  1037. ).not.toThrow();
  1038. expect(() =>
  1039. atom.config.set('core.packagesWithKeymapsDisabled', [])
  1040. ).not.toThrow();
  1041. });
  1042. });
  1043. describe("when the package's keymaps are disabled and re-enabled after it is activated", () => {
  1044. it('removes and re-adds the keymaps', async () => {
  1045. const element1 = createTestElement('test-1');
  1046. atom.packages.observePackagesWithKeymapsDisabled();
  1047. await atom.packages.activatePackage('package-with-keymaps-manifest');
  1048. atom.config.set('core.packagesWithKeymapsDisabled', [
  1049. 'package-with-keymaps-manifest'
  1050. ]);
  1051. expect(
  1052. atom.keymaps.findKeyBindings({
  1053. keystrokes: 'ctrl-z',
  1054. target: element1
  1055. })
  1056. ).toHaveLength(0);
  1057. atom.config.set('core.packagesWithKeymapsDisabled', []);
  1058. expect(
  1059. atom.keymaps.findKeyBindings({
  1060. keystrokes: 'ctrl-z',
  1061. target: element1
  1062. })[0].command
  1063. ).toBe('keymap-1');
  1064. });
  1065. });
  1066. describe('when the package is de-activated and re-activated', () => {
  1067. let element, events, userKeymapPath;
  1068. beforeEach(() => {
  1069. userKeymapPath = path.join(temp.mkdirSync(), 'user-keymaps.cson');
  1070. spyOn(atom.keymaps, 'getUserKeymapPath').andReturn(userKeymapPath);
  1071. element = createTestElement('test-1');
  1072. jasmine.attachToDOM(element);
  1073. events = [];
  1074. element.addEventListener('user-command', e => events.push(e));
  1075. element.addEventListener('test-1', e => events.push(e));
  1076. });
  1077. afterEach(() => {
  1078. element.remove();
  1079. // Avoid leaking user keymap subscription
  1080. atom.keymaps.watchSubscriptions[userKeymapPath].dispose();
  1081. delete atom.keymaps.watchSubscriptions[userKeymapPath];
  1082. temp.cleanupSync();
  1083. });
  1084. it("doesn't override user-defined keymaps", async () => {
  1085. fs.writeFileSync(
  1086. userKeymapPath,
  1087. `".test-1": {"ctrl-z": "user-command"}`
  1088. );
  1089. atom.keymaps.loadUserKeymap();
  1090. await atom.packages.activatePackage('package-with-keymaps');
  1091. atom.keymaps.handleKeyboardEvent(
  1092. buildKeydownEvent('z', { ctrl: true, target: element })
  1093. );
  1094. expect(events.length).toBe(1);
  1095. expect(events[0].type).toBe('user-command');
  1096. await atom.packages.deactivatePackage('package-with-keymaps');
  1097. await atom.packages.activatePackage('package-with-keymaps');
  1098. atom.keymaps.handleKeyboardEvent(
  1099. buildKeydownEvent('z', { ctrl: true, target: element })
  1100. );
  1101. expect(events.length).toBe(2);
  1102. expect(events[1].type).toBe('user-command');
  1103. });
  1104. });
  1105. });
  1106. describe('menu loading', () => {
  1107. beforeEach(() => {
  1108. atom.contextMenu.definitions = [];
  1109. atom.menu.template = [];
  1110. });
  1111. describe("when the metadata does not contain a 'menus' manifest", () => {
  1112. it('loads all the .cson/.json files in the menus directory', async () => {
  1113. const element = createTestElement('test-1');
  1114. expect(atom.contextMenu.templateForElement(element)).toEqual([]);
  1115. await atom.packages.activatePackage('package-with-menus');
  1116. expect(atom.menu.template.length).toBe(2);
  1117. expect(atom.menu.template[0].label).toBe('Second to Last');
  1118. expect(atom.menu.template[1].label).toBe('Last');
  1119. expect(atom.contextMenu.templateForElement(element)[0].label).toBe(
  1120. 'Menu item 1'
  1121. );
  1122. expect(atom.contextMenu.templateForElement(element)[1].label).toBe(
  1123. 'Menu item 2'
  1124. );
  1125. expect(atom.contextMenu.templateForElement(element)[2].label).toBe(
  1126. 'Menu item 3'
  1127. );
  1128. });
  1129. });
  1130. describe("when the metadata contains a 'menus' manifest", () => {
  1131. it('loads only the menus specified by the manifest, in the specified order', async () => {
  1132. const element = createTestElement('test-1');
  1133. expect(atom.contextMenu.templateForElement(element)).toEqual([]);
  1134. await atom.packages.activatePackage('package-with-menus-manifest');
  1135. expect(atom.menu.template[0].label).toBe('Second to Last');
  1136. expect(atom.menu.template[1].label).toBe('Last');
  1137. expect(atom.contextMenu.templateForElement(element)[0].label).toBe(
  1138. 'Menu item 2'
  1139. );
  1140. expect(atom.contextMenu.templateForElement(element)[1].label).toBe(
  1141. 'Menu item 1'
  1142. );
  1143. expect(
  1144. atom.contextMenu.templateForElement(element)[2]
  1145. ).toBeUndefined();
  1146. });
  1147. });
  1148. describe('when the menu file is empty', () => {
  1149. it('does not throw an error on activation', async () => {
  1150. await atom.packages.activatePackage('package-with-empty-menu');
  1151. expect(atom.packages.isPackageActive('package-with-empty-menu')).toBe(
  1152. true
  1153. );
  1154. });
  1155. });
  1156. });
  1157. describe('stylesheet loading', () => {
  1158. describe("when the metadata contains a 'styleSheets' manifest", () => {
  1159. it('loads style sheets from the styles directory as specified by the manifest', async () => {
  1160. const one = require.resolve(
  1161. './fixtures/packages/package-with-style-sheets-manifest/styles/1.css'
  1162. );
  1163. const two = require.resolve(
  1164. './fixtures/packages/package-with-style-sheets-manifest/styles/2.less'
  1165. );
  1166. const three = require.resolve(
  1167. './fixtures/packages/package-with-style-sheets-manifest/styles/3.css'
  1168. );
  1169. expect(atom.themes.stylesheetElementForId(one)).toBeNull();
  1170. expect(atom.themes.stylesheetElementForId(two)).toBeNull();
  1171. expect(atom.themes.stylesheetElementForId(three)).toBeNull();
  1172. await atom.packages.activatePackage(
  1173. 'package-with-style-sheets-manifest'
  1174. );
  1175. expect(atom.themes.stylesheetElementForId(one)).not.toBeNull();
  1176. expect(atom.themes.stylesheetElementForId(two)).not.toBeNull();
  1177. expect(atom.themes.stylesheetElementForId(three)).toBeNull();
  1178. expect(
  1179. getComputedStyle(document.querySelector('#jasmine-content'))
  1180. .fontSize
  1181. ).toBe('1px');
  1182. });
  1183. });
  1184. describe("when the metadata does not contain a 'styleSheets' manifest", () => {
  1185. it('loads all style sheets from the styles directory', async () => {
  1186. const one = require.resolve(
  1187. './fixtures/packages/package-with-styles/styles/1.css'
  1188. );
  1189. const two = require.resolve(
  1190. './fixtures/packages/package-with-styles/styles/2.less'
  1191. );
  1192. const three = require.resolve(
  1193. './fixtures/packages/package-with-styles/styles/3.test-context.css'
  1194. );
  1195. const four = require.resolve(
  1196. './fixtures/packages/package-with-styles/styles/4.css'
  1197. );
  1198. expect(atom.themes.stylesheetElementForId(one)).toBeNull();
  1199. expect(atom.themes.stylesheetElementForId(two)).toBeNull();
  1200. expect(atom.themes.stylesheetElementForId(three)).toBeNull();
  1201. expect(atom.themes.stylesheetElementForId(four)).toBeNull();
  1202. await atom.packages.activatePackage('package-with-styles');
  1203. expect(atom.themes.stylesheetElementForId(one)).not.toBeNull();
  1204. expect(atom.themes.stylesheetElementForId(two)).not.toBeNull();
  1205. expect(atom.themes.stylesheetElementForId(three)).not.toBeNull();
  1206. expect(atom.themes.stylesheetElementForId(four)).not.toBeNull();
  1207. expect(
  1208. getComputedStyle(document.querySelector('#jasmine-content'))
  1209. .fontSize
  1210. ).toBe('3px');
  1211. });
  1212. });
  1213. it("assigns the stylesheet's context based on the filename", async () => {
  1214. await atom.packages.activatePackage('package-with-styles');
  1215. let count = 0;
  1216. for (let styleElement of atom.styles.getStyleElements()) {
  1217. if (styleElement.sourcePath.match(/1.css/)) {
  1218. expect(styleElement.context).toBe(undefined);
  1219. count++;
  1220. }
  1221. if (styleElement.sourcePath.match(/2.less/)) {
  1222. expect(styleElement.context).toBe(undefined);
  1223. count++;
  1224. }
  1225. if (styleElement.sourcePath.match(/3.test-context.css/)) {
  1226. expect(styleElement.context).toBe('test-context');
  1227. count++;
  1228. }
  1229. if (styleElement.sourcePath.match(/4.css/)) {
  1230. expect(styleElement.context).toBe(undefined);
  1231. count++;
  1232. }
  1233. }
  1234. expect(count).toBe(4);
  1235. });
  1236. });
  1237. describe('grammar loading', () => {
  1238. it("loads the package's grammars", async () => {
  1239. await atom.packages.activatePackage('package-with-grammars');
  1240. expect(atom.grammars.selectGrammar('a.alot').name).toBe('Alot');
  1241. expect(atom.grammars.selectGrammar('a.alittle').name).toBe('Alittle');
  1242. });
  1243. it('loads any tree-sitter grammars defined in the package', async () => {
  1244. atom.config.set('core.useTreeSitterParsers', true);
  1245. await atom.packages.activatePackage('package-with-tree-sitter-grammar');
  1246. const grammar = atom.grammars.selectGrammar('test.somelang');
  1247. expect(grammar.name).toBe('Some Language');
  1248. await grammar.getQuery('highlightsQuery');
  1249. expect(grammar.highlightsQuery.includes('(empty)')).toBe(true);
  1250. });
  1251. });
  1252. describe('scoped-property loading', () => {
  1253. it('loads the scoped properties', async () => {
  1254. await atom.packages.activatePackage('package-with-settings');
  1255. expect(
  1256. atom.config.get('editor.increaseIndentPattern', {
  1257. scope: ['.source.omg']
  1258. })
  1259. ).toBe('^a');
  1260. });
  1261. });
  1262. describe('URI handler registration', () => {
  1263. it("registers the package's specified URI handler", async () => {
  1264. const uri = 'atom://package-with-uri-handler/some/url?with=args';
  1265. const mod = require('./fixtures/packages/package-with-uri-handler');
  1266. spyOn(mod, 'handleURI');
  1267. spyOn(atom.packages, 'hasLoadedInitialPackages').andReturn(true);
  1268. const activationPromise = atom.packages.activatePackage(
  1269. 'package-with-uri-handler'
  1270. );
  1271. atom.dispatchURIMessage(uri);
  1272. await activationPromise;
  1273. expect(mod.handleURI).toHaveBeenCalledWith(url.parse(uri, true), uri);
  1274. });
  1275. });
  1276. describe('service registration', () => {
  1277. it("registers the package's provided and consumed services", async () => {
  1278. const consumerModule = require('./fixtures/packages/package-with-consumed-services');
  1279. let firstServiceV3Disposed = false;
  1280. let firstServiceV4Disposed = false;
  1281. let secondServiceDisposed = false;
  1282. spyOn(consumerModule, 'consumeFirstServiceV3').andReturn(
  1283. new Disposable(() => {
  1284. firstServiceV3Disposed = true;
  1285. })
  1286. );
  1287. spyOn(consumerModule, 'consumeFirstServiceV4').andReturn(
  1288. new Disposable(() => {
  1289. firstServiceV4Disposed = true;
  1290. })
  1291. );
  1292. spyOn(consumerModule, 'consumeSecondService').andReturn(
  1293. new Disposable(() => {
  1294. secondServiceDisposed = true;
  1295. })
  1296. );
  1297. await atom.packages.activatePackage('package-with-consumed-services');
  1298. await atom.packages.activatePackage('package-with-provided-services');
  1299. expect(consumerModule.consumeFirstServiceV3.callCount).toBe(1);
  1300. expect(consumerModule.consumeFirstServiceV3).toHaveBeenCalledWith(
  1301. 'first-service-v3'
  1302. );
  1303. expect(consumerModule.consumeFirstServiceV4).toHaveBeenCalledWith(
  1304. 'first-service-v4'
  1305. );
  1306. expect(consumerModule.consumeSecondService).toHaveBeenCalledWith(
  1307. 'second-service'
  1308. );
  1309. consumerModule.consumeFirstServiceV3.reset();
  1310. consumerModule.consumeFirstServiceV4.reset();
  1311. consumerModule.consumeSecondService.reset();
  1312. await atom.packages.deactivatePackage('package-with-provided-services');
  1313. expect(firstServiceV3Disposed).toBe(true);
  1314. expect(firstServiceV4Disposed).toBe(true);
  1315. expect(secondServiceDisposed).toBe(true);
  1316. await atom.packages.deactivatePackage('package-with-consumed-services');
  1317. await atom.packages.activatePackage('package-with-provided-services');
  1318. expect(consumerModule.consumeFirstServiceV3).not.toHaveBeenCalled();
  1319. expect(consumerModule.consumeFirstServiceV4).not.toHaveBeenCalled();
  1320. expect(consumerModule.consumeSecondService).not.toHaveBeenCalled();
  1321. });
  1322. it('ignores provided and consumed services that do not exist', async () => {
  1323. const addErrorHandler = jasmine.createSpy();
  1324. atom.notifications.onDidAddNotification(addErrorHandler);
  1325. await atom.packages.activatePackage(
  1326. 'package-with-missing-consumed-services'
  1327. );
  1328. await atom.packages.activatePackage(
  1329. 'package-with-missing-provided-services'
  1330. );
  1331. expect(
  1332. atom.packages.isPackageActive(
  1333. 'package-with-missing-consumed-services'
  1334. )
  1335. ).toBe(true);
  1336. expect(
  1337. atom.packages.isPackageActive(
  1338. 'package-with-missing-provided-services'
  1339. )
  1340. ).toBe(true);
  1341. expect(addErrorHandler.callCount).toBe(0);
  1342. });
  1343. });
  1344. });
  1345. describe('::serialize', () => {
  1346. it('does not serialize packages that threw an error during activation', async () => {
  1347. spyOn(atom, 'inSpecMode').andReturn(false);
  1348. spyOn(console, 'warn');
  1349. const badPack = await atom.packages.activatePackage(
  1350. 'package-that-throws-on-activate'
  1351. );
  1352. spyOn(badPack.mainModule, 'serialize').andCallThrough();
  1353. atom.packages.serialize();
  1354. expect(badPack.mainModule.serialize).not.toHaveBeenCalled();
  1355. });
  1356. it("absorbs exceptions that are thrown by the package module's serialize method", async () => {
  1357. spyOn(console, 'error');
  1358. await atom.packages.activatePackage('package-with-serialize-error');
  1359. await atom.packages.activatePackage('package-with-serialization');
  1360. atom.packages.serialize();
  1361. expect(
  1362. atom.packages.packageStates['package-with-serialize-error']
  1363. ).toBeUndefined();
  1364. expect(atom.packages.packageStates['package-with-serialization']).toEqual(
  1365. { someNumber: 1 }
  1366. );
  1367. expect(console.error).toHaveBeenCalled();
  1368. });
  1369. });
  1370. describe('::deactivatePackages()', () => {
  1371. it('deactivates all packages but does not serialize them', async () => {
  1372. const pack1 = await atom.packages.activatePackage(
  1373. 'package-with-deactivate'
  1374. );
  1375. const pack2 = await atom.packages.activatePackage(
  1376. 'package-with-serialization'
  1377. );
  1378. spyOn(pack1.mainModule, 'deactivate');
  1379. spyOn(pack2.mainModule, 'serialize');
  1380. await atom.packages.deactivatePackages();
  1381. expect(pack1.mainModule.deactivate).toHaveBeenCalled();
  1382. expect(pack2.mainModule.serialize).not.toHaveBeenCalled();
  1383. });
  1384. });
  1385. describe('::deactivatePackage(id)', () => {
  1386. afterEach(() => atom.packages.unloadPackages());
  1387. it("calls `deactivate` on the package's main module if activate was successful", async () => {
  1388. spyOn(atom, 'inSpecMode').andReturn(false);
  1389. const pack = await atom.packages.activatePackage(
  1390. 'package-with-deactivate'
  1391. );
  1392. expect(
  1393. atom.packages.isPackageActive('package-with-deactivate')
  1394. ).toBeTruthy();
  1395. spyOn(pack.mainModule, 'deactivate').andCallThrough();
  1396. await atom.packages.deactivatePackage('package-with-deactivate');
  1397. expect(pack.mainModule.deactivate).toHaveBeenCalled();
  1398. expect(atom.packages.isPackageActive('package-with-module')).toBeFalsy();
  1399. spyOn(console, 'warn');
  1400. const badPack = await atom.packages.activatePackage(
  1401. 'package-that-throws-on-activate'
  1402. );
  1403. expect(
  1404. atom.packages.isPackageActive('package-that-throws-on-activate')
  1405. ).toBeTruthy();
  1406. spyOn(badPack.mainModule, 'deactivate').andCallThrough();
  1407. await atom.packages.deactivatePackage('package-that-throws-on-activate');
  1408. expect(badPack.mainModule.deactivate).not.toHaveBeenCalled();
  1409. expect(
  1410. atom.packages.isPackageActive('package-that-throws-on-activate')
  1411. ).toBeFalsy();
  1412. });
  1413. it("absorbs exceptions that are thrown by the package module's deactivate method", async () => {
  1414. spyOn(console, 'error');
  1415. await atom.packages.activatePackage('package-that-throws-on-deactivate');
  1416. await atom.packages.deactivatePackage(
  1417. 'package-that-throws-on-deactivate'
  1418. );
  1419. expect(console.error).toHaveBeenCalled();
  1420. });
  1421. it("removes the package's grammars", async () => {
  1422. await atom.packages.activatePackage('package-with-grammars');
  1423. await atom.packages.deactivatePackage('package-with-grammars');
  1424. expect(atom.grammars.selectGrammar('a.alot').name).toBe('Null Grammar');
  1425. expect(atom.grammars.selectGrammar('a.alittle').name).toBe(
  1426. 'Null Grammar'
  1427. );
  1428. });
  1429. it("removes the package's keymaps", async () => {
  1430. await atom.packages.activatePackage('package-with-keymaps');
  1431. await atom.packages.deactivatePackage('package-with-keymaps');
  1432. expect(
  1433. atom.keymaps.findKeyBindings({
  1434. keystrokes: 'ctrl-z',
  1435. target: createTestElement('test-1')
  1436. })
  1437. ).toHaveLength(0);
  1438. expect(
  1439. atom.keymaps.findKeyBindings({
  1440. keystrokes: 'ctrl-z',
  1441. target: createTestElement('test-2')
  1442. })
  1443. ).toHaveLength(0);
  1444. });
  1445. it("removes the package's stylesheets", async () => {
  1446. await atom.packages.activatePackage('package-with-styles');
  1447. await atom.packages.deactivatePackage('package-with-styles');
  1448. const one = require.resolve(
  1449. './fixtures/packages/package-with-style-sheets-manifest/styles/1.css'
  1450. );
  1451. const two = require.resolve(
  1452. './fixtures/packages/package-with-style-sheets-manifest/styles/2.less'
  1453. );
  1454. const three = require.resolve(
  1455. './fixtures/packages/package-with-style-sheets-manifest/styles/3.css'
  1456. );
  1457. expect(atom.themes.stylesheetElementForId(one)).not.toExist();
  1458. expect(atom.themes.stylesheetElementForId(two)).not.toExist();
  1459. expect(atom.themes.stylesheetElementForId(three)).not.toExist();
  1460. });
  1461. it("removes the package's scoped-properties", async () => {
  1462. await atom.packages.activatePackage('package-with-settings');
  1463. expect(
  1464. atom.config.get('editor.increaseIndentPattern', {
  1465. scope: ['.source.omg']
  1466. })
  1467. ).toBe('^a');
  1468. await atom.packages.deactivatePackage('package-with-settings');
  1469. expect(
  1470. atom.config.get('editor.increaseIndentPattern', {
  1471. scope: ['.source.omg']
  1472. })
  1473. ).toBeUndefined();
  1474. });
  1475. it('invokes ::onDidDeactivatePackage listeners with the deactivated package', async () => {
  1476. await atom.packages.activatePackage('package-with-main');
  1477. let deactivatedPackage;
  1478. atom.packages.onDidDeactivatePackage(pack => {
  1479. deactivatedPackage = pack;
  1480. });
  1481. await atom.packages.deactivatePackage('package-with-main');
  1482. expect(deactivatedPackage.name).toBe('package-with-main');
  1483. });
  1484. });
  1485. describe('::activate()', () => {
  1486. beforeEach(() => {
  1487. spyOn(atom, 'inSpecMode').andReturn(false);
  1488. jasmine.snapshotDeprecations();
  1489. spyOn(console, 'warn');
  1490. atom.packages.loadPackages();
  1491. const loadedPackages = atom.packages.getLoadedPackages();
  1492. expect(loadedPackages.length).toBeGreaterThan(0);
  1493. });
  1494. afterEach(async () => {
  1495. await atom.packages.deactivatePackages();
  1496. atom.packages.unloadPackages();
  1497. jasmine.restoreDeprecationsSnapshot();
  1498. });
  1499. it('sets hasActivatedInitialPackages', async () => {
  1500. spyOn(atom.styles, 'getUserStyleSheetPath').andReturn(null);
  1501. spyOn(atom.packages, 'activatePackages');
  1502. expect(atom.packages.hasActivatedInitialPackages()).toBe(false);
  1503. await atom.packages.activate();
  1504. expect(atom.packages.hasActivatedInitialPackages()).toBe(true);
  1505. });
  1506. it('activates all the packages, and none of the themes', () => {
  1507. const packageActivator = spyOn(atom.packages, 'activatePackages');
  1508. const themeActivator = spyOn(atom.themes, 'activatePackages');
  1509. atom.packages.activate();
  1510. expect(packageActivator).toHaveBeenCalled();
  1511. expect(themeActivator).toHaveBeenCalled();
  1512. const packages = packageActivator.mostRecentCall.args[0];
  1513. for (let pack of packages) {
  1514. expect(['atom', 'textmate']).toContain(pack.getType());
  1515. }
  1516. const themes = themeActivator.mostRecentCall.args[0];
  1517. themes.map(theme => expect(['theme']).toContain(theme.getType()));
  1518. });
  1519. it('calls callbacks registered with ::onDidActivateInitialPackages', async () => {
  1520. const package1 = atom.packages.loadPackage('package-with-main');
  1521. const package2 = atom.packages.loadPackage('package-with-index');
  1522. const package3 = atom.packages.loadPackage(
  1523. 'package-with-activation-commands'
  1524. );
  1525. spyOn(atom.packages, 'getLoadedPackages').andReturn([
  1526. package1,
  1527. package2,
  1528. package3
  1529. ]);
  1530. spyOn(atom.themes, 'activatePackages');
  1531. atom.packages.activate();
  1532. await new Promise(resolve =>
  1533. atom.packages.onDidActivateInitialPackages(resolve)
  1534. );
  1535. jasmine.unspy(atom.packages, 'getLoadedPackages');
  1536. expect(atom.packages.getActivePackages().includes(package1)).toBe(true);
  1537. expect(atom.packages.getActivePackages().includes(package2)).toBe(true);
  1538. expect(atom.packages.getActivePackages().includes(package3)).toBe(false);
  1539. });
  1540. });
  1541. describe('::enablePackage(id) and ::disablePackage(id)', () => {
  1542. describe('with packages', () => {
  1543. it('enables a disabled package', async () => {
  1544. const packageName = 'package-with-main';
  1545. atom.config.pushAtKeyPath('core.disabledPackages', packageName);
  1546. atom.packages.observeDisabledPackages();
  1547. expect(atom.config.get('core.disabledPackages')).toContain(packageName);
  1548. const pack = atom.packages.enablePackage(packageName);
  1549. await new Promise(resolve =>
  1550. atom.packages.onDidActivatePackage(resolve)
  1551. );
  1552. expect(atom.packages.getLoadedPackages()).toContain(pack);
  1553. expect(atom.packages.getActivePackages()).toContain(pack);
  1554. expect(atom.config.get('core.disabledPackages')).not.toContain(
  1555. packageName
  1556. );
  1557. });
  1558. it('disables an enabled package', async () => {
  1559. const packageName = 'package-with-main';
  1560. const pack = await atom.packages.activatePackage(packageName);
  1561. atom.packages.observeDisabledPackages();
  1562. expect(atom.config.get('core.disabledPackages')).not.toContain(
  1563. packageName
  1564. );
  1565. await new Promise(resolve => {
  1566. atom.packages.onDidDeactivatePackage(resolve);
  1567. atom.packages.disablePackage(packageName);
  1568. });
  1569. expect(atom.packages.getActivePackages()).not.toContain(pack);
  1570. expect(atom.config.get('core.disabledPackages')).toContain(packageName);
  1571. });
  1572. it('returns null if the package cannot be loaded', () => {
  1573. spyOn(console, 'warn');
  1574. expect(atom.packages.enablePackage('this-doesnt-exist')).toBeNull();
  1575. expect(console.warn.callCount).toBe(1);
  1576. });
  1577. it('does not disable an already disabled package', () => {
  1578. const packageName = 'package-with-main';
  1579. atom.config.pushAtKeyPath('core.disabledPackages', packageName);
  1580. atom.packages.observeDisabledPackages();
  1581. expect(atom.config.get('core.disabledPackages')).toContain(packageName);
  1582. atom.packages.disablePackage(packageName);
  1583. const packagesDisabled = atom.config
  1584. .get('core.disabledPackages')
  1585. .filter(pack => pack === packageName);
  1586. expect(packagesDisabled.length).toEqual(1);
  1587. });
  1588. });
  1589. describe('with themes', () => {
  1590. beforeEach(() => atom.themes.activateThemes());
  1591. afterEach(() => atom.themes.deactivateThemes());
  1592. it('enables and disables a theme', async () => {
  1593. const packageName = 'theme-with-package-file';
  1594. expect(atom.config.get('core.themes')).not.toContain(packageName);
  1595. expect(atom.config.get('core.disabledPackages')).not.toContain(
  1596. packageName
  1597. );
  1598. // enabling of theme
  1599. const pack = atom.packages.enablePackage(packageName);
  1600. await new Promise(resolve =>
  1601. atom.packages.onDidActivatePackage(resolve)
  1602. );
  1603. expect(atom.packages.isPackageActive(packageName)).toBe(true);
  1604. expect(atom.config.get('core.themes')).toContain(packageName);
  1605. expect(atom.config.get('core.disabledPackages')).not.toContain(
  1606. packageName
  1607. );
  1608. await new Promise(resolve => {
  1609. atom.themes.onDidChangeActiveThemes(resolve);
  1610. atom.packages.disablePackage(packageName);
  1611. });
  1612. expect(atom.packages.getActivePackages()).not.toContain(pack);
  1613. expect(atom.config.get('core.themes')).not.toContain(packageName);
  1614. expect(atom.config.get('core.themes')).not.toContain(packageName);
  1615. expect(atom.config.get('core.disabledPackages')).not.toContain(
  1616. packageName
  1617. );
  1618. });
  1619. });
  1620. });
  1621. describe('::getAvailablePackageNames', () => {
  1622. it('detects a symlinked package', () => {
  1623. const packageSymLinkedSource = path.join(
  1624. __dirname,
  1625. 'fixtures',
  1626. 'packages',
  1627. 'folder',
  1628. 'package-symlinked'
  1629. );
  1630. const destination = path.join(
  1631. atom.packages.getPackageDirPaths()[0],
  1632. 'package-symlinked'
  1633. );
  1634. if (!fs.isDirectorySync(destination)) {
  1635. fs.symlinkSync(packageSymLinkedSource, destination, 'junction');
  1636. }
  1637. const availablePackages = atom.packages.getAvailablePackageNames();
  1638. expect(availablePackages.includes('package-symlinked')).toBe(true);
  1639. fs.removeSync(destination);
  1640. });
  1641. });
  1642. });