Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

Commit

Permalink
add tests for digester
Browse files Browse the repository at this point in the history
  • Loading branch information
cdnbacon committed Jan 3, 2013
1 parent a61e213 commit 605793b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -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"
Expand All @@ -21,4 +22,4 @@
"send": "~0.1.0",
"mkdirp": "~0.3.4"
}
}
}
22 changes: 15 additions & 7 deletions src/digester.js
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
65 changes: 65 additions & 0 deletions 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');
}
});
});
});
Binary file added test/fixtures/spacer1.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/spacer2.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 605793b

Please sign in to comment.