Merge pull request #4 from WindowsAzure/release-0.0.2
Release 0.0.2 merge to master
This commit is contained in:
Коммит
dd873e4956
25
README.md
25
README.md
|
@ -72,3 +72,28 @@ c3.configure('production', function (c) {
|
|||
c.useSql('realDatabase', 'actualDb');
|
||||
});
|
||||
```
|
||||
|
||||
Are you making changes to a global configuration in your unit tests, and want
|
||||
to ensure you've restored the state after your test? Use a snapshot:
|
||||
|
||||
```javascript
|
||||
|
||||
var c4 = envconf.createConfig();
|
||||
c4.configure(function (c) {
|
||||
c.set('originalValue', 'one');
|
||||
});
|
||||
|
||||
// set up contents of c4
|
||||
|
||||
var snapshot = c4.snapshot();
|
||||
|
||||
c4.configure(function (c) {
|
||||
c.set('originalValue', 'two');
|
||||
});
|
||||
|
||||
c4.restore(snapshot);
|
||||
|
||||
c4.get('originalValue').should.equal('one');
|
||||
```
|
||||
|
||||
Snapshot/restore also saves and restores any child configurations.
|
||||
|
|
31
lib/index.js
31
lib/index.js
|
@ -15,6 +15,20 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
function Snapshot(settings, environments) {
|
||||
var self = this;
|
||||
self.settings = {};
|
||||
self.environments = {};
|
||||
|
||||
Object.keys(settings).forEach(function (key) {
|
||||
self.settings[key] = settings[key];
|
||||
});
|
||||
|
||||
Object.keys(environments).forEach(function (key) {
|
||||
self.environments[key] = environments[key].snapshot();
|
||||
});
|
||||
}
|
||||
|
||||
function Config(parent, defaultEnvVar, customizer) {
|
||||
var environments = { };
|
||||
var settings = { };
|
||||
|
@ -60,6 +74,23 @@ function Config(parent, defaultEnvVar, customizer) {
|
|||
return settings.hasOwnProperty(setting) || (!!parent && parent.has(setting));
|
||||
};
|
||||
|
||||
config.snapshot = function () {
|
||||
return new Snapshot(settings, environments);
|
||||
};
|
||||
|
||||
config.restore = function (snapshot) {
|
||||
if (!(snapshot instanceof Snapshot)) {
|
||||
throw new Error('Invalid snapshot');
|
||||
}
|
||||
|
||||
settings = snapshot.settings;
|
||||
|
||||
environments = {};
|
||||
Object.keys(snapshot.environments).forEach(function (key) {
|
||||
config(key).restore(snapshot.environments[key]);
|
||||
});
|
||||
};
|
||||
|
||||
Object.defineProperties(config, {
|
||||
default: {
|
||||
get: function () {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
"Rodrigues, Andre <andrerod@microsoft.com>",
|
||||
"Tavares, Chris <ctavares@microsoft.com>"
|
||||
],
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"description": "a module for express-style programmatic configuration",
|
||||
"main": "lib/index.js",
|
||||
"tags": ["config", "environment"],
|
||||
|
|
|
@ -280,3 +280,55 @@ describe('Config customization', function () {
|
|||
spy.callCount.should.equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Snapshot and restore', function () {
|
||||
it('should save and restore configuration state', function () {
|
||||
var c = envconf.createConfig();
|
||||
c.configure(function () {
|
||||
c.set('first', 'one');
|
||||
c.set('second', 'two');
|
||||
});
|
||||
|
||||
var ss = c.snapshot();
|
||||
c.set('first', 'changed');
|
||||
c.set('added', 'new value');
|
||||
|
||||
c.restore(ss);
|
||||
|
||||
c.get('first').should.equal('one');
|
||||
c.get('second').should.equal('two');
|
||||
should.not.exist(c.get('added'));
|
||||
});
|
||||
|
||||
it('should save and restore child environments', function () {
|
||||
var c = envconf.createConfig();
|
||||
c.configure('dev', function (dev) {
|
||||
dev.set('devFirst', 'one');
|
||||
dev.configure('devsub', function (devsub) {
|
||||
devsub.set('subsubFirst', 'a sub sub value');
|
||||
});
|
||||
});
|
||||
|
||||
c.configure('prod', function (prod) {
|
||||
prod.set('prodvalue', 'a value');
|
||||
});
|
||||
|
||||
var snapshot = c.snapshot();
|
||||
|
||||
c('dev').set('devFirst', 'changed value');
|
||||
c('prod').configure('addedEnvironment', function (added) {
|
||||
added.set('newEnvValue', 1234);
|
||||
});
|
||||
|
||||
c.restore(snapshot);
|
||||
|
||||
c('dev').get('devFirst').should.equal('one');
|
||||
c('prod').environments.should.not.include('addedEnvironment');
|
||||
});
|
||||
|
||||
it('should fail if trying to restore an invalid snapshot', function () {
|
||||
var c = envconf.createConfig();
|
||||
|
||||
(function () { c.restore([{}, {}]); }).should.throw();
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче