cache_persistent.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var parser = require('../');
  2. var test = require('tap').test;
  3. var path = require('path');
  4. var fs = require('fs');
  5. var files = {
  6. foo: path.join(__dirname, '/files/foo.js'),
  7. bar: path.join(__dirname, '/files/bar.js')
  8. };
  9. test('uses persistent cache', function (t) {
  10. t.plan(1);
  11. var p = parser({
  12. persistentCache: function (file, id, pkg, fallback, cb) {
  13. if (file === files.bar) {
  14. return fallback(null, cb);
  15. }
  16. cb(null, {
  17. source: 'file at ' + file + '@' + id,
  18. package: pkg,
  19. deps: { './bar': files.bar }
  20. });
  21. }
  22. });
  23. p.end({ id: 'foo', file: files.foo, entry: false });
  24. var rows = [];
  25. p.on('data', function (row) { rows.push(row) });
  26. p.on('end', function () {
  27. t.same(rows.sort(cmp), [
  28. {
  29. id: files.bar,
  30. file: files.bar,
  31. source: fs.readFileSync(files.bar, 'utf8'),
  32. deps: {}
  33. },
  34. {
  35. id: 'foo',
  36. file: files.foo,
  37. source: 'file at ' + files.foo + '@' + files.foo,
  38. deps: { './bar': files.bar }
  39. }
  40. ].sort(cmp));
  41. });
  42. });
  43. test('passes persistent cache error through', function (t) {
  44. t.plan(1);
  45. var p = parser({
  46. persistentCache: function (file, id, pkg, fallback, cb) {
  47. cb(new Error('foo'));
  48. }
  49. });
  50. p.end({ id: 'foo', file: files.foo, entry: false });
  51. p.on('error', function (err) { t.equals(err.message, 'foo') });
  52. });
  53. test('allow passing of the raw source as string', function (t) {
  54. t.plan(1);
  55. var p = parser({
  56. persistentCache: function (file, id, pkg, fallback, cb) {
  57. fallback(fs.readFileSync(files.bar, 'utf8'), cb);
  58. }
  59. });
  60. p.end({ id: 'foo', file: files.foo, entry: false });
  61. var rows = [];
  62. p.on('data', function (row) { rows.push(row) });
  63. p.on('end', function () {
  64. t.same(rows.sort(cmp), [
  65. {
  66. id: 'foo',
  67. file: files.foo,
  68. source: fs.readFileSync(files.bar, 'utf8'),
  69. deps: {}
  70. }
  71. ].sort(cmp));
  72. });
  73. });
  74. test('send file event with persistent cache', function (t) {
  75. t.plan(2);
  76. var p = parser({
  77. persistentCache: function (file, id, pkg, fallback, cb) {
  78. cb(null, {
  79. source: 'file at ' + file + '@' + id,
  80. package: pkg,
  81. deps: {}
  82. });
  83. }
  84. });
  85. p.end({ id: 'foo', file: files.foo, entry: false });
  86. p.on('file', function (file, id) {
  87. t.same(file, path.resolve(files.foo));
  88. t.same(id, path.resolve(files.foo));
  89. });
  90. });
  91. test('errors of transforms occur in the correct order with a persistent cache', function (t) {
  92. t.plan(3);
  93. var p = parser({
  94. transform: [
  95. path.join(__dirname, 'cache_persistent', 'error_transform')
  96. ],
  97. persistentCache: function (file, id, pkg, fallback, cb) {
  98. fallback(fs.readFileSync(files.foo, 'utf8'), cb);
  99. }
  100. });
  101. p.end({ id: 'foo', file: files.foo, entry: false });
  102. var order = 0;
  103. p.on('file', function (file, id) {
  104. t.same(order, 0);
  105. order += 1;
  106. });
  107. p.on('error', function (err) {
  108. t.same(order, 1);
  109. t.same(err.message, 'rawr while parsing file: ' + path.resolve(files.foo));
  110. });
  111. });
  112. function cmp (a, b) { return a.id < b.id ? -1 : 1 }