config-utilities
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -5,2 +5,15 @@ # Changelog | ||
## [v1.1.0] - 2019-01-20 | ||
### Added | ||
- Code coverage | ||
- CI support | ||
- NPM ignore file | ||
### Fixed | ||
- Wrong test file structure didn't allow for execution of two tests | ||
- README.md documentation for `resolvePaths({ normalize })` not showing default value | ||
### Changed | ||
- Removed test categories "Options" and "Arguments" | ||
## [v1.0.4] - 2019-01-14 | ||
@@ -7,0 +20,0 @@ ### Fixed |
{ | ||
"name": "config-utilities", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Configuration file utilities", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npx mocha", | ||
"test": "npx nyc mocha --exit", | ||
"lint": "npx eslint --fix *.js" | ||
@@ -29,2 +29,3 @@ }, | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0", | ||
"sinon": "^7.2.2", | ||
@@ -31,0 +32,0 @@ "sinon-chai": "^3.3.0" |
@@ -5,2 +5,5 @@ # Configuration utilities | ||
[](https://gitlab.com/qafir/node-config-utilities/commits/master) | ||
[](https://gitlab.com/qafir/node-config-utilities/commits/master) | ||
## Functions | ||
@@ -36,3 +39,3 @@ | ||
* iterationLimit: Iteration limit to avoid circular dependencies (*defaults to 1000; only needed for huge configuration objects*) | ||
* normalize: Normalize returned slashes to forward-slashes | ||
* normalize: Normalize returned slashes to forward-slashes (*default is false*) | ||
* relative: True to have the output paths relative to the base directory (*default is false*) | ||
@@ -39,0 +42,0 @@ |
338
test.js
@@ -74,2 +74,3 @@ /* eslint-env mocha, chai */ | ||
describe('resolvePaths()', function () { | ||
const allForwardSlashes = ({ a }) => !a.includes('\\'); | ||
const driveLetter = WIN32 ? process.cwd().charAt(0) : ''; | ||
@@ -92,35 +93,31 @@ const paths = { | ||
describe('Options', function () { | ||
const allForwardSlashes = ({ a }) => !a.includes('\\'); | ||
it('should respect the baseDir option', function () { | ||
const baseDir = '/my/base/dir'; | ||
it('should respect the baseDir option', function () { | ||
const baseDir = '/my/base/dir'; | ||
expect(resolvePaths(paths, { baseDir })).to.deep.equal({ | ||
myFolder1: path.resolve(baseDir), | ||
myFolder2: path.resolve('/home'), | ||
specialFile: path.resolve('/home/special.txt'), | ||
myFile: path.resolve(baseDir, 'hey'), | ||
}); | ||
expect(resolvePaths(paths, { baseDir })).to.deep.equal({ | ||
myFolder1: path.resolve(baseDir), | ||
myFolder2: path.resolve('/home'), | ||
specialFile: path.resolve('/home/special.txt'), | ||
myFile: path.resolve(baseDir, 'hey'), | ||
}); | ||
}); | ||
it('should throw if an iteration limit is exceeded', function () { | ||
expect(() => resolvePaths({ | ||
...paths, | ||
specialFile2: '$myFolder/nothing', | ||
}, { iterationLimit: 1 })).to.throw(); | ||
}); | ||
it('should throw if an iteration limit is exceeded', function () { | ||
expect(() => resolvePaths({ | ||
...paths, | ||
specialFile2: '$myFolder/nothing', | ||
}, { iterationLimit: 1 })).to.throw(); | ||
}); | ||
if (WIN32) { | ||
it('should respect the normalize option', function () { | ||
expect( | ||
resolvePaths({ a: '/a/b' }), | ||
).not.to.satisfy(allForwardSlashes); | ||
if (WIN32) { | ||
it('should respect the normalize option', function () { | ||
expect( | ||
resolvePaths({ a: '/a/b' }), | ||
).not.to.satisfy(allForwardSlashes); | ||
expect( | ||
resolvePaths({ a: '/a/b' }, { normalize: true }), | ||
).to.satisfy(allForwardSlashes); | ||
}); | ||
} else it('(Windows-specific test)'); | ||
}); | ||
expect( | ||
resolvePaths({ a: '/a/b' }, { normalize: true }), | ||
).to.satisfy(allForwardSlashes); | ||
}); | ||
} else it('(Windows-specific test)'); | ||
@@ -155,34 +152,32 @@ it('should respect the relative option', function () { | ||
describe('Arguments', function () { | ||
it('should only allow option "iterationLimit" to be an integer', function () { | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { iterationLimit: '/' }); | ||
}).to.throw(); | ||
it('should only allow option "iterationLimit" to be an integer', function () { | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { iterationLimit: '/' }); | ||
}).to.throw(); | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { iterationLimit: 1000 }); | ||
}).not.to.throw(); | ||
}); | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { iterationLimit: 1000 }); | ||
}).not.to.throw(); | ||
}); | ||
it('should only allow the "baseDir" option to be a string', function () { | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { baseDir: '/' }); | ||
}).not.to.throw(); | ||
it('should only allow the "baseDir" option to be a string', function () { | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { baseDir: '/' }); | ||
}).not.to.throw(); | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { baseDir: 1000 }); | ||
}).to.throw(); | ||
}); | ||
expect(function () { | ||
resolvePaths({ | ||
home: '.', | ||
userHome: '$home/user/second' | ||
}, { baseDir: 1000 }); | ||
}).to.throw(); | ||
}); | ||
@@ -198,65 +193,59 @@ }); | ||
describe('Options', function () { | ||
it('should not load excluded modules', function () { | ||
expect(readConfig(tempCfgPath, { exclude: ['paths'] })) | ||
.to.not.have.any.keys(['paths']); | ||
}); | ||
it('should not load excluded modules', function () { | ||
expect(readConfig(tempCfgPath, { exclude: ['paths'] })) | ||
.to.not.have.any.keys(['paths']); | ||
}); | ||
it('should tranform module names if a transform function is provided', function () { | ||
const tf1 = modName => (modName === 'paths' ? 'paths-changed' : modName); | ||
const tf2 = (modName, modContent) => (modContent.namedTask_paths ? 'paths-changed' : modName); | ||
it('should tranform module names if a transform function is provided', function () { | ||
const tf1 = modName => (modName === 'paths' ? 'paths-changed' : modName); | ||
const tf2 = (modName, modContent) => (modContent.namedTask_paths ? 'paths-changed' : modName); | ||
expect(readConfig(tempCfgPath, { transform: tf1 })) | ||
.to.include.all.keys(['paths-changed']) | ||
.and.to.not.have.any.keys(['paths']); | ||
expect(readConfig(tempCfgPath, { transform: tf1 })) | ||
.to.include.all.keys(['paths-changed']) | ||
.and.to.not.have.any.keys(['paths']); | ||
expect(readConfig(tempCfgPath, { transform: tf2 })) | ||
.to.include.all.keys(['paths-changed']) | ||
.and.to.not.have.any.keys(['paths']); | ||
}); | ||
expect(readConfig(tempCfgPath, { transform: tf2 })) | ||
.to.include.all.keys(['paths-changed']) | ||
.and.to.not.have.any.keys(['paths']); | ||
}); | ||
describe('Arguments', function () { | ||
it('should only allow a string configuration folder argument', function () { | ||
expect(function () { readConfig(5); }).to.throw(); | ||
}); | ||
it('should only allow a string configuration folder argument', function () { | ||
expect(function () { readConfig(5); }).to.throw(); | ||
}); | ||
it('should only allow the "exclude" option to be of type string or Array[string]', function () { | ||
expect(function () { readConfig(tempDirPath, { exclude: { something: 1 } }); }) | ||
.to.throw(); | ||
it('should only allow the "exclude" option to be of type string or Array[string]', function () { | ||
expect(function () { readConfig(tempDirPath, { exclude: { something: 1 } }); }) | ||
.to.throw(); | ||
expect(function () { readConfig(tempDirPath, { exclude: { something: [1] } }); }) | ||
.to.throw(); | ||
expect(function () { readConfig(tempDirPath, { exclude: { something: [1] } }); }) | ||
.to.throw(); | ||
expect(function () { readConfig(tempDirPath, { exclude: 'a' }); }) | ||
.not.to.throw(); | ||
expect(function () { readConfig(tempDirPath, { exclude: 'a' }); }) | ||
.not.to.throw(); | ||
expect(function () { readConfig(tempDirPath, { exclude: ['a'] }); }) | ||
.not.to.throw(); | ||
}); | ||
expect(function () { readConfig(tempDirPath, { exclude: ['a'] }); }) | ||
.not.to.throw(); | ||
}); | ||
it('should only allow the "transform" option to be a function', function () { | ||
expect(function () { readConfig(tempDirPath, { transform: { something: 1 } }); }) | ||
.to.throw(); | ||
it('should only allow the "transform" option to be a function', function () { | ||
expect(function () { readConfig(tempDirPath, { transform: { something: 1 } }); }) | ||
.to.throw(); | ||
expect(function () { readConfig(tempDirPath, { transform: x => x }); }) | ||
.not.to.throw(); | ||
}); | ||
expect(function () { readConfig(tempDirPath, { transform: x => x }); }) | ||
.not.to.throw(); | ||
}); | ||
it('should allow the function to be called with the "opts" argument only', function () { | ||
expect(function () { readConfig({}); }) | ||
.to.not.throw(); | ||
it('should allow the function to be called with the "opts" argument only', function () { | ||
const opts = { exclude: ['paths'] }; | ||
const r1 = readConfig(opts); | ||
const r2 = readConfig(tempCfgPath, opts); | ||
{ | ||
const opts = { exclude: ['paths'] }; | ||
const r1 = readConfig(opts); | ||
const r2 = readConfig(tempCfgPath, opts); | ||
expect(r1).to.deep.equal(r2); | ||
expect(r1).to.deep.equal(r2); | ||
} | ||
}); | ||
expect(function () { readConfig({}); }) | ||
.to.not.throw(); | ||
}); | ||
it('should allow the "exclude" option to be empty arrays', function () { | ||
expect(function () { readConfig({ exclude: [] }); }).not.to.throw(); | ||
}); | ||
it('should allow the "exclude" option to be an empty array', function () { | ||
expect(function () { readConfig({ exclude: [] }); }).not.to.throw(); | ||
}); | ||
@@ -285,6 +274,3 @@ }); | ||
const tasks = readTasks(tempTaskPath, { | ||
transform: taskName => taskName.replace( | ||
/_/, | ||
'-' | ||
) | ||
transform: taskName => taskName.replace(/_/, '-') | ||
}); | ||
@@ -296,94 +282,88 @@ const taskName = `task-${modules[0]}`; | ||
describe('Options', function () { | ||
it('should not load excluded modules', function () { | ||
expect(readTasks(tempTaskPath, { exclude: ['paths'] })) | ||
.to.not.have.any.keys(['paths']); | ||
}); | ||
it('should not load excluded modules', function () { | ||
expect(readTasks(tempTaskPath, { exclude: ['paths'] })) | ||
.to.not.have.any.keys(['paths']); | ||
}); | ||
it('should not return excluded tasks', function () { | ||
expect(readTasks(tempTaskPath, { excludeTasks: 'task_index' })) | ||
.not.to.have.any.key(['task_index']); | ||
}); | ||
it('should not return excluded tasks', function () { | ||
expect(readTasks(tempTaskPath, { excludeTasks: 'task_index' })) | ||
.not.to.have.any.key(['task_index']); | ||
}); | ||
it('should transform the returned task names according to the "transform" option', function () { | ||
expect(readTasks(tempTaskPath, { transform: taskName => taskName.toUpperCase() })) | ||
.to.include.all.keys(['TASK_INDEX']) | ||
.but.not.to.have.any.keys(['task_index']); | ||
it('should transform the returned task names according to the "transform" option', function () { | ||
expect(readTasks(tempTaskPath, { transform: taskName => taskName.toUpperCase() })) | ||
.to.include.all.keys(['TASK_INDEX']) | ||
.but.not.to.have.any.keys(['task_index']); | ||
expect(readTasks(tempTaskPath)) | ||
.to.include.all.keys(['task_index']) | ||
.but.not.to.include.any.keys(['TASK_INDEX']); | ||
}); | ||
expect(readTasks(tempTaskPath)) | ||
.to.include.all.keys(['task_index']) | ||
.but.not.to.include.any.keys(['TASK_INDEX']); | ||
}); | ||
describe('Arguments', function () { | ||
it('should allow the function to be called with the "opts" argument only', function () { | ||
expect(function () { readTasks({}); }) | ||
.to.not.throw(); | ||
it('should allow the function to be called with the "opts" argument only', function () { | ||
const tasks = modules.slice().map(modName => `task_${modName}`); | ||
const removedTasks = tasks.splice(tasks.indexOf('task_paths'), 1); | ||
const opts = { excludeTasks: removedTasks }; | ||
const r1 = readTasks(opts); | ||
const r2 = readTasks(tempTaskPath, opts); | ||
{ | ||
const tasks = modules.slice().map(modName => `task_${modName}`); | ||
const removedTasks = tasks.splice(tasks.indexOf('task_paths'), 1); | ||
const opts = { excludeTasks: removedTasks }; | ||
const r1 = readTasks(opts); | ||
const r2 = readTasks(tempTaskPath, opts); | ||
expect(r1) | ||
.to.include.all.keys(tasks) | ||
.but.not.to.include.any.keys(removedTasks); | ||
expect(r1) | ||
.to.include.all.keys(tasks) | ||
.but.not.to.include.any.keys(removedTasks); | ||
expect(r2) | ||
.to.include.all.keys(tasks) | ||
.but.not.to.include.any.keys(removedTasks); | ||
expect(r2) | ||
.to.include.all.keys(tasks) | ||
.but.not.to.include.any.keys(removedTasks); | ||
} | ||
}); | ||
expect(function () { readTasks({}); }) | ||
.to.not.throw(); | ||
}); | ||
it('should only allow a string task folder argument', function () { | ||
expect(function () { readTasks(5); }).to.throw(); | ||
}); | ||
it('should only allow a string task folder argument', function () { | ||
expect(function () { readTasks(5); }).to.throw(); | ||
}); | ||
it('should only allow the "exclude" option to be of type string or Array[string]', function () { | ||
expect(function () { readTasks(tempDirPath, { exclude: { something: 1 } }); }) | ||
.to.throw(); | ||
it('should only allow the "exclude" option to be of type string or Array[string]', function () { | ||
expect(function () { readTasks(tempDirPath, { exclude: { something: 1 } }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { exclude: { something: [1] } }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { exclude: { something: [1] } }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { exclude: 'a' }); }) | ||
.not.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { exclude: 'a' }); }) | ||
.not.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { exclude: ['a'] }); }) | ||
.not.to.throw(); | ||
}); | ||
expect(function () { readTasks(tempDirPath, { exclude: ['a'] }); }) | ||
.not.to.throw(); | ||
}); | ||
it('should only allow the "excludeTasks" option to be of type string or Array[string]', function () { | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: { something: 1 } }); }) | ||
.to.throw(); | ||
it('should only allow the "excludeTasks" option to be of type string or Array[string]', function () { | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: { something: 1 } }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: { something: [1] } }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: { something: [1] } }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: 'a' }); }) | ||
.not.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: 'a' }); }) | ||
.not.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: ['a'] }); }) | ||
.not.to.throw(); | ||
}); | ||
expect(function () { readTasks(tempDirPath, { excludeTasks: ['a'] }); }) | ||
.not.to.throw(); | ||
}); | ||
it('should only allow the "transform" option to be a function', function () { | ||
expect(function () { readTasks(tempDirPath, { transform: 'asd' }); }) | ||
.to.throw(); | ||
it('should only allow the "transform" option to be a function', function () { | ||
expect(function () { readTasks(tempDirPath, { transform: 'asd' }); }) | ||
.to.throw(); | ||
expect(function () { readTasks(tempDirPath, { transform: x => x }); }) | ||
.not.to.throw(); | ||
}); | ||
expect(function () { readTasks(tempDirPath, { transform: x => x }); }) | ||
.not.to.throw(); | ||
}); | ||
it('should allow the "exclude" option to be empty arrays', function () { | ||
expect(function () { readConfig({ exclude: [] }); }).not.to.throw(); | ||
}); | ||
it('should allow the "exclude" option to be an empty array', function () { | ||
expect(function () { readConfig({ exclude: [] }); }).not.to.throw(); | ||
}); | ||
it('should allow the "excludeTasks" option to be empty arrays', function () { | ||
expect(function () { readConfig({ excludeTasks: [] }); }).not.to.throw(); | ||
}); | ||
it('should allow the "excludeTasks" option to be an empty array', function () { | ||
expect(function () { readConfig({ excludeTasks: [] }); }).not.to.throw(); | ||
}); | ||
}); |
27317
8
123
9
528