Skip to content

Commit

Permalink
Add verify_beautification option to fail when file
Browse files Browse the repository at this point in the history
hasn't been beautified instead of mutating the file in place
  • Loading branch information
johnkpaul committed Jul 17, 2013
1 parent 0702e0f commit ce62b4c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
21 changes: 19 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@ module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
jsbeautifier: {
files: ['package.json', '<%= jshint.files %>']
default: {
src: ['package.json', '<%= jshint.files %>']
},
has_not_been_beautified: {
src: ['test/fixtures/not-been-beautified.js'],
options: {
verify_beautification: true
}
},
has_been_beautified: {
src: ['test/fixtures/been-beautified.js'],
options: {
verify_beautification: true
}
},
options: {
indent_size: 4
}
},
nodeunit: {
all: ['test/**/*.js']
Expand Down Expand Up @@ -35,6 +52,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// By default, beautifiy, lint and run all tests.
grunt.registerTask('default', ['jsbeautifier', 'jshint', 'nodeunit']);
grunt.registerTask('default', ['jsbeautifier:default', 'jshint', 'nodeunit']);

};
14 changes: 11 additions & 3 deletions tasks/jsbeautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ module.exports = function(grunt) {
var result = beautify(original, params);
result += '\n';
grunt.verbose.ok();
if (original !== result) {
grunt.file.write(src, result);
changedFileCount++;
if (params.verify_beautification) {
if (original !== result) {
grunt.fail.warn(src.cyan + ' was not beautified');
}

} else {
if (original !== result) {
grunt.file.write(src, result);
changedFileCount++;
}

}
fileCount++;
});
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/been-beautified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var temp = {
hello: 1,
world: 2
}
4 changes: 4 additions & 0 deletions test/fixtures/not-been-beautified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var temp = {
hello: 1,
world: 2
}
26 changes: 21 additions & 5 deletions test/jsbeautifier_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
var grunt = require('grunt');
var exec = require('child_process').exec;

/*
======== A Handy Little Nodeunit Reference ========
Expand All @@ -26,11 +27,26 @@ exports['jsbeautifier'] = {
// setup here
done();
},
'helper': function(test) {
'Verify beautification with unbeautified file': function(test) {
test.expect(1);
// tests here
// test.equal(grunt.helper('jsbeautifier'), 'jsbeautifier!!!', 'should return the correct value.');
test.equal(true, true);
test.done();
exec('grunt jsbeautifier:has_not_been_beautified', {
cwd: __dirname + '/../'
},
function(err, stdout, stderr) {
test.notEqual(err, null, 'Grunt fails because file has not been beautified');
test.done();
});

},
'Verify beautification with beautified file': function(test) {
test.expect(1);
exec('grunt jsbeautifier:has_been_beautified', {
cwd: __dirname + '/../'
},
function(err, stdout, stderr) {
test.equal(err, null, 'Grunt passes because file has been beautified');
test.done();
});

}
};

0 comments on commit ce62b4c

Please sign in to comment.