compile-cache-spec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * EDITOR NOTE: Manually added arrow return function syntax to match other tests.
  3. * decaffeinate suggestions:
  4. * DS101: Remove unnecessary use of Array.from
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  7. */
  8. const path = require('path');
  9. const temp = require('temp').track();
  10. const babelCompiler = require('../src/babel');
  11. const CoffeeScript = require('coffeescript');
  12. const {TypeScriptSimple} = require('typescript-simple');
  13. const CSON = require('season');
  14. const CompileCache = require('../src/compile-cache');
  15. describe('CompileCache', () => {
  16. let [atomHome, fixtures] = Array.from([]);
  17. beforeEach(() => {
  18. fixtures = atom.project.getPaths()[0];
  19. atomHome = temp.mkdirSync('fake-atom-home');
  20. CSON.setCacheDir(null);
  21. CompileCache.resetCacheStats();
  22. spyOn(babelCompiler, 'compile');
  23. spyOn(CoffeeScript, 'compile').andReturn('the-coffee-code');
  24. return spyOn(TypeScriptSimple.prototype, 'compile').andReturn('the-typescript-code');
  25. });
  26. afterEach(() => {
  27. CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME);
  28. CSON.setCacheDir(CompileCache.getCacheDirectory());
  29. try {
  30. return temp.cleanupSync();
  31. } catch (error) {}
  32. });
  33. describe('addPathToCache(filePath, atomHome)', () => {
  34. describe('when the given file is plain javascript', () => it('does not compile or cache the file', function() {
  35. CompileCache.addPathToCache(path.join(fixtures, 'sample.js'), atomHome);
  36. return expect(CompileCache.getCacheStats()['.js']).toEqual({hits: 0, misses: 0});
  37. }));
  38. /**
  39. * TODO: FAILING TEST - This test fails with the following output:
  40. * TypeError: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
  41. */
  42. xdescribe('when the given file uses babel', () => it('compiles the file with babel and caches it', function() {
  43. CompileCache.addPathToCache(path.join(fixtures, 'babel', 'babel-comment.js'), atomHome);
  44. expect(CompileCache.getCacheStats()['.js']).toEqual({hits: 0, misses: 1});
  45. expect(babelCompiler.compile.callCount).toBe(1);
  46. CompileCache.addPathToCache(path.join(fixtures, 'babel', 'babel-comment.js'), atomHome);
  47. expect(CompileCache.getCacheStats()['.js']).toEqual({hits: 1, misses: 1});
  48. return expect(babelCompiler.compile.callCount).toBe(1);
  49. }));
  50. describe('when the given file is coffee-script', () => it('compiles the file with coffee-script and caches it', function() {
  51. CompileCache.addPathToCache(path.join(fixtures, 'coffee.coffee'), atomHome);
  52. expect(CompileCache.getCacheStats()['.coffee']).toEqual({hits: 0, misses: 1});
  53. expect(CoffeeScript.compile.callCount).toBe(1);
  54. CompileCache.addPathToCache(path.join(fixtures, 'coffee.coffee'), atomHome);
  55. expect(CompileCache.getCacheStats()['.coffee']).toEqual({hits: 1, misses: 1});
  56. return expect(CoffeeScript.compile.callCount).toBe(1);
  57. }));
  58. describe('when the given file is typescript', () => it('compiles the file with typescript and caches it', function() {
  59. CompileCache.addPathToCache(path.join(fixtures, 'typescript', 'valid.ts'), atomHome);
  60. expect(CompileCache.getCacheStats()['.ts']).toEqual({hits: 0, misses: 1});
  61. expect(TypeScriptSimple.prototype.compile.callCount).toBe(1);
  62. CompileCache.addPathToCache(path.join(fixtures, 'typescript', 'valid.ts'), atomHome);
  63. expect(CompileCache.getCacheStats()['.ts']).toEqual({hits: 1, misses: 1});
  64. return expect(TypeScriptSimple.prototype.compile.callCount).toBe(1);
  65. }));
  66. return describe('when the given file is CSON', () => it('compiles the file to JSON and caches it', function() {
  67. spyOn(CSON, 'setCacheDir').andCallThrough();
  68. spyOn(CSON, 'readFileSync').andCallThrough();
  69. CompileCache.addPathToCache(path.join(fixtures, 'cson.cson'), atomHome);
  70. expect(CSON.readFileSync).toHaveBeenCalledWith(path.join(fixtures, 'cson.cson'));
  71. expect(CSON.setCacheDir).toHaveBeenCalledWith(path.join(atomHome, '/compile-cache'));
  72. CSON.readFileSync.reset();
  73. CSON.setCacheDir.reset();
  74. CompileCache.addPathToCache(path.join(fixtures, 'cson.cson'), atomHome);
  75. expect(CSON.readFileSync).toHaveBeenCalledWith(path.join(fixtures, 'cson.cson'));
  76. return expect(CSON.setCacheDir).not.toHaveBeenCalled();
  77. }));
  78. });
  79. return describe('overriding Error.prepareStackTrace', function() {
  80. it('removes the override on the next tick, and always assigns the raw stack', function() {
  81. if (process.platform === 'win32') { return; } // Flakey Error.stack contents on Win32
  82. Error.prepareStackTrace = () => 'a-stack-trace';
  83. let error = new Error("Oops");
  84. expect(error.stack).toBe('a-stack-trace');
  85. expect(Array.isArray(error.getRawStack())).toBe(true);
  86. waits(1);
  87. return runs(function() {
  88. error = new Error("Oops again");
  89. expect(error.stack).toContain('compile-cache-spec.js');
  90. return expect(Array.isArray(error.getRawStack())).toBe(true);
  91. });
  92. });
  93. it('does not infinitely loop when the original prepareStackTrace value is reassigned', function() {
  94. const originalPrepareStackTrace = Error.prepareStackTrace;
  95. Error.prepareStackTrace = () => 'a-stack-trace';
  96. Error.prepareStackTrace = originalPrepareStackTrace;
  97. const error = new Error('Oops');
  98. expect(error.stack).toContain('compile-cache-spec.js');
  99. return expect(Array.isArray(error.getRawStack())).toBe(true);
  100. });
  101. return it('does not infinitely loop when the assigned prepareStackTrace calls the original prepareStackTrace', function() {
  102. const originalPrepareStackTrace = Error.prepareStackTrace;
  103. Error.prepareStackTrace = function(error, stack) {
  104. error.foo = 'bar';
  105. return originalPrepareStackTrace(error, stack);
  106. };
  107. const error = new Error('Oops');
  108. expect(error.stack).toContain('compile-cache-spec.js');
  109. expect(error.foo).toBe('bar');
  110. return expect(Array.isArray(error.getRawStack())).toBe(true);
  111. });
  112. });
  113. });