mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Update tests to meet semistandard code standards
Switch to const/let instead of var
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
var fs = require('fs');
|
||||
var assert = require('assert');
|
||||
var exifReader = require('exif-reader');
|
||||
var icc = require('icc');
|
||||
const fs = require('fs');
|
||||
const assert = require('assert');
|
||||
const exifReader = require('exif-reader');
|
||||
const icc = require('icc');
|
||||
|
||||
var sharp = require('../../index');
|
||||
var fixtures = require('../fixtures');
|
||||
const sharp = require('../../index');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('Image metadata', function() {
|
||||
|
||||
it('JPEG', function(done) {
|
||||
sharp(fixtures.inputJpg).metadata(function(err, metadata) {
|
||||
describe('Image metadata', function () {
|
||||
it('JPEG', function (done) {
|
||||
sharp(fixtures.inputJpg).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual(2725, metadata.width);
|
||||
@@ -28,8 +27,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('JPEG with EXIF/ICC', function(done) {
|
||||
sharp(fixtures.inputJpgWithExif).metadata(function(err, metadata) {
|
||||
it('JPEG with EXIF/ICC', function (done) {
|
||||
sharp(fixtures.inputJpgWithExif).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual(450, metadata.width);
|
||||
@@ -43,14 +42,14 @@ describe('Image metadata', function() {
|
||||
// EXIF
|
||||
assert.strictEqual('object', typeof metadata.exif);
|
||||
assert.strictEqual(true, metadata.exif instanceof Buffer);
|
||||
var exif = exifReader(metadata.exif);
|
||||
const exif = exifReader(metadata.exif);
|
||||
assert.strictEqual('object', typeof exif);
|
||||
assert.strictEqual('object', typeof exif.image);
|
||||
assert.strictEqual('number', typeof exif.image.XResolution);
|
||||
// ICC
|
||||
assert.strictEqual('object', typeof metadata.icc);
|
||||
assert.strictEqual(true, metadata.icc instanceof Buffer);
|
||||
var profile = icc.parse(metadata.icc);
|
||||
const profile = icc.parse(metadata.icc);
|
||||
assert.strictEqual('object', typeof profile);
|
||||
assert.strictEqual('Generic RGB Profile', profile.description);
|
||||
done();
|
||||
@@ -58,8 +57,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
|
||||
if (sharp.format.tiff.input.file) {
|
||||
it('TIFF', function(done) {
|
||||
sharp(fixtures.inputTiff).metadata(function(err, metadata) {
|
||||
it('TIFF', function (done) {
|
||||
sharp(fixtures.inputTiff).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', metadata.format);
|
||||
assert.strictEqual(2464, metadata.width);
|
||||
@@ -77,8 +76,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
}
|
||||
|
||||
it('PNG', function(done) {
|
||||
sharp(fixtures.inputPng).metadata(function(err, metadata) {
|
||||
it('PNG', function (done) {
|
||||
sharp(fixtures.inputPng).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', metadata.format);
|
||||
assert.strictEqual(2809, metadata.width);
|
||||
@@ -95,8 +94,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Transparent PNG', function(done) {
|
||||
sharp(fixtures.inputPngWithTransparency).metadata(function(err, metadata) {
|
||||
it('Transparent PNG', function (done) {
|
||||
sharp(fixtures.inputPngWithTransparency).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', metadata.format);
|
||||
assert.strictEqual(2048, metadata.width);
|
||||
@@ -114,8 +113,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
|
||||
if (sharp.format.webp.input.file) {
|
||||
it('WebP', function(done) {
|
||||
sharp(fixtures.inputWebP).metadata(function(err, metadata) {
|
||||
it('WebP', function (done) {
|
||||
sharp(fixtures.inputWebP).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('webp', metadata.format);
|
||||
assert.strictEqual(1024, metadata.width);
|
||||
@@ -134,8 +133,8 @@ describe('Image metadata', function() {
|
||||
}
|
||||
|
||||
if (sharp.format.gif.input.file) {
|
||||
it('GIF via giflib', function(done) {
|
||||
sharp(fixtures.inputGif).metadata(function(err, metadata) {
|
||||
it('GIF via giflib', function (done) {
|
||||
sharp(fixtures.inputGif).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('gif', metadata.format);
|
||||
assert.strictEqual(800, metadata.width);
|
||||
@@ -150,8 +149,8 @@ describe('Image metadata', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('GIF grey+alpha via giflib', function(done) {
|
||||
sharp(fixtures.inputGifGreyPlusAlpha).metadata(function(err, metadata) {
|
||||
it('GIF grey+alpha via giflib', function (done) {
|
||||
sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('gif', metadata.format);
|
||||
assert.strictEqual(2, metadata.width);
|
||||
@@ -169,8 +168,8 @@ describe('Image metadata', function() {
|
||||
}
|
||||
|
||||
if (sharp.format.openslide.input.file) {
|
||||
it('Aperio SVS via openslide', function(done) {
|
||||
sharp(fixtures.inputSvs).metadata(function(err, metadata) {
|
||||
it('Aperio SVS via openslide', function (done) {
|
||||
sharp(fixtures.inputSvs).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('openslide', metadata.format);
|
||||
assert.strictEqual(2220, metadata.width);
|
||||
@@ -188,8 +187,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
}
|
||||
|
||||
it('File in, Promise out', function(done) {
|
||||
sharp(fixtures.inputJpg).metadata().then(function(metadata) {
|
||||
it('File in, Promise out', function (done) {
|
||||
sharp(fixtures.inputJpg).metadata().then(function (metadata) {
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual(2725, metadata.width);
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
@@ -205,8 +204,8 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Non-existent file in, Promise out', function(done) {
|
||||
sharp('fail').metadata().then(function(metadata) {
|
||||
it('Non-existent file in, Promise out', function (done) {
|
||||
sharp('fail').metadata().then(function (metadata) {
|
||||
throw new Error('Non-existent file');
|
||||
}, function (err) {
|
||||
assert.ok(!!err);
|
||||
@@ -214,10 +213,10 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Stream in, Promise out', function(done) {
|
||||
var readable = fs.createReadStream(fixtures.inputJpg);
|
||||
var pipeline = sharp();
|
||||
pipeline.metadata().then(function(metadata) {
|
||||
it('Stream in, Promise out', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const pipeline = sharp();
|
||||
pipeline.metadata().then(function (metadata) {
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual(2725, metadata.width);
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
@@ -230,15 +229,15 @@ describe('Image metadata', function() {
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
}).catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
readable.pipe(pipeline);
|
||||
});
|
||||
|
||||
it('Stream', function(done) {
|
||||
var readable = fs.createReadStream(fixtures.inputJpg);
|
||||
var pipeline = sharp().metadata(function(err, metadata) {
|
||||
it('Stream', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const pipeline = sharp().metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual(2725, metadata.width);
|
||||
@@ -256,9 +255,9 @@ describe('Image metadata', function() {
|
||||
readable.pipe(pipeline);
|
||||
});
|
||||
|
||||
it('Resize to half width using metadata', function(done) {
|
||||
var image = sharp(fixtures.inputJpg);
|
||||
image.metadata(function(err, metadata) {
|
||||
it('Resize to half width using metadata', function (done) {
|
||||
const image = sharp(fixtures.inputJpg);
|
||||
image.metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', metadata.format);
|
||||
assert.strictEqual(2725, metadata.width);
|
||||
@@ -271,7 +270,7 @@ describe('Image metadata', function() {
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
image.resize(Math.floor(metadata.width / 2)).toBuffer(function(err, data, info) {
|
||||
image.resize(Math.floor(metadata.width / 2)).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(1362, info.width);
|
||||
@@ -281,27 +280,27 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Keep EXIF metadata and add sRGB profile after a resize', function(done) {
|
||||
it('Keep EXIF metadata and add sRGB profile after a resize', function (done) {
|
||||
sharp(fixtures.inputJpgWithExif)
|
||||
.resize(320, 240)
|
||||
.withMetadata()
|
||||
.toBuffer(function(err, buffer) {
|
||||
.toBuffer(function (err, buffer) {
|
||||
if (err) throw err;
|
||||
sharp(buffer).metadata(function(err, metadata) {
|
||||
sharp(buffer).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, metadata.hasProfile);
|
||||
assert.strictEqual(8, metadata.orientation);
|
||||
assert.strictEqual('object', typeof metadata.exif);
|
||||
assert.strictEqual(true, metadata.exif instanceof Buffer);
|
||||
// EXIF
|
||||
var exif = exifReader(metadata.exif);
|
||||
const exif = exifReader(metadata.exif);
|
||||
assert.strictEqual('object', typeof exif);
|
||||
assert.strictEqual('object', typeof exif.image);
|
||||
assert.strictEqual('number', typeof exif.image.XResolution);
|
||||
// ICC
|
||||
assert.strictEqual('object', typeof metadata.icc);
|
||||
assert.strictEqual(true, metadata.icc instanceof Buffer);
|
||||
var profile = icc.parse(metadata.icc);
|
||||
const profile = icc.parse(metadata.icc);
|
||||
assert.strictEqual('object', typeof profile);
|
||||
assert.strictEqual('RGB', profile.colorSpace);
|
||||
assert.strictEqual('Perceptual', profile.intent);
|
||||
@@ -311,13 +310,13 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Remove EXIF metadata after a resize', function(done) {
|
||||
it('Remove EXIF metadata after a resize', function (done) {
|
||||
sharp(fixtures.inputJpgWithExif)
|
||||
.resize(320, 240)
|
||||
.withMetadata(false)
|
||||
.toBuffer(function(err, buffer) {
|
||||
.toBuffer(function (err, buffer) {
|
||||
if (err) throw err;
|
||||
sharp(buffer).metadata(function(err, metadata) {
|
||||
sharp(buffer).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
@@ -328,12 +327,12 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Remove metadata from PNG output', function(done) {
|
||||
it('Remove metadata from PNG output', function (done) {
|
||||
sharp(fixtures.inputJpgWithExif)
|
||||
.png()
|
||||
.toBuffer(function(err, buffer) {
|
||||
.toBuffer(function (err, buffer) {
|
||||
if (err) throw err;
|
||||
sharp(buffer).metadata(function(err, metadata) {
|
||||
sharp(buffer).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
@@ -344,30 +343,30 @@ describe('Image metadata', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('File input with corrupt header fails gracefully', function(done) {
|
||||
it('File input with corrupt header fails gracefully', function (done) {
|
||||
sharp(fixtures.inputJpgWithCorruptHeader)
|
||||
.metadata(function(err) {
|
||||
.metadata(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Buffer input with corrupt header fails gracefully', function(done) {
|
||||
it('Buffer input with corrupt header fails gracefully', function (done) {
|
||||
sharp(fs.readFileSync(fixtures.inputJpgWithCorruptHeader))
|
||||
.metadata(function(err) {
|
||||
.metadata(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Invalid withMetadata parameters', function() {
|
||||
it('String orientation', function() {
|
||||
assert.throws(function() {
|
||||
describe('Invalid withMetadata parameters', function () {
|
||||
it('String orientation', function () {
|
||||
assert.throws(function () {
|
||||
sharp().withMetadata({orientation: 'zoinks'});
|
||||
});
|
||||
});
|
||||
it('Negative orientation', function() {
|
||||
assert.throws(function() {
|
||||
it('Negative orientation', function () {
|
||||
assert.throws(function () {
|
||||
sharp().withMetadata({orientation: -1});
|
||||
});
|
||||
});
|
||||
@@ -376,8 +375,8 @@ describe('Image metadata', function() {
|
||||
sharp().withMetadata({ orientation: 0 });
|
||||
});
|
||||
});
|
||||
it('Too large orientation', function() {
|
||||
assert.throws(function() {
|
||||
it('Too large orientation', function () {
|
||||
assert.throws(function () {
|
||||
sharp().withMetadata({orientation: 9});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user