Skip to content

Commit

Permalink
Merge pull request #72 in BUDC/estatico from feature/ESTATICO_69_bett…
Browse files Browse the repository at this point in the history
…er_image_comparison_for_test to release/github

* commit 'f5b3b2813fc1e34de7cbb59c5c92db637a2ca95e':
  Promises-based test for media:imageversions Added npm promise as a dev dependency.
  New image testing
  • Loading branch information
Olga Skurativska committed Jan 21, 2016
2 parents 2c90886 + f5b3b28 commit 01cd5ba
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 555 deletions.
63 changes: 52 additions & 11 deletions helpers/tests.js
Expand Up @@ -3,7 +3,24 @@
var should = require('should'), // jshint ignore:line
glob = require('glob'),
path = require('path'),
fs = require('fs');
fs = require('fs'),
Promise = require('promise'),
gm = require('gm'),

stringComparison = function(expectedFilePath, resultFilePath) {
// reading files as strings and trimming them - good for non-binary files
var expectedFile = fs.readFileSync(expectedFilePath, 'utf8').trim(),
resultFile = fs.readFileSync(resultFilePath, 'utf8').trim();

resultFile.should.equal(expectedFile, expectedFilePath);
},

bufferComparison = function() {
var expectedFile = fs.readFileSync(expectedFilePath),
resultFile = fs.readFileSync(resultFilePath);

resultFile.compare(expectedFile).should.equal(0, expectedFilePath);
};

module.exports = {
compareResultFilesToExpected: function(testCaseFolderName, options) {
Expand All @@ -15,20 +32,44 @@ module.exports = {
// Compares files from expected folder and the results of gulp task, executed with fixtures as test data
expectedResults.forEach(function(expectedFilePath) {
var expectedFileName = path.relative(pathPrefix + '/expected/', expectedFilePath),
resultFilePath = path.resolve(pathPrefix + '/results/' + expectedFileName),
expectedFile,
resultFile;
resultFilePath = path.resolve(pathPrefix + '/results/' + expectedFileName);

if (options && options.asBuffer) {
expectedFile = fs.readFileSync(expectedFilePath);
resultFile = fs.readFileSync(resultFilePath);
resultFile.compare(expectedFile).should.equal(0, expectedFilePath);
bufferComparison(expectedFilePath, resultFilePath);
} else {
// reading files as strings and trimming them - good for non-binary files
expectedFile = fs.readFileSync(expectedFilePath, 'utf8').trim();
resultFile = fs.readFileSync(resultFilePath, 'utf8').trim();
resultFile.should.equal(expectedFile, expectedFilePath);
stringComparison(expectedFilePath, resultFilePath);
}
});
},

compareImagesToExpected: function(testCaseFolderName, cb) {
var pathPrefix = path.join(__dirname, '/../test/', testCaseFolderName),
expectedResults = glob.sync(pathPrefix + '/expected/**/*', {
nodir: true
});

console.log('Starting image comparison...');

Promise.all(expectedResults.map(function(expectedFilePath) {
var expectedFileName = path.relative(pathPrefix + '/expected/', expectedFilePath),
resultFilePath = path.resolve(pathPrefix + '/results/' + expectedFileName);

return new Promise(function(resolve, reject) {
gm.compare(expectedFilePath, resultFilePath, 0.01, function(error, equal) {
if (equal) {
resolve();
} else {
reject(expectedFileName + ': differs from expected.');
}
});
});
})).then(function(message) {
console.log('All images are equal.');
cb();
},

function(error) {
cb(new Error(error));
});
}
};

0 comments on commit 01cd5ba

Please sign in to comment.