Skip to content

Commit

Permalink
Merge pull request #2 from pelias/expose_config_loader
Browse files Browse the repository at this point in the history
extract config loader
  • Loading branch information
missinglink authored Jan 17, 2019
2 parents f1ce479 + 8781da8 commit fbc89c9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 27 deletions.
32 changes: 5 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const config = require('pelias-config');
const through = require('through2');
const stream = require('./stream');
const parser = require('./parser');
const loader = require('./loader');

function blacklistStream( blacklist ){

Expand All @@ -19,37 +18,16 @@ function blacklistStream( blacklist ){

// no blacklist was provided via function arguments

// check pelias-config for a list of blacklist files to load
const settings = config.generate();

// config does not contain the relevant properties
// return a no-op passthrough stream
if( !settings.imports || !settings.imports.blacklist ){
return through.obj();
}

// config does not contain a valid list of files
// return a no-op passthrough stream
const bl = settings.imports.blacklist;
if( !Array.isArray( bl.files ) || bl.files.length === 0 ){
return through.obj();
}

// load the blacklist files
// note: this throws an exception if a file is not found
const blacklists = bl.files.map( filename => parser( filename ) );

// merge all the blacklists togther
var merged = {};
blacklists.forEach( b => { for( var k in b ){ merged[k] = b[k]; } } );
// attempt to load the blacklist data from the pelias config file
blacklist = loader();

// blacklist is empty, return a no-op passthrough stream
if( Object.keys( merged ).length === 0 ){
if( Object.keys( blacklist ).length === 0 ){
return through.obj();
}

// return a blacklist stream based off files specified in pelias-config
return stream( merged );
return stream( blacklist );
}

module.exports = blacklistStream;
40 changes: 40 additions & 0 deletions loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const config = require('pelias-config');
const parser = require('./parser');

// load the pelias config file and return a merged
// object containing the blacklist data.
function loader(){

// check pelias-config for a list of blacklist files to load
const settings = config.generate();

// config does not contain the relevant properties
// return a no-op passthrough stream
if( !settings.imports || !settings.imports.blacklist ){
return {};
}

// config does not contain a valid list of files
// return a no-op passthrough stream
const bl = settings.imports.blacklist;
if( !Array.isArray( bl.files ) || bl.files.length === 0 ){
return {};
}

// load the blacklist files
// note: this throws an exception if a file is not found
const blacklists = bl.files.map( filename => parser( filename ) );

// merge all the blacklists togther
var merged = {};
blacklists.forEach( b => { for( var k in b ){ merged[k] = b[k]; } } );

// blacklist is empty, return a no-op passthrough stream
if( Object.keys( merged ).length === 0 ){
return {};
}

return merged;
}

module.exports = loader;
85 changes: 85 additions & 0 deletions test/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const fs = require('fs');
const loader = require('../loader');

module.exports.tests = {};

module.exports.tests.loader = function(test, common) {

// pelias config missing imports property
test('pelias config missing imports property', function(t) {

// write a temporary config for testing
fs.writeFileSync('/tmp/tmp-pelias-config1.json', `{}`);
process.env.PELIAS_CONFIG = '/tmp/tmp-pelias-config1.json';

const blacklist = loader();
t.equals( Object.keys( blacklist ).length, 0 );
t.end();

// clean up
delete process.env.PELIAS_CONFIG;
});

// pelias config imports.blacklist.files property not an array
test('pelias config imports.blacklist.files property not an array', function(t) {

// write a temporary config for testing
fs.writeFileSync('/tmp/tmp-pelias-config2.json', `{ "imports": { "blacklist": { "files": "foo" } } }`);
process.env.PELIAS_CONFIG = '/tmp/tmp-pelias-config2.json';

const blacklist = loader();
t.equals( Object.keys( blacklist ).length, 0 );
t.end();

// clean up
delete process.env.PELIAS_CONFIG;
});

// pelias config imports.blacklist.files property empty
test('pelias config imports.blacklist.files property empty', function(t) {

// write a temporary config for testing
fs.writeFileSync('/tmp/tmp-pelias-config3.json', `{ "imports": { "blacklist": { "files": [] } } }`);
process.env.PELIAS_CONFIG = '/tmp/tmp-pelias-config3.json';

const blacklist = loader();
t.equals( Object.keys( blacklist ).length, 0 );
t.end();

// clean up
delete process.env.PELIAS_CONFIG;
});

// pelias config imports.blacklist.files contains valid files
test('pelias config imports.blacklist.files property empty', function(t) {

// write a temporary files for testing
fs.writeFileSync('/tmp/tmp-pelias-blacklist1', `source1:layer1:1`);
fs.writeFileSync('/tmp/tmp-pelias-blacklist2', `source2:layer2:2`);
fs.writeFileSync('/tmp/tmp-pelias-config4.json', `{
"imports": { "blacklist": { "files": [
"/tmp/tmp-pelias-blacklist1",
"/tmp/tmp-pelias-blacklist2"
] }}
}`);
process.env.PELIAS_CONFIG = '/tmp/tmp-pelias-config4.json';

const blacklist = loader();
t.equals( Object.keys( blacklist ).length, 2 );
t.end();

// clean up
delete process.env.PELIAS_CONFIG;
});
};

module.exports.all = function (tape, common) {

function test(name, testFunction) {
return tape('loader: ' + name, testFunction);
}

for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
1 change: 1 addition & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const common = {
const tests = [
require('./parser.js'),
require('./stream.js'),
require('./loader.js'),
require('./index.js'),
];

Expand Down

0 comments on commit fbc89c9

Please sign in to comment.