<commit_before>var fs = require('fs'),
    cluster = require('cluster'),
    rimraf = require('rimraf'),
    program = require('commander'),
    _ = require('lodash'),
    dialects = ['mysql', 'postgres'],
    exec = './node_modules/mocha/bin/_mocha',
    reportDir = './test/reports/',
    args = [],
    tasks = [],
    last, dialect, arg;

if (cluster.isMaster) {
    program
        .version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
        .usage('[options] [files]')
        .option('--dialect <dialect>', 'mysql|postgres', /(?:mysql|postgres)/i)
        .option('--cover', 'add coverage information');

    program.parse(process.argv);

    if (program.dialect) {
        dialects = [program.dialect];
    }

    for (var i = 0, len = dialects.length; i < len; i++) {
        tasks.push(cover.bind(null, dialects[i]));
    }

    if (program.cover) {
        args = [
            'cover',
            '--dir', './test/reports/coverage',
            '--report', 'none',
            // '--print', 'none',
            '--include-pid',
            exec,
            '--'
        ];
        exec = './node_modules/istanbul/lib/cli.js';

        tasks.push(function() {
            require('child_process').spawn('node', [
                exec, 'report',
                '--dir', reportDir + 'coverage'
            ], {
                stdio: 'inherit'
            });
        });
    }

    for (var i = 2, len = process.argv.length; i < len; i++) {
        arg = process.argv[i];
        if (arg === '--dialect') {
            i++;
        } else if (!/^--(?:dialect=|cover$)/.test(arg)) {
            args.push(arg);
        }
    }

    args.push.apply(args, ['-O', '']);
    last = args.length - 1;

    rimraf(reportDir, function() {
        cluster.on('exit', iterate);
        iterate();
    });
}

function iterate(worker, code, signal) {
    var task = tasks.shift();
    if ((code === 0 || code === undefined) && task) {
        task();
    }
}

function cover(dialect) {
    if (dialect) {
        args[last] = 'reportDir=' + reportDir + dialect;

        cluster.setupMaster({
            exec: exec,
            args: args
        });

        cluster.fork(_.defaults({
            DIALECT: dialect
        }, process.env));
    }<commit_msg>fix fork causing test hang<commit_after>var fs = require('fs'),
    cluster = require('cluster'),
    rimraf = require('rimraf'),
    program = require('commander'),
    defaults = require('lodash/defaults'),
    dialects = ['mysql', 'postgres'],
    exec = './node_modules/mocha/bin/_mocha',
    reportDir = './test/reports/',
    args = [],
    tasks = [], last, dialect, arg;

const {fork, spawn} = require('child_process');

if (cluster.isMaster) {
    program
        .version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
        .usage('[options] [files]')
        .option('--dialect <dialect>', 'mysql|postgres', /(?:mysql|postgres)/i)
        .option('--cover', 'add coverage information');

    program.parse(process.argv);

    if (program.dialect) {
        dialects = [program.dialect];
    }

    for (var i = 0, len = dialects.length; i < len; i++) {
        tasks.push(testSuite.bind(null, dialects[i]));
    }

    if (program.cover) {
        args = [
            'cover',
            '--dir', reportDir + 'coverage',
            '--report', 'none',
            // '--print', 'none',
            '--include-pid',
            exec,
            '--'
        ];
        exec = './node_modules/istanbul/lib/cli.js';

        tasks.push(function() {
            spawn('node', [
                exec, 'report',
                '--dir', reportDir + 'coverage'
            ], {
                stdio: 'inherit'
            });
        });
    }

    for (var i = 2, len = process.argv.length; i < len; i++) {
        arg = process.argv[i];
        if (arg === '--dialect') {
            i++;
        } else if (!/^--(?:dialect=|cover$)/.test(arg)) {
            args.push(arg);
        }
    }

    args.push.apply(args, ['-O', '']);
    last = args.length - 1;

    rimraf(reportDir, function() {
        iterate();
    });
}

function iterate(code) {
    var task = tasks.shift();
    if (!code && task) {
        task();
    }
}

function testSuite(dialect) {
    args[last] = 'reportDir=' + reportDir + dialect;

    const child = fork(exec, args, {
        env: defaults({
            DIALECT: dialect
        }, process.env)
    });

    child.on("error", iterate);
    child.on("exit", iterate);
{'fraction_non_alphanumeric': 0.19025573192239859, 'fraction_numerical': 0.001984126984126984, 'mean_word_length': 1.8023471278567016, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '6665433', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1404, 'n_words': 279}