<commit_before>var Promise      = require('bluebird');
var expect       = require('expect.js');
var EventEmitter = require('events').EventEmitter;
var fs           = Promise.promisifyAll(require('fs'));
var mockfs       = require('mock-fs');
var fixtures     = require('../fixtures/mockfsTestFiles.js');
var Detector     = require('../../lib/detector.js');

describe('Detector', function() {
  // Used to test emitted events
  var found;
  var listener = function(magicNumber) {
    found.push(magicNumber);
  };

  before(function() {
    mockfs(fixtures);
  });

  after(function() {
    mockfs.restore();
  });

  beforeEach(function() {
    found = [];
  });

  describe('constructor', function() {
    it('inherits from EventEmitter', function() {
      expect(new Detector()).to.be.an(EventEmitter);
    });

    it('accepts an array of file paths', function() {
      var filePaths = ['path1.js', 'path2.js'];
      var detector = new Detector(filePaths);

      expect(detector._filePaths).to.be(filePaths);
    });

    it('accepts a boolean to enforce the use of const', function() {
      var detector = new Detector([], true);
      expect(detector._enforceConst).to.be(true);
    });

    it('accepts an array of numbers to ignore', function() {
      var ignore = [1, 2, 3.4];
      var detector = new Detector([], false, ignore);
      expect(detector._ignore).to.be(ignore);
    });
  });

  describe('run', function() {
    it('is compatible with callbacks', function(done) {
      var detector = new Detector(['emptyFile.js']);
      detector.run(function(err) {
        done(err);
      });
    });

    it('is compatible with promises', function(done) {
      var detector = new Detector(['emptyFile.js']);
      detector.run().then(function() {
        done();
      }).catch(function(err) {
        done(err);
      });
    });

    it('returns an Error if not given an array of file paths', function(done) {
      var detector = new Detector();
      detector.run().catch(function(err) {
        expect(err).to.be.an(Error);
        expect(err.message).to.be('filePaths must be a non-empty array of paths');
        done();
      });
    });
  });

  it('emits end on completion, passing the number of files parsed', function(done) {
    var detector = new Detector(['emptyFile.js', 'singleVariable.js']);
    detector.on('end', function(numFiles) {
      expect(numFiles).to.be(2);
      done();
    });

    detector.run().catch(function(err) {
      done(err);
    });
  });

  it('emits no events when parsing an empty file', function(done) {
    var detector = new Detector(['emptyFile.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  it('emits no events when the file contains only named constants', function(done) {
    var detector = new Detector(['singleVariable.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(function(err) {
      done(err);
    });
  });

 it('emits no events for literals assigned to object properties', function(done) {
    var detector = new Detector(['objectProperties.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.have.length(2);
      expect(found[0].lineSource).to.be('colors.BLUE = 2 + 1;');
      expect(found[1].lineSource).to.be('colors.BLUE = 2 + 1;');
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  it('emits no events for numbers marked by ignore:line', function(done) {
    var detector = new Detector(['lineIgnore.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  it('emits no events between ignore:start / ignore:end', function(done) {
    var detector = new Detector(['blockIgnore.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  it('emits a "found" event containing a magic number, when found', function(done) {
    var detector = new Detector(['secondsInMinute.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.have.length(1);
      expect(found[0].value).to.be(60);
      expect(found[0].file).to.be('secondsInMinute.js');
      expect(found[0].startColumn).to.be(9);
      expect(found[0].endColumn).to.be(11);
      expect(found[0].fileLength).to.be(3);
      expect(found[0].lineNumber).to.be(2);
      expect(found[0].lineSource).to.be('  return 60;');
      expect(found[0].contextLines).to.eql([
        'function getSecondsInMinute() {', '  return 60;', '}'
      ]);
      expect(found[0].contextIndex).to.eql(1);
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  it('skips unnamed constants within the ignore list', function(done) {
    var detector = new Detector(['ignore.js'], false, [0]);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.have.length(1);
      expect(found[0].value).to.be(1);
      done();
    }).catch(function(err) {
      done(err);
    });
  });

  describe('with enforceConst set to true', function() {
    it('emits a "found" event for variable declarations', function(done) {
      var detector = new Detector(['constVariable.js'], true);
      detector.on('found', listener);

      detector.run().then(function() {
        expect(found).to.have.length(1);
        expect(found[0].value).to.be(10);
        done();
      }).catch(function(err) {
        done(err);
      });
    });

    it('emits a "found" event for object expressions', function(done) {
      var detector = new Detector(['constObject.js'], true);
      detector.on('found', listener);

      detector.run().then(function() {
        expect(found).to.have.length(1);
        expect(found[0].value).to.be(10);
        done();
      }).catch(function(err) {
        done(err);
      });
    });
  });
});
<commit_msg>Cleanup catch callbacks in specs<commit_after>var Promise      = require('bluebird');
var expect       = require('expect.js');
var EventEmitter = require('events').EventEmitter;
var fs           = Promise.promisifyAll(require('fs'));
var mockfs       = require('mock-fs');
var fixtures     = require('../fixtures/mockfsTestFiles.js');
var Detector     = require('../../lib/detector.js');

describe('Detector', function() {
  // Used to test emitted events
  var found;
  var listener = function(magicNumber) {
    found.push(magicNumber);
  };

  before(function() {
    mockfs(fixtures);
  });

  after(function() {
    mockfs.restore();
  });

  beforeEach(function() {
    found = [];
  });

  describe('constructor', function() {
    it('inherits from EventEmitter', function() {
      expect(new Detector()).to.be.an(EventEmitter);
    });

    it('accepts an array of file paths', function() {
      var filePaths = ['path1.js', 'path2.js'];
      var detector = new Detector(filePaths);

      expect(detector._filePaths).to.be(filePaths);
    });

    it('accepts a boolean to enforce the use of const', function() {
      var detector = new Detector([], true);
      expect(detector._enforceConst).to.be(true);
    });

    it('accepts an array of numbers to ignore', function() {
      var ignore = [1, 2, 3.4];
      var detector = new Detector([], false, ignore);
      expect(detector._ignore).to.be(ignore);
    });
  });

  describe('run', function() {
    it('is compatible with callbacks', function(done) {
      var detector = new Detector(['emptyFile.js']);
      detector.run(function(err) {
        done(err);
      });
    });

    it('is compatible with promises', function(done) {
      var detector = new Detector(['emptyFile.js']);
      detector.run().then(function() {
        done();
      }).catch(done);
    });

    it('returns an Error if not given an array of file paths', function(done) {
      var detector = new Detector();
      detector.run().catch(function(err) {
        expect(err).to.be.an(Error);
        expect(err.message).to.be('filePaths must be a non-empty array of paths');
        done();
      });
    });
  });

  it('emits end on completion, passing the number of files parsed', function(done) {
    var detector = new Detector(['emptyFile.js', 'singleVariable.js']);
    detector.on('end', function(numFiles) {
      expect(numFiles).to.be(2);
      done();
    });

    detector.run().catch(done);
  });

  it('emits no events when parsing an empty file', function(done) {
    var detector = new Detector(['emptyFile.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(done);
  });

  it('emits no events when the file contains only named constants', function(done) {
    var detector = new Detector(['singleVariable.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(done);
  });

 it('emits no events for literals assigned to object properties', function(done) {
    var detector = new Detector(['objectProperties.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.have.length(2);
      expect(found[0].lineSource).to.be('colors.BLUE = 2 + 1;');
      expect(found[1].lineSource).to.be('colors.BLUE = 2 + 1;');
      done();
    }).catch(done);
  });

  it('emits no events for numbers marked by ignore:line', function(done) {
    var detector = new Detector(['lineIgnore.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(done);
  });

  it('emits no events between ignore:start / ignore:end', function(done) {
    var detector = new Detector(['blockIgnore.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.be.empty();
      done();
    }).catch(done);
  });

  it('emits a "found" event containing a magic number, when found', function(done) {
    var detector = new Detector(['secondsInMinute.js']);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.have.length(1);
      expect(found[0].value).to.be(60);
      expect(found[0].file).to.be('secondsInMinute.js');
      expect(found[0].startColumn).to.be(9);
      expect(found[0].endColumn).to.be(11);
      expect(found[0].fileLength).to.be(3);
      expect(found[0].lineNumber).to.be(2);
      expect(found[0].lineSource).to.be('  return 60;');
      expect(found[0].contextLines).to.eql([
        'function getSecondsInMinute() {', '  return 60;', '}'
      ]);
      expect(found[0].contextIndex).to.eql(1);
      done();
    }).catch(done);
  });

  it('skips unnamed constants within the ignore list', function(done) {
    var detector = new Detector(['ignore.js'], false, [0]);
    detector.on('found', listener);

    detector.run().then(function() {
      expect(found).to.have.length(1);
      expect(found[0].value).to.be(1);
      done();
    }).catch(done);
  });

  describe('with enforceConst set to true', function() {
    it('emits a "found" event for variable declarations', function(done) {
      var detector = new Detector(['constVariable.js'], true);
      detector.on('found', listener);

      detector.run().then(function() {
        expect(found).to.have.length(1);
        expect(found[0].value).to.be(10);
        done();
      }).catch(done);
    });

    it('emits a "found" event for object expressions', function(done) {
      var detector = new Detector(['constObject.js'], true);
      detector.on('found', listener);

      detector.run().then(function() {
        expect(found).to.have.length(1);
        expect(found[0].value).to.be(10);
        done();
      }).catch(done);
    });
  });
});

{'fraction_non_alphanumeric': 0.1837476257329259, 'fraction_numerical': 0.00792798744735321, 'mean_word_length': 3.0046296296296298, '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': '1277063', 'n_tokens_mistral': 3790, 'n_tokens_neox': 3736, 'n_words': 771}