main.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. 'use strict';
  2. var concatStream = require('concat-stream');
  3. var replacePlugin = require('../');
  4. var fs = require('fs');
  5. var should = require('should');
  6. var File = require('vinyl');
  7. describe('gulp-replace', function() {
  8. describe('replacePlugin()', function() {
  9. var replacements;
  10. beforeEach(function () {
  11. replacements = [
  12. 'cow',
  13. 'chicken',
  14. 'duck',
  15. 'person'
  16. ];
  17. });
  18. describe('buffered input', function () {
  19. var file, check;
  20. beforeEach(function () {
  21. file = new File({
  22. path: 'test/fixtures/helloworld.txt',
  23. contents: fs.readFileSync('test/fixtures/helloworld.txt')
  24. });
  25. check = function (stream, done, cb) {
  26. stream.on('data', function (newFile) {
  27. cb(newFile);
  28. done();
  29. });
  30. stream.write(file);
  31. stream.end();
  32. };
  33. });
  34. it('should replace string on a buffer', function(done) {
  35. var stream = replacePlugin('world', 'person');
  36. check(stream, done, function (newFile) {
  37. String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  38. });
  39. });
  40. it('should replace regex on a buffer', function(done) {
  41. var stream = replacePlugin(/world/g, 'person');
  42. check(stream, done, function (newFile) {
  43. String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  44. });
  45. });
  46. it('should replace regex on a buffer with a function', function(done) {
  47. var stream = replacePlugin(/world/g, function() { return 'person'; });
  48. check(stream, done, function (newFile) {
  49. String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  50. });
  51. });
  52. it('should replace string on a buffer with a function', function(done) {
  53. var stream = replacePlugin('world', function() { return 'person'; });
  54. check(stream, done, function (newFile) {
  55. String(newFile.contents).should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  56. });
  57. });
  58. it('should call function once for each replacement when replacing a string on a buffer', function(done) {
  59. var stream = replacePlugin('world', function() { return replacements.shift(); });
  60. check(stream, done, function (newFile) {
  61. String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
  62. });
  63. });
  64. it('should call function once for each replacement when replacing a regex on a buffer', function(done) {
  65. var stream = replacePlugin(/world/g, function() { return replacements.shift(); });
  66. check(stream, done, function (newFile) {
  67. String(newFile.contents).should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
  68. });
  69. });
  70. it('should trigger events on a buffer', function(done) {
  71. var stream = replacePlugin('world', 'elephant')
  72. stream.on('finish', function() {
  73. // No assertion required, we should end up here, if we don't the test will time out
  74. done();
  75. });
  76. stream.write(file);
  77. stream.end();
  78. });
  79. });
  80. describe('streamed input', function () {
  81. var file, check;
  82. beforeEach(function () {
  83. file = new File({
  84. base: 'test/fixtures/',
  85. path: 'test/fixtures/helloworld.txt',
  86. contents: fs.createReadStream('test/fixtures/helloworld.txt')
  87. });
  88. check = function (stream, done, cb) {
  89. stream.on('data', function(newFile) {
  90. newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
  91. cb(data);
  92. done();
  93. }));
  94. });
  95. stream.write(file);
  96. stream.end();
  97. };
  98. });
  99. it('should have this.file set to the vinyl file object', function(done) {
  100. var stream = replacePlugin('world', function() {
  101. this.file.should.equal(file);
  102. return 'person';
  103. });
  104. check(stream, done, function (data) {
  105. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  106. });
  107. });
  108. it('should have this.file set to the vinyl file object for regex replaces', function(done) {
  109. var stream = replacePlugin(/world/g, function() {
  110. this.file.should.equal(file);
  111. return 'person';
  112. });
  113. check(stream, done, function (data) {
  114. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  115. });
  116. });
  117. it('should replace filenames', function(done) {
  118. var stream = replacePlugin('world', function() {
  119. return this.file.relative;
  120. });
  121. check(stream, done, function (data) {
  122. data.should.equal(fs.readFileSync('test/expected/hellofilename.txt', 'utf8'));
  123. });
  124. });
  125. it('should replace string on a stream', function(done) {
  126. var stream = replacePlugin('world', 'person');
  127. check(stream, done, function (data) {
  128. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  129. });
  130. });
  131. it('should replace regex on a stream', function(done) {
  132. var stream = replacePlugin(/world/g, 'person');
  133. check(stream, done, function (data) {
  134. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  135. });
  136. });
  137. it('should replace regex on a stream with a function', function(done) {
  138. var stream = replacePlugin(/world/g, function() { return 'person'; });
  139. check(stream, done, function (data) {
  140. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  141. });
  142. });
  143. it('should replace string on a stream with a function', function(done) {
  144. var stream = replacePlugin('world', function() { return 'person'; });
  145. check(stream, done, function (data) {
  146. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  147. });
  148. });
  149. it('should call function once for each replacement when replacing a string on a stream', function(done) {
  150. var stream = replacePlugin('world', function() { return replacements.shift(); });
  151. check(stream, done, function (data) {
  152. data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
  153. });
  154. });
  155. it('should call function once for each replacement when replacing a regex on a stream', function(done) {
  156. var stream = replacePlugin(/world/g, function() { return replacements.shift(); });
  157. check(stream, done, function (data) {
  158. data.should.equal(fs.readFileSync('test/expected/hellofarm.txt', 'utf8'));
  159. });
  160. });
  161. });
  162. describe('options', function () {
  163. describe('skipBinary', function () {
  164. var stream;
  165. beforeEach(function () {
  166. stream = replacePlugin('world', 'person', {skipBinary: true});
  167. });
  168. it('should ignore binary files when skipBinary is enabled', function(done) {
  169. var file = new File({
  170. path: 'test/fixtures/binary.png',
  171. contents: fs.readFileSync('test/fixtures/binary.png')
  172. });
  173. stream.on('data', function(newFile) {
  174. newFile.contents.should.eql(fs.readFileSync('test/expected/binary.png'));
  175. done();
  176. });
  177. stream.write(file);
  178. stream.end();
  179. });
  180. it('should replace string on non binary files when skipBinary is enabled', function(done) {
  181. var file = new File({
  182. path: 'test/fixtures/helloworld.txt',
  183. contents: fs.createReadStream('test/fixtures/helloworld.txt')
  184. });
  185. stream.on('data', function(newFile) {
  186. newFile.contents.pipe(concatStream({encoding: 'string'}, function(data) {
  187. data.should.equal(fs.readFileSync('test/expected/helloworld.txt', 'utf8'));
  188. done();
  189. }));
  190. });
  191. stream.write(file);
  192. stream.end();
  193. });
  194. it('should be true by default', function(done) {
  195. stream = replacePlugin('world', 'person');
  196. var file = new File({
  197. path: 'test/fixtures/binary.png',
  198. contents: fs.readFileSync('test/fixtures/binary.png')
  199. });
  200. stream.on('data', function(newFile) {
  201. newFile.contents.should.eql(fs.readFileSync('test/expected/binary.png'));
  202. done();
  203. });
  204. stream.write(file);
  205. stream.end();
  206. });
  207. });
  208. });
  209. });
  210. });