<commit_before>'use strict';

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var fs = require('fs');
var path = require('path');

chai.use(chaiAsPromised);
var should = chai.should();

var polly = require('..');

describe('The wait and retry policy with a asynchronous node call', function () {
    beforeEach(function () {
        polly.defaults.delay = 1;
    });

    it('should return the result when no error', function (done) {

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {
                fs.readFile(path.join(__dirname, './hello.txt'), cb);
            }, function (err, data) {
                should.not.exist(err);
                data.toString().should.equal('Hello world');
                done();
            });
    });

    it('should reject after an error', function (done) {

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                done();
            });
    });

    it('should retry once after an error and still fail', function (done) {
        var count = 0;

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(2);
                done();
            });
    });

    it('should retry five times after an error and still fail', function (done) {
        var count = 0;

        polly()
            .waitAndRetry([1, 1, 1, 1, 1])
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(6);
                done();
            });
    });

    it('should retry five times after an error and still fail', function (done) {
        var count = 0;

        polly()
            .waitAndRetry(5)
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(6);
                done();
            });
    });

    it('should retry once after an error and succeed', function (done) {
        var count = 0;

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {

                count++;
                if (count === 1) {
                    cb(new Error("Wrong value"));
                } else {
                    cb(undefined, 42);
                }
            }, function (err, data) {
                should.not.exist(err);
                data.should.equal(42);
                count.should.equal(2);
                done();
            });
    });

    it('should retry four times after an error and succeed', function (done) {
        var count = 0;

        polly()
            .waitAndRetry(5)
            .executeForNode(function (cb) {
                count++;
                if (count < 5) {
                    cb(new Error("Wrong value"));
                } else {
                    cb(undefined, 42);
                }
            }, function (err, data) {
                should.not.exist(err);
                data.should.equal(42);
                count.should.equal(5);
                done();
            });
    });

    it('should retry once because we are handling the error', function (done) {
        var count = 0;

        polly()
            .handle(function(err) {
                return err.code === 'ENOENT' && err.errno === -4058;
            })
            .waitAndRetry()
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(2);
                done();
            });
    });

    it('should now retry because we are not handling the error', function (done) {
        var count = 0;

        polly()
            .handle(function(err) {
                return err.code === 'ENOENT' && err.errno !== -4058;
            })
            .waitAndRetry()
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(1);
                done();
            });
    });
});
<commit_msg>Don't check the errno because these are different on Linux versus Windows<commit_after>'use strict';

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var fs = require('fs');
var path = require('path');

chai.use(chaiAsPromised);
var should = chai.should();

var polly = require('..');

describe('The wait and retry policy with a asynchronous node call', function () {
    beforeEach(function () {
        polly.defaults.delay = 1;
    });

    it('should return the result when no error', function (done) {

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {
                fs.readFile(path.join(__dirname, './hello.txt'), cb);
            }, function (err, data) {
                should.not.exist(err);
                data.toString().should.equal('Hello world');
                done();
            });
    });

    it('should reject after an error', function (done) {

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                done();
            });
    });

    it('should retry once after an error and still fail', function (done) {
        var count = 0;

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(2);
                done();
            });
    });

    it('should retry five times after an error and still fail', function (done) {
        var count = 0;

        polly()
            .waitAndRetry([1, 1, 1, 1, 1])
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(6);
                done();
            });
    });

    it('should retry five times after an error and still fail', function (done) {
        var count = 0;

        polly()
            .waitAndRetry(5)
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(6);
                done();
            });
    });

    it('should retry once after an error and succeed', function (done) {
        var count = 0;

        polly()
            .waitAndRetry()
            .executeForNode(function (cb) {

                count++;
                if (count === 1) {
                    cb(new Error("Wrong value"));
                } else {
                    cb(undefined, 42);
                }
            }, function (err, data) {
                should.not.exist(err);
                data.should.equal(42);
                count.should.equal(2);
                done();
            });
    });

    it('should retry four times after an error and succeed', function (done) {
        var count = 0;

        polly()
            .waitAndRetry(5)
            .executeForNode(function (cb) {
                count++;
                if (count < 5) {
                    cb(new Error("Wrong value"));
                } else {
                    cb(undefined, 42);
                }
            }, function (err, data) {
                should.not.exist(err);
                data.should.equal(42);
                count.should.equal(5);
                done();
            });
    });

    it('should retry once because we are handling the no such file or directory error', function (done) {
        var count = 0;

        polly()
            .handle(function(err) {
                return err.code === 'ENOENT'
            })
            .waitAndRetry()
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(2);
                done();
            });
    });

    it('should not retry because we are not handling the no such file or directory error', function (done) {
        var count = 0;

        polly()
            .handle(function(err) {
                return err.code !== 'ENOENT'
            })
            .waitAndRetry()
            .executeForNode(function (cb) {
                count++;
                fs.readFile(path.join(__dirname, './not-there.txt'), cb);
            }, function (err, data) {
                should.exist(err);
                err.should.be.instanceof(Error);
                should.not.exist(data);
                count.should.equal(1);
                done();
            });
    });
});

{'fraction_non_alphanumeric': 0.13140968375915668, 'fraction_numerical': 0.006432017152045739, 'mean_word_length': 1.220349067830226, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2542630', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2821, 'n_words': 651}