Skip to content

Commit

Permalink
Merge 319a80c into f9a3049
Browse files Browse the repository at this point in the history
  • Loading branch information
miyajan committed Mar 23, 2017
2 parents f9a3049 + 319a80c commit 7ad47d4
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
coverage/
node_modules/
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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.
Expand Down
28 changes: 26 additions & 2 deletions lib/cli.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion lib/request.js
Expand Up @@ -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})`));
});
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions 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'));
});
});
});
3 changes: 3 additions & 0 deletions test/resources/jflintrc-exist/.jflintrc
@@ -0,0 +1,3 @@
{
"jenkinsUrl": "http://jenkins.example.com"
}
1 change: 1 addition & 0 deletions test/resources/jflintrc-exist/jflintrc-not-exist/dummy.txt
@@ -0,0 +1 @@
dummy

0 comments on commit 7ad47d4

Please sign in to comment.