diff --git a/.gitignore b/.gitignore index c2658d7..b509c88 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +coverage/ node_modules/ diff --git a/README.md b/README.md index 5dec8eb..d4672a4 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ You can load options from json file like the following. Config file is searched in the following order. * the path specified by ```-c|--config``` +* ```.jflintrc``` from the current directory all the way up to the filesystem root * ```${HOME}/.jflintrc``` If the same setting is specified in both option and config file, the option setting overrides the config setting. diff --git a/lib/cli.js b/lib/cli.js index 98f3663..7f91ff7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -40,7 +40,7 @@ class CommandLine { const jenkinsfile = this._program.args[0]; let config = {}; - const configPath = this._program.config || path.join(os.homedir(), '.jflintrc'); + const configPath = this._program.config || this._findConfigPath(this._process.cwd()); try { config = JSON.parse(fs.readFileSync(configPath)); } catch (e) { @@ -71,10 +71,34 @@ class CommandLine { this._process.exit(1); } }).catch(e => { - console.error(e.message); + this._process.stderr.write(`${e.message}\n`); this._process.exit(1); }); } + + _findConfigPath(dir) { + const name = '.jflintrc'; + const filename = path.normalize(path.join(dir, name)); + if (this._isFileExist(filename)) { + return filename; + } + + const parent = path.resolve(dir, '../'); + if (dir === parent) { + return path.join(os.homedir(), '.jflintrc'); + } + + return this._findConfigPath(parent); + } + + _isFileExist(path) { + try { + fs.statSync(path); + return true; + } catch (e) { + return false; + } + } } module.exports = CommandLine; diff --git a/lib/request.js b/lib/request.js index 0f00a56..f75f865 100644 --- a/lib/request.js +++ b/lib/request.js @@ -19,7 +19,7 @@ function request(url, method, headers, opt_body) { if (response.ok) { return response.text(); } - return Promise.reject(new Error(response.statusText)); + return Promise.reject(new Error(`Request Error. (url: ${url}, statusText: ${response.statusText})`)); }); } diff --git a/package.json b/package.json index aa5c238..26a1e93 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ }, "scripts": { "ci": "npm run test:cover", - "test": "./node_modules/.bin/_mocha --require intelli-espower-loader", - "test:cover": "./node_modules/.bin/istanbul --include-all-sources cover ./node_modules/.bin/_mocha --report lcovonly -- --require intelli-espower-loader" + "test": "./node_modules/.bin/_mocha --require intelli-espower-loader --recursive", + "test:cover": "./node_modules/.bin/istanbul --include-all-sources cover ./node_modules/.bin/_mocha --report lcovonly -- --require intelli-espower-loader --recursive" }, "repository": { "type": "git", diff --git a/test/lib/cli.js b/test/lib/cli.js new file mode 100644 index 0000000..d795a7f --- /dev/null +++ b/test/lib/cli.js @@ -0,0 +1,36 @@ +'use strict'; + +const CommandLine = require('../../lib/cli'); +const assert = require('assert'); +const os = require('os'); +const path = require('path'); + +describe('CommandLine', function() { + let sut; + + beforeEach(function() { + sut = new CommandLine(process); + }); + + describe('_findConfigPath', function() { + it('should return the directory\'s config path if .jflintrc exists in the directory', function() { + const dir = path.normalize(`${__dirname}/../resources/jflintrc-exist`); + assert(sut._findConfigPath(dir) === path.join(dir, '.jflintrc')); + }); + + it('should return the parent directory\'s config path if .jflintrc exists in the parent directory', function() { + const dir = path.normalize(`${__dirname}/../resources/jflintrc-exist/jflintrc-not-exist`); + const parentDir = path.normalize(`${__dirname}/../resources/jflintrc-exist`); + assert(sut._findConfigPath(dir) === path.join(parentDir, '.jflintrc')); + }); + + it('should return the home directory\'s config path if .jflintrc does not exist from the directory up to the root', function() { + // setup + sut._isFileExist = () => false; + + // exercise & verify + const dir = path.normalize(`${__dirname}`); + assert(sut._findConfigPath(dir) === path.join(os.homedir(), '.jflintrc')); + }); + }); +}); diff --git a/test/resources/jflintrc-exist/.jflintrc b/test/resources/jflintrc-exist/.jflintrc new file mode 100644 index 0000000..b454a01 --- /dev/null +++ b/test/resources/jflintrc-exist/.jflintrc @@ -0,0 +1,3 @@ +{ + "jenkinsUrl": "http://jenkins.example.com" +} diff --git a/test/resources/jflintrc-exist/jflintrc-not-exist/dummy.txt b/test/resources/jflintrc-exist/jflintrc-not-exist/dummy.txt new file mode 100644 index 0000000..421376d --- /dev/null +++ b/test/resources/jflintrc-exist/jflintrc-not-exist/dummy.txt @@ -0,0 +1 @@ +dummy