Browse files

add complexity checks in the build

  • Loading branch information...
1 parent d062a87 commit 21bcf08ae76ef79606fb9c3cd46d37bc46566da6 @netroy netroy committed Nov 18, 2013
Showing with 66 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 .travis.yml
  3. +4 −1 Makefile
  4. +4 −3 package.json
  5. +56 −0 specs/complexity.js
View
1 .gitignore
@@ -3,3 +3,4 @@ coverage.html
lib-cov
node_modules
npm-debug.log
+reports
View
2 .travis.yml
@@ -11,4 +11,4 @@ install:
- npm install --loglevel error
script:
- - make lint test
+ - npm test
View
5 Makefile
@@ -5,11 +5,14 @@ BIN = ./node_modules/.bin/mocha
LINT = ./node_modules/.bin/jshint
WATCH =
-all: lint test
+all: lint complexity test
lint:
@$(LINT) lib/
+complexity:
+ @$(BIN) --ui $(UI) --reporter $(REPORTER) specs/complexity.js
+
test:
@$(BIN) --ui $(UI) --reporter $(REPORTER) $(WATCH) $(TESTS)
View
7 package.json
@@ -37,9 +37,10 @@
"url": "https://github.com/netroy/image-size/issues"
},
"devDependencies": {
- "mocha": "~1.13.0",
+ "escomplex-js": "~0.1.0",
"expect.js": "~0.2.0",
- "jshint": "~2.1.11",
- "glob": "~3.2.6"
+ "glob": "~3.2.7",
+ "jshint": "~2.3.0",
+ "mocha": "~1.14.0"
}
}
View
56 specs/complexity.js
@@ -0,0 +1,56 @@
+var esj = require('escomplex-js');
+var expect = require('expect.js');
+var glob = require('glob');
+
+var path = require('path');
+var fs = require('fs');
+
+var options = {
+ 'max_complexity': 5,
+ 'max_cyclomatic_density': 110,
+ 'max_dependencies': 10,
+ 'maintainability': 100
+};
+
+describe('Code complexity', function () {
+ // find all source files
+ var codeFiles = glob.sync('lib/**/*.js');
+ // loop through them
+ codeFiles.forEach(function (file) {
+
+ describe(file, function () {
+ // get the absolute path
+ file = path.resolve(file);
+ // read the file
+ var code = fs.readFileSync(file, 'utf-8');
+
+ // calculate the complexity
+ var analysis = esj.analyse(code);
+
+ // Halstead
+ // TODO: analysis.aggregate.halstead
+
+ // Complexity
+ describe('functions', function () {
+ var fns = analysis.functions || [];
+ fns.forEach(function (fn) {
+ it(fn.name, function () {
+ // TODO: add checks on fn.halstead
+ expect(fn.cyclomatic).to.be.lessThan(options.max_complexity);
+ expect(fn.cyclomaticDensity).to.be.lessThan(options.max_cyclomatic_density);
+ });
+ });
+ });
+
+ // prevent too many dependencies
+ it('dependencies', function () {
+ expect(analysis.dependencies.length).to.be.lessThan(options.max_dependencies);
+ });
+
+ // keep functions maintainable
+ it('maintainability', function () {
+ expect(analysis.maintainability).to.be.greaterThan(options.maintainability);
+ });
+ });
+ });
+});

0 comments on commit 21bcf08

Please sign in to comment.