UNPKG

33.4 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Dependencies
5 */
6import replace, {sync, replaceInFile, replaceInFileSync} from './replace-in-file';
7const fs = require('fs');
8const writeFile = Promise.promisify(fs.writeFile);
9const deleteFile = Promise.promisify(fs.unlink);
10
11/**
12 * Specifications
13 */
14describe('Replace in file', () => {
15
16 //Test JSON
17 const testData = 'a re place c';
18
19 /**
20 * Prepare test files
21 */
22 beforeEach(() => Promise.all([
23 writeFile('test1', testData, 'utf8'),
24 writeFile('test2', testData, 'utf8'),
25 writeFile('test3', 'nope', 'utf8'),
26 ]));
27
28 /**
29 * Clean up test files
30 */
31 afterEach(() => Promise.all([
32 deleteFile('test1'),
33 deleteFile('test2'),
34 deleteFile('test3'),
35 ]));
36
37 /**
38 * Async with promises
39 */
40 describe('Async with promises', () => {
41
42 it('should throw an error when no config provided', () => {
43 return expect(replace()).to.eventually.be.rejectedWith(Error);
44 });
45
46 it('should throw an error when invalid config provided', () => {
47 return expect(replace(42)).to.eventually.be.rejectedWith(Error);
48 });
49
50 it('should throw an error when no `files` defined', () => {
51 return expect(replace({
52 from: /re\splace/g,
53 to: 'b',
54 })).to.eventually.be.rejectedWith(Error);
55 });
56
57 it('should throw an error when no `from` defined', () => {
58 return expect(replace({
59 files: 'test1',
60 to: 'b',
61 })).to.eventually.be.rejectedWith(Error);
62 });
63
64 it('should throw an error when no `to` defined', () => {
65 return expect(replace({
66 files: 'test1',
67 from: /re\splace/g,
68 })).to.eventually.be.rejectedWith(Error);
69 });
70
71 it('should replace contents in a single file with regex', done => {
72 replace({
73 files: 'test1',
74 from: /re\splace/g,
75 to: 'b',
76 }).then(() => {
77 const test1 = fs.readFileSync('test1', 'utf8');
78 const test2 = fs.readFileSync('test2', 'utf8');
79 expect(test1).to.equal('a b c');
80 expect(test2).to.equal(testData);
81 done();
82 });
83 });
84
85 it('should pass file as an arg to a "from" function', done => {
86 replace({
87 files: 'test1',
88 from: (file) => {
89 expect(file).to.equal('test1');
90 return /re\splace/g;
91 },
92 to: 'b',
93 }).then(() => {
94 const test1 = fs.readFileSync('test1', 'utf8');
95 const test2 = fs.readFileSync('test2', 'utf8');
96 expect(test1).to.equal('a b c');
97 expect(test2).to.equal(testData);
98 done();
99 });
100 });
101
102 it(`should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex`, done => {
103 replace({
104 files: 'test1',
105 from: /re\splace/g,
106 to: (match, ...args) => {
107 const file = args.pop();
108 expect(match).to.equal('re place');
109 expect(file).to.equal('test1');
110 return 'b';
111 },
112 }).then(() => {
113 const test1 = fs.readFileSync('test1', 'utf8');
114 const test2 = fs.readFileSync('test2', 'utf8');
115 expect(test1).to.equal('a b c');
116 expect(test2).to.equal(testData);
117 done();
118 });
119 });
120
121 it('should replace contents with a string replacement', done => {
122 replace({
123 files: 'test1',
124 from: 're place',
125 to: 'b',
126 }).then(() => {
127 const test1 = fs.readFileSync('test1', 'utf8');
128 expect(test1).to.equal('a b c');
129 done();
130 });
131 });
132
133 it(`should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement`, done => {
134 replace({
135 files: 'test1',
136 from: 're place',
137 to: (match, ...args) => {
138 const file = args.pop();
139 expect(match).to.equal('re place');
140 expect(file).to.equal('test1');
141 return 'b';
142 },
143 }).then(() => {
144 const test1 = fs.readFileSync('test1', 'utf8');
145 expect(test1).to.equal('a b c');
146 done();
147 });
148 });
149
150 it('should replace contents in a an array of files', done => {
151 replace({
152 files: ['test1', 'test2'],
153 from: /re\splace/g,
154 to: 'b',
155 }).then(() => {
156 const test1 = fs.readFileSync('test1', 'utf8');
157 const test2 = fs.readFileSync('test2', 'utf8');
158 expect(test1).to.equal('a b c');
159 expect(test2).to.equal('a b c');
160 done();
161 });
162 });
163
164 it('should expand globs', done => {
165 replace({
166 files: 'test*',
167 from: /re\splace/g,
168 to: 'b',
169 }).then(() => {
170 const test1 = fs.readFileSync('test1', 'utf8');
171 const test2 = fs.readFileSync('test2', 'utf8');
172 expect(test1).to.equal('a b c');
173 expect(test2).to.equal('a b c');
174 done();
175 });
176 });
177
178 it('should expand globs while excluding ignored files', done => {
179 replace({
180 files: 'test*',
181 ignore: 'test1',
182 from: /re\splace/g,
183 to: 'b',
184 }).then(() => {
185 const test1 = fs.readFileSync('test1', 'utf8');
186 const test2 = fs.readFileSync('test2', 'utf8');
187 expect(test1).to.equal('a re place c');
188 expect(test2).to.equal('a b c');
189 done();
190 });
191 });
192
193 it('should replace substrings', done => {
194 replace({
195 files: 'test1',
196 from: /(re)\s(place)/g,
197 to: '$2 $1',
198 }).then(() => {
199 const test1 = fs.readFileSync('test1', 'utf8');
200 expect(test1).to.equal('a place re c');
201 done();
202 });
203 });
204
205 it('should fulfill the promise on success', () => {
206 return replace({
207 files: 'test1',
208 from: /re\splace/g,
209 to: 'b',
210 }).should.be.fulfilled;
211 });
212
213 it('should reject the promise with an error on failure', () => {
214 return expect(replace({
215 files: 'nope',
216 from: /re\splace/g,
217 to: 'b',
218 })).to.eventually.be.rejectedWith(Error);
219 });
220
221 it('should not reject the promise if allowEmptyPaths is true', () => {
222 return replace({
223 files: 'nope',
224 allowEmptyPaths: true,
225 from: /re\splace/g,
226 to: 'b',
227 }).should.be.fulfilled;
228 });
229
230 it('should return a results array', done => {
231 replace({
232 files: 'test1',
233 from: /re\splace/g,
234 to: 'b',
235 }).then(results => {
236 expect(results).to.be.instanceof(Array);
237 expect(results).to.have.length(1);
238 expect(results[0].file).to.equal('test1');
239 done();
240 });
241 });
242
243 it('should mark if something was replaced', done => {
244 replace({
245 files: 'test1',
246 from: /re\splace/g,
247 to: 'b',
248 }).then(results => {
249 expect(results[0].hasChanged).to.equal(true);
250 done();
251 });
252 });
253
254 it('should not mark if nothing was replaced', done => {
255 replace({
256 files: 'test1',
257 from: 'nope',
258 to: 'b',
259 }).then(results => {
260 expect(results[0].hasChanged).to.equal(false);
261 done();
262 });
263 });
264
265 it('should return correct results for multiple files', done => {
266 replace({
267 files: ['test1', 'test2', 'test3'],
268 from: /re\splace/g,
269 to: 'b',
270 }).then(results => {
271 expect(results).to.have.length(3);
272 expect(results[0].file).to.equal('test1');
273 expect(results[0].hasChanged).to.equal(true);
274 expect(results[1].file).to.equal('test2');
275 expect(results[1].hasChanged).to.equal(true);
276 expect(results[2].file).to.equal('test3');
277 expect(results[2].hasChanged).to.equal(false);
278 done();
279 });
280 });
281
282 it('should make multiple replacements with the same string', done => {
283 replace({
284 files: ['test1', 'test2', 'test3'],
285 from: [/re/g, /place/g],
286 to: 'b',
287 }).then(() => {
288 const test1 = fs.readFileSync('test1', 'utf8');
289 const test2 = fs.readFileSync('test2', 'utf8');
290 expect(test1).to.equal('a b b c');
291 expect(test2).to.equal('a b b c');
292 done();
293 });
294 });
295
296 it('should make multiple replacements with different strings', done => {
297 replace({
298 files: ['test1', 'test2', 'test3'],
299 from: [/re/g, /place/g],
300 to: ['b', 'e'],
301 }).then(() => {
302 const test1 = fs.readFileSync('test1', 'utf8');
303 const test2 = fs.readFileSync('test2', 'utf8');
304 expect(test1).to.equal('a b e c');
305 expect(test2).to.equal('a b e c');
306 done();
307 });
308 });
309
310 it('should not replace with missing replacement values', done => {
311 replace({
312 files: ['test1', 'test2', 'test3'],
313 from: [/re/g, /place/g],
314 to: ['b'],
315 }).then(() => {
316 const test1 = fs.readFileSync('test1', 'utf8');
317 const test2 = fs.readFileSync('test2', 'utf8');
318 expect(test1).to.equal('a b place c');
319 expect(test2).to.equal('a b place c');
320 done();
321 });
322 });
323
324 it('should not replace in a dry run', done => {
325 replace({
326 files: ['test1', 'test2'],
327 from: /re\splace/g,
328 to: 'b',
329 dry: true,
330 }).then(() => {
331 const test1 = fs.readFileSync('test1', 'utf8');
332 const test2 = fs.readFileSync('test2', 'utf8');
333 expect(test1).to.equal('a re place c');
334 expect(test2).to.equal('a re place c');
335 done();
336 });
337 });
338
339 it('should return changed files for a dry run', done => {
340 replace({
341 files: ['test1', 'test2', 'test3'],
342 from: /re\splace/g,
343 to: 'b',
344 dry: true,
345 }).then(results => {
346 expect(results).to.have.length(3);
347 expect(results[0].file).to.equal('test1');
348 expect(results[0].hasChanged).to.equal(true);
349 expect(results[1].file).to.equal('test2');
350 expect(results[1].hasChanged).to.equal(true);
351 expect(results[2].file).to.equal('test3');
352 expect(results[2].hasChanged).to.equal(false);
353 done();
354 });
355 });
356
357 it('should accept glob configuration', done => {
358 replace({
359 files: 'test1',
360 from: /re\splace/g,
361 to: 'b',
362 allowEmptyPaths: true,
363 glob: {
364 ignore: ['test1'],
365 },
366 }).then(() => {
367 const test1 = fs.readFileSync('test1', 'utf8');
368 expect(test1).to.equal('a re place c');
369 done();
370 });
371 });
372
373 it('should ignore empty glob configuration', done => {
374 replace({
375 files: 'test1',
376 from: /re\splace/g,
377 to: 'b',
378 glob: null,
379 }).then(() => {
380 const test1 = fs.readFileSync('test1', 'utf8');
381 expect(test1).to.equal('a b c');
382 done();
383 });
384 });
385
386 it('should count matches if specified in config', done => {
387 replace({
388 files: 'test1',
389 from: [/re/g, /place/g],
390 to: 'test',
391 countMatches: true,
392 }).then(results => {
393 expect(results[0].numMatches).to.equal(2);
394 done();
395 });
396 });
397
398 it('should not count matches if not specified in config', done => {
399 replace({
400 files: 'test1',
401 from: [/re/g, /place/g],
402 to: 'test',
403 }).then(results => {
404 expect(results[0].numMatches).to.be.undefined;
405 done();
406 });
407 });
408
409 it('should return 0 matches if match not found', done => {
410 replace({
411 files: 'test1',
412 from: 'nope',
413 to: 'test',
414 countMatches: true,
415 }).then(results => {
416 expect(results[0].numMatches).to.equal(0);
417 done();
418 });
419 });
420 });
421
422 /**
423 * Async with callback
424 */
425 describe('Async with callback', () => {
426
427 it('should throw an error when no config provided', done => {
428 replace(null, (error) => {
429 expect(error).to.be.instanceof(Error);
430 done();
431 });
432 });
433
434 it('should throw an error when invalid config provided', done => {
435 replace(42, (error) => {
436 expect(error).to.be.instanceof(Error);
437 done();
438 });
439 });
440
441 it('should throw an error when no `files` defined', done => {
442 replace({
443 from: /re\splace/g,
444 to: 'b',
445 }, (error) => {
446 expect(error).to.be.instanceof(Error);
447 done();
448 });
449 });
450
451 it('should throw an error when no `from` defined', done => {
452 replace({
453 files: 'test1',
454 to: 'b',
455 }, (error) => {
456 expect(error).to.be.instanceof(Error);
457 done();
458 });
459 });
460
461 it('should throw an error when no `to` defined', done => {
462 replace({
463 files: 'test1',
464 from: /re\splace/g,
465 }, (error) => {
466 expect(error).to.be.instanceof(Error);
467 done();
468 });
469 });
470
471 it('should replace contents in a single file with regex', done => {
472 replace({
473 files: 'test1',
474 from: /re\splace/g,
475 to: 'b',
476 }, () => {
477 const test1 = fs.readFileSync('test1', 'utf8');
478 const test2 = fs.readFileSync('test2', 'utf8');
479 expect(test1).to.equal('a b c');
480 expect(test2).to.equal(testData);
481 done();
482 });
483 });
484
485 it('should pass file as an arg to a "from" function', done => {
486 replace({
487 files: 'test1',
488 from: (file) => {
489 expect(file).to.equal('test1');
490 return /re\splace/g;
491 },
492 to: 'b',
493 }, () => {
494 const test1 = fs.readFileSync('test1', 'utf8');
495 const test2 = fs.readFileSync('test2', 'utf8');
496 expect(test1).to.equal('a b c');
497 expect(test2).to.equal(testData);
498 done();
499 });
500 });
501
502 it(`should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex`, done => {
503 replace({
504 files: 'test1',
505 from: /re\splace/g,
506 to: (match, ...args) => {
507 const file = args.pop();
508 expect(match).to.equal('re place');
509 expect(file).to.equal('test1');
510 return 'b';
511 },
512 }, () => {
513 const test1 = fs.readFileSync('test1', 'utf8');
514 const test2 = fs.readFileSync('test2', 'utf8');
515 expect(test1).to.equal('a b c');
516 expect(test2).to.equal(testData);
517 done();
518 });
519 });
520
521 it('should replace contents with a string replacement', done => {
522 replace({
523 files: 'test1',
524 from: 're place',
525 to: 'b',
526 }, () => {
527 const test1 = fs.readFileSync('test1', 'utf8');
528 expect(test1).to.equal('a b c');
529 done();
530 });
531 });
532
533 it(`should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement`, done => {
534 replace({
535 files: 'test1',
536 from: 're place',
537 to: (match, ...args) => {
538 const file = args.pop();
539 expect(match).to.equal('re place');
540 expect(file).to.equal('test1');
541 return 'b';
542 },
543 }, () => {
544 const test1 = fs.readFileSync('test1', 'utf8');
545 expect(test1).to.equal('a b c');
546 done();
547 });
548 });
549
550 it('should replace contents in a an array of files', done => {
551 replace({
552 files: ['test1', 'test2'],
553 from: /re\splace/g,
554 to: 'b',
555 }, () => {
556 const test1 = fs.readFileSync('test1', 'utf8');
557 const test2 = fs.readFileSync('test2', 'utf8');
558 expect(test1).to.equal('a b c');
559 expect(test2).to.equal('a b c');
560 done();
561 });
562 });
563
564 it('should expand globs', done => {
565 replace({
566 files: 'test*',
567 from: /re\splace/g,
568 to: 'b',
569 }, () => {
570 const test1 = fs.readFileSync('test1', 'utf8');
571 const test2 = fs.readFileSync('test2', 'utf8');
572 expect(test1).to.equal('a b c');
573 expect(test2).to.equal('a b c');
574 done();
575 });
576 });
577
578 it('should expand globs while excluding ignored files', done => {
579 replace({
580 files: 'test*',
581 ignore: 'test1',
582 from: /re\splace/g,
583 to: 'b',
584 }, () => {
585 const test1 = fs.readFileSync('test1', 'utf8');
586 const test2 = fs.readFileSync('test2', 'utf8');
587 expect(test1).to.equal('a re place c');
588 expect(test2).to.equal('a b c');
589 done();
590 });
591 });
592
593 it('should not return an error on success', done => {
594 replace({
595 files: 'test1',
596 from: /re\splace/g,
597 to: 'b',
598 }, (error) => {
599 expect(error).to.equal(null);
600 done();
601 });
602 });
603
604 it('should return an error on failure', done => {
605 replace({
606 files: 'nope',
607 from: /re\splace/g,
608 to: 'b',
609 }, (error) => {
610 expect(error).to.be.instanceof(Error);
611 done();
612 });
613 });
614
615 it('should not return an error if allowEmptyPaths is true', done => {
616 replace({
617 files: 'nope',
618 allowEmptyPaths: true,
619 from: /re\splace/g,
620 to: 'b',
621 }, (error) => {
622 expect(error).to.equal(null);
623 done();
624 });
625 });
626
627 it('should return a results array', done => {
628 replace({
629 files: 'test1',
630 from: /re\splace/g,
631 to: 'b',
632 }, (error, results) => {
633 expect(results).to.be.instanceof(Array);
634 expect(results).to.have.length(1);
635 expect(results[0].file).to.equal('test1');
636 done();
637 });
638 });
639
640 it('should mark if something was replaced', done => {
641 replace({
642 files: 'test1',
643 from: /re\splace/g,
644 to: 'b',
645 }, (error, results) => {
646 expect(results[0].hasChanged).to.equal(true);
647 done();
648 });
649 });
650
651 it('should not mark if nothing was replaced', done => {
652 replace({
653 files: 'test1',
654 from: 'nope',
655 to: 'b',
656 }, (error, results) => {
657 expect(results[0].hasChanged).to.equal(false);
658 done();
659 });
660 });
661
662 it('should return correct results for multiple files', done => {
663 replace({
664 files: ['test1', 'test2', 'test3'],
665 from: /re\splace/g,
666 to: 'b',
667 }, (error, results) => {
668 expect(results).to.have.length(3);
669 expect(results[0].file).to.equal('test1');
670 expect(results[0].hasChanged).to.equal(true);
671 expect(results[1].file).to.equal('test2');
672 expect(results[1].hasChanged).to.equal(true);
673 expect(results[2].file).to.equal('test3');
674 expect(results[2].hasChanged).to.equal(false);
675 done();
676 });
677 });
678
679 it('should make multiple replacements with the same string', done => {
680 replace({
681 files: ['test1', 'test2', 'test3'],
682 from: [/re/g, /place/g],
683 to: 'b',
684 }, () => {
685 const test1 = fs.readFileSync('test1', 'utf8');
686 const test2 = fs.readFileSync('test2', 'utf8');
687 expect(test1).to.equal('a b b c');
688 expect(test2).to.equal('a b b c');
689 done();
690 });
691 });
692
693 it('should make multiple replacements with different strings', done => {
694 replace({
695 files: ['test1', 'test2', 'test3'],
696 from: [/re/g, /place/g],
697 to: ['b', 'e'],
698 }, () => {
699 const test1 = fs.readFileSync('test1', 'utf8');
700 const test2 = fs.readFileSync('test2', 'utf8');
701 expect(test1).to.equal('a b e c');
702 expect(test2).to.equal('a b e c');
703 done();
704 });
705 });
706
707 it('should not replace with missing replacement values', done => {
708 replace({
709 files: ['test1', 'test2', 'test3'],
710 from: [/re/g, /place/g],
711 to: ['b'],
712 }, () => {
713 const test1 = fs.readFileSync('test1', 'utf8');
714 const test2 = fs.readFileSync('test2', 'utf8');
715 expect(test1).to.equal('a b place c');
716 expect(test2).to.equal('a b place c');
717 done();
718 });
719 });
720
721 it('should work without expanding globs if disabled', done => {
722 replace({
723 files: ['test1', 'test2'],
724 from: /re\splace/g,
725 to: 'b',
726 disableGlobs: true,
727 }, () => {
728 const test1 = fs.readFileSync('test1', 'utf8');
729 const test2 = fs.readFileSync('test2', 'utf8');
730 expect(test1).to.equal('a b c');
731 expect(test2).to.equal('a b c');
732 done();
733 });
734 });
735
736 it('should count matches if specified in config', done => {
737 replace({
738 files: 'test1',
739 from: [/re/g, /place/g],
740 to: 'test',
741 countMatches: true,
742 }, (error, results) => {
743 expect(results[0].numMatches).to.equal(2);
744 done();
745 });
746 });
747
748 it('should not count matches if not specified in config', done => {
749 replace({
750 files: 'test1',
751 from: [/re/g, /place/g],
752 to: 'test',
753 }, (error, results) => {
754 expect(results[0].numMatches).to.be.undefined;
755 done();
756 });
757 });
758
759 it('should return 0 matches if match not found', done => {
760 replace({
761 files: 'test1',
762 from: 'nope',
763 to: 'test',
764 countMatches: true,
765 }, (error, results) => {
766 expect(results[0].numMatches).to.equal(0);
767 done();
768 });
769 });
770 });
771
772 /**
773 * Sync
774 */
775 describe('Sync', () => {
776
777 it('should throw an error when no config provided', () => {
778 expect(function() {
779 replace.sync();
780 }).to.throw(Error);
781 });
782
783 it('should throw an error when invalid config provided', () => {
784 expect(function() {
785 replace.sync(42);
786 }).to.throw(Error);
787 });
788
789 it('should throw an error when no `files` defined', () => {
790 expect(function() {
791 replace.sync({
792 from: /re\splace/g,
793 to: 'b',
794 });
795 }).to.throw(Error);
796 });
797
798 it('should throw an error when no `from` defined', () => {
799 expect(function() {
800 replace.sync({
801 files: 'test1',
802 to: 'b',
803 });
804 }).to.throw(Error);
805 });
806
807 it('should throw an error when no `to` defined', () => {
808 expect(function() {
809 replace.sync({
810 files: 'test1',
811 from: /re\splace/g,
812 });
813 }).to.throw(Error);
814 });
815
816 it('should support the encoding parameter', () => {
817 expect(function() {
818 replace.sync({
819 files: 'test1',
820 from: /re\splace/g,
821 to: 'b',
822 encoding: 'utf-8',
823 });
824 }).to.not.throw(Error);
825 });
826
827 it('should fall back to utf-8 encoding with invalid configuration', () => {
828 expect(function() {
829 replace.sync({
830 files: 'test1',
831 from: /re\splace/g,
832 to: 'b',
833 encoding: '',
834 });
835 }).to.not.throw(Error);
836 expect(function() {
837 replace.sync({
838 files: 'test1',
839 from: /re\splace/g,
840 to: 'b',
841 encoding: null,
842 });
843 }).to.not.throw(Error);
844 });
845
846 it('should replace contents in a single file with regex', function() {
847 replace.sync({
848 files: 'test1',
849 from: /re\splace/g,
850 to: 'b',
851 });
852 const test1 = fs.readFileSync('test1', 'utf8');
853 const test2 = fs.readFileSync('test2', 'utf8');
854 expect(test1).to.equal('a b c');
855 expect(test2).to.equal(testData);
856 });
857
858 it('should pass file as an arg to a "from" function', function() {
859 replace.sync({
860 files: 'test1',
861 from: (file) => {
862 expect(file).to.equal('test1');
863 return /re\splace/g;
864 },
865 to: 'b',
866 });
867 const test1 = fs.readFileSync('test1', 'utf8');
868 const test2 = fs.readFileSync('test2', 'utf8');
869 expect(test1).to.equal('a b c');
870 expect(test2).to.equal(testData);
871 });
872
873 it(`should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex`, function() {
874 replace.sync({
875 files: 'test1',
876 from: /re\splace/g,
877 to: (match, ...args) => {
878 const file = args.pop();
879 expect(match).to.equal('re place');
880 expect(file).to.equal('test1');
881 return 'b';
882 },
883 });
884 const test1 = fs.readFileSync('test1', 'utf8');
885 const test2 = fs.readFileSync('test2', 'utf8');
886 expect(test1).to.equal('a b c');
887 expect(test2).to.equal(testData);
888 });
889
890 it('should replace contents with a string replacement', function() {
891 replace.sync({
892 files: 'test1',
893 from: 're place',
894 to: 'b',
895 });
896 const test1 = fs.readFileSync('test1', 'utf8');
897 expect(test1).to.equal('a b c');
898 });
899
900 it(`should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement`, function() {
901 replace.sync({
902 files: 'test1',
903 from: 're place',
904 to: (match, ...args) => {
905 const file = args.pop();
906 expect(match).to.equal('re place');
907 expect(file).to.equal('test1');
908 return 'b';
909 },
910 });
911 const test1 = fs.readFileSync('test1', 'utf8');
912 expect(test1).to.equal('a b c');
913 });
914
915 it('should replace contents in a an array of files', function() {
916 replace.sync({
917 files: ['test1', 'test2'],
918 from: /re\splace/g,
919 to: 'b',
920 });
921 const test1 = fs.readFileSync('test1', 'utf8');
922 const test2 = fs.readFileSync('test2', 'utf8');
923 expect(test1).to.equal('a b c');
924 expect(test2).to.equal('a b c');
925 });
926
927 it('should expand globs', function() {
928 replace.sync({
929 files: 'test*',
930 from: /re\splace/g,
931 to: 'b',
932 });
933 const test1 = fs.readFileSync('test1', 'utf8');
934 const test2 = fs.readFileSync('test2', 'utf8');
935 expect(test1).to.equal('a b c');
936 expect(test2).to.equal('a b c');
937 });
938
939 it('should return a results array', function() {
940 const results = replace.sync({
941 files: 'test1',
942 from: /re\splace/g,
943 to: 'b',
944 });
945 expect(results).to.be.instanceof(Array);
946 expect(results).to.have.length(1);
947 expect(results[0].file).to.equal('test1');
948 });
949
950 it('should mark if something was replaced', function() {
951 const results = replace.sync({
952 files: 'test1',
953 from: /re\splace/g,
954 to: 'b',
955 });
956 expect(results[0].hasChanged).to.equal(true);
957 });
958
959 it('should not mark if nothing was replaced', function() {
960 const results = replace.sync({
961 files: 'test1',
962 from: 'nope',
963 to: 'b',
964 });
965 expect(results[0].hasChanged).to.equal(false);
966 });
967
968 it('should return corret results for multiple files', function() {
969 const results = replace.sync({
970 files: ['test1', 'test2', 'test3'],
971 from: /re\splace/g,
972 to: 'b',
973 });
974 expect(results).to.have.length(3);
975 expect(results[0].file).to.equal('test1');
976 expect(results[0].hasChanged).to.equal(true);
977 expect(results[1].file).to.equal('test2');
978 expect(results[1].hasChanged).to.equal(true);
979 expect(results[2].file).to.equal('test3');
980 expect(results[2].hasChanged).to.equal(false);
981 });
982
983 it('should make multiple replacements with the same string', () => {
984 replace.sync({
985 files: ['test1', 'test2', 'test3'],
986 from: [/re/g, /place/g],
987 to: 'b',
988 });
989 const test1 = fs.readFileSync('test1', 'utf8');
990 const test2 = fs.readFileSync('test2', 'utf8');
991 expect(test1).to.equal('a b b c');
992 expect(test2).to.equal('a b b c');
993 });
994
995 it('should make multiple replacements with different strings', () => {
996 replace.sync({
997 files: ['test1', 'test2', 'test3'],
998 from: [/re/g, /place/g],
999 to: ['b', 'e'],
1000 });
1001 const test1 = fs.readFileSync('test1', 'utf8');
1002 const test2 = fs.readFileSync('test2', 'utf8');
1003 expect(test1).to.equal('a b e c');
1004 expect(test2).to.equal('a b e c');
1005 });
1006
1007 it('should not replace with missing replacement values', () => {
1008 replace.sync({
1009 files: ['test1', 'test2', 'test3'],
1010 from: [/re/g, /place/g],
1011 to: ['b'],
1012 });
1013 const test1 = fs.readFileSync('test1', 'utf8');
1014 const test2 = fs.readFileSync('test2', 'utf8');
1015 expect(test1).to.equal('a b place c');
1016 expect(test2).to.equal('a b place c');
1017 });
1018
1019 it('should expand globs while excluding ignored files', () => {
1020 replace.sync({
1021 files: 'test*',
1022 ignore: 'test1',
1023 from: /re\splace/g,
1024 to: 'b',
1025 });
1026 const test1 = fs.readFileSync('test1', 'utf8');
1027 const test2 = fs.readFileSync('test2', 'utf8');
1028 expect(test1).to.equal('a re place c');
1029 expect(test2).to.equal('a b c');
1030 });
1031
1032 it('should support an array of ignored files', () => {
1033 replace.sync({
1034 files: 'test*',
1035 ignore: ['test1', 'test3'],
1036 from: /re\splace/g,
1037 to: 'b',
1038 });
1039 const test1 = fs.readFileSync('test1', 'utf8');
1040 const test2 = fs.readFileSync('test2', 'utf8');
1041 expect(test1).to.equal('a re place c');
1042 expect(test2).to.equal('a b c');
1043 });
1044
1045 it('should not fail when the ignore parameter is undefined', () => {
1046 replace.sync({
1047 files: 'test*',
1048 ignore: undefined,
1049 from: /re\splace/g,
1050 to: 'b',
1051 });
1052 const test1 = fs.readFileSync('test1', 'utf8');
1053 const test2 = fs.readFileSync('test2', 'utf8');
1054 expect(test1).to.equal('a b c');
1055 expect(test2).to.equal('a b c');
1056 });
1057
1058 it('should work without expanding globs if disabled', () => {
1059 replace.sync({
1060 files: ['test1', 'test2'],
1061 from: /re\splace/g,
1062 to: 'b',
1063 disableGlobs: true,
1064 });
1065 const test1 = fs.readFileSync('test1', 'utf8');
1066 const test2 = fs.readFileSync('test2', 'utf8');
1067 expect(test1).to.equal('a b c');
1068 expect(test2).to.equal('a b c');
1069 });
1070
1071 it('should not replace in a dry run', () => {
1072 replace.sync({
1073 files: ['test1', 'test2'],
1074 from: /re\splace/g,
1075 to: 'b',
1076 dry: true,
1077 });
1078 const test1 = fs.readFileSync('test1', 'utf8');
1079 const test2 = fs.readFileSync('test2', 'utf8');
1080 expect(test1).to.equal('a re place c');
1081 expect(test2).to.equal('a re place c');
1082 });
1083
1084 it('should return changed files for a dry run', () => {
1085 const results = replace.sync({
1086 files: ['test1', 'test2', 'test3'],
1087 from: /re\splace/g,
1088 to: 'b',
1089 dry: true,
1090 });
1091 expect(results).to.have.length(3);
1092 expect(results[0].file).to.equal('test1');
1093 expect(results[0].hasChanged).to.equal(true);
1094 expect(results[1].file).to.equal('test2');
1095 expect(results[1].hasChanged).to.equal(true);
1096 expect(results[2].file).to.equal('test3');
1097 expect(results[2].hasChanged).to.equal(false);
1098 });
1099
1100 it('should count matches and replacements if specified in config', () => {
1101 const results = replace.sync({
1102 files: 'test1',
1103 from: [/re/g, /place/g],
1104 to: 'test',
1105 countMatches: true,
1106 });
1107 expect(results[0].numMatches).to.equal(2);
1108 expect(results[0].numReplacements).to.equal(2);
1109 });
1110
1111 it('should differentiate between matches and replacements', () => {
1112 const results = replace.sync({
1113 files: 'test1',
1114 from: [/re/g, /place/g],
1115 to: 're',
1116 countMatches: true,
1117 });
1118 expect(results[0].numMatches).to.equal(2);
1119 expect(results[0].numReplacements).to.equal(1);
1120 });
1121
1122 it('should count multiple replacements correctly', () => {
1123 const results = replace.sync({
1124 files: 'test1',
1125 from: [/re/g, /place/g],
1126 to: 'place',
1127 countMatches: true,
1128 });
1129 expect(results[0].numMatches).to.equal(3);
1130 expect(results[0].numReplacements).to.equal(1);
1131 });
1132
1133 it(`should not count matches or replacements if not specified in config`, () => {
1134 const results = replace.sync({
1135 files: 'test1',
1136 from: [/re/g, /place/g],
1137 to: 'test',
1138 });
1139 expect(results[0].numMatches).to.be.undefined;
1140 expect(results[0].numReplacements).to.be.undefined;
1141 });
1142
1143 it('should return 0 matches and replacements if match not found', () => {
1144 const results = replace.sync({
1145 files: 'test1',
1146 from: 'nope',
1147 to: 'test',
1148 countMatches: true,
1149 });
1150 expect(results[0].numMatches).to.equal(0);
1151 expect(results[0].numReplacements).to.equal(0);
1152 });
1153 });
1154
1155 describe('module export', () => {
1156 it('default module export refers to async replace implementation', () => {
1157 expect(replace).to.be.a('function');
1158 });
1159
1160 it(`exports named replaceInFile, replaceInFileSync and sync from module facade`, () => {
1161 expect(replaceInFile).to.be.a('function');
1162 expect(replaceInFileSync).to.be.a('function');
1163 expect(sync).to.be.a('function');
1164 });
1165
1166 it('exposes inner functions as own fields of replace', () => {
1167 expect(replace.replaceInFile).to.equal(replace);
1168 expect(replace.sync).to.equal(replaceInFileSync);
1169 expect(replace.replaceInFileSync).to.equal(replaceInFileSync);
1170 });
1171 });
1172});