1 | var config = require('cogs/src/config');
|
2 | var crypto = require('crypto');
|
3 | var expect = require('chai').expect;
|
4 | var fs = require('fs');
|
5 | var getBuild = require('cogs/src/get-build');
|
6 | var glob = require('glob');
|
7 | var path = require('path');
|
8 |
|
9 | var beforeEach = global.beforeEach;
|
10 | var describe = global.describe;
|
11 | var it = global.it;
|
12 |
|
13 | exports.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 |
|
35 |
|
36 |
|
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 |
|
49 | exports.getHash = function (buffer) {
|
50 | var hash = crypto.createHash('md5');
|
51 | hash.end(buffer);
|
52 | return hash.read().toString('hex');
|
53 | };
|
54 |
|
55 | exports.getFileBuffer = function (filePath) {
|
56 | return fs.readFileSync(filePath);
|
57 | };
|
58 |
|
59 | exports.getFileHash = function (filePath) {
|
60 | return exports.getHash(exports.getFileBuffer(filePath));
|
61 | };
|
62 |
|
63 | exports.getGlobBuffer = function (pattern) {
|
64 | return JSON.stringify(glob.sync(pattern));
|
65 | };
|
66 |
|
67 | exports.getGlobHash = function (pattern) {
|
68 | return exports.getHash(exports.getGlobBuffer(pattern));
|
69 | };
|