main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var should = require('should');
  2. var flatten = require('../');
  3. var Vinyl = require('vinyl');
  4. var gulp = require('gulp');
  5. var path = require('path');
  6. var fileInstance;
  7. describe('gulp-flatten', function () {
  8. beforeEach(function () {
  9. fileInstance = new Vinyl({
  10. cwd: '/some/project/',
  11. base: '/some/project/src/',
  12. path: '/some/project/src/assets/css/app.css',
  13. contents: new Buffer('html { background-color: #777; }')
  14. });
  15. });
  16. describe('flatten()', function () {
  17. it('should strip relative path without options', function (done) {
  18. var stream = flatten();
  19. stream.on('error', done);
  20. stream.on('data', function(newFile) {
  21. should.exist(newFile);
  22. should.exist(newFile.path);
  23. should.exist(newFile.relative);
  24. newFile.relative.should.equal('app.css');
  25. done();
  26. });
  27. stream.write(fileInstance);
  28. });
  29. it('should replace relative path with option path', function (done) {
  30. var stream = flatten({newPath: 'new/path'});
  31. stream.on('error', done);
  32. stream.on('data', function(newFile) {
  33. should.exist(newFile);
  34. should.exist(newFile.path);
  35. should.exist(newFile.relative);
  36. newFile.relative.should.equal('new/path/app.css'.split('/').join(path.sep));
  37. done();
  38. });
  39. stream.write(fileInstance);
  40. });
  41. it('should emit arg error with nonstring option', function (done) {
  42. var stream = flatten({newPath: 123});
  43. stream.on('error', function (err) {
  44. should.exist(err);
  45. should.exist(err.message);
  46. should.ok(err.message === 'The "path" argument must be of type string')
  47. done();
  48. });
  49. stream.write(fileInstance);
  50. });
  51. it('should ignore directories', function (done) {
  52. var stream = flatten();
  53. stream.on('error', done);
  54. stream.on('data', function(newFile) {
  55. should.exist(newFile);
  56. should.exist(newFile.path);
  57. should.exist(newFile.relative);
  58. newFile.relative.should.equal('test.css');
  59. done();
  60. });
  61. gulp.task('dottask', function () {
  62. gulp.src(path.join(__dirname, '/test_dir/**/*.css'))
  63. .pipe(stream);
  64. });
  65. gulp.start('dottask');
  66. });
  67. it('should strip relative path at the specified depth if depth option is passed', function (done) {
  68. var stream = flatten({includeParents: 2});
  69. stream.on('error', done);
  70. stream.on('data', function(newFile) {
  71. should.exist(newFile);
  72. should.exist(newFile.path);
  73. should.exist(newFile.relative);
  74. newFile.relative.should.equal('one/two/app.css'.split('/').join(path.sep));
  75. done();
  76. });
  77. fileInstance.path = '/some/project/src/one/two/three/four/app.css';
  78. stream.write(fileInstance);
  79. });
  80. it('should leave path from the end if depth option is passed as negative number', function (done) {
  81. var stream = flatten({includeParents: -2});
  82. stream.on('error', done);
  83. stream.on('data', function(newFile) {
  84. should.exist(newFile);
  85. should.exist(newFile.path);
  86. should.exist(newFile.relative);
  87. newFile.relative.should.equal('three/four/app.css'.split('/').join(path.sep));
  88. done();
  89. });
  90. fileInstance.path = '/some/project/src/one/two/three/four/app.css';
  91. stream.write(fileInstance);
  92. });
  93. it('should make no changes if the absolute depth option is greater than the tree depth', function (done) {
  94. var stream = flatten({includeParents: 8});
  95. stream.on('error', done);
  96. stream.on('data', function(newFile) {
  97. should.exist(newFile);
  98. should.exist(newFile.path);
  99. should.exist(newFile.relative);
  100. newFile.relative.should.equal('one/two/three/four/app.css'.split('/').join(path.sep));
  101. done();
  102. });
  103. fileInstance.path = '/some/project/src/one/two/three/four/app.css';
  104. stream.write(fileInstance);
  105. });
  106. });
  107. });