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

chore(NA): extend patches to lodash/fp and test for using template as an iteratee #13

Merged
merged 1 commit into from
Jul 2, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/setup_node_env/harden.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ hook(['child_process'], function (exports, name) {
return require(`./patches/${name}`)(exports); // eslint-disable-line import/no-dynamic-require
});

hook(['lodash'], function(exports, name) {
return require(`./patches/${name}`)(exports); // eslint-disable-line import/no-dynamic-require
hook(['lodash', 'lodash/fp'], function (exports) {
return require(`./patches/lodash`)(exports); // eslint-disable-line import/no-dynamic-require
});
6 changes: 3 additions & 3 deletions src/setup_node_env/patches/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
* under the License.
*/

module.exports = function(lodash) {
module.exports = function (lodash) {
lodash.template = new Proxy(lodash.template, {
apply: function(target, thisArg, args) {
apply: function (target, thisArg, args) {
var options;
if (args.length === 1) {
options = {
sourceURL: '',
};
} else {
options = { ...args[1] };
options = Object.assign({}, args[1]);
options.sourceURL = (options.sourceURL + '').replace(/\s/g, ' ');
}

Expand Down
30 changes: 24 additions & 6 deletions test/harden/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

require('../../src/setup_node_env');
const _ = require('elasticsearch/node_modules/lodash'); // our fork has been patched already, using a non-patched version
const _fp = require('elasticsearch/node_modules/lodash'); // our fork has been patched already, using a non-patched version
const test = require('tape');

Object.prototype.sourceURL = '\u2028\u2029\n;global.whoops=true'; // eslint-disable-line no-extend-native
Expand All @@ -27,33 +28,33 @@ test.onFinish(() => {
delete Object.prototype.sourceURL;
});

test('test setup ok', t => {
test('test setup ok', (t) => {
t.equal({}.sourceURL, '\u2028\u2029\n;global.whoops=true');
t.end();
});

test(`_.template('<%= foo %>')`, t => {
test(`_.template('<%= foo %>')`, (t) => {
const output = _.template('<%= foo %>')({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

test(`_.template('<%= foo %>', {})`, t => {
test(`_.template('<%= foo %>', {})`, (t) => {
const output = _.template('<%= foo %>', Object.freeze({}))({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

test(`_.template('<%= data.foo %>', { variable: 'data' })`, t => {
test(`_.template('<%= data.foo %>', { variable: 'data' })`, (t) => {
const output = _.template('<%= data.foo %>', Object.freeze({ variable: 'data' }))({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, t => {
test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, (t) => {
// throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do
const template = _.template('<% throw new Error() %>', Object.freeze({ sourceURL: '/foo/bar' }));
t.plan(2);
Expand All @@ -66,7 +67,7 @@ test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, t => {
}
});

test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true' })`, t => {
test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true' })`, (t) => {
// throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do
const template = _.template(
'<% throw new Error() %>',
Expand All @@ -82,6 +83,23 @@ test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true
}
});

test(`_.template used as an iteratee call(`, (t) => {
const templateStrArr = ['<%= data.foo %>', 'example <%= data.foo %>'];
const output = _.map(templateStrArr, _.template);

t.equal(output[0]({ data: { foo: 'bar' } }), 'bar');
t.equal(output[1]({ data: { foo: 'bar' } }), 'example bar');
t.equal(global.whoops, undefined);
t.end();
});

test(`_fp.template('<%= foo %>')`, (t) => {
const output = _fp.template('<%= foo %>')({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

function parsePathFromStack(stack) {
const lines = stack.split('\n');
// the frame starts at the second line
Expand Down