<commit_before>'use strict';

let willInterruptProcess = require('../../../lib/utilities/will-interrupt-process');
let MockProcess = require('../../helpers/mock-process');
let captureExit;

let td = require('testdouble');
let chai = require('../../chai');
let expect = chai.expect;

describe('will interrupt process', function() {
  let cb;
  beforeEach(function() {
    captureExit = require('capture-exit');
    cb = td.function();
  });

  afterEach(function() {
    willInterruptProcess.release();
  });

  describe('capture-exit', function() {
    it('adds exit handler', function() {
      willInterruptProcess.capture(new MockProcess());
      willInterruptProcess.addHandler(cb);

      expect(captureExit.listenerCount()).to.equal(1);<commit_msg>more unit tests for will-interrupt-process<commit_after>'use strict';

let willInterruptProcess = require('../../../lib/utilities/will-interrupt-process');
let MockProcess = require('../../helpers/mock-process');
let captureExit;

let td = require('testdouble');
let chai = require('../../chai');
let expect = chai.expect;

describe('will interrupt process', function() {
  let cb;
  beforeEach(function() {
    captureExit = require('capture-exit');
    cb = td.function();
  });

  afterEach(function() {
    willInterruptProcess.release();
  });

  describe('capture', function() {
    it('throws if already captured', function() {
      const mockProcess = new MockProcess();

      willInterruptProcess.capture(mockProcess);
      try {
        willInterruptProcess.capture(mockProcess);
        expect(true).to.equal(false);
      } catch (e) {
        expect(e.message).to.equal('process already captured');
      }
    });

    it('throws if the process is not an EventEmitter instance', function() {
      const dissallowedArgs = [null, true, '', [], {}];

      dissallowedArgs.forEach(arg => {
        try {
          willInterruptProcess.capture(arg);
          expect(true).to.equal(false);
        } catch (e) {
          expect(e.message).to.equal('attempt to capture bad process instance');
        }
      });
    });

    it('sets process maxListeners count', function() {
      const mockProcess = new MockProcess();

      willInterruptProcess.capture(mockProcess);

      expect(mockProcess.getMaxListeners()).to.equal(1000);
    });
  });

  describe('addHandler', function() {
    it('throws if process is not captured', function() {
      try {
        willInterruptProcess.addHandler(() => {});
        expect(true).to.equal(false);
      } catch (e) {
        expect(e.message).to.equal('process is not captured');
      }

      const mockProcess = new MockProcess();

      willInterruptProcess.capture(mockProcess);
      willInterruptProcess.release();
    });

    it('throws if process is released', function() {
      willInterruptProcess.capture(new MockProcess());
      willInterruptProcess.release();

      try {
        willInterruptProcess.addHandler(() => {});
        expect(true).to.equal(false);
      } catch (e) {
        expect(e.message).to.equal('process is not captured');
      }
    });
  });

  describe('capture-exit', function() {
    it('adds exit handler', function() {
      willInterruptProcess.capture(new MockProcess());
      willInterruptProcess.addHandler(cb);

      expect(captureExit.listenerCount()).to.equal(1);
{'fraction_non_alphanumeric': 0.15892053973013492, 'fraction_numerical': 0.0017991004497751124, 'mean_word_length': 3.222784810126582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2363153', 'n_tokens_mistral': 939, 'n_tokens_neox': 919, 'n_words': 186}