Permalink
Browse files

Add tests

  • Loading branch information...
1 parent 9b039de commit 6f3fd0fea7270f16121d487b61feb66f41d34818 Frank Hellwig committed Sep 26, 2012
Showing with 25 additions and 15 deletions.
  1. +25 −15 pkgconfig.js
View
40 pkgconfig.js
@@ -90,33 +90,43 @@ function readFile(pathname) {
} else if (isFile(pathname + '.json')) {
return readJsonFile(pathname + '.json');
} else {
- msg = ' (tried .js and .json)'; // note leading space
+ msg = '(.js|.json)';
}
}
- throw new Error(strformat("File not found '{0}'{1}", pathname, msg));
+ throw new Error(strformat("File not found '{0}{1}'", pathname, msg));
}
/**
- * The function exported by this module.
+ * Process the options parameter and return a valid options object.
*/
-function pkgconfig(options) {
- if (typeof options === 'undefined') {
- options = {};
+function processOptions(options) {
+ var opt;
+ if (typeof options === 'object') {
+ opt = options;
} else if (typeof options === 'string') {
- options = {config: options};
- } else if (typeof options !== 'object') {
+ opt = {config: options};
+ } else if (typeof options === 'undefined') {
+ opt = {}; // use default values in options schema
+ } else {
throw new TypeError('options must be an object or a string');
}
- options = jsvutil.validate(options, OPTIONS_SCHEMA);
+ return jsvutil.validate(opt, OPTIONS_SCHEMA);
+}
+
+/**
+ * The function exported by this module.
+ */
+function pkgconfig(options) {
+ var opt = processOptions(options);
var pkginfo = findpkg(module.parent);
- if (typeof options.schema === 'string') {
- options.schema = readFile(pkginfo.resolve(options.schema));
+ if (typeof opt.schema === 'string') {
+ opt.schema = readFile(pkginfo.resolve(opt.schema));
}
- if (typeof options.config === 'string') {
- options.config = readFile(pkginfo.resolve(options.config));
+ if (typeof opt.config === 'string') {
+ opt.config = readFile(pkginfo.resolve(opt.config));
}
- jsvutil.check(options.schema);
- return jsvutil.validate(options.config, options.schema);
+ jsvutil.check(opt.schema);
+ return jsvutil.validate(opt.config, opt.schema);
}
module.exports = pkgconfig;

0 comments on commit 6f3fd0f

Please sign in to comment.