main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* jshint node: true */
  2. /* global describe, it, beforeEach */
  3. 'use strict';
  4. var header = require('../');
  5. var should = require('should');
  6. var gutil = require('gulp-util');
  7. var fs = require('fs');
  8. var path = require('path');
  9. var es = require('event-stream');
  10. var File = require('vinyl');
  11. require('mocha');
  12. describe('gulp-header', function() {
  13. var fakeFile;
  14. function getFakeFile(fileContent){
  15. return new File({
  16. path: './test/fixture/file.js',
  17. cwd: './test/',
  18. base: './test/fixture/',
  19. contents: new Buffer(fileContent || '')
  20. });
  21. }
  22. function getFakeFileReadStream(){
  23. return new File({
  24. contents: es.readArray(['Hello world'])
  25. });
  26. }
  27. beforeEach(function(){
  28. fakeFile = getFakeFile('Hello world');
  29. });
  30. describe('header', function() {
  31. it('file should pass through', function(done) {
  32. var file_count = 0;
  33. var stream = header();
  34. stream.on('data', function(newFile){
  35. should.exist(newFile);
  36. should.exist(newFile.path);
  37. should.exist(newFile.relative);
  38. should.exist(newFile.contents);
  39. newFile.path.should.equal('./test/fixture/file.js');
  40. newFile.relative.should.equal('file.js');
  41. newFile.contents.toString('utf8').should.equal('Hello world');
  42. ++file_count;
  43. });
  44. stream.once('end', function () {
  45. file_count.should.equal(1);
  46. done();
  47. });
  48. stream.write(fakeFile);
  49. stream.end();
  50. });
  51. it('should prepend the header to the file content', function(done) {
  52. var myHeader = header('And then i said : ');
  53. myHeader.write(fakeFile);
  54. myHeader.once('data', function(file) {
  55. should(file.isBuffer()).ok;
  56. should.exist(file.contents);
  57. file.contents.toString('utf8').should.equal('And then i said : Hello world');
  58. done();
  59. });
  60. });
  61. it('should prepend the header to the file content (stream)', function(done) {
  62. var myHeader = header('And then i said : ');
  63. myHeader.write(getFakeFileReadStream());
  64. myHeader.once('data', function(file) {
  65. should(file.isStream()).ok;
  66. file.contents.pipe(es.wait(function(err, data) {
  67. data.toString('utf8').should.equal('And then i said : Hello world');
  68. done();
  69. }));
  70. });
  71. });
  72. it('should format the header', function(done) {
  73. var stream = header('And then <%= foo %> said : ', { foo : 'you' } );
  74. //var stream = header('And then ${foo} said : ', { foo : 'you' } );
  75. stream.on('data', function (newFile) {
  76. should.exist(newFile.contents);
  77. newFile.contents.toString('utf8').should.equal('And then you said : Hello world');
  78. });
  79. stream.once('end', done);
  80. stream.write(fakeFile);
  81. stream.end();
  82. });
  83. it('should format the header (ES6 delimiters)', function(done) {
  84. var stream = header('And then ${foo} said : ', { foo : 'you' } );
  85. stream.on('data', function (newFile) {
  86. should.exist(newFile.contents);
  87. newFile.contents.toString('utf8').should.equal('And then you said : Hello world');
  88. });
  89. stream.once('end', done);
  90. stream.write(fakeFile);
  91. stream.end();
  92. });
  93. it('should access to the current file', function(done) {
  94. var stream = header([
  95. '<%= file.relative %>',
  96. '<%= file.path %>',
  97. ''].join('\n'));
  98. stream.on('data', function (newFile) {
  99. should.exist(newFile.contents);
  100. newFile.contents.toString('utf8').should.equal('file.js\n./test/fixture/file.js\nHello world');
  101. });
  102. stream.once('end', done);
  103. stream.write(fakeFile);
  104. stream.end();
  105. });
  106. });
  107. });