Permalink
Browse files

- Refactored the code base to work with this.file instead of this.files.

- Refactored the tests to make more explicit assertions.
  • Loading branch information...
1 parent ca35b3c commit cc9e0e4c0cddbff849d29885abcb1de9b79827aa Darío Javier Cravero committed Dec 10, 2012
View
@@ -1,2 +1,2 @@
node_modules
-test/fixtures/output/*.js
+test/fixtures/output/*
View
@@ -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
}
}
},
View
0 bin/grunt-md5 100644 → 100755
No changes.
View
@@ -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");
+ }
});
});
};
No changes.
No changes.
File renamed without changes.
View
@@ -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();
}
};

0 comments on commit cc9e0e4

Please sign in to comment.