Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't treat strings as regex in insertIntoFile #7475

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/utilities/insert-into-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ function insertIntoFile(fullPath, contentsToInsert, providedOptions) {
contentsToWrite += contentsToInsert;
} else {
let contentMarker = options[insertBehavior];
let matches = contentsToWrite.match(contentMarker);
if (matches) {
contentMarker = matches[0];
if (contentMarker instanceof RegExp) {
let matches = contentsToWrite.match(contentMarker);
if (matches) {
contentMarker = matches[0];
}
}
let contentMarkerIndex = contentsToWrite.indexOf(contentMarker);

Expand Down
72 changes: 45 additions & 27 deletions tests/unit/utilities/insert-into-file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,39 +202,57 @@ describe('insertIntoFile()', function() {
});
});

it('options.after supports regex', function() {
let toInsert = 'blahzorz blammo';
let line1 = 'line1 is here';
let line2 = 'line2 here';
let line3 = 'line3';
let originalContent = [line1, line2, line3].join(EOL);
describe('regex', function() {
it('options.after supports regex', function() {
let toInsert = 'blahzorz blammo';
let line1 = 'line1 is here';
let line2 = 'line2 here';
let line3 = 'line3';
let originalContent = [line1, line2, line3].join(EOL);

fs.writeFileSync(filePath, originalContent, { encoding: 'utf8' });
fs.writeFileSync(filePath, originalContent, { encoding: 'utf8' });

return insertIntoFile(filePath, toInsert, { after: /line2 here(\r?\n)/ })
.then(function() {
let contents = fs.readFileSync(filePath, { encoding: 'utf8' });
return insertIntoFile(filePath, toInsert, { after: /line2 here(\r?\n)/ })
.then(function() {
let contents = fs.readFileSync(filePath, { encoding: 'utf8' });

expect(contents).to.equal([line1, line2, toInsert, line3].join(EOL),
'inserted contents should be inserted after the `after` value');
});
});
expect(contents).to.equal([line1, line2, toInsert, line3].join(EOL),
'inserted contents should be inserted after the `after` value');
});
});

it('options.before supports regex', function() {
let toInsert = 'blahzorz blammo';
let line1 = 'line1 is here';
let line2 = 'line2 here';
let line3 = 'line3';
let originalContent = [line1, line2, line3].join(EOL);
it('options.before supports regex', function() {
let toInsert = 'blahzorz blammo';
let line1 = 'line1 is here';
let line2 = 'line2 here';
let line3 = 'line3';
let originalContent = [line1, line2, line3].join(EOL);

fs.writeFileSync(filePath, originalContent, { encoding: 'utf8' });
fs.writeFileSync(filePath, originalContent, { encoding: 'utf8' });

return insertIntoFile(filePath, toInsert, { before: /line2 here(\r?\n)/ })
.then(function() {
let contents = fs.readFileSync(filePath, { encoding: 'utf8' });
return insertIntoFile(filePath, toInsert, { before: /line2 here(\r?\n)/ })
.then(function() {
let contents = fs.readFileSync(filePath, { encoding: 'utf8' });

expect(contents).to.equal([line1, toInsert, line2, line3].join(EOL),
'inserted contents should be inserted before the `before` value');
});
expect(contents).to.equal([line1, toInsert, line2, line3].join(EOL),
'inserted contents should be inserted before the `before` value');
});
});

it('options.after doesn\'t treat strings as regex', function() {
let toInsert = 'blahzorz blammo';

fs.writeFileSync(filePath, '', { encoding: 'utf8' });

expect(() => insertIntoFile(filePath, toInsert, { after: '"predef": [\n' })).to.not.throw();
});

it('options.before doesn\'t treat strings as regex', function() {
let toInsert = 'blahzorz blammo';

fs.writeFileSync(filePath, '', { encoding: 'utf8' });

expect(() => insertIntoFile(filePath, toInsert, { before: '"predef": [\n' })).to.not.throw();
});
});
});