Skip to content

Commit

Permalink
Introduce visual testing
Browse files Browse the repository at this point in the history
  • Loading branch information
johansatge committed Dec 2, 2018
1 parent c9716cf commit ba9f663
Show file tree
Hide file tree
Showing 24 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1 +1 @@
test/samples/transformed/*.jpg
test/.tmp
13 changes: 12 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,15 @@
"bugs": {
"url": "https://github.com/johansatge/jpeg-autorotate/issues"
},
"keywords": ["jpeg", "rotate", "autorotate", "orientation", "exif", "thumbnail", "cli"],
"keywords": [
"jpeg",
"rotate",
"autorotate",
"orientation",
"exif",
"thumbnail",
"cli"
],
"dependencies": {
"async": "^2.6.1",
"colors": "^1.3.0",
Expand All @@ -30,7 +38,10 @@
"chai": "^4.1.2",
"eslint": "^5.1.0",
"eslint-plugin-prettier": "^2.6.2",
"fs-extra": "^7.0.1",
"mocha": "^5.2.0",
"pixelmatch": "^4.0.2",
"pngjs": "^3.3.3",
"prettier": "^1.13.7"
},
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -189,3 +189,5 @@ This project is released under the [MIT License](license.md).
* [eslint](http://eslint.org)
* [glob](https://github.com/isaacs/node-glob)
* [prettier](https://prettier.io/)
* [fs-extra](https://github.com/jprichardson/node-fs-extra/)
* [pixelmatch](https://github.com/mapbox/pixelmatch)
Binary file modified test/samples/image_1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_2_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_3_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_4_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_5_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_6.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_6_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_7.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_7_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_8.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_8_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_exif.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/samples/image_exif_dest.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_no_orientation.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/samples/image_unknown_orientation.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed test/samples/transformed/.gitkeep
Empty file.
67 changes: 46 additions & 21 deletions test/test.js
@@ -1,29 +1,38 @@
'use strict'

const expect = require('chai').expect
const fs = require('fs')
const exec = require('child_process').exec
const piexif = require('piexifjs')
const jpegjs = require('jpeg-js')
const jo = require('../src/main.js')
const before = require('mocha').before
const describe = require('mocha').describe
const exec = require('child_process').exec
const expect = require('chai').expect
const fs = require('fs-extra')
const it = require('mocha').it
const jo = require('../src/main.js')
const jpegjs = require('jpeg-js')
const path = require('path')
const piexif = require('piexifjs')
const pixelmatch = require('pixelmatch')
const PNG = require('pngjs').PNG

const tmp_path = path.join(__dirname, '.tmp')

require('chai').should()

describe('jpeg-autorotate', function() {
itShouldTransform(__dirname + '/samples/image_2.jpg', 'image_2.jpg')
itShouldTransform(__dirname + '/samples/image_3.jpg', 'image_3.jpg')
itShouldTransform(__dirname + '/samples/image_4.jpg', 'image_4.jpg')
itShouldTransform(__dirname + '/samples/image_5.jpg', 'image_5.jpg')
itShouldTransform(__dirname + '/samples/image_6.jpg', 'image_6.jpg')
itShouldTransform(__dirname + '/samples/image_7.jpg', 'image_7.jpg')
itShouldTransform(__dirname + '/samples/image_8.jpg', 'image_8.jpg')
itShouldTransform(__dirname + '/samples/image_exif.jpg', 'image_exif.jpg')
itShouldTransform(fs.readFileSync(__dirname + '/samples/image_8.jpg'), 'From a buffer')
before(function() {
return fs.emptyDir(tmp_path)
})
itShouldTransform(path.join(__dirname, '/samples/image_2.jpg'), 'image_2.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_3.jpg'), 'image_3.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_4.jpg'), 'image_4.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_5.jpg'), 'image_5.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_6.jpg'), 'image_6.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_7.jpg'), 'image_7.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_8.jpg'), 'image_8.jpg')
itShouldTransform(path.join(__dirname, '/samples/image_exif.jpg'), 'image_exif.jpg')
itShouldTransform(fs.readFileSync(path.join(__dirname, '/samples/image_8.jpg')), 'From a buffer')

it('Should return an error if the orientation is 1', function(done) {
jo.rotate(__dirname + '/samples/image_1.jpg', {}, function(error, buffer) {
jo.rotate(path.join(__dirname, '/samples/image_1.jpg'), {}, function(error, buffer) {
error.should.have.property('code').equal(jo.errors.correct_orientation)
Buffer.isBuffer(buffer).should.be.ok
done()
Expand All @@ -40,7 +49,7 @@ describe('jpeg-autorotate', function() {
})

it('Should return an error if the file is not an image', function(done) {
jo.rotate(__dirname + '/samples/textfile.md', {}, function(error, buffer, orientation) {
jo.rotate(path.join(__dirname, '/samples/textfile.md'), {}, function(error, buffer, orientation) {
error.should.have.property('code').equal(jo.errors.read_exif)
expect(buffer).to.equal(null)
expect(orientation).to.equal(null)
Expand All @@ -58,7 +67,7 @@ describe('jpeg-autorotate', function() {
})

it('Should work if `options` is not an object', function(done) {
jo.rotate(__dirname + '/samples/image_2.jpg', 'options', function(error, buffer, orientation) {
jo.rotate(path.join(__dirname, '/samples/image_2.jpg'), 'options', function(error, buffer, orientation) {
expect(error).to.equal(null)
Buffer.isBuffer(buffer).should.be.ok
expect(orientation).to.equal(2)
Expand All @@ -67,7 +76,7 @@ describe('jpeg-autorotate', function() {
})

it('Should return an error if the image has no orientation tag', function(done) {
jo.rotate(__dirname + '/samples/image_no_orientation.jpg', {}, function(error, buffer, orientation) {
jo.rotate(path.join(__dirname, '/samples/image_no_orientation.jpg'), {}, function(error, buffer, orientation) {
error.should.have.property('code').equal(jo.errors.no_orientation)
Buffer.isBuffer(buffer).should.be.ok
expect(orientation).to.equal(null)
Expand All @@ -76,7 +85,7 @@ describe('jpeg-autorotate', function() {
})

it('Should return an error if the image has an unknown orientation tag', function(done) {
jo.rotate(__dirname + '/samples/image_unknown_orientation.jpg', {}, function(error, buffer, orientation) {
jo.rotate(path.join(__dirname, '/samples/image_unknown_orientation.jpg'), {}, function(error, buffer, orientation) {
error.should.have.property('code').equal(jo.errors.unknown_orientation)
Buffer.isBuffer(buffer).should.be.ok
expect(orientation).to.equal(null)
Expand Down Expand Up @@ -133,7 +142,23 @@ function itShouldTransform(path_or_buffer, label) {
throw new Error('EXIF do not match')
}
if (typeof path_or_buffer === 'string') {
fs.writeFileSync(path_or_buffer.replace('samples/', 'samples/transformed/'), buffer)
const target_buffer = fs.readFileSync(path_or_buffer.replace('.jpg', '_dest.jpg'))
const target_jpeg = jpegjs.decode(target_buffer)
const diff_png = new PNG({width: target_jpeg.width, height: target_jpeg.height})
const diff_pixels = pixelmatch(
jpegjs.decode(buffer).data,
target_jpeg.data,
diff_png.data,
target_jpeg.width,
target_jpeg.height,
{
threshold: 0.25,
}
)
const diff_path = path.join(tmp_path, path.parse(path_or_buffer).base.replace('.jpg', '.diff.png'))
diff_png.pack().pipe(fs.createWriteStream(diff_path))
fs.writeFileSync(path_or_buffer.replace('samples/', '.tmp/'), buffer)
expect(diff_pixels).to.equal(0)
}
done()
})
Expand Down

0 comments on commit ba9f663

Please sign in to comment.