From 605793bb3e62731235465e09f5fe6ce2e826b242 Mon Sep 17 00:00:00 2001 From: Mike Gradek Date: Thu, 3 Jan 2013 13:33:33 -0500 Subject: [PATCH] add tests for digester --- package.json | 5 +-- src/digester.js | 22 +++++++++---- test/digester.js | 65 ++++++++++++++++++++++++++++++++++++++ test/fixtures/spacer1.gif | Bin 0 -> 43 bytes test/fixtures/spacer2.gif | Bin 0 -> 43 bytes 5 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 test/digester.js create mode 100644 test/fixtures/spacer1.gif create mode 100644 test/fixtures/spacer2.gif diff --git a/package.json b/package.json index 7c2fbc6..8dade3c 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "vows": "~0.7.0", "mocha": "~1.7.4", "supertest": "~0.5.1", - "express": "~2.5.11" + "express": "~2.5.11", + "should": "~1.2.1" }, "scripts": { "test": "./node_modules/.bin/mocha --reporter spec --bail" @@ -21,4 +22,4 @@ "send": "~0.1.0", "mkdirp": "~0.3.4" } -} \ No newline at end of file +} diff --git a/src/digester.js b/src/digester.js index c345bdd..3d6225f 100644 --- a/src/digester.js +++ b/src/digester.js @@ -32,20 +32,28 @@ function routeName(entry) { digester.process = function (opts, callback) { var root = opts.root || '.'; var vdir = opts.vdir || '/images/'; + var filter = opts.fileFilter || ['*.gif', '*.jpg', '*.jpeg', '*.png']; + var silent = opts.silent; var strategy = selectStrategy(opts); - var start = Date.now(); - var base = process.cwd(); + var start = Date.now(); + var base = process.cwd(); vdir = url.parse(vdir); - readdirp({root: root}, function (err, tree) { + function log () { + if (!silent) { + console.log.apply(null, Array.prototype.slice.call(arguments)); + } + } + + readdirp({root: root, fileFilter: filter}, function (err, tree) { if (err) { return callback(err); } var processed = []; var entries = tree.files.filter(notHidden).map(toPaths); - console.log('processing %d file(s)', entries.length); + log('processing %d file(s)', entries.length); async.forEachLimit( entries, @@ -68,7 +76,7 @@ digester.process = function (opts, callback) { } function processEntry(entry, done) { - console.log(' \x1b[36mprocess\x1b[0m : ' + entry.key); + log(' \x1b[36mprocess\x1b[0m : ' + entry.key); strategy(entry, function (err, entry) { if (!err) { @@ -88,10 +96,10 @@ digester.process = function (opts, callback) { entry.route = routeName(entry); return entry; }) - .values(); + .value(); var delay = Date.now() - start; - console.log('processed %d file(s) in %d ms', entries.length, delay); + log('processed %d file(s) in %d ms', entries.length, delay); return callback(null, config); } diff --git a/test/digester.js b/test/digester.js new file mode 100644 index 0000000..65532d0 --- /dev/null +++ b/test/digester.js @@ -0,0 +1,65 @@ +var vows = require('should'); +var path = require('path'); + +describe('Digester', function () { + var digester = require('../src/digester'); + + describe('digest directory', function () { + var entries, error; + + before(function (done) { + var opts = { + root: __dirname, + fileFilter: ["*.gif"], + silent: true + }; + + digester.process(opts, cb); + + function cb (err, res) { + error = err; + entries = res; + done(); + } + }); + + it('entries should be an array', function () { + entries.should.be.an.instanceOf(Array); + }); + + it('entries should be .gif', function () { + entries.forEach(isGif); + + function isGif(e) { + path.extname(e.fullPath).should.equal(".gif"); + } + }); + + it('entries should sorted by fullPath', function () { + var paths = entries.map(toPath).sort(); + + entries.forEach(checkSort); + + function toPath(e) { + return e.fullPath; + } + + function checkSort(e, i, arr) { + e.fullPath.should.equal(paths[i]); + } + }); + + it('entries should have properties', function () { + entries.forEach(verify); + + function verify (e) { + e.should.have.property('fullPath'); + e.should.have.property('key'); + e.should.have.property('url'); + e.should.have.property('mtime'); + e.should.have.property('digest'); + e.should.have.property('route'); + } + }); + }); +}); \ No newline at end of file diff --git a/test/fixtures/spacer1.gif b/test/fixtures/spacer1.gif new file mode 100644 index 0000000000000000000000000000000000000000..35d42e808f0a8017b8d52a06be2f8fec0b466a66 GIT binary patch literal 43 scmZ?wbhEHbWMp7uXkcLY|NlP&1B2pE7Dgb&paUX6G7L;iE{qJ;0LZEa`2YX_ literal 0 HcmV?d00001 diff --git a/test/fixtures/spacer2.gif b/test/fixtures/spacer2.gif new file mode 100644 index 0000000000000000000000000000000000000000..35d42e808f0a8017b8d52a06be2f8fec0b466a66 GIT binary patch literal 43 scmZ?wbhEHbWMp7uXkcLY|NlP&1B2pE7Dgb&paUX6G7L;iE{qJ;0LZEa`2YX_ literal 0 HcmV?d00001