Permalink
Browse files

add tests

  • Loading branch information...
1 parent 9ee9e90 commit af97de4202890a090b5ac95f156006b27d08d5e7 @tkellen tkellen committed Oct 3, 2012
View
@@ -31,8 +31,6 @@ module.exports = function(grunt) {
// Configuration to be run (and then tested).
concat: {
default_options: {
- options: {
- },
files: {
'tmp/default_options': ['test/fixtures/file1', 'test/fixtures/file2']
}
View
@@ -10,39 +10,57 @@
module.exports = function(grunt) {
+ // Internal lib.
+ var comment = require('./lib/comment').init(grunt);
+
grunt.registerMultiTask('concat', 'Concatenate files.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
- punctuation: '.',
- separator: ', '
+ separator: grunt.util.linefeed,
+ banner: '',
+ stripBanners: false,
+ process: false
});
+ // Normalize boolean options that accept options objects.
+ if (options.stripBanners === true) { options.stripBanners = {}; }
+ if (options.process === true) { options.process = {}; }
+
+ // Process banner.
+ var banner = grunt.template.process(options.banner);
+
// Iterate over all specified file groups.
this.files.forEach(function(fileObj) {
// The source files to be concatenated. The "nonull" option is used
// to retain invalid files/patterns so they can be warned about.
var files = grunt.file.expand({nonull: true}, fileObj.src);
- // Concat specified files.
- var src = files.map(function(filepath) {
+ // Concat banner + specified files.
+ var src = banner + files.map(function(filepath) {
// Warn if a source file/pattern was invalid.
if (!grunt.file.exists(filepath)) {
grunt.log.error('Source file "' + filepath + '" not found.');
return '';
}
// Read file source.
- return grunt.file.read(filepath);
- }).join(options.separator);
-
- // Handle options.
- src += options.punctuation;
+ var src = grunt.file.read(filepath);
+ // Process files as templates if requested.
+ if (options.process) {
+ src = grunt.template.process(src, options.process);
+ }
+ // Strip banners if requested.
+ if (options.stripBanners) {
+ src = comment.stripBanner(src, options.stripBanners);
+ }
+ return src;
+ }).join(grunt.util.normalizelf(options.separator));
// Write the destination file.
grunt.file.write(fileObj.dest, src);
// Print a success message.
grunt.log.writeln('File "' + fileObj.dest + '" created.');
- });
+ }, this);
});
-};
+};
View
@@ -0,0 +1,34 @@
+/*
+ * grunt-contrib-concat
+ * http://gruntjs.com/
+ *
+ * Copyright (c) 2012 "Cowboy" Ben Alman, contributors
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+exports.init = function(/*grunt*/) {
+ var exports = {};
+
+ // Return the given source cude with any leading banner comment stripped.
+ exports.stripBanner = function(src, options) {
+ if (!options) { options = {}; }
+ var m = [];
+ if (options.line) {
+ // Strip // ... leading banners.
+ m.push('(?:.*\\/\\/.*\\n)*\\s*');
+ }
+ if (options.block) {
+ // Strips all /* ... */ block comment banners.
+ m.push('\\/\\*[\\s\\S]*?\\*\\/');
+ } else {
+ // Strips only /* ... */ block comment banners, excluding /*! ... */.
+ m.push('\\/\\*[^!][\\s\\S]*?\\*\\/');
+ }
+ var re = new RegExp('^\\s*(?:' + m.join('|') + ')\\s*', '');
+ return src.replace(re, '');
+ };
+
+ return exports;
+};
View
@@ -1,6 +1,7 @@
'use strict';
var grunt = require('grunt');
+var comment = require('../tasks/lib/comment').init(grunt);
exports.concat = {
default_options: function(test) {
@@ -21,4 +22,22 @@ exports.concat = {
test.done();
},
-};
+
+ strip_banner: function(test) {
+ test.expect(7);
+
+ var src = grunt.file.read('test/fixtures/banner.js');
+ test.equal(comment.stripBanner(src), '// Comment\n\n/* Comment */\n', 'It should strip the top banner.');
+ test.equal(comment.stripBanner(src, {block: true}), '// Comment\n\n/* Comment */\n', 'It should strip the top banner.');
+
+ src = grunt.file.read('test/fixtures/banner2.js');
+ test.equal(comment.stripBanner(src), '\n/*! SAMPLE\n * BANNER */\n\n// Comment\n\n/* Comment */\n', 'It should not strip the top banner.');
+ test.equal(comment.stripBanner(src, {block: true}), '// Comment\n\n/* Comment */\n', 'It should strip the top banner.');
+
+ src = grunt.file.read('test/fixtures/banner3.js');
+ test.equal(comment.stripBanner(src), '\n// This is\n// A sample\n// Banner\n\n// But this is not\n\n/* And neither\n * is this\n */\n', 'It should not strip the top banner.');
+ test.equal(comment.stripBanner(src, {block: true}), '\n// This is\n// A sample\n// Banner\n\n// But this is not\n\n/* And neither\n * is this\n */\n', 'It should not strip the top banner.');
+ test.equal(comment.stripBanner(src, {line: true}), '// But this is not\n\n/* And neither\n * is this\n */\n', 'It should strip the top banner.');
+ test.done();
+ }
+};
@@ -1 +1 @@
-file1: file2 !!!
+file1: file2
@@ -1 +1,2 @@
-file1, file2.
+file1
+file2
@@ -0,0 +1,11 @@
+
+/* THIS
+ * IS
+ * A
+ * SAMPLE
+ * BANNER!
+ */
+
+// Comment
+
+/* Comment */
@@ -0,0 +1,7 @@
+
+/*! SAMPLE
+ * BANNER */
+
+// Comment
+
+/* Comment */
@@ -0,0 +1,10 @@
+
+// This is
+// A sample
+// Banner
+
+// But this is not
+
+/* And neither
+ * is this
+ */

0 comments on commit af97de4

Please sign in to comment.