Skip to content

Commit

Permalink
add back merged flat & ini tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 21, 2016
1 parent 6ff269d commit e833af7
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 47 deletions.
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"node": true
},
"rules": {
"dot-notation": 2,
"indent": [2, 4, {"SwitchCase": 1}],
"one-var": [2, "never"],
"no-trailing-spaces": [2, { "skipBlankLines": false }],
"keyword-spacing": [2, {"overrides": {
}}]
}
}
48 changes: 23 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,11 @@

Haraka config file loader and parser


# Config Files

Haraka's config loader can load several types of configuration files.

The API is fairly simple:

// From within a plugin:
var cfg = this.config.get(name, [type], [callback], [options]);

`name` is not a full path, but a filename in the config/ directory. For example:
## Config file type/formats

var cfg = this.config.get('rambling.paths', 'list');

This will load the file config/rambling.paths in the Haraka directory.

# Config file type/formats

`type` can be one of:
Haraka's config loader can load several types of configuration files.

* 'value' - load a flat file containing a single value (default)
* 'ini' - load an ini file
Expand All @@ -33,17 +19,29 @@ This will load the file config/rambling.paths in the Haraka directory.
* 'data' - load a flat file containing a list of values, keeping comments and whitespace.
* 'binary' - load a binary file into a Buffer

If the ini, json, or yaml files have `.ini`, `.json` or `.yaml` suffixes,
the `type` parameter can be omitted.

See the [File Formats](#file_formats) section below for a more detailed
explaination of each of the formats.

# Usage

// From within a plugin:
var cfg = this.config.get(name, [type], [callback], [options]);

This will load the file config/rambling.paths in the Haraka directory.

`name` is not a full path, but a filename in the config/ directory. For example:

var cfg = this.config.get('rambling.paths', 'list');

`type` can be any of the types listed above.

If the file name has an `.ini`, `.json` or `.yaml` suffix,
the `type` parameter can be omitted.

`callback` is an optional callback function that will be called when
an update is detected on the file after the configuration cache has been
updated by re-reading the file. You can use this to refresh configuration
variables within your plugin if you are not calling `config.get` within
one of the hooks (e.g. if you use the `register()` function):
updated by re-reading the file. Use this to refresh configuration
variables within your plugin. Example:

`````javascript
var cfg; // variable global to this plugin only
Expand All @@ -56,20 +54,20 @@ exports.register = function () {

exports.load_my_plugin_ini = function () {
var plugin = this;
plugin.cfg = plugin.config.get('my_plugin.ini', function () {
plugin.cfg = plugin.config.get('my_plugin.ini', function onIniChange () {
// This closure is run a few milliseconds after my_plugin.ini changes
// Re-run the outer function again
plugin.load_my_plugin_ini();
});
plugin.loginfo('cfg=' + JSON.stringify(cfg));
plugin.loginfo('cfg=' + JSON.stringify(plugin.cfg));
}

exports.hook_connect = function (next, connection) {
// plugin.cfg in here will always be up-to-date
}
`````

The optional `options` object can accepts the following keys:
The `options` object can accepts the following keys:

* `no_watch` (default: false) - prevents Haraka from watching for updates.
* `no_cache` (default: false) - prevents Haraka from caching the file. This
Expand Down
65 changes: 43 additions & 22 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

var path = require('path');

// var Plugin = require('./fixtures/stub_plugin');
var config = require('../config');
// var plugin = require('../plugins');

var cb = function () { return false; };
var opts = { booleans: ['arg1'] };

exports.module_config = {
'new' : function (test) {
test.expect(1);
var c = config.module_config('foo', 'bar');
test.ok(c.root_path);
test.done();
}
};

exports.arrange_args = {
// config.get('name');
'name' : function (test) {
Expand Down Expand Up @@ -148,7 +155,7 @@ var yamlRes = {

function _test_get(test, name, type, callback, options, expected) {
test.expect(1);
test.deepEqual(config.get(name,type,callback,options), expected);
test.deepEqual(config.get(name, type, callback, options), expected);
test.done();
}

Expand Down Expand Up @@ -237,22 +244,36 @@ exports.get = {
},
};

// exports.plugin_get_merge = {
// 'INSTALLED node_modules package plugin: (test-plugin)': function (test) {
// process.env.HARAKA = path.resolve(__dirname, '..', 'tests', 'installation');

// var p = new plugin.Plugin('test-plugin');

// test.expect(2);
// var flat_config = p.config.get('test-plugin-flat');
// test.equal(flat_config, 'flatisloaded');
// var ini_config = p.config.get('test-plugin.ini', 'ini');
// test.deepEqual(ini_config, {
// main: { main1: 'foo', main2: 'blah' },
// sub1: { sub1: 'foo', sub2: 'blah' },
// sub2: { sub1: 'foo', sub2: 'foo' },
// sub3: { new: 'foo' }
// });
// test.done();
// },
// }
exports.merged = {
'before_merge' : function (test) {
test.expect(1);
this.config = require('../config').module_config(
path.join('test','default')
);
test.deepEqual(this.config.get('test.ini'),
{ main: {}, defaults: { one: 'one', two: 'two' } }
);
test.done();
},
'after_merge': function (test) {
test.expect(1);
this.config = require('../config').module_config(
path.join('test','default'),
path.join('test','override')
);
test.deepEqual(this.config.get('test.ini'),
{ main: {}, defaults: { one: 'three', two: 'four' } }
);
test.done();
},
'flat overridden' : function (test) {
test.expect(1);
this.config = require('../config').module_config(
path.join('test','default'),
path.join('test','override')
);
var cfg = this.config.get('test.flat');
test.equal(cfg, 'flatoverrode');
test.done();
},
}
1 change: 1 addition & 0 deletions test/default/config/test.flat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flatisloaded
5 changes: 5 additions & 0 deletions test/default/config/test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

[defaults]
one=one
two=two

1 change: 1 addition & 0 deletions test/override/config/test.flat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flatoverrode
5 changes: 5 additions & 0 deletions test/override/config/test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

[defaults]
one=three
two=four

0 comments on commit e833af7

Please sign in to comment.