123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751 |
- 'use strict';
- /**
- * Dependencies
- */
- import transform, {sync, processFile, processFileSync} from './process-file';
- const fs = require('fs');
- const writeFile = Promise.promisify(fs.writeFile);
- const deleteFile = Promise.promisify(fs.unlink);
- /**
- * Specifications
- */
- describe('Process a file', () => {
- //Test JSON
- const testData = 'a re place c';
- /**
- * Prepare test files
- */
- beforeEach(() => Promise.all([
- writeFile('test1', testData, 'utf8'),
- writeFile('test2', testData, 'utf8'),
- writeFile('test3', 'nope', 'utf8'),
- ]));
- /**
- * Clean up test files
- */
- afterEach(() => Promise.all([
- deleteFile('test1'),
- deleteFile('test2'),
- deleteFile('test3'),
- ]));
- function fromToToProcessor(config) {
- const from = config.from;
- const to = config.to;
- delete config.from;
- delete config.to;
- config.processor = (content) => {
- return content.replace(from, to);
- };
- return config;
- }
- /**
- * Async with promises
- */
- describe('Async with promises', () => {
- it('should run processor', done => {
- transform({
- files: 'test1',
- processor: (input) => {
- return input.replace(/re\splace/g, 'b');
- },
- }).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- console.log(test1);
- expect(test1).to.equal('a b c');
- expect(test2).to.equal(testData);
- done();
- });
- });
- it('should throw an error when no config provided', () => {
- return expect(transform()).to.eventually.be.rejectedWith(Error);
- });
- it('should throw an error when invalid config provided', () => {
- return expect(transform(42)).to.eventually.be.rejectedWith(Error);
- });
- it('should throw an error when no `files` defined', () => {
- return expect(transform(fromToToProcessor({
- from: /re\splace/g,
- to: 'b',
- }))).to.eventually.be.rejectedWith(Error);
- });
- it('should replace contents in a single file with regex', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal(testData);
- done();
- });
- });
- it('should replace contents with a string replacement', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: 're place',
- to: 'b',
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- expect(test1).to.equal('a b c');
- done();
- });
- });
- it('should replace contents in a an array of files', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- it('should expand globs', done => {
- transform(fromToToProcessor({
- files: 'test*',
- from: /re\splace/g,
- to: 'b',
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- it('should expand globs while excluding ignored files', done => {
- transform(fromToToProcessor({
- files: 'test*',
- ignore: 'test1',
- from: /re\splace/g,
- to: 'b',
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a re place c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- it('should replace substrings', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /(re)\s(place)/g,
- to: '$2 $1',
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- expect(test1).to.equal('a place re c');
- done();
- });
- });
- it('should fulfill the promise on success', () => {
- return transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- })).should.be.fulfilled;
- });
- it('should reject the promise with an error on failure', () => {
- return expect(transform(fromToToProcessor({
- files: 'nope',
- from: /re\splace/g,
- to: 'b',
- }))).to.eventually.be.rejectedWith(Error);
- });
- it('should not reject the promise if allowEmptyPaths is true', () => {
- return transform(fromToToProcessor({
- files: 'nope',
- allowEmptyPaths: true,
- from: /re\splace/g,
- to: 'b',
- })).should.be.fulfilled;
- });
- it('should return a results array', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- })).then(results => {
- expect(results).to.be.instanceof(Array);
- expect(results).to.have.length(1);
- expect(results[0].file).to.equal('test1');
- done();
- });
- });
- it('should mark if something was replaced', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- })).then(results => {
- expect(results[0].hasChanged).to.equal(true);
- done();
- });
- });
- it('should not mark if nothing was replaced', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: 'nope',
- to: 'b',
- })).then(results => {
- expect(results[0].hasChanged).to.equal(false);
- done();
- });
- });
- it('should return correct results for multiple files', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2', 'test3'],
- from: /re\splace/g,
- to: 'b',
- })).then(results => {
- expect(results).to.have.length(3);
- expect(results[0].file).to.equal('test1');
- expect(results[0].hasChanged).to.equal(true);
- expect(results[1].file).to.equal('test2');
- expect(results[1].hasChanged).to.equal(true);
- expect(results[2].file).to.equal('test3');
- expect(results[2].hasChanged).to.equal(false);
- done();
- });
- });
- it('should not replace in a dry run', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- dry: true,
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a re place c');
- expect(test2).to.equal('a re place c');
- done();
- });
- });
- it('should return changed files for a dry run', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2', 'test3'],
- from: /re\splace/g,
- to: 'b',
- dry: true,
- })).then(results => {
- expect(results).to.have.length(3);
- expect(results[0].file).to.equal('test1');
- expect(results[0].hasChanged).to.equal(true);
- expect(results[1].file).to.equal('test2');
- expect(results[1].hasChanged).to.equal(true);
- expect(results[2].file).to.equal('test3');
- expect(results[2].hasChanged).to.equal(false);
- done();
- });
- });
- it('should accept glob configuration', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- allowEmptyPaths: true,
- glob: {
- ignore: ['test1'],
- },
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- expect(test1).to.equal('a re place c');
- done();
- });
- });
- it('should ignore empty glob configuration', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- glob: null,
- })).then(() => {
- const test1 = fs.readFileSync('test1', 'utf8');
- expect(test1).to.equal('a b c');
- done();
- });
- });
- });
- /**
- * Async with callback
- */
- describe('Async with callback', () => {
- it('should throw an error when no config provided', done => {
- transform(null, (error) => {
- expect(error).to.be.instanceof(Error);
- done();
- });
- });
- it('should throw an error when invalid config provided', done => {
- transform(42, (error) => {
- expect(error).to.be.instanceof(Error);
- done();
- });
- });
- it('should throw an error when no `files` defined', done => {
- transform(fromToToProcessor({
- from: /re\splace/g,
- to: 'b',
- }), (error) => {
- expect(error).to.be.instanceof(Error);
- done();
- });
- });
- it('should replace contents in a single file with regex', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }), () => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal(testData);
- done();
- });
- });
- it('should replace contents with a string replacement', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: 're place',
- to: 'b',
- }), () => {
- const test1 = fs.readFileSync('test1', 'utf8');
- expect(test1).to.equal('a b c');
- done();
- });
- });
- it('should replace contents in a an array of files', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- }), () => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- it('should expand globs', done => {
- transform(fromToToProcessor({
- files: 'test*',
- from: /re\splace/g,
- to: 'b',
- }), () => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- it('should expand globs while excluding ignored files', done => {
- transform(fromToToProcessor({
- files: 'test*',
- ignore: 'test1',
- from: /re\splace/g,
- to: 'b',
- }), () => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a re place c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- it('should not return an error on success', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }), (error) => {
- expect(error).to.equal(null);
- done();
- });
- });
- it('should return an error on failure', done => {
- transform(fromToToProcessor({
- files: 'nope',
- from: /re\splace/g,
- to: 'b',
- }), (error) => {
- expect(error).to.be.instanceof(Error);
- done();
- });
- });
- it('should not return an error if allowEmptyPaths is true', done => {
- transform(fromToToProcessor({
- files: 'nope',
- allowEmptyPaths: true,
- from: /re\splace/g,
- to: 'b',
- }), (error) => {
- expect(error).to.equal(null);
- done();
- });
- });
- it('should return a results array', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }), (error, results) => {
- expect(results).to.be.instanceof(Array);
- expect(results).to.have.length(1);
- expect(results[0].file).to.equal('test1');
- done();
- });
- });
- it('should mark if something was replaced', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }), (error, results) => {
- expect(results[0].hasChanged).to.equal(true);
- done();
- });
- });
- it('should not mark if nothing was replaced', done => {
- transform(fromToToProcessor({
- files: 'test1',
- from: 'nope',
- to: 'b',
- }), (error, results) => {
- expect(results[0].hasChanged).to.equal(false);
- done();
- });
- });
- it('should return correct results for multiple files', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2', 'test3'],
- from: /re\splace/g,
- to: 'b',
- }), (error, results) => {
- expect(results).to.have.length(3);
- expect(results[0].file).to.equal('test1');
- expect(results[0].hasChanged).to.equal(true);
- expect(results[1].file).to.equal('test2');
- expect(results[1].hasChanged).to.equal(true);
- expect(results[2].file).to.equal('test3');
- expect(results[2].hasChanged).to.equal(false);
- done();
- });
- });
- it('should work without expanding globs if disabled', done => {
- transform(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- disableGlobs: true,
- }), () => {
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- done();
- });
- });
- });
- /**
- * Sync
- */
- describe('Sync', () => {
- it('should throw an error when no config provided', () => {
- expect(function() {
- transform.sync();
- }).to.throw(Error);
- });
- it('should throw an error when invalid config provided', () => {
- expect(function() {
- transform.sync(42);
- }).to.throw(Error);
- });
- it('should throw an error when no `files` defined', () => {
- expect(function() {
- transform.sync(fromToToProcessor({
- from: /re\splace/g,
- to: 'b',
- }));
- }).to.throw(Error);
- });
- it('should support the encoding parameter', () => {
- expect(function() {
- transform.sync(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- encoding: 'utf-8',
- }));
- }).to.not.throw(Error);
- });
- it('should fall back to utf-8 encoding with invalid configuration', () => {
- expect(function() {
- transform.sync(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- encoding: '',
- }));
- }).to.not.throw(Error);
- expect(function() {
- transform.sync(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- encoding: null,
- }));
- }).to.not.throw(Error);
- });
- it('should replace contents in a single file with regex', function() {
- transform.sync(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal(testData);
- });
- it('should replace contents with a string replacement', function() {
- transform.sync(fromToToProcessor({
- files: 'test1',
- from: 're place',
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- expect(test1).to.equal('a b c');
- });
- it('should replace contents in a an array of files', function() {
- transform.sync(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- });
- it('should expand globs', function() {
- transform.sync(fromToToProcessor({
- files: 'test*',
- from: /re\splace/g,
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- });
- it('should return a results array', function() {
- const results = transform.sync(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }));
- expect(results).to.be.instanceof(Array);
- expect(results).to.have.length(1);
- expect(results[0].file).to.equal('test1');
- });
- it('should mark if something was replaced', function() {
- const results = transform.sync(fromToToProcessor({
- files: 'test1',
- from: /re\splace/g,
- to: 'b',
- }));
- expect(results[0].hasChanged).to.equal(true);
- });
- it('should not mark if nothing was replaced', function() {
- const results = transform.sync(fromToToProcessor({
- files: 'test1',
- from: 'nope',
- to: 'b',
- }));
- expect(results[0].hasChanged).to.equal(false);
- });
- it('should return corret results for multiple files', function() {
- const results = transform.sync(fromToToProcessor({
- files: ['test1', 'test2', 'test3'],
- from: /re\splace/g,
- to: 'b',
- }));
- expect(results).to.have.length(3);
- expect(results[0].file).to.equal('test1');
- expect(results[0].hasChanged).to.equal(true);
- expect(results[1].file).to.equal('test2');
- expect(results[1].hasChanged).to.equal(true);
- expect(results[2].file).to.equal('test3');
- expect(results[2].hasChanged).to.equal(false);
- });
- it('should expand globs while excluding ignored files', () => {
- transform.sync(fromToToProcessor({
- files: 'test*',
- ignore: 'test1',
- from: /re\splace/g,
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a re place c');
- expect(test2).to.equal('a b c');
- });
- it('should support an array of ignored files', () => {
- transform.sync(fromToToProcessor({
- files: 'test*',
- ignore: ['test1', 'test3'],
- from: /re\splace/g,
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a re place c');
- expect(test2).to.equal('a b c');
- });
- it('should not fail when the ignore parameter is undefined', () => {
- transform.sync(fromToToProcessor({
- files: 'test*',
- ignore: undefined,
- from: /re\splace/g,
- to: 'b',
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- });
- it('should work without expanding globs if disabled', () => {
- transform.sync(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- disableGlobs: true,
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a b c');
- expect(test2).to.equal('a b c');
- });
- it('should not replace in a dry run', () => {
- transform.sync(fromToToProcessor({
- files: ['test1', 'test2'],
- from: /re\splace/g,
- to: 'b',
- dry: true,
- }));
- const test1 = fs.readFileSync('test1', 'utf8');
- const test2 = fs.readFileSync('test2', 'utf8');
- expect(test1).to.equal('a re place c');
- expect(test2).to.equal('a re place c');
- });
- it('should return changed files for a dry run', () => {
- const results = transform.sync(fromToToProcessor({
- files: ['test1', 'test2', 'test3'],
- from: /re\splace/g,
- to: 'b',
- dry: true,
- }));
- expect(results).to.have.length(3);
- expect(results[0].file).to.equal('test1');
- expect(results[0].hasChanged).to.equal(true);
- expect(results[1].file).to.equal('test2');
- expect(results[1].hasChanged).to.equal(true);
- expect(results[2].file).to.equal('test3');
- expect(results[2].hasChanged).to.equal(false);
- });
- });
- describe('module export', () => {
- it('default module export refers to async replace implementation', () => {
- expect(transform).to.be.a('function');
- });
- it(`exports named processFile, processFileSync and sync from module facade`, () => {
- expect(processFile).to.be.a('function');
- expect(processFileSync).to.be.a('function');
- expect(sync).to.be.a('function');
- });
- it('exposes inner functions as own fields of transform', () => {
- expect(transform.processFile).to.equal(transform);
- expect(transform.sync).to.equal(processFileSync);
- expect(transform.processFileSync).to.equal(processFileSync);
- });
- });
- });
|