<commit_before>var should = require('should'),
    gd = require('../index.js'),
    _ = require('underscore'),
    fs = require('fs'),
    util = require('util'),
    stream = require('stream'),
    samples = require('./samples.js'),
    h = require('./helpers.js')

describe('gd', function () {
    describe('Image.prototype', function () {
        describe('save()', function () {
            var image = h.createImage()

            it('should syncronously return a buffer when called with empty target argument', function () {
                _.each(samples.types, function (type) {
                    var buffer = image.save({format: type})
                    checkGeneratedImage(image, buffer, type)
                })
            })

            _.each(samples.types, function (type) {
                it('should asynchronously return a ' + type + ' buffer when called with empty target argument and a callback', function (done) {
                    var buffer = image.save({format: type}, function (err, buffer) {
                        checkGeneratedImage(image, buffer, type)
                        done()
                    })
                })
            })

            _.each(samples.types, function (type) {
                it('should synchronously write a ' + type + ' file', function () {
                    var tmpFilename = __dirname + '/save_sync_test.' + type
                    after(_.partial(fs.unlinkSync, tmpFilename))

                    image.save(tmpFilename)

                    var buffer = fs.readFileSync(tmpFilename)
                    checkGeneratedImage(image, buffer, type)
                })
            })

            _.each(samples.types, function (type) {
                it('should asynchronously write a ' + type + ' to a file', function (done) {
                    var tmpFilename = __dirname + '/save_async_test.' + type
                    image.save(tmpFilename, {format: type}, function (err) {
                        var buffer = fs.readFileSync(tmpFilename)
                        fs.unlinkSync(tmpFilename)
                        checkGeneratedImage(image, buffer, type)
                        done()
                    })
                })
            })

            _.each(samples.types, function (type) {
                it('should asycnronously write a ' + type + ' to a stream', function (done) {
                    var stream = h.WritableStream()
                    stream.on('finish', function () {
                        checkGeneratedImage(image, this.written, type)
                        done()
                    })
                    image.save(stream, {format: type}, function (err) {
                        should(err).equal(null)
                    })
                })
            })

            it('should throw gd.SynchronousStreamAccessError exception on sync save to a stream', function () {
                h.testErrorSync(gd.SynchronousStreamAccessError, function () {
                    image.save(h.WritableStream(), {format: 'jpeg'})
                })
            })

            it('should throw gd.FileWriteError exception when failed to synchronously save to a file', function () {
                h.testErrorSync(gd.FileWriteError, function () {
                    image.save('nosuchdir/some.jpg')
                })
            })

            it('should return gd.FileWriteError error when failed to asynchronously save to a file', function (done) {
                h.testErrorAsync(gd.FileWriteError, done, function (callback) {
                    image.save('nosuchdir/some.jpg', callback)
                })
            })

            _.each({'.jpeg': 'jpeg', '.jpg': 'jpeg', '.gif': 'gif', '.png': 'png'}, function (format, extname) {
                it('should detect ' + format + ' destination format by ' + extname + ' filename extension', function () {
                    _.each([extname, extname.toUpperCase()], function (extname) {
                        var tmpFilename = __dirname + '/format_detection_test' + extname
                        image.save(tmpFilename)
                        gd.open(tmpFilename).format.should.be.equal(format)
                        fs.unlinkSync(tmpFilename)
                    })
                })
            })

            it('should throw gd.DestinationFormatRequiredError exception when no target format set', function () {
                h.testErrorSync(gd.DestinationFormatRequiredError, function () {
                    var buffer = image.save()
                })
            })

            it('should return gd.DestinationFormatRequiredError error when no target format set', function (done) {
                h.testErrorAsync(gd.DestinationFormatRequiredError, done, function (callback) {
                    image.save(h.WritableStream(), callback)
                })
            })

            it('should throw gd.UnknownImageFormatError exception when unknown target format set', function () {
                h.testErrorSync(gd.UnknownImageFormatError, function () {
                    var buffer = image.save({format: 'tiff'})
                })
            })

            it('should return gd.UnknownImageFormatError error when unknown target format set', function (done) {
                h.testErrorAsync(gd.UnknownImageFormatError, done, function (callback) {
                    image.save({format: 'tiff'}, callback)
                })
            })

            it('should throw gd.UnknownDestinationTypeError exception when target is not a file, a buffer or a writable stream', function () {
                h.testErrorSync(gd.UnknownDestinationTypeError, function () {
                    var buffer = image.save(0xDEADBEEF, {format: 'jpeg'})
                })
            })

            it('should asynchronously return gd.UnknownDestinationTypeError error when target is not a file, a buffer or a writable stream', function (done) {
                h.testErrorAsync(gd.UnknownDestinationTypeError, done, function (callback) {
                    image.save(0xDEADBEEF, {format: 'jpeg'}, callback)
                })
            })

            it('should return the image object when synchronously saving to file', function () {
                var tmpFilename = './return_value_test.jpg'
                var result = image.save(tmpFilename)
                result.should.be.equal(image)
                fs.unlinkSync(tmpFilename)
            })

            it('should return the image object when asynchronously saving to file', function () {
                var tmpFilename = './return_value_test.jpg'
                var result = image.save(tmpFilename, function () {
                    fs.unlinkSync(tmpFilename)
                })
                result.should.be.equal(image)
            })

            it('should return the image object when saving to stream', function () {
                var stream = h.WritableStream()
                var result = image.save(stream, {format: 'jpeg'}, function () {})
                result.should.be.equal(image)
            })

            it('should return the image object when asynchronously saving to buffer', function () {
                var result = image.save({format: 'jpeg'}, function (err, buffer) {})
                result.should.be.equal(image)
            })

            it('should choose the format in this order: file extension, options.format, image.format', function () {
                var image = h.createImage(),
                    jpegFilename = 'choose_format.jpg',
                    tmpFilename = 'choose_format.tmp'
                image.format = 'gif'
                after(_.partial(fs.unlinkSync, jpegFilename))
                after(_.partial(fs.unlinkSync, tmpFilename))

                image.save(jpegFilename, {format: 'png'})
                gd.open(jpegFilename).format.should.be.equal('jpeg')

                image.save(tmpFilename, {format: 'png'})
                gd.open(tmpFilename).format.should.be.equal('png')

                image.save(tmpFilename)
                gd.open(tmpFilename).format.should.be.equal('gif')
            })

            it('should set jpeg quality with `quality` option', function () {
                var highQualityJpeg = image.save({format: 'jpeg', quality: 100}),
                    lowQualityJpeg  = image.save({format: 'jpeg', quality: 1})
                lowQualityJpeg.length.should.be.below(highQualityJpeg.length)
            })

            it('should set png compression type with `compression` option', function () {
                var highCompressionPng = image.save({format: 'png', compression: 9}),
                    lowCompressionPng  = image.save({format: 'png', compression: 1})
                highCompressionPng.length.should.be.below(lowCompressionPng.length)
            })

            it('shoud substitute {ext} in the filename with the extention based on target format', function () {
                var typeExtensions = {
                    jpeg: 'jpg',
                    png:  'png',
                    gif:  'gif',
                }

                _.each(samples.buffersByType, function (buffer, type) {
                    var filenameTemplate = './extByTypeTest.{ext}',
                        expectedFilename = filenameTemplate.replace('{ext}', typeExtensions[type])
                    after(_.partial(fs.unlinkSync, expectedFilename))

                    fs.existsSync(expectedFilename).should.not.be.ok
                    image.save(filenameTemplate, {format: type})
                    fs.existsSync(expectedFilename).should.be.ok
                })

            })
        })

        describe('resize()', function () {
            var target = {width: 50, height: 50}

            it('should save image ratio on resize', function () {<commit_msg>Fixed temporary test files path<commit_after>var should = require('should'),
    gd = require('../index.js'),
    _ = require('underscore'),
    fs = require('fs'),
    util = require('util'),
    stream = require('stream'),
    samples = require('./samples.js'),
    h = require('./helpers.js')

describe('gd', function () {
    describe('Image.prototype', function () {
        describe('save()', function () {
            var image = h.createImage()

            it('should syncronously return a buffer when called with empty target argument', function () {
                _.each(samples.types, function (type) {
                    var buffer = image.save({format: type})
                    checkGeneratedImage(image, buffer, type)
                })
            })

            _.each(samples.types, function (type) {
                it('should asynchronously return a ' + type + ' buffer when called with empty target argument and a callback', function (done) {
                    var buffer = image.save({format: type}, function (err, buffer) {
                        checkGeneratedImage(image, buffer, type)
                        done()
                    })
                })
            })

            _.each(samples.types, function (type) {
                it('should synchronously write a ' + type + ' file', function () {
                    var tmpFilename = __dirname + '/save_sync_test.' + type
                    after(_.partial(fs.unlinkSync, tmpFilename))

                    image.save(tmpFilename)

                    var buffer = fs.readFileSync(tmpFilename)
                    checkGeneratedImage(image, buffer, type)
                })
            })

            _.each(samples.types, function (type) {
                it('should asynchronously write a ' + type + ' to a file', function (done) {
                    var tmpFilename = __dirname + '/save_async_test.' + type
                    image.save(tmpFilename, {format: type}, function (err) {
                        var buffer = fs.readFileSync(tmpFilename)
                        fs.unlinkSync(tmpFilename)
                        checkGeneratedImage(image, buffer, type)
                        done()
                    })
                })
            })

            _.each(samples.types, function (type) {
                it('should asycnronously write a ' + type + ' to a stream', function (done) {
                    var stream = h.WritableStream()
                    stream.on('finish', function () {
                        checkGeneratedImage(image, this.written, type)
                        done()
                    })
                    image.save(stream, {format: type}, function (err) {
                        should(err).equal(null)
                    })
                })
            })

            it('should throw gd.SynchronousStreamAccessError exception on sync save to a stream', function () {
                h.testErrorSync(gd.SynchronousStreamAccessError, function () {
                    image.save(h.WritableStream(), {format: 'jpeg'})
                })
            })

            it('should throw gd.FileWriteError exception when failed to synchronously save to a file', function () {
                h.testErrorSync(gd.FileWriteError, function () {
                    image.save('nosuchdir/some.jpg')
                })
            })

            it('should return gd.FileWriteError error when failed to asynchronously save to a file', function (done) {
                h.testErrorAsync(gd.FileWriteError, done, function (callback) {
                    image.save('nosuchdir/some.jpg', callback)
                })
            })

            _.each({'.jpeg': 'jpeg', '.jpg': 'jpeg', '.gif': 'gif', '.png': 'png'}, function (format, extname) {
                it('should detect ' + format + ' destination format by ' + extname + ' filename extension', function () {
                    _.each([extname, extname.toUpperCase()], function (extname) {
                        var tmpFilename = __dirname + '/format_detection_test' + extname
                        image.save(tmpFilename)
                        gd.open(tmpFilename).format.should.be.equal(format)
                        fs.unlinkSync(tmpFilename)
                    })
                })
            })

            it('should throw gd.DestinationFormatRequiredError exception when no target format set', function () {
                h.testErrorSync(gd.DestinationFormatRequiredError, function () {
                    var buffer = image.save()
                })
            })

            it('should return gd.DestinationFormatRequiredError error when no target format set', function (done) {
                h.testErrorAsync(gd.DestinationFormatRequiredError, done, function (callback) {
                    image.save(h.WritableStream(), callback)
                })
            })

            it('should throw gd.UnknownImageFormatError exception when unknown target format set', function () {
                h.testErrorSync(gd.UnknownImageFormatError, function () {
                    var buffer = image.save({format: 'tiff'})
                })
            })

            it('should return gd.UnknownImageFormatError error when unknown target format set', function (done) {
                h.testErrorAsync(gd.UnknownImageFormatError, done, function (callback) {
                    image.save({format: 'tiff'}, callback)
                })
            })

            it('should throw gd.UnknownDestinationTypeError exception when target is not a file, a buffer or a writable stream', function () {
                h.testErrorSync(gd.UnknownDestinationTypeError, function () {
                    var buffer = image.save(0xDEADBEEF, {format: 'jpeg'})
                })
            })

            it('should asynchronously return gd.UnknownDestinationTypeError error when target is not a file, a buffer or a writable stream', function (done) {
                h.testErrorAsync(gd.UnknownDestinationTypeError, done, function (callback) {
                    image.save(0xDEADBEEF, {format: 'jpeg'}, callback)
                })
            })

            it('should return the image object when synchronously saving to file', function () {
                var tmpFilename = __dirname + '/return_value_test.jpg'
                var result = image.save(tmpFilename)
                result.should.be.equal(image)
                fs.unlinkSync(tmpFilename)
            })

            it('should return the image object when asynchronously saving to file', function () {
                var tmpFilename = __dirname + '/return_value_test.jpg'
                var result = image.save(tmpFilename, function () {
                    fs.unlinkSync(tmpFilename)
                })
                result.should.be.equal(image)
            })

            it('should return the image object when saving to stream', function () {
                var stream = h.WritableStream()
                var result = image.save(stream, {format: 'jpeg'}, function () {})
                result.should.be.equal(image)
            })

            it('should return the image object when asynchronously saving to buffer', function () {
                var result = image.save({format: 'jpeg'}, function (err, buffer) {})
                result.should.be.equal(image)
            })

            it('should choose the format in this order: file extension, options.format, image.format', function () {
                var image = h.createImage(),
                    jpegFilename = __dirname + '/choose_format.jpg',
                    tmpFilename = __dirname + '/choose_format.tmp'
                image.format = 'gif'
                after(_.partial(fs.unlinkSync, jpegFilename))
                after(_.partial(fs.unlinkSync, tmpFilename))

                image.save(jpegFilename, {format: 'png'})
                gd.open(jpegFilename).format.should.be.equal('jpeg')

                image.save(tmpFilename, {format: 'png'})
                gd.open(tmpFilename).format.should.be.equal('png')

                image.save(tmpFilename)
                gd.open(tmpFilename).format.should.be.equal('gif')
            })

            it('should set jpeg quality with `quality` option', function () {
                var highQualityJpeg = image.save({format: 'jpeg', quality: 100}),
                    lowQualityJpeg  = image.save({format: 'jpeg', quality: 1})
                lowQualityJpeg.length.should.be.below(highQualityJpeg.length)
            })

            it('should set png compression type with `compression` option', function () {
                var highCompressionPng = image.save({format: 'png', compression: 9}),
                    lowCompressionPng  = image.save({format: 'png', compression: 1})
                highCompressionPng.length.should.be.below(lowCompressionPng.length)
            })

            it('shoud substitute {ext} in the filename with the extention based on target format', function () {
                var typeExtensions = {
                    jpeg: 'jpg',
                    png:  'png',
                    gif:  'gif',
                }

                _.each(samples.buffersByType, function (buffer, type) {
                    var filenameTemplate = __dirname + '/extByTypeTest.{ext}',
                        expectedFilename = filenameTemplate.replace('{ext}', typeExtensions[type])
                    after(_.partial(fs.unlinkSync, expectedFilename))

                    fs.existsSync(expectedFilename).should.not.be.ok
                    image.save(filenameTemplate, {format: type})
                    fs.existsSync(expectedFilename).should.be.ok
                })

            })
        })

        describe('resize()', function () {
            var target = {width: 50, height: 50}

            it('should save image ratio on resize', function () {
{'fraction_non_alphanumeric': 0.10883112935096519, 'fraction_numerical': 0.0012159902720778233, 'mean_word_length': 1.6163838812301166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1917184', 'n_tokens_mistral': 4954, 'n_tokens_neox': 4579, 'n_words': 1151}