<commit_before>"use strict";

var fs      = require("fs");
var path    = require("path");
var spawn   = require("child_process").spawn;
var resolve = require("resolve").sync;
var lodash  = require("lodash");

module.exports = {
  getConfigPath: function () {
    return path.resolve(process.cwd(), "buster.js");
  },

  generateBusterFile: function (options, tests) {
    options = lodash.extend({
      name:        "testrun " + ~~(9999 * Math.random()),
      environment: "node",
      rootPath:    process.cwd()
    }, options);

    options.tests = tests.map(function (file) {
      return path.relative(options.rootPath, file);
    });

    var content = [
      "'use strict';",
      "var config = module.exports;",
      "config['" + options.name + "'] = " + JSON.stringify(options)
    ].join("\n");

    fs.writeFileSync(this.getConfigPath(), content);
  },

  deleteBusterFile: function () {
    if (fs.existsSync(this.getConfigPath())) {
      fs.unlinkSync(this.getConfigPath());
    }
  },

  findBusterExecutable: function () {
    // Will be smth like node_modules/buster/lib/buster.js
    var busterPath = resolve("buster", { basedir: process.cwd() });

    // Will be smth like node_modules/buster/lib
    var busterDir  = path.dirname(busterPath);

    // Resolve to node_modules/buster/bin/buster-test
    return path.resolve(busterDir, "..", "bin", "buster-test");
  },

  runBuster: function (stdout, stderr, callback) {
    var code = 0;
    var call = spawn(
      this.findBusterExecutable(),
      [ "-c", this.getConfigPath() ]
    );

    call.stdout.on("data", function(data) {
      var lines = data.toString().split("\n");

      // This is overly bad and needs to get fixed.
      // Ideally one would correctly read the exit code of buster.
      lines.forEach(function (line) {
        if (!!line.match(/(Failure|Error|Timeout):\s/)) {
          code = 1;
        }
      });

      stdout.write(data);
    });

    call.stderr.on("data", function (data) {
      stderr.write(data);
    });

    call.on("exit", function () {
      callback(code);
    });
  }
};<commit_msg>Exit with an error if command errored<commit_after>"use strict";

var fs      = require("fs");
var path    = require("path");
var spawn   = require("child_process").spawn;
var resolve = require("resolve").sync;
var lodash  = require("lodash");

module.exports = {
  getConfigPath: function () {
    return path.resolve(process.cwd(), "buster.js");
  },

  generateBusterFile: function (options, tests) {
    options = lodash.extend({
      name:        "testrun " + ~~(9999 * Math.random()),
      environment: "node",
      rootPath:    process.cwd()
    }, options);

    options.tests = tests.map(function (file) {
      return path.relative(options.rootPath, file);
    });

    var content = [
      "'use strict';",
      "var config = module.exports;",
      "config['" + options.name + "'] = " + JSON.stringify(options)
    ].join("\n");

    fs.writeFileSync(this.getConfigPath(), content);
  },

  deleteBusterFile: function () {
    if (fs.existsSync(this.getConfigPath())) {
      fs.unlinkSync(this.getConfigPath());
    }
  },

  findBusterExecutable: function () {
    // Will be smth like node_modules/buster/lib/buster.js
    var busterPath = resolve("buster", { basedir: process.cwd() });

    // Will be smth like node_modules/buster/lib
    var busterDir  = path.dirname(busterPath);

    // Resolve to node_modules/buster/bin/buster-test
    return path.resolve(busterDir, "..", "bin", "buster-test");
  },

  runBuster: function (stdout, stderr, callback) {
    var code = 0;
    var call = spawn(
      this.findBusterExecutable(),
      [ "-c", this.getConfigPath() ]
    );

    call.stdout.on("data", function(data) {
      var lines = data.toString().split("\n");

      // This is overly bad and needs to get fixed.
      // Ideally one would correctly read the exit code of buster.
      lines.forEach(function (line) {
        if (!!line.match(/(Failure|Error|Timeout):\s/)) {
          code = 1;
        }
      });

      stdout.write(data);
    });

    call.stderr.on("data", function (data) {
      code = 2;
      stderr.write(data);
    });

    call.on("exit", function () {
      callback(code);
    });
  }
};
{'fraction_non_alphanumeric': 0.16627524659464538, 'fraction_numerical': 0.0030530765617660873, 'mean_word_length': 2.684256055363322, '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': '5780064', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1343, 'n_words': 290}