module-cache-spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const path = require('path');
  2. const Module = require('module');
  3. const fs = require('fs-plus');
  4. const temp = require('temp').track();
  5. const ModuleCache = require('../src/module-cache');
  6. describe('ModuleCache', function() {
  7. beforeEach(() => spyOn(Module, '_findPath').andCallThrough());
  8. afterEach(function() {
  9. try {
  10. temp.cleanupSync();
  11. } catch (error) {}
  12. });
  13. it('resolves Electron module paths without hitting the filesystem', function() {
  14. const { builtins } = ModuleCache.cache;
  15. expect(Object.keys(builtins).length).toBeGreaterThan(0);
  16. for (let builtinName in builtins) {
  17. const builtinPath = builtins[builtinName];
  18. expect(require.resolve(builtinName)).toBe(builtinPath);
  19. expect(fs.isFileSync(require.resolve(builtinName))).toBeTruthy();
  20. }
  21. expect(Module._findPath.callCount).toBe(0);
  22. });
  23. it('resolves relative core paths without hitting the filesystem', function() {
  24. ModuleCache.add(atom.getLoadSettings().resourcePath, {
  25. _atomModuleCache: {
  26. extensions: {
  27. '.json': [path.join('spec', 'fixtures', 'module-cache', 'file.json')]
  28. }
  29. }
  30. });
  31. expect(require('./fixtures/module-cache/file.json').foo).toBe('bar');
  32. expect(Module._findPath.callCount).toBe(0);
  33. });
  34. it('resolves module paths when a compatible version is provided by core', function() {
  35. const packagePath = fs.realpathSync(temp.mkdirSync('atom-package'));
  36. ModuleCache.add(packagePath, {
  37. _atomModuleCache: {
  38. folders: [
  39. {
  40. paths: [''],
  41. dependencies: {
  42. 'underscore-plus': '*'
  43. }
  44. }
  45. ]
  46. }
  47. });
  48. ModuleCache.add(atom.getLoadSettings().resourcePath, {
  49. _atomModuleCache: {
  50. dependencies: [
  51. {
  52. name: 'underscore-plus',
  53. version: require('underscore-plus/package.json').version,
  54. path: path.join(
  55. 'node_modules',
  56. 'underscore-plus',
  57. 'lib',
  58. 'underscore-plus.js'
  59. )
  60. }
  61. ]
  62. }
  63. });
  64. const indexPath = path.join(packagePath, 'index.js');
  65. fs.writeFileSync(
  66. indexPath,
  67. `\
  68. exports.load = function() { require('underscore-plus'); };\
  69. `
  70. );
  71. const packageMain = require(indexPath);
  72. Module._findPath.reset();
  73. packageMain.load();
  74. expect(Module._findPath.callCount).toBe(0);
  75. });
  76. it('does not resolve module paths when no compatible version is provided by core', function() {
  77. const packagePath = fs.realpathSync(temp.mkdirSync('atom-package'));
  78. ModuleCache.add(packagePath, {
  79. _atomModuleCache: {
  80. folders: [
  81. {
  82. paths: [''],
  83. dependencies: {
  84. 'underscore-plus': '0.0.1'
  85. }
  86. }
  87. ]
  88. }
  89. });
  90. ModuleCache.add(atom.getLoadSettings().resourcePath, {
  91. _atomModuleCache: {
  92. dependencies: [
  93. {
  94. name: 'underscore-plus',
  95. version: require('underscore-plus/package.json').version,
  96. path: path.join(
  97. 'node_modules',
  98. 'underscore-plus',
  99. 'lib',
  100. 'underscore-plus.js'
  101. )
  102. }
  103. ]
  104. }
  105. });
  106. const indexPath = path.join(packagePath, 'index.js');
  107. fs.writeFileSync(
  108. indexPath,
  109. `\
  110. exports.load = function() { require('underscore-plus'); };\
  111. `
  112. );
  113. spyOn(process, 'cwd').andReturn('/'); // Required when running this test from CLI
  114. const packageMain = require(indexPath);
  115. Module._findPath.reset();
  116. expect(() => packageMain.load()).toThrow();
  117. expect(Module._findPath.callCount).toBe(1);
  118. });
  119. });