Skip to content

Commit

Permalink
Compare images mathematically instead of checksums, closes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
terales committed Dec 30, 2017
1 parent e8eb2d4 commit fd878a8
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,7 @@ before_script:
- greenkeeper-lockfile-update

script:
- ./node_modules/.bin/nyc --check-coverage --lines 90 npm test
- ./node_modules/.bin/nyc --check-coverage --lines 90 npm test --serial

after_script:
- ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls
Expand Down
145 changes: 145 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"start": "node src/server.js",
"start-dev": "nodemon src/server.js --watch src/",
"test": "standard --fix && ava --serial"
"test": "standard && ava",
"fix-style": "standard --fix"
},
"dependencies": {
"dotenv": "4.0.0",
Expand All @@ -19,8 +20,10 @@
"devDependencies": {
"ava": "0.24.0",
"coveralls": "3.0.0",
"jpeg-js": "0.3.3",
"nodemon": "1.14.6",
"nyc": "11.4.1",
"pixelmatch": "4.0.2",
"rimraf": "2.6.2",
"standard": "10.0.3",
"supertest": "3.0.0"
Expand Down
38 changes: 22 additions & 16 deletions tests/macros/validateReceivedImages.js
@@ -1,9 +1,15 @@
// Native Node.js modules
const fs = require('fs')
const path = require('path')
const promisify = require('util').promisify

// Third party dependencies
const supertest = require('supertest')
const crypto = require('crypto')
const rimraf = require('rimraf')
const pixelmatch = require('pixelmatch')
const jpegParser = require('jpeg-js')

// Local modules
const app = require('./../../src/app')
const getSourceSamples = require('./../helpers/getSourceSamples')

Expand All @@ -15,9 +21,14 @@ module.exports = function validateReceivedImages (t, source) {

return Promise.all(samples.files.map(file =>
Promise.all([
getFileHash(samples.dir, file),
getReceivedHash(receivablesDir, source, file)
]).then(hashes => t.is(hashes[0], hashes[1], file + ' is different'))
path.join(samples.dir, file),
getReceivedImage(receivablesDir, source, file)
]).then(images => {
const sample = parseImage(images[0])
const received = parseImage(images[1])
const diff = pixelmatch(sample, received, null, sample.width, sample.height)
t.is(diff, 0, file + ' is different')
})
)
)
}
Expand All @@ -27,23 +38,18 @@ function clearDir (dir) {
fs.mkdirSync(dir)
}

function getReceivedHash (receivablesDir, source, file) {
function getReceivedImage (receivablesDir, source, file) {
return new Promise(resolve => {
supertest(app)
.get(`/${source}/` + path.parse(file).name)
.pipe(fs.createWriteStream(path.join(receivablesDir, file)))
.on('close', () => resolve(getFileHash(receivablesDir, file)))
.on('finish', () => resolve(path.join(receivablesDir, file)))
})
}

function getFileHash (dir, file) {
return new Promise(resolve => {
const hash = crypto.createHash('sha1')
hash.on('readable', () => {
const result = hash.read()
if (result) { resolve(result.toString('hex')) }
})

fs.ReadStream(path.join(dir, file)).pipe(hash)
})
async function parseImage (file) {
return jpegParser.decode(
await promisify(fs.readFile)(file),
true
)
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests/sources/google/samples/yegor256@gmail.com.jpg
Binary file not shown.

0 comments on commit fd878a8

Please sign in to comment.