|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +const sharp = require('../../'); |
| 6 | +const fixtures = require('../fixtures'); |
| 7 | + |
| 8 | +describe('failOnError', function () { |
| 9 | + it('handles truncated JPEG by default', function (done) { |
| 10 | + sharp(fixtures.inputJpgTruncated) |
| 11 | + .resize(320, 240) |
| 12 | + // .toFile(fixtures.expected('truncated.jpg'), done); |
| 13 | + .toBuffer(function (err, data, info) { |
| 14 | + if (err) throw err; |
| 15 | + assert.strictEqual('jpeg', info.format); |
| 16 | + assert.strictEqual(320, info.width); |
| 17 | + assert.strictEqual(240, info.height); |
| 18 | + fixtures.assertSimilar(fixtures.expected('truncated.jpg'), data, done); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('handles truncated PNG by default', function (done) { |
| 23 | + sharp(fixtures.inputPngTruncated) |
| 24 | + .resize(320, 240) |
| 25 | + // .toFile(fixtures.expected('truncated.png'), done); |
| 26 | + .toBuffer(function (err, data, info) { |
| 27 | + if (err) throw err; |
| 28 | + assert.strictEqual('png', info.format); |
| 29 | + assert.strictEqual(320, info.width); |
| 30 | + assert.strictEqual(240, info.height); |
| 31 | + done(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('rejects invalid values', function () { |
| 36 | + assert.doesNotThrow(function () { |
| 37 | + sharp(fixtures.inputJpg, { failOnError: true }); |
| 38 | + }); |
| 39 | + |
| 40 | + assert.throws(function () { |
| 41 | + sharp(fixtures.inputJpg, { failOnError: 'zoinks' }); |
| 42 | + }); |
| 43 | + |
| 44 | + assert.throws(function () { |
| 45 | + sharp(fixtures.inputJpg, { failOnError: 1 }); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns errors to callback for truncated JPEG when failOnError is set', function (done) { |
| 50 | + sharp(fixtures.inputJpgTruncated, { failOnError: true }).toBuffer(function (err, data, info) { |
| 51 | + assert.ok(err.message.includes('VipsJpeg: Premature end of JPEG file'), err); |
| 52 | + assert.equal(data, null); |
| 53 | + assert.equal(info, null); |
| 54 | + done(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns errors to callback for truncated PNG when failOnError is set', function (done) { |
| 59 | + sharp(fixtures.inputPngTruncated, { failOnError: true }).toBuffer(function (err, data, info) { |
| 60 | + assert.ok(err.message.includes('vipspng: libpng read error'), err); |
| 61 | + assert.equal(data, null); |
| 62 | + assert.equal(info, null); |
| 63 | + done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('rejects promises for truncated JPEG when failOnError is set', function (done) { |
| 68 | + sharp(fixtures.inputJpgTruncated, { failOnError: true }) |
| 69 | + .toBuffer() |
| 70 | + .then(() => { |
| 71 | + throw new Error('Expected rejection'); |
| 72 | + }) |
| 73 | + .catch(err => { |
| 74 | + done(err.message.includes('VipsJpeg: Premature end of JPEG file') ? undefined : err); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments