Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add absolute version (dev)dependencies rule (#36)
* Feature add absolute dep rule * Feature add absolute devDep rule
- Loading branch information
1 parent
2a85ec9
commit e5c0381
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const areVersRangesValid = require('./../validators/dependency-audit').areVersRangesValid; | ||
const LintIssue = require('./../LintIssue'); | ||
const lintId = 'prefer-absolute-version-dependencies'; | ||
const nodeName = 'dependencies'; | ||
const message = 'You are using an invalid version range. Please use absolute versions.'; | ||
const ruleType = 'dependencies-version-range'; | ||
|
||
const lint = function(packageJsonData, lintType) { | ||
const rangeSpecifier = '='; | ||
|
||
if (!areVersRangesValid(packageJsonData, nodeName, rangeSpecifier)) { | ||
return new LintIssue(lintId, lintType, nodeName, message); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
module.exports.lint = lint; | ||
module.exports.ruleType = ruleType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const areVersRangesValid = require('./../validators/dependency-audit').areVersRangesValid; | ||
const LintIssue = require('./../LintIssue'); | ||
const lintId = 'prefer-absolute-version-devDependencies'; | ||
const nodeName = 'devDependencies'; | ||
const message = 'You are using an invalid version range. Please use absolute versions.'; | ||
const ruleType = 'devDependencies-version-range'; | ||
|
||
const lint = function(packageJsonData, lintType) { | ||
const rangeSpecifier = '='; | ||
|
||
if (!areVersRangesValid(packageJsonData, nodeName, rangeSpecifier)) { | ||
return new LintIssue(lintId, lintType, nodeName, message); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
module.exports.lint = lint; | ||
module.exports.ruleType = ruleType; |
45 changes: 45 additions & 0 deletions
45
tests/unit/rules/prefer-absolute-version-dependenciesTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const should = require('should'); | ||
const requireHelper = require('../../require_helper'); | ||
const lint = requireHelper('rules/prefer-absolute-version-dependencies').lint; | ||
|
||
describe('prefer-absolute-version-dependencies Unit Tests', function() { | ||
context('when package.json has node with an invalid value', function() { | ||
it('LintIssue object should be returned', function() { | ||
const packageJsonData = { | ||
dependencies: { | ||
'npm-package-json-lint': '~1.0.0' | ||
} | ||
}; | ||
const response = lint(packageJsonData, 'error'); | ||
|
||
response.lintId.should.equal('prefer-absolute-version-dependencies'); | ||
response.lintType.should.equal('error'); | ||
response.node.should.equal('dependencies'); | ||
response.lintMessage.should.equal('You are using an invalid version range. Please use absolute versions.'); | ||
}); | ||
}); | ||
|
||
context('when package.json has node with a valid value (= prefixed)', function() { | ||
it('LintIssue object should be returned', function() { | ||
const packageJsonData = { | ||
dependencies: { | ||
'gulp-npm-package-json-lint': '=1.0.0' | ||
} | ||
}; | ||
const response = lint(packageJsonData, 'error'); | ||
|
||
response.should.be.true(); | ||
}); | ||
}); | ||
|
||
context('when package.json does not have node', function() { | ||
it('true should be returned', function() { | ||
const packageJsonData = {}; | ||
const response = lint(packageJsonData, 'error'); | ||
|
||
response.should.be.true(); | ||
}); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
tests/unit/rules/prefer-absolute-version-devDependenciesTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const should = require('should'); | ||
const requireHelper = require('../../require_helper'); | ||
const lint = requireHelper('rules/prefer-absolute-version-devDependencies').lint; | ||
|
||
describe('prefer-absolute-version-devDependencies Unit Tests', function() { | ||
context('when package.json has node with an invalid value', function() { | ||
it('LintIssue object should be returned', function() { | ||
const packageJsonData = { | ||
devDependencies: { | ||
'npm-package-json-lint': '~1.0.0' | ||
} | ||
}; | ||
const response = lint(packageJsonData, 'error'); | ||
|
||
response.lintId.should.equal('prefer-absolute-version-devDependencies'); | ||
response.lintType.should.equal('error'); | ||
response.node.should.equal('devDependencies'); | ||
response.lintMessage.should.equal('You are using an invalid version range. Please use absolute versions.'); | ||
}); | ||
}); | ||
|
||
context('when package.json has node with a valid value (= prefixed)', function() { | ||
it('LintIssue object should be returned', function() { | ||
const packageJsonData = { | ||
devDependencies: { | ||
'gulp-npm-package-json-lint': '=1.0.0' | ||
} | ||
}; | ||
const response = lint(packageJsonData, 'error'); | ||
|
||
response.should.be.true(); | ||
}); | ||
}); | ||
|
||
context('when package.json does not have node', function() { | ||
it('true should be returned', function() { | ||
const packageJsonData = {}; | ||
const response = lint(packageJsonData, 'error'); | ||
|
||
response.should.be.true(); | ||
}); | ||
}); | ||
}); |