UNPKG

2 kBJavaScriptView Raw
1var config = require('cogs/src/config');
2var crypto = require('crypto');
3var expect = require('chai').expect;
4var fs = require('fs');
5var getBuild = require('cogs/src/get-build');
6var glob = require('glob');
7var path = require('path');
8
9var beforeEach = global.beforeEach;
10var describe = global.describe;
11var it = global.it;
12
13exports.run = function (configs) {
14 Object.keys(configs).forEach(function (configPath) {
15 var builds = configs[configPath];
16
17 describe(configPath, function () {
18 beforeEach(function () {
19 config.set(require(path.resolve(configPath)));
20 });
21
22 Object.keys(builds).forEach(function (inputPath) {
23 var expected = builds[inputPath];
24
25 describe(inputPath, function () {
26 var expectsError = expected === Error;
27
28 it(expectsError ? 'fails' : 'succeeds', function (cb) {
29 getBuild(inputPath, function (er, build) {
30 if (expectsError) expect(er).to.be.an.instanceOf(Error);
31 else if (er) return cb(er);
32 else {
33
34 // Chai doesn't do a good job of diffing nested node Buffers, so
35 // check just the buffers first (which it diffs just fine), then
36 // check the entire build.
37 expect(build.buffer).to.deep.equal(expected.buffer);
38 expect(build).to.deep.equal(expected);
39 }
40 cb();
41 });
42 });
43 });
44 });
45 });
46 });
47};
48
49exports.getHash = function (buffer) {
50 var hash = crypto.createHash('md5');
51 hash.end(buffer);
52 return hash.read().toString('hex');
53};
54
55exports.getFileBuffer = function (filePath) {
56 return fs.readFileSync(filePath);
57};
58
59exports.getFileHash = function (filePath) {
60 return exports.getHash(exports.getFileBuffer(filePath));
61};
62
63exports.getGlobBuffer = function (pattern) {
64 return JSON.stringify(glob.sync(pattern));
65};
66
67exports.getGlobHash = function (pattern) {
68 return exports.getHash(exports.getGlobBuffer(pattern));
69};