From cc9e0e4c0cddbff849d29885abcb1de9b79827aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Javier=20Cravero?= Date: Sun, 9 Dec 2012 22:05:18 -0300 Subject: [PATCH] - Refactored the code base to work with this.file instead of this.files. - Refactored the tests to make more explicit assertions. --- .gitignore | 2 +- Gruntfile.js | 38 +++++++++++-- bin/grunt-md5 | 0 tasks/grunt-md5.js | 75 +++++++++++-------------- test/fixtures/js/test-b.js | 0 test/fixtures/output/.gitkeep | 0 test/fixtures/{js/test-a.js => test.js} | 0 test/md5_test.js | 48 ++++++++++++---- 8 files changed, 106 insertions(+), 57 deletions(-) mode change 100644 => 100755 bin/grunt-md5 delete mode 100644 test/fixtures/js/test-b.js delete mode 100644 test/fixtures/output/.gitkeep rename test/fixtures/{js/test-a.js => test.js} (100%) diff --git a/.gitignore b/.gitignore index fe74a26..db6d24e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules -test/fixtures/output/*.js +test/fixtures/output/* diff --git a/Gruntfile.js b/Gruntfile.js index 40d973b..4c91080 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -24,9 +24,9 @@ module.exports = function(grunt) { boss: true, eqnull: true, node: true, - es5: true - }, - globals: {} + es5: true, + globals: {} + } }, clean: { output: { @@ -36,10 +36,38 @@ module.exports = function(grunt) { md5: { main: { files: { - 'test/fixtures/output': 'test/fixtures/js/*' + 'test/fixtures/output/main': 'test/fixtures/test.js' + }, + options: { + keepExtension: true, + keepBasename: true + } + }, + noExtension: { + files: { + 'test/fixtures/output/noExtension': 'test/fixtures/test.js' + }, + options: { + keepExtension: false, + keepBasename: true + } + }, + noBasename: { + files: { + 'test/fixtures/output/noBasename': 'test/fixtures/test.js' + }, + options: { + keepExtension: true, + keepBasename: false + } + }, + noBasenameOrExtension: { + files: { + 'test/fixtures/output/noBasenameOrExtension': 'test/fixtures/test.js' }, options: { - keepExtension: true + keepExtension: false, + keepBasename: false } } }, diff --git a/bin/grunt-md5 b/bin/grunt-md5 old mode 100644 new mode 100755 diff --git a/tasks/grunt-md5.js b/tasks/grunt-md5.js index 05cdf27..e370723 100644 --- a/tasks/grunt-md5.js +++ b/tasks/grunt-md5.js @@ -17,60 +17,53 @@ module.exports = function(grunt) { var _ = grunt.util._, path = require('path'); grunt.registerMultiTask('md5', 'Generate a md5 filename', function() { - var options = this.options(); - var srcFiles; - var destDir; + var options = this.options(), + srcFiles = grunt.file.expandFiles(this.file.srcRaw), + destDir = this.file.dest; grunt.verbose.writeflags(options, 'Options'); - this.files.forEach(function(file) { + grunt.verbose.writeln('Files: ' + srcFiles); + grunt.verbose.writeln('Destination directory:' + destDir); - srcFiles = grunt.file.expandFiles(file.src); - destDir = file.dest; - - if (typeof srcFiles === 'undefined') { - // TODO generate error if file does not exists - return; - } + if (typeof srcFiles === 'undefined') { + grunt.fail.warn("Files object doesn't exist"); + } + srcFiles.forEach(function(srcFile) { if (grunt.file.exists(destDir) === false) { + grunt.verbose.writeln("Creating destination directory as it didn't exist."); grunt.file.mkdir(destDir); } - destDir = grunt.file.expandDirs(destDir)[0]; - - srcFiles.forEach(function(srcFile) { - - try { - var srcCode = grunt.file.read(srcFile); - var ext = '', basename = ''; - // keep extension unless you explicitly tell to not - if (options.keepExtension !== false) { - ext = path.extname(srcFile); - } - // keep basename unless you explicitly tell to not - if (options.keepBasename !== false) { - basename = path.basename(srcFile, ext || path.extname(srcFile)); - } - var filename = basename + '-' + - require('crypto'). - createHash('md5'). - update(srcCode). - digest('hex') + ext; + try { + var srcCode = grunt.file.read(srcFile), ext = '', basename = '', filename, destFile; + // keep extension unless you explicitly tell to not + if (options.keepExtension !== false) { + ext = path.extname(srcFile); + } + // keep basename unless you explicitly tell to not + if (options.keepBasename !== false) { + basename = path.basename(srcFile, ext || path.extname(srcFile)) + '-'; + } + filename = basename + + require('crypto'). + createHash('md5'). + update(srcCode). + digest('hex') + ext; - var destFile = require('path').join(destDir, filename); + destFile = path.join(destDir, filename); - grunt.file.copy(srcFile, destFile); + grunt.file.copy(srcFile, destFile); - if (_.isFunction(options.callback)) { - options.callback(destFile, srcFile, srcCode); - } - grunt.log.writeln('File \'' + destFile + '\' created.'); - } catch(err) { - grunt.log.error(err); - grunt.fail.warn("Fail to generate an MD5 file name"); + if (_.isFunction(options.callback)) { + options.callback(destFile, srcFile, srcCode); } - }); + grunt.log.writeln("File '" + destFile + "' created."); + } catch(err) { + grunt.log.error(err); + grunt.fail.warn("Fail to generate an MD5 file name"); + } }); }); }; diff --git a/test/fixtures/js/test-b.js b/test/fixtures/js/test-b.js deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/output/.gitkeep b/test/fixtures/output/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/js/test-a.js b/test/fixtures/test.js similarity index 100% rename from test/fixtures/js/test-a.js rename to test/fixtures/test.js diff --git a/test/md5_test.js b/test/md5_test.js index eb7bb41..d7a8aa6 100644 --- a/test/md5_test.js +++ b/test/md5_test.js @@ -1,26 +1,54 @@ 'use strict'; -var fs = require('fs'); var grunt = require('grunt'); exports.md5 = { main: function(test) { - test.expect(2); + test.expect(1); - var filenameA = require('crypto'). + var md5Filename = 'test-' + require('crypto'). createHash('md5'). - update(fs.readFileSync('test/fixtures/js/test-a.js', 'utf8')). + update(grunt.file.read('test/fixtures/test.js')). digest('hex') + '.js'; - var resultA = fs.existsSync('test/fixtures/output/'+filenameA); - test.ok(resultA, 'should generate a md5 filename'); - var filenameB = require('crypto'). + test.ok(grunt.file.exists('test/fixtures/output/main/' + md5Filename), + 'should generate an MD5 filename keeping its basename and extension'); + test.done(); + }, + noExtension: function(test) { + test.expect(1); + + var md5Filename = 'test-' + require('crypto'). + createHash('md5'). + update(grunt.file.read('test/fixtures/test.js')). + digest('hex'); + + test.ok(grunt.file.exists('test/fixtures/output/noExtension/' + md5Filename), + 'should generate an MD5 filename keeping only its basename'); + test.done(); + }, + noBasename: function(test) { + test.expect(1); + + var md5Filename = require('crypto'). createHash('md5'). - update(fs.readFileSync('test/fixtures/js/test-b.js', 'utf8')). + update(grunt.file.read('test/fixtures/test.js')). digest('hex') + '.js'; - var resultB = fs.existsSync('test/fixtures/output/'+filenameB); - test.ok(resultB, 'should generate a md5 filename'); + test.ok(grunt.file.exists('test/fixtures/output/noBasename/' + md5Filename), + 'should generate an MD5 filename keeping only its extension'); + test.done(); + }, + noBasenameOrExtension: function(test) { + test.expect(1); + + var md5Filename = require('crypto'). + createHash('md5'). + update(grunt.file.read('test/fixtures/test.js')). + digest('hex'); + + test.ok(grunt.file.exists('test/fixtures/output/noBasenameOrExtension/' + md5Filename), + 'should generate an MD5 filename without keeping its basename or extension'); test.done(); } };