Skip to content

Commit

Permalink
process.env.HARAKA_TEST_DIR (#27)
Browse files Browse the repository at this point in the history
* add process.env.HARAKA_TEST_DIR support
* add __dirname/../../config to config_dir_candidates
  • Loading branch information
msimerson committed Sep 20, 2017
1 parent 320406e commit 1d17634
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 149 deletions.
28 changes: 17 additions & 11 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@

# 1.0.13 - 2017-06-16
## 1.0.14 - 2017-09-19

- add __dirname/../../config to config_dir_candidates for haraka/Haraka/tests/*
- sync process.env.HARAKA_TEST_DIR from haraka/Haraka/config
- eslint no-var updates #25

## 1.0.13 - 2017-06-16

- lint updates for eslint 4

# 1.0.12 - 2017-05-21
## 1.0.12 - 2017-05-21

- unref() the setInterval so that Haraka can gracefully exit

# 1.0.11 - 2017-03-04
## 1.0.11 - 2017-03-04

- add config.getDir, loads all files in a directory

# 1.0.10 - 2017-02-05
## 1.0.10 - 2017-02-05

- log error vs throw on bad YAML
- log error vs throw on bad YAML
- fix appveyor badge URL

# 1.0.9 - 2017-01-27
## 1.0.9 - 2017-01-27

- config cache fix (see haraka/Haraka#1738)
- config: add overrides handling (sync with Haraka)
Expand All @@ -25,24 +31,24 @@
- use haraka-eslint plugin (vs local copy of .eslintrc)
- lint updates

# 1.0.8 - 2017-01-02
## 1.0.8 - 2017-01-02

- version bump, lint updates & sync
- lint fixes

# 1.0.7 - 2016-11-17
## 1.0.7 - 2016-11-17

- update tests for appveyor (Windows) compatibility #9

# 1.0.6 - 2016-11-10
## 1.0.6 - 2016-11-10

- handle invalid .ini lines properly (skip them)

# 1.0.5 - 2016-10-25
## 1.0.5 - 2016-10-25

- do not leave behind a `*` section in config (due to wildcard boolean)

# 1.0.3
## 1.0.3

- added wildcard boolean support
- reduce node required 4.3 -> 0.10.43
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 1.0.{build}

environment:
nodejs_version: "4"
nodejs_version: "6"

# Install scripts. (runs after repo cloning)
install:
Expand Down
10 changes: 6 additions & 4 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function Config (root_path, no_overrides) {
cfg.overrides_path = path.join(overrides_path, 'config');
}
return cfg;
};
}
if (process.env.HARAKA_TEST_DIR) {
this.root_path = path.join(process.env.HARAKA_TEST_DIR, 'config');
return;
}
if (process.env.HARAKA && !no_overrides) {
this.overrides_path = root_path || cfreader.config_path;
this.root_path = path.join(process.env.HARAKA, 'config');
Expand Down Expand Up @@ -59,9 +63,7 @@ function merge_config (defaults, overrides, type) {
return overrides;
}

if (overrides != null) {
return overrides;
}
if (overrides != null) return overrides;

return defaults;
}
Expand Down
17 changes: 10 additions & 7 deletions configfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ cfreader._enoent_files = {};
cfreader._sedation_timers = {};
cfreader._overrides = {};

const config_dir_candidates = [
let config_dir_candidates = [
// these work when this file is loaded as require('./config.js')
path.join(__dirname, 'config'), // Haraka ./config dir
__dirname, // npm packaged plugins
];
Expand All @@ -47,10 +48,12 @@ function get_path_to_config_dir () {
return;
}

// these work when this is loaded with require('haraka-config')
if (/node_modules\/haraka-config$/.test(__dirname)) {
// loaded by a npm packaged module
cfreader.config_path = path.resolve(__dirname, '..', '..');
return;
config_dir_candidates = [
path.join(__dirname, '..', '..', 'config'), // haraka/Haraka/*
path.join(__dirname, '..', '..'), // npm packaged modules
]
}

for (let i=0; i < config_dir_candidates.length; i++) {
Expand Down Expand Up @@ -271,16 +274,16 @@ cfreader.read_dir = function (name, opts, done) {
.then((result) => {
return fsReadDir(name);
})
.then((result2) => {
.then((fileList) => {
const reader = require('./readers/' + type);
const promises = [];
result2.forEach(function (file) {
fileList.forEach((file) => {
promises.push(reader.loadPromise(path.resolve(name, file)))
});
return Promise.all(promises);
})
.then((fileList) => {
// console.log(fileList);
// console.log(fileList);
done(null, fileList);
})
.catch((error) => {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"name": "haraka-config",
"license": "MIT",
"description": "Haraka's config file loader",
"version": "1.0.13",
"version": "1.0.14",
"homepage": "http://haraka.github.io",
"repository": {
"type": "git",
"url": "git@github.com:haraka/haraka-config.git"
},
"main": "config.js",
"engines": {
"node": ">= 4"
"node": ">= 6"
},
"dependencies": {
"js-yaml": "^3.5.3"
Expand All @@ -23,13 +23,13 @@
"nodeunit": "*"
},
"bugs": {
"mail": "helpme@gmail.com",
"mail": "haraka.mail@gmail.com",
"url": "https://github.com/haraka/haraka-config/issues"
},
"scripts": {
"test": "./run_tests",
"lint": "./node_modules/.bin/eslint *.js test/**/*.js",
"lintfix": "./node_modules/.bin/eslint --fix *.js test/**/*.js",
"lint": "./node_modules/.bin/eslint *.js test/*.js test/*/*.js",
"lintfix": "./node_modules/.bin/eslint --fix *.js test/*.js test/*/*.js",
"cover": "./node_modules/.bin/istanbul cov run_tests"
}
}
77 changes: 43 additions & 34 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

process.env.NODE_ENV = 'test'

var fs = require('fs');
var path = require('path');
const fs = require('fs');
const path = require('path');

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

function clearRequireCache () {
// node_unit runs all the tests in the same process, so the process.env
Expand Down Expand Up @@ -36,7 +36,7 @@ exports.config = {
},
'module_config' : function (test) {
test.expect(2);
var c = this.config.module_config('foo', 'bar');
const c = this.config.module_config('foo', 'bar');
test.equal(c.root_path, path.join('foo','config'));
test.equal(c.overrides_path, path.join('bar','config'));
test.done();
Expand All @@ -48,7 +48,7 @@ exports.config_path = {
test.expect(1);
process.env.HARAKA = '/tmp';
clearRequireCache();
var config = require('../config');
const config = require('../config');
// console.log(config);
test.equal(config.root_path, path.join('/tmp','config'));
test.done();
Expand All @@ -58,9 +58,9 @@ exports.config_path = {
process.env.HARAKA = '';
process.env.NODE_ENV = 'not-test';
clearRequireCache();
var config = require('../config');
const config = require('../config');
// ./config doesn't exist so path will be resolved ./
test.ok(/haraka\-config$/.test(config.root_path));
test.ok(/haraka-config$/.test(config.root_path));
process.env.NODE_ENV = 'test';
test.done();
},
Expand Down Expand Up @@ -174,13 +174,13 @@ exports.arrange_args = {
},
};

var jsonRes = {
const jsonRes = {
matt: 'waz here',
array: [ 'has an element' ],
objecty: { 'has a property': 'with a value' }
};

var yamlRes = {
const yamlRes = {
main: {
bool_true: true,
bool_false: false,
Expand All @@ -206,8 +206,8 @@ var yamlRes = {

function _test_get (test, name, type, callback, options, expected) {
test.expect(1);
var config = require('../config');
var cfg = config.get(name, type, callback, options);
const config = require('../config');
const cfg = config.get(name, type, callback, options);
test.deepEqual(cfg, expected);
test.done();
}
Expand All @@ -220,7 +220,7 @@ exports.get = {
},
'test (non-existing, cached)' : function (test) {
test.expect(1);
var cfg = this.config.get('test', null, null);
const cfg = this.config.get('test', null, null);
test.deepEqual(cfg, null);
test.done();
},
Expand Down Expand Up @@ -296,7 +296,7 @@ exports.get = {
// config.get('test.bin', 'binary');
'test.bin, type=binary' : function (test) {
test.expect(2);
var res = this.config.get('test.binary', 'binary');
const res = this.config.get('test.binary', 'binary');
test.equal(res.length, 120);
test.ok(Buffer.isBuffer(res));
test.done();
Expand All @@ -307,28 +307,28 @@ exports.merged = {
'setUp' : setUp,
'before_merge' : function (test) {
test.expect(1);
var lc = this.config.module_config(
const lc = this.config.module_config(
path.join('test','default')
);
test.deepEqual(lc.get('test.ini'),
{ main: {}, defaults: { one: 'one', two: 'two' } }
);
);
test.done();
},
'after_merge': function (test) {
test.expect(1);
var lc = this.config.module_config(
const lc = this.config.module_config(
path.join('test','default'),
path.join('test','override')
);
test.deepEqual(lc.get('test.ini'),
{ main: {}, defaults: { one: 'three', two: 'four' } }
);
);
test.done();
},
'flat overridden' : function (test) {
test.expect(1);
var lc = this.config.module_config(
const lc = this.config.module_config(
path.join('test','default'),
path.join('test','override')
);
Expand All @@ -337,12 +337,23 @@ exports.merged = {
},
}

const tmpFile = path.resolve('test', 'config', 'dir', '4.ext');

function cleanup (done) {
fs.unlink(tmpFile, () => {
done();
})
}

exports.getDir = {
'setUp' : setUp,
'setUp' : function (done) {
process.env.HARAKA = '';
clearRequireCache();
this.config = require('../config');
cleanup(done);
},
'tearDown' : function (done) {
fs.unlink(path.resolve('test','config','dir', '4.ext'), function () {
done();
})
cleanup(done);
},
'loads all files in dir' : function (test) {
test.expect(4);
Expand All @@ -365,10 +376,9 @@ exports.getDir = {
},
'reloads when file in dir is touched' : function (test) {
test.expect(6);
var self = this;
var tmpFile = path.resolve('test','config','dir', '4.ext');
var callCount = 0;
var getDirDone = function (err, files) {
const self = this;
let callCount = 0;
function getDirDone (err, files) {
// console.log('Loading: test/config/dir');
if (err) console.error(err);
callCount++;
Expand All @@ -378,22 +388,21 @@ exports.getDir = {
test.equal(files.length, 3);
test.equal(files[0].data, 'contents1\n');
test.equal(files[2].data, 'contents3\n');
fs.writeFile(tmpFile, 'contents4\n', function (err, res) {
test.equal(err, null);
fs.writeFile(tmpFile, 'contents4\n', (err2, res) => {
test.equal(err2, null);
// console.log('file touched, waiting for callback');
// console.log(res);
});
return;
}
if (callCount === 2) {
test.equal(files[3].data, 'contents4\n');
test.done();
}
}
var getDir = function () {
var opts = { type: 'binary', watchCb: getDir };
self.config.getDir('dir', opts, getDirDone);
};
function getDir () {
const opts2 = { type: 'binary', watchCb: getDir };
self.config.getDir('dir', opts2, getDirDone);
}
getDir();
}
}
Loading

0 comments on commit 1d17634

Please sign in to comment.