<commit_before>var
  assert = require('assert'),
  rs = require('readable-stream'),
  sinon = require('sinon'),
  pathmodify = require('../../'),
  mod = pathmodify.mod,
  assign = Object.assign || require('object-assign'),
  EventEmitter = require('events').EventEmitter,
  util = require('util');

var real_pathmodify = pathmodify;

pathmodify = function (b, opts) {
  real_pathmodify.call(this, b, opts);
  b.__test.init_new_resolver();
};

describe('Plugin', function () {
  var
    b,
    resolver_done,
    // Pathmodify opts.
    opts;

  // Browserify stub constructor.
  function Browserify () {
    EventEmitter.call(this);
    this._init();
  }

  util.inherits(Browserify, EventEmitter);

  /**
   * Logic common to construction and reset.
   * Not part of actual Browserify API.
   */
  Browserify.prototype._init = function () {
    var b = this;
    /**
     * Make a get() for pipeline phase stub.
     */
    function make_getter () {
      return sinon.spy(function (key) {
        return this._phases[key];
      });
    };
    // make_getter

    /**
     * Make a pipeline phase stub.
     */
    function make_phase () {
      return {
        _phases: [],
        get: make_getter(),
        push: function (phase) {
          this._phases.push(phase);
        },
      };
    }
    // make_phase

    // Pipeline stub.
    var pipeline = {};

    pipeline.get = make_getter();

    pipeline._phases = {
      deps: make_phase(),
      pack: make_phase(),
    };

    // Augment the object with properties / methods related to testing.
    b.__test = {
      init_new_resolver: function () {
        this.__test.new_resolver = sinon.spy(
          this.pipeline.get("deps").get(0), "resolver");
      }.bind(b),
    };

    // Stub the original resolver.
    b.__test.original_resolver = sinon.spy(function (id, opts, cb) {
      var
        cfg = this.__test.original_resolver.cb_args,
        res = cfg.res;
      if (! (cfg.err || res)) res = id;
      cb(
        cfg.err,
        res,
        cfg.pkg
      );
    }.bind(b));

    b.__test.original_resolver.cb_args = {};

    // Placeholder for deps[0] (module-deps).
    pipeline.get('deps').push({
      resolver: b.__test.original_resolver,
    });

    // Placeholder for pack[0] (browser-pack).
    pipeline.get('pack').push({
    });

    b.pipeline = pipeline;
    b._expose = {};
  };
  // _init

  Browserify.prototype.reset = function () {
    this._init();
    this.emit('reset');
  };

  function make_browserify_stub () {
    var b = new Browserify;
    sinon.spy(b, "on");
    return b;
  }

  beforeEach(function () {
    b = make_browserify_stub();
    resolver_done = sinon.spy();
  });
  // beforeEach

  it("Works without opts", function () {
    assert.doesNotThrow(pathmodify.bind(null, b));
    assert(b.on.callCount === 1);
  });

  it("Doesn't leak internal data via opts", function () {
    var
      opts = {},
      num_opts = Object.keys(opts).length;

    pathmodify(b, opts);
    assert(
      Object.keys(opts).length === num_opts,
      "Opts has been mutated"
    );
  });

  it("Pushes aliaser stream", function () {
    var aliaser;

    pathmodify(b);

    aliaser = b.pipeline.get('deps').get(1);

    assert(
      aliaser,
      "Aliaser doesn't exist"
    );
    // Duck type as transform stream.
    assert(
      aliaser._transform,
      "Aliaser doesn't look like a transform stream"
    );
    assert(
      aliaser.label === "pathmodify:deps:post",
      "Aliaser doesn't have correct label"
    );<commit_msg>Add test for mutation of opts.mods.<commit_after>var
  assert = require('assert'),
  rs = require('readable-stream'),
  sinon = require('sinon'),
  pathmodify = require('../../'),
  mod = pathmodify.mod,
  assign = Object.assign || require('object-assign'),
  EventEmitter = require('events').EventEmitter,
  util = require('util');

var real_pathmodify = pathmodify;

pathmodify = function (b, opts) {
  real_pathmodify.call(this, b, opts);
  b.__test.init_new_resolver();
};

describe('Plugin', function () {
  var
    b,
    resolver_done,
    // Pathmodify opts.
    opts;

  // Browserify stub constructor.
  function Browserify () {
    EventEmitter.call(this);
    this._init();
  }

  util.inherits(Browserify, EventEmitter);

  /**
   * Logic common to construction and reset.
   * Not part of actual Browserify API.
   */
  Browserify.prototype._init = function () {
    var b = this;
    /**
     * Make a get() for pipeline phase stub.
     */
    function make_getter () {
      return sinon.spy(function (key) {
        return this._phases[key];
      });
    };
    // make_getter

    /**
     * Make a pipeline phase stub.
     */
    function make_phase () {
      return {
        _phases: [],
        get: make_getter(),
        push: function (phase) {
          this._phases.push(phase);
        },
      };
    }
    // make_phase

    // Pipeline stub.
    var pipeline = {};

    pipeline.get = make_getter();

    pipeline._phases = {
      deps: make_phase(),
      pack: make_phase(),
    };

    // Augment the object with properties / methods related to testing.
    b.__test = {
      init_new_resolver: function () {
        this.__test.new_resolver = sinon.spy(
          this.pipeline.get("deps").get(0), "resolver");
      }.bind(b),
    };

    // Stub the original resolver.
    b.__test.original_resolver = sinon.spy(function (id, opts, cb) {
      var
        cfg = this.__test.original_resolver.cb_args,
        res = cfg.res;
      if (! (cfg.err || res)) res = id;
      cb(
        cfg.err,
        res,
        cfg.pkg
      );
    }.bind(b));

    b.__test.original_resolver.cb_args = {};

    // Placeholder for deps[0] (module-deps).
    pipeline.get('deps').push({
      resolver: b.__test.original_resolver,
    });

    // Placeholder for pack[0] (browser-pack).
    pipeline.get('pack').push({
    });

    b.pipeline = pipeline;
    b._expose = {};
  };
  // _init

  Browserify.prototype.reset = function () {
    this._init();
    this.emit('reset');
  };

  function make_browserify_stub () {
    var b = new Browserify;
    sinon.spy(b, "on");
    return b;
  }

  beforeEach(function () {
    b = make_browserify_stub();
    resolver_done = sinon.spy();
  });
  // beforeEach

  it("Works without opts", function () {
    assert.doesNotThrow(pathmodify.bind(null, b));
    assert(b.on.callCount === 1);
  });

  it("Doesn't leak internal data via opts", function () {
    var
      opts = {},
      num_opts = Object.keys(opts).length;

    pathmodify(b, opts);
    assert(
      Object.keys(opts).length === num_opts,
      "Opts has been mutated"
    );
  });

  it("Doesn't allow mutation of opts.mods after instantiation", function () {
    var
      require_id = "dep-before",
      opts = {mods: [
      ]};

    pathmodify(b, opts);
    opts.mods.push(sinon.spy());

    b.__test.new_resolver(require_id, {}, resolver_done);

    assert(
      opts.mods[0].callCount === 0,
      "Mod added after instantiation was called"
    );
  });

  it("Pushes aliaser stream", function () {
    var aliaser;

    pathmodify(b);

    aliaser = b.pipeline.get('deps').get(1);

    assert(
      aliaser,
      "Aliaser doesn't exist"
    );
    // Duck type as transform stream.
    assert(
      aliaser._transform,
      "Aliaser doesn't look like a transform stream"
    );
    assert(
      aliaser.label === "pathmodify:deps:post",
      "Aliaser doesn't have correct label"
    );
{'fraction_non_alphanumeric': 0.149605245550649, 'fraction_numerical': 0.0016057808109193096, 'mean_word_length': 2.289612676056338, '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': '4180986', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2449, 'n_words': 526}