summaryrefslogtreecommitdiff
path: root/platform/node/test/compare.js
blob: 3e5221de55d7e87354caaa4f532ddbf66e8730b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var spawn = require('child_process').spawn;

module.exports = function compare(actual, expected, diff, t, callback) {
    var compare = spawn('compare', ['-metric', 'MAE', actual, expected, diff]);
    var error = '';

    compare.stderr.on('data', function(data) {
        error += data.toString();
    });

    compare.on('error', function(err) {
        t.error(err);
    });

    compare.on('exit', function(code) {
        // The compare program returns 2 on error otherwise 0 if the images are similar or 1 if they are dissimilar.
        if (code === 2) {
            callback(error.trim(), Infinity);
        } else {
            var match = error.match(/^\d+(?:\.\d+)?\s+\(([^\)]+)\)\s*$/);
            var difference = match ? parseFloat(match[1]) : Infinity;
            callback(match ? '' : error, difference);
        }
    });

    compare.stdin.end();
};