Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verify_beautification option #15

Merged
merged 1 commit into from
Jul 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});

}
};