Skip to content

Commit

Permalink
New image testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ol-ko committed Jan 21, 2016
1 parent 2c90886 commit 8ce8db0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
66 changes: 55 additions & 11 deletions helpers/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
var should = require('should'), // jshint ignore:line
glob = require('glob'),
path = require('path'),
fs = require('fs');
fs = require('fs'),
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 +29,50 @@ 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);
}
});
},

compareResultImagesToExpected: function(testCaseFolderName, callback) {
var pathPrefix = path.join(__dirname, '/../test/', testCaseFolderName),
expectedResults = glob.sync(pathPrefix + '/expected/**/*', {
nodir: true
}),
filesChecked = 0,
unequalFiles = [],
gm = require('gm'),
onFilesChecked = function() {
if (filesChecked < expectedResults.length) {
setTimeout(onFilesChecked, 50);
} else {
try {
unequalFiles.length.should.equal(0, 'Images different from expected: ' + unequalFiles.join(', '));
callback();
} catch (error) {
callback(error);
}
}
};

// 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);

gm.compare(expectedFilePath, resultFilePath, 0.01, function(error, equal) {
filesChecked++;
if (!equal) {
unequalFiles.push(expectedFilePath);
}
});
});

onFilesChecked();
}
};
9 changes: 5 additions & 4 deletions test/imageversions/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ module.exports = {

// Calling the task function
task.task(testTaskConfig, done);

//
},

// Using task name as key
default: function() {
helpers.compareResultFilesToExpected('imageversions', {
asBuffer: true
});
default: function(done) {
this.timeout(5000);
helpers.compareResultImagesToExpected('imageversions', done);
},

after: function(done) {
Expand Down

0 comments on commit 8ce8db0

Please sign in to comment.