<commit_before>/**
 * Configure Rowdy.
 *
 * Override options with:
 * ```
 * ROWDY_OPTIONS='{ clientLogger: true }'
 * ```
 *
 * Hook to a configuration with:
 * ```
 * ROWDY_SETTINGS="local.chrome"
 * ```
 *
 * Basic configurations for a Browser `settings` entry:
 * ```
 * {
 *   // Selenium Desired Capabilities.
 *   // https://code.google.com/p/selenium/wiki/DesiredCapabilities
 *   desiredCapabilities: {},
 *
 *   // WD.js/WebdriverIO remote parameters.
 *   // https://github.com/admc/wd#named-parameters
 *   // https://github.com/webdriverio/webdriverio/blob/master/examples/
 *   //         webdriverio.browserstack.js
 *   //         webdriverio.saucelabs.js
 *   remote: {},
 *
 *   // Uses SauceLabs SAAS.
 *   isSauceLabs: true,
 *
 *   // Uses BrowserStack SAAS.
 *   isBrowserStack: true,
 * }
 * ```
 */
var _ = require("lodash");
var guacamole = require("guacamole");

var ROWDY_SETTINGS = process.env.ROWDY_SETTINGS || "";
var ROWDY_OPTIONS = process.env.ROWDY_OPTIONS || "{}";
var QUOTES_RE = /^[\s\\\'\"]*|[\s\\\'\"]*$/g;

/**
 * Driver libraries.
 *
 * Map enum to `require` statement.
 */
var DRIVER_LIBS = {
  WD: "wd",                   // https://github.com/admc/wd
  WEBDRIVERIO: "webdriverio"  // http://webdriver.io/
};

/**
 * Global default: local phantomjs.
 *
 * Note: Only if *no* default top-level setting specified.
 */
var GLOBAL_DEFAULT = {
  desiredCapabilities: {
    browserName: "phantomjs"
  },
  server: {
    start: true,
    phantomPath: false
  }
};

var DEFAULTS = {
  /**
   * Misc. options.
   *
   * Options can be globally overriden with a merge of a stringified JSON
   * object like:
   * ```
   * ROWDY_OPTIONS='{ "clientLogger": true, "serverLogger": true }'
   * ROWDY_OPTIONS='{ "driverLib": "wd" }'
   * ROWDY_OPTIONS='{ "driverLib": "webdriverio" }'
   * ```
   */
  options: {
    clientLogger: false,
    serverLogger: false,
    serverDebug: false,
    driverLib: DRIVER_LIBS.WD // `wd` or `webdriverio`
  },

  settings: {}
};

// TODO: Refactor and decompose out the `max-statements` warning.
/*eslint-disable max-statements*/
module.exports = function (cfg) {
  // Merge config.
  cfg = _.cloneDeep(_.merge({}, DEFAULTS, cfg));

  // --------------------------------------------------------------------------
  // Options: Rowdy configuration.
  // --------------------------------------------------------------------------
  try {
    _.merge(cfg.options, JSON.parse(ROWDY_OPTIONS.replace(QUOTES_RE, "")));
  } catch (err) {
    /*eslint-disable no-console*/
    console.error("Unable to parse ROWDY_OPTIONS: " + err.toString());
    /*eslint-enable no-console*/
    throw err;
  }

  // Validate driver library.
  if (!_.includes(DRIVER_LIBS, cfg.options.driverLib)) {
    throw new Error("Unknown driver library option: " + cfg.options.driverLib);
  }

  // --------------------------------------------------------------------------
  // Settings: Extract current desired setting path
  // --------------------------------------------------------------------------

  var settingsPath = ROWDY_SETTINGS.replace(QUOTES_RE, "").split(".");
  settingsPath = _.chain(settingsPath)
    .map(function (v) { return v.trim(); })
    .filter(_.identity)
    .value();


  // --------------------------------------------------------------------------
  // SauceLabs: Perform browser object substitutions (Guacamole)
  // --------------------------------------------------------------------------

  // If guacamole is enabled, insert sauceLabs browser objects where needed.
  if (cfg.settings && cfg.settings.guacamoleEnabled) {
    if (settingsPath.length === 2 && settingsPath[0] === "sauceLabs") {
      //
      // TODO: Fix this path
      //
      guacamole.useShrinkwrap("../../../guacamole-shrinkwrap.json");

      var browserId = settingsPath[1];
      var matchingCapabilities = guacamole.get({ id: browserId });

      if (matchingCapabilities.length !== 1) {
        throw new Error("Unknown SauceLabs browser id: " + browserId);
      }

      if (!cfg.settings.sauceLabs) {
        cfg.settings.sauceLabs = {};
      }
      // Insert coniguration
      cfg.settings.sauceLabs[browserId] = {desiredCapabilities: matchingCapabilities[0] };
    }
  }

  // --------------------------------------------------------------------------
  // Settings: Available Selenium-ish options.
  // --------------------------------------------------------------------------
  if (!cfg.settings.default) {
    cfg.settings.default = GLOBAL_DEFAULT;
  }

  // Then, apply merge settings defaults to other objects one level down.
  _.each(cfg.settings, function (objs, objsKey) {
    if (objsKey === "default" && !objs.default) { return; }
    _.each(objs, function (obj, objKey) {
      if (objKey === "default" || !_.isObject(obj)) { return; }
      // Merge defaults
      objs[objKey] = _.merge({}, objs.default, obj);
    });
  });

  // Finally, choose desired configuration and place on `setting`.
  // If no settings path, _try_ to go for `local.default`, then `default`.
  if (!settingsPath.length) {
    if (cfg.settings.local && cfg.settings.local.default) {
      settingsPath = ["local", "default"];
    } else {
      settingsPath = ["default"];
    }
  }

  // --------------------------------------------------------------------------
  // Setting: The actual chosen Selenium-ish setting.
  // --------------------------------------------------------------------------
  cfg.setting = _.reduce(settingsPath, function (memo, key) {
    if (!_.has(memo, key)) {
      throw new Error("Bad ROWDY_SETTINGS path: " + ROWDY_SETTINGS);
    }

    return memo[key];
  }, cfg.settings);

  return cfg;
};
/*eslint-enable max-statements*/
<commit_msg>fix lint<commit_after>/**
 * Configure Rowdy.
 *
 * Override options with:
 * ```
 * ROWDY_OPTIONS='{ clientLogger: true }'
 * ```
 *
 * Hook to a configuration with:
 * ```
 * ROWDY_SETTINGS="local.chrome"
 * ```
 *
 * Basic configurations for a Browser `settings` entry:
 * ```
 * {
 *   // Selenium Desired Capabilities.
 *   // https://code.google.com/p/selenium/wiki/DesiredCapabilities
 *   desiredCapabilities: {},
 *
 *   // WD.js/WebdriverIO remote parameters.
 *   // https://github.com/admc/wd#named-parameters
 *   // https://github.com/webdriverio/webdriverio/blob/master/examples/
 *   //         webdriverio.browserstack.js
 *   //         webdriverio.saucelabs.js
 *   remote: {},
 *
 *   // Uses SauceLabs SAAS.
 *   isSauceLabs: true,
 *
 *   // Uses BrowserStack SAAS.
 *   isBrowserStack: true,
 * }
 * ```
 */
var _ = require("lodash");
var guacamole = require("guacamole");

var ROWDY_SETTINGS = process.env.ROWDY_SETTINGS || "";
var ROWDY_OPTIONS = process.env.ROWDY_OPTIONS || "{}";
var QUOTES_RE = /^[\s\\\'\"]*|[\s\\\'\"]*$/g;

/**
 * Driver libraries.
 *
 * Map enum to `require` statement.
 */
var DRIVER_LIBS = {
  WD: "wd",                   // https://github.com/admc/wd
  WEBDRIVERIO: "webdriverio"  // http://webdriver.io/
};

/**
 * Global default: local phantomjs.
 *
 * Note: Only if *no* default top-level setting specified.
 */
var GLOBAL_DEFAULT = {
  desiredCapabilities: {
    browserName: "phantomjs"
  },
  server: {
    start: true,
    phantomPath: false
  }
};

var DEFAULTS = {
  /**
   * Misc. options.
   *
   * Options can be globally overriden with a merge of a stringified JSON
   * object like:
   * ```
   * ROWDY_OPTIONS='{ "clientLogger": true, "serverLogger": true }'
   * ROWDY_OPTIONS='{ "driverLib": "wd" }'
   * ROWDY_OPTIONS='{ "driverLib": "webdriverio" }'
   * ```
   */
  options: {
    clientLogger: false,
    serverLogger: false,
    serverDebug: false,
    driverLib: DRIVER_LIBS.WD // `wd` or `webdriverio`
  },

  settings: {}
};

// TODO: Refactor and decompose out the `max-statements` warning.
/*eslint-disable max-statements*/
module.exports = function (cfg) {
  // Merge config.
  cfg = _.cloneDeep(_.merge({}, DEFAULTS, cfg));

  // --------------------------------------------------------------------------
  // Options: Rowdy configuration.
  // --------------------------------------------------------------------------
  try {
    _.merge(cfg.options, JSON.parse(ROWDY_OPTIONS.replace(QUOTES_RE, "")));
  } catch (err) {
    /*eslint-disable no-console*/
    console.error("Unable to parse ROWDY_OPTIONS: " + err.toString());
    /*eslint-enable no-console*/
    throw err;
  }

  // Validate driver library.
  if (!_.includes(DRIVER_LIBS, cfg.options.driverLib)) {
    throw new Error("Unknown driver library option: " + cfg.options.driverLib);
  }

  // --------------------------------------------------------------------------
  // Settings: Extract current desired setting path
  // --------------------------------------------------------------------------

  var settingsPath = ROWDY_SETTINGS.replace(QUOTES_RE, "").split(".");
  settingsPath = _.chain(settingsPath)
    .map(function (v) { return v.trim(); })
    .filter(_.identity)
    .value();


  // --------------------------------------------------------------------------
  // SauceLabs: Perform browser object substitutions (Guacamole)
  // --------------------------------------------------------------------------

  // If guacamole is enabled, insert sauceLabs browser objects where needed.
  if (cfg.settings && cfg.settings.guacamoleEnabled) {
    if (settingsPath.length === 2 && settingsPath[0] === "sauceLabs") {
      //
      // TODO: Fix this path
      //
      guacamole.useShrinkwrap("../../../guacamole-shrinkwrap.json");

      var browserId = settingsPath[1];
      var matchingCapabilities = guacamole.get({ id: browserId });

      if (matchingCapabilities.length !== 1) {
        throw new Error("Unknown SauceLabs browser id: " + browserId);
      }

      if (!cfg.settings.sauceLabs) {
        cfg.settings.sauceLabs = {};
      }
      // Insert coniguration
      cfg.settings.sauceLabs[browserId] = {
        desiredCapabilities: matchingCapabilities[0]
      };
    }
  }

  // --------------------------------------------------------------------------
  // Settings: Available Selenium-ish options.
  // --------------------------------------------------------------------------
  if (!cfg.settings.default) {
    cfg.settings.default = GLOBAL_DEFAULT;
  }

  // Then, apply merge settings defaults to other objects one level down.
  _.each(cfg.settings, function (objs, objsKey) {
    if (objsKey === "default" && !objs.default) { return; }
    _.each(objs, function (obj, objKey) {
      if (objKey === "default" || !_.isObject(obj)) { return; }
      // Merge defaults
      objs[objKey] = _.merge({}, objs.default, obj);
    });
  });

  // Finally, choose desired configuration and place on `setting`.
  // If no settings path, _try_ to go for `local.default`, then `default`.
  if (!settingsPath.length) {
    if (cfg.settings.local && cfg.settings.local.default) {
      settingsPath = ["local", "default"];
    } else {
      settingsPath = ["default"];
    }
  }

  // --------------------------------------------------------------------------
  // Setting: The actual chosen Selenium-ish setting.
  // --------------------------------------------------------------------------
  cfg.setting = _.reduce(settingsPath, function (memo, key) {
    if (!_.has(memo, key)) {
      throw new Error("Bad ROWDY_SETTINGS path: " + ROWDY_SETTINGS);
    }

    return memo[key];
  }, cfg.settings);

  return cfg;
};
/*eslint-enable max-statements*/

{'fraction_non_alphanumeric': 0.27107380202868137, 'fraction_numerical': 0.0008744316194473592, 'mean_word_length': 1.7592279855247286, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '1012076', 'n_tokens_mistral': 3464, 'n_tokens_neox': 3396, 'n_words': 725}