test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var expect = require('chai').expect,
  2. through = require('through2'),
  3. Vinyl = require('vinyl'),
  4. fs = require('fs'),
  5. path = require('path'),
  6. concatCss = require('../');
  7. function expected(file) {
  8. var base = path.join(process.cwd(), 'test/expected');
  9. var filepath = path.resolve(base, file);
  10. return new Vinyl({
  11. path: filepath,
  12. cwd: process.cwd(),
  13. base: base,
  14. contents: fs.readFileSync(filepath)
  15. });
  16. }
  17. function fixture(file) {
  18. var base = path.join(process.cwd(), 'test/fixtures');
  19. var filepath = path.join(base, file);
  20. return new Vinyl({
  21. path: filepath,
  22. cwd: process.cwd(),
  23. base: base,
  24. contents: fs.readFileSync(filepath)
  25. });
  26. }
  27. describe('gulp-concat-css', function() {
  28. it('should only bubble up imports', function(done) {
  29. var now = Date.now();
  30. var stream = concatCss('build/bundle-bubbleonly.css', {inlineImports: false, rebaseUrls: false});
  31. var expectedFile = expected('build/bundle-bubbleonly.css');
  32. stream
  33. .pipe(through.obj(function(file, enc, cb) {
  34. //fs.writeFileSync("bundle.css", file.contents);
  35. expect(String(file.contents)).to.be.equal(String(expectedFile.contents));
  36. expect(path.basename(file.path)).to.be.equal(path.basename(expectedFile.path));
  37. expect(file.cwd, "cwd").to.be.equal(expectedFile.cwd);
  38. expect(file.relative, "relative").to.be.equal(expectedFile.relative);
  39. console.log('Execution time: ' + (Date.now() - now) + 'ms');
  40. done();
  41. }));
  42. stream.write(fixture('main.css'));
  43. stream.write(fixture('vendor/vendor.css'));
  44. stream.end();
  45. });
  46. it('should only rebase urls', function(done) {
  47. var now = Date.now();
  48. var stream = concatCss('build/bundle-rebase.css', {inlineImports: false});
  49. var expectedFile = expected('build/bundle-rebase.css');
  50. stream
  51. .pipe(through.obj(function(file, enc, cb) {
  52. //fs.writeFileSync("bundle.css", file.contents);
  53. expect(String(file.contents)).to.be.equal(String(expectedFile.contents));
  54. expect(path.basename(file.path)).to.be.equal(path.basename(expectedFile.path));
  55. expect(file.cwd, "cwd").to.be.equal(expectedFile.cwd);
  56. expect(file.relative, "relative").to.be.equal(expectedFile.relative);
  57. console.log('Execution time: ' + (Date.now() - now) + 'ms');
  58. done();
  59. }));
  60. stream.write(fixture('main.css'));
  61. stream.write(fixture('vendor/vendor.css'));
  62. stream.end();
  63. });
  64. it('should only inline imports', function(done) {
  65. var now = Date.now();
  66. var stream = concatCss('build/bundle-import.css', {inlineImports: true, rebaseUrls: false});
  67. var expectedFile = expected('build/bundle-import.css');
  68. stream
  69. .pipe(through.obj(function(file, enc, cb) {
  70. //fs.writeFileSync("bundle.css", file.contents);
  71. expect(String(file.contents)).to.be.equal(String(expectedFile.contents));
  72. expect(path.basename(file.path)).to.be.equal(path.basename(expectedFile.path));
  73. expect(file.cwd, "cwd").to.be.equal(expectedFile.cwd);
  74. expect(file.relative, "relative").to.be.equal(expectedFile.relative);
  75. console.log('Execution time: ' + (Date.now() - now) + 'ms');
  76. done();
  77. }));
  78. stream.write(fixture('main.css'));
  79. stream.write(fixture('vendor/vendor.css'));
  80. stream.end();
  81. });
  82. it('should concat, rebase urls, inline imports and bubble up external imports', function(done) {
  83. var now = Date.now();
  84. var stream = concatCss('build/bundle-all.css');
  85. var expectedFile = expected('build/bundle-all.css');
  86. stream
  87. .pipe(through.obj(function(file, enc, cb) {
  88. //fs.writeFileSync("bundle.css", file.contents);
  89. expect(String(file.contents)).to.be.equal(String(expectedFile.contents));
  90. expect(path.basename(file.path)).to.be.equal(path.basename(expectedFile.path));
  91. expect(file.cwd, "cwd").to.be.equal(expectedFile.cwd);
  92. expect(file.relative, "relative").to.be.equal(expectedFile.relative);
  93. console.log('Execution time: ' + (Date.now() - now) + 'ms');
  94. done();
  95. }));
  96. stream.write(fixture('main.css'));
  97. stream.write(fixture('vendor/vendor.css'));
  98. stream.end();
  99. });
  100. it('should not crash if no file is provided', function(done) {
  101. var stream = concatCss('build/bundle-all.css');
  102. stream
  103. .on('error', function() {
  104. done(false);
  105. })
  106. .pipe(through.obj(function(file, enc, cb) {
  107. done(false);
  108. }, function() {
  109. done();
  110. }));
  111. stream.end();
  112. });
  113. });