main.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. 'use strict';
  2. var concatFilenames = require('../index.js'),
  3. chai = require('chai'),
  4. sinonChai = require('sinon-chai'),
  5. expect = chai.expect,
  6. assert = require('stream-assert'),
  7. File = require('gulp-util').File,
  8. gulp = require('gulp'),
  9. path = require('path');
  10. chai.use(sinonChai);
  11. function fixtures(glob) {
  12. return path.join(__dirname, 'fixtures', glob);
  13. }
  14. describe('Given gulp-concat-filenames', function () {
  15. describe('When I do not provide a filename', function () {
  16. it('Then it should error', function () {
  17. function nofilenameProvided() {
  18. concatFilenames();
  19. }
  20. expect(nofilenameProvided)
  21. .to
  22. .throw(Error, 'Missing fileName option for gulp-concat-filenames');
  23. });
  24. });
  25. describe('When I do provide a filename', function () {
  26. it('Then it should support null files', function (done) {
  27. var expectedFilenames = ([
  28. path.join(__dirname, 'fixtures/fork.txt'),
  29. path.join(__dirname, 'fixtures/knife.js'),
  30. path.join(__dirname, 'fixtures/spoon.html'),
  31. ]
  32. .join('\n') + '\n')
  33. .replace(/\\/g, '\/')
  34. .toString();
  35. gulp
  36. .src(fixtures('*'), {
  37. read: false
  38. })
  39. .pipe(concatFilenames('example.js'))
  40. .pipe(assert.first(function (d) {
  41. var contents = d.contents.toString();
  42. expect(contents)
  43. .to
  44. .equal(expectedFilenames);
  45. }))
  46. .pipe(assert.end(done));
  47. });
  48. it('Then it support streamed files', function (done) {
  49. var expectedFilenames = ([
  50. path.join(__dirname, 'fixtures/fork.txt'),
  51. path.join(__dirname, 'fixtures/knife.js'),
  52. path.join(__dirname, 'fixtures/spoon.html'),
  53. ]
  54. .join('\n') + '\n')
  55. .replace(/\\/g, '\/')
  56. .toString();
  57. gulp
  58. .src(fixtures('*'), {
  59. buffer: false
  60. })
  61. .pipe(concatFilenames('example.js'))
  62. .pipe(assert.first(function (d) {
  63. var contents = d.contents.toString();
  64. expect(contents)
  65. .to
  66. .equal(expectedFilenames);
  67. }))
  68. .pipe(assert.end(done));
  69. });
  70. describe('When I do not provide the "root" optional argument', function () {
  71. it('then it will concatinate all filenames into a single file with absolute paths', function (done) {
  72. var expectedFilenames = ([
  73. path.join(__dirname, 'fixtures/fork.txt'),
  74. path.join(__dirname, 'fixtures/knife.js'),
  75. path.join(__dirname, 'fixtures/spoon.html'),
  76. ]
  77. .join('\n') + '\n')
  78. .replace(/\\/g, '\/')
  79. .toString();
  80. gulp
  81. .src(fixtures('*'))
  82. .pipe(concatFilenames('mainfest.txt'))
  83. .pipe(assert.first(function (d) {
  84. var contents = d.contents.toString();
  85. expect(contents)
  86. .to
  87. .equal(expectedFilenames);
  88. }))
  89. .pipe(assert.end(done));
  90. });
  91. });
  92. describe('When I do provide the "root" optional argument', function () {
  93. it('then it will concatinate all filenames into a single file with relative paths', function (done) {
  94. var expectedFilenames = ([
  95. '../test/fixtures/fork.txt',
  96. '../test/fixtures/knife.js',
  97. '../test/fixtures/spoon.html',
  98. ].join('\n') + '\n').toString();
  99. gulp
  100. .src(fixtures('*'))
  101. .pipe(concatFilenames('mainfest.txt', {
  102. root: 'fixtures'
  103. }))
  104. .pipe(assert.first(function (d) {
  105. var contents = d.contents.toString();
  106. expect(contents)
  107. .to
  108. .equal(expectedFilenames);
  109. }))
  110. .pipe(assert.end(done));
  111. });
  112. describe('When I provide prepend and append arguments', function () {
  113. it('then it will concatinate all filenames into a single file with prefix and suffix strings', function (done) {
  114. var expectedFilenames = ([
  115. 'XXX../test/fixtures/fork.txtYYY',
  116. 'XXX../test/fixtures/knife.jsYYY',
  117. 'XXX../test/fixtures/spoon.htmlYYY',
  118. ].join('\n') + '\n').toString();
  119. gulp
  120. .src(fixtures('*'))
  121. .pipe(concatFilenames('mainfest.txt', {
  122. root: 'fixtures',
  123. prepend: 'XXX',
  124. append: 'YYY'
  125. }))
  126. .pipe(assert.first(function (d) {
  127. var contents = d.contents.toString();
  128. expect(contents)
  129. .to
  130. .equal(expectedFilenames);
  131. }))
  132. .pipe(assert.end(done));
  133. });
  134. });
  135. describe('When I provide a differnt newLine argument', function () {
  136. it('then it will concatinate all filenames into a single file with an alternate newLine', function (done) {
  137. var expectedFilenames = ([
  138. '../test/fixtures/fork.txt',
  139. '../test/fixtures/knife.js',
  140. '../test/fixtures/spoon.html',
  141. ].join('XXX') + 'XXX').toString();
  142. gulp
  143. .src(fixtures('*'))
  144. .pipe(concatFilenames('mainfest.txt', {
  145. root: 'fixtures',
  146. newLine: 'XXX'
  147. }))
  148. .pipe(assert.first(function (d) {
  149. var contents = d.contents.toString();
  150. expect(contents)
  151. .to
  152. .equal(expectedFilenames);
  153. }))
  154. .pipe(assert.end(done));
  155. });
  156. });
  157. describe('When I provide a formatting function', function () {
  158. it('then it will apply that function to each entry', function (done) {
  159. var expectedFilenames = ([
  160. 'XXX../test/fixtures/fork.txtYYY',
  161. 'XXX../test/fixtures/knife.jsYYY',
  162. 'XXX../test/fixtures/spoon.htmlYYY',
  163. ].join('\n') + '\n').toString().toUpperCase();
  164. gulp
  165. .src(fixtures('*'))
  166. .pipe(concatFilenames('mainfest.txt', {
  167. root: 'fixtures',
  168. template: function(filename) {
  169. return 'XXX' + filename.toUpperCase() + 'YYY';
  170. }
  171. }))
  172. .pipe(assert.first(function (d) {
  173. var contents = d.contents.toString();
  174. expect(contents)
  175. .to
  176. .equal(expectedFilenames);
  177. }))
  178. .pipe(assert.end(done));
  179. });
  180. });
  181. describe('When I provide a formatting function with a syntax error', function () {
  182. it('then it will fail and throw an error', function (done) {
  183. gulp
  184. .src(fixtures('*'))
  185. .pipe(concatFilenames('mainfest.txt', {
  186. root: 'fixtures',
  187. template: function(filename) {
  188. /* jshint ignore:start */
  189. y = 0; // deliberate syntax error
  190. /* jshint ignore:end */
  191. return filename;
  192. }
  193. }))
  194. .on('error', function (err) {
  195. expect(err.message).to.have.string('Error in template function');
  196. done();
  197. });
  198. });
  199. });
  200. describe('When I provide a formatting function that does not return a string', function () {
  201. it('then it will fail and throw an error', function (done) {
  202. gulp
  203. .src(fixtures('*'))
  204. .pipe(concatFilenames('mainfest.txt', {
  205. root: 'fixtures',
  206. template: function() {
  207. return 5;
  208. }
  209. }))
  210. .on('error', function (err) {
  211. expect(err.message).to.equal('Error in template function');
  212. done();
  213. });
  214. });
  215. });
  216. });
  217. });
  218. });