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

Config object must not be extendable after first call to get() #473

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ util.makeImmutable = function(object, property, value) {
}
}

// Make sure new properties cannot be added in the future
Object.preventExtensions(object);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is available in all nodejs versions supported by node-config. Travis tests failing, probably the indication.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the issue comes from Object.preventExtensions. Another test is suddenly failing but I'm not sure why. Any idea ?

Configuration for module developers
    ✗ Prototypes are applied by setModuleDefaults even if no previous config exists for the module 
      TypeError: Cannot read property 'parm1' of undefined 
      at /home/travis/build/lorenwest/node-config/lib/config.js:1485:37 
      at Array.forEach (<anonymous>) 
      at Config.util.extendDeep (/home/travis/build/lorenwest/node-config/lib/config.js:1478:9) 
      at Config.util.setModuleDefaults (/home/travis/build/lorenwest/node-config/lib/config.js:427:8) 
      at Object.Prototypes are applied by setModuleDefaults even if no previous config exists for the module (/home/travis/build/lorenwest/node-config/test/2-config-test.js:358:19) 
      at runTest (/home/travis/build/lorenwest/node-config/node_modules/vows/lib/vows.js:136:26) 
      at EventEmitter.<anonymous> (/home/travis/build/lorenwest/node-config/node_modules/vows/lib/vows.js:81:9) 
      at emitOne (events.js:121:20) 
      at EventEmitter.emit (events.js:211:7) 
      at EventEmitter.options.Emitter.emit (/home/travis/build/lorenwest/node-config/node_modules/vows/lib/vows.js:241:24) 
      at /home/travis/build/lorenwest/node-config/node_modules/vows/lib/vows/suite.js:170:45 
      at _combinedTickCallback (internal/process/next_tick.js:131:7) 
      at process._tickCallback (internal/process/next_tick.js:180:9) 
      at Function.Module.runMain (module.js:686:11) 
      at startup (bootstrap_node.js:187:16) 
      at bootstrap_node.js:608:3

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are not failing on the main branch, it must be something here.

According to http://node.green/ Object.preventExtensions() is partially supported on Node 4. I'm not clear if the functionality we need from it is there are not. We could check to see if the preventExtensions method exists on the Object class, and if so, add in this extra safety check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My last message was not very well phrased, what I meant is that my modifications broke another, unrelated test. This is true for all node versions on Travis.

The tests added in this branch pass for all versions of Node, even 4. Do you still want to add the check ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still interested if the other test failures can be resolved without regressions in another feature.


return object;
};

Expand Down
13 changes: 12 additions & 1 deletion test/2-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,21 @@ vows.describe('Test suite for node-config')
assert.equal(CONFIG.MuteThis, 'world');
},

'Added props sticks': function () {
CONFIG.ExtendThis.hello = 'world';
assert.equal(CONFIG.ExtendThis.hello, 'world');
},

'No mutation after the first get()': function () {
assert.equal(CONFIG.get('MuteThis'), 'world');
CONFIG.MuteThis = 'backToHello';
assert.equal(CONFIG.MuteThis, 'world');
},

'No extensions after the first get()': function () {
CONFIG.ExtendThis.newProp = 'shouldNotStick';
assert.equal(CONFIG.ExtendThis.hello, 'world');
assert.equal(typeof CONFIG.ExtendThis.newProp, 'undefined');
}
},

Expand Down Expand Up @@ -352,4 +363,4 @@ vows.describe('Test suite for node-config')
}
},
})
.export(module);
.export(module);
1 change: 1 addition & 0 deletions test/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
parm2: 22
},
MuteThis: 'hello',
ExtendThis: {},
get customerDbPort() {
return '' + this.Customers.dbPort;
},
Expand Down