gherkin-lint
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"env": { | ||
"node": true | ||
"node": true, | ||
"mocha": true | ||
}, | ||
@@ -5,0 +6,0 @@ "extends": "eslint:recommended", |
{ | ||
"name": "gherkin-lint", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A Gherkin linter/validator written in javascript", | ||
@@ -30,3 +30,7 @@ "author": "Vasiliki Siakka", | ||
"devDependencies": { | ||
"eslint": "2.13.1" | ||
"chai": "3.5.0", | ||
"eslint": "2.13.1", | ||
"mocha": "2.5.3", | ||
"mocha-sinon": "1.1.5", | ||
"sinon": "1.17.4" | ||
}, | ||
@@ -37,7 +41,7 @@ "bin": { | ||
"scripts": { | ||
"demo": "node ./src/main.js .", | ||
"demo": "node ./src/main.js . -c ./test-data-wip/.gherkin-lintrc", | ||
"lint": "eslint .", | ||
"test": "npm run lint" | ||
"test": "npm run lint && mocha --recursive" | ||
}, | ||
"license": "ISC" | ||
} |
#!/usr/bin/env node | ||
var program = require('commander'); | ||
var glob = require('glob'); | ||
var linter = require('./linter.js'); | ||
var rules = require('./rules.js'); | ||
var fs = require('fs'); | ||
var featureFinder = require('./feature-finder.js'); | ||
var configParser = require('./config-parser.js'); | ||
var defaultConfigFileName = '.gherkin-lintrc'; | ||
var defaultIgnoreFileName = '.gherkin-lintignore'; | ||
function list(val) { | ||
@@ -17,9 +13,9 @@ return val.split(','); | ||
program | ||
.option('-f, --format [format]', 'Output format. Defaults to stylish') | ||
.option('-i, --ignore <...>', 'Comma seperated list of files/glob patterns that the linter should ignore. Overrides ' + defaultIgnoreFileName + ' file', list) | ||
.option('-c, --config [config]', 'Configuration file. Defaults to ' + defaultConfigFileName) | ||
.option('-f, --format [format]', 'output format. Defaults to stylish') | ||
.option('-i, --ignore <...>', 'comma seperated list of files/glob patterns that the linter should ignore, overrides ' + featureFinder.defaultIgnoreFileName + ' file', list) | ||
.option('-c, --config [config]', 'configuration file, defaults to ' + configParser.defaultConfigFileName) | ||
.parse(process.argv); | ||
var files = getFeatureFiles(program.args, program.ignore); | ||
var config = getConfiguration(program.config); | ||
var files = featureFinder.getFeatureFiles(program.args, program.ignore); | ||
var config = configParser.getConfiguration(program.config); | ||
var results = linter.lint(files, config); | ||
@@ -50,70 +46,1 @@ printResults(results, program.format); | ||
} | ||
function getConfiguration(configPath) { | ||
if (configPath) { | ||
if (!fs.existsSync(configPath)) { | ||
throw new Error('Could not find specified config file "' + configPath + '"'); | ||
} | ||
} else { | ||
if (!fs.existsSync(defaultConfigFileName)) { | ||
throw new Error('Could not find default config file "' + defaultConfigFileName +'" in the working ' + | ||
'directory. To use a custom name/location provide the config file using the "-c" arg.'); | ||
} | ||
configPath = defaultConfigFileName; | ||
} | ||
var config = JSON.parse(fs.readFileSync(configPath)); | ||
verifyConfigurationFile(config); | ||
return config; | ||
} | ||
function verifyConfigurationFile(config) { | ||
for (var rule in config) { | ||
if (!rules.doesRuleExist(rule)) { | ||
throw new Error('Rule "' + rule + '" does not exist'); | ||
} | ||
rules.verifyRuleConfiguration(rule, config[rule]); | ||
} | ||
} | ||
function getIgnorePatterns(ignoreArg) { | ||
if (ignoreArg) { | ||
return ignoreArg; | ||
} else if (fs.existsSync(defaultIgnoreFileName)) { | ||
// return an array where each element of the array is a line of the ignore file | ||
return fs.readFileSync(defaultIgnoreFileName) | ||
.toString() | ||
.split(/[\n|\r]/) | ||
.filter(function(i) { | ||
// remove empty strings | ||
if (i !== '') { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} else { | ||
// Ignore node_modules by default | ||
return 'node_modules/**'; | ||
} | ||
} | ||
function getFeatureFiles(args, ignoreArg) { | ||
var files = []; | ||
args.forEach(function(arg) { | ||
var pattern = ''; | ||
if (arg == '.') { | ||
pattern = '**/*.feature'; | ||
} else if (arg.match(/.*\/\*\*/)) { | ||
pattern = arg.slice(0, -1) + '.feature'; | ||
} else if(arg.match(/\/$/)) { | ||
pattern = arg + '**/*.feature'; | ||
} else if (arg.match(/.*\.feature/)) { | ||
pattern = arg; | ||
} else { | ||
throw new Error('Invalid input format. To run the linter please specify a feature file, directory or glob.'); | ||
} | ||
var globOptions = {ignore: getIgnorePatterns(ignoreArg)}; | ||
files = files.concat(glob.sync(pattern, globOptions)); | ||
}); | ||
return files; | ||
} |
@@ -58,30 +58,3 @@ // Operations on rules | ||
function verifyRuleConfiguration(rule, ruleConfig) { | ||
var enablingSettings = ['on', 'off']; | ||
var genericErrorMsg = 'Invalid rule configuration for "' + rule + '" - '; | ||
if (Array.isArray(ruleConfig)) { | ||
if (enablingSettings.indexOf(ruleConfig[0]) == -1) { | ||
throw new Error(genericErrorMsg + 'The first part of the config should be "on" or "off"'); | ||
} | ||
if (ruleConfig.length != 2 ) { | ||
throw new Error(genericErrorMsg + ' The config should only have 2 parts.'); | ||
} | ||
var ruleObj = getRule(rule); | ||
var extraConfig = typeof(ruleConfig[1]) === 'string' ? [ruleConfig[1]] : ruleConfig[1]; | ||
for (var subConfig in extraConfig) { | ||
if (ruleObj.availableConfigs[subConfig] === undefined) { | ||
throw new Error(genericErrorMsg + ' The rule does not have the specified configuration option "' + subConfig + '"'); | ||
} | ||
} | ||
} else { | ||
if (enablingSettings.indexOf(ruleConfig) == -1) { | ||
throw new Error(genericErrorMsg + 'The the config should be "on" or "off"'); | ||
} | ||
} | ||
} | ||
module.exports = { | ||
@@ -91,3 +64,3 @@ doesRuleExist: doesRuleExist, | ||
runAllEnabledRules: runAllEnabledRules, | ||
verifyRuleConfiguration: verifyRuleConfiguration | ||
getRule: getRule | ||
}; |
@@ -9,3 +9,3 @@ var fs = require('fs'); | ||
lines.forEach(function(line) { | ||
if (line[line.length - 1] == ' ') { | ||
if (/[\s]+$/.test(line)) { | ||
errors.push({message: 'Trailing spaces are not allowed', | ||
@@ -12,0 +12,0 @@ rule : rule, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38818
55
770
5
8