<commit_before>exports = module.exports = {};

var fs = require("fs"),
    util = require("util"),
    esprima = require("esprima"),
    escodegen = require("escodegen"),
    estraverse = require("estraverse"),
    cp = require("child_process"),
    config = require(__dirname + "/config.js");

var DEBUG = false;
function log_debug(msg) {
    if (DEBUG)
        console.log(msg);
}

//Globals
var tmp_dir,
    // keep track of the number of attempts so far
    round = 0,
    ext,
    ast,
    smallest,
    testSucceededAtLeastOnce;

//Options
var /** only knock out entire statements */
    quick = false,

    /** Repeat until a fixpoint is found */
    findFixpoint = true,

    /** command to invoke to determine success/failure */
    cmd = null,

    /** error message indicating failure of command */
    errmsg = null,

    /** message indicating failure of command, either on stdout or stderr */
    msg = null,

    /** file to minimise */
    file = null,

    /** predicate to use for minimisation */
    predicate = {},

    /** arguments to pass to the predicate */
    predicate_args = [],

    /** file to record predicate results to */
    record = null,

    /** array to read predicate results from */
    replay = null, replay_idx = -1;

function parseOptions(options) {
    quick = options.quick;
    findFixpoint = options.findFixpoint;
    cmd = options.cmd;
    errmsg = options.errmsg;
    msg = options.msg;
    file = options.file;
    predicate = options.predicate; 
    predicate_args = options.predicate_args;
    record = options.record;
    replay = options.replay;
    replay_idx = options.replay_idx;
}


exports.main = function (options) {
    parseOptions(options);
    // check that we have something to minimise
    if (!file)
        usage();

        // initialise predicate module
    if (typeof predicate.init === 'function')
        predicate.init(predicate_args);

    // if no predicate module was specified, synthesise one from the other options
    if (!predicate.test) {
        predicate.cmd = predicate.cmd || cmd;

        if (replay) {
            predicate.test = function (fn) {
                var stats = fs.statSync(fn);
                console.log("Testing candidate " + fn +
                    " (" + stats.size + " bytes)");
                var res = replay[replay_idx++] === 'true';
                if (res)
                    console.log("    aborted with relevant error (recorded)");
                else
                    console.log("    completed successfully (recorded)");
                return res;
            };
        } else {
            if (!predicate.cmd) {
                console.error("No test command specified.");
                process.exit(-1);
            }

            if (typeof predicate.checkResult !== 'function') {
                if (errmsg || msg) {
                    predicate.checkResult = function (error, stdout, stderr) {
                        if ((errmsg && stderr && stderr.indexOf(errmsg) !== -1) ||
                            (msg && ((stderr && stderr.indexOf(msg) !== -1) ||
                                (stdout && stdout.indexOf(msg) !== -1)))) {
                            console.log("    aborted with relevant error");
                            return true;
                        } else if (error) {
                            console.log("    aborted with other error");
                            return false;
                        } else {
                            console.log("    completed successfully");
                            return false;
                        }
                    };
                } else {
                    predicate.checkResult = function (error, stdout, stderr) {
                        if (error) {
                            console.log("    aborted with error");
                            return true;
                        } else {
                            console.log("    completed successfully");
                            return false;
                        }
                    };
                }
            }

            predicate.test = function (fn) {
                var stats = fs.statSync(fn);
                console.log("Testing candidate " + fn +
                    " (" + stats.size + " bytes)");
                var start = new Date();
                var stdout_file = fn + ".stdout",
                    stderr_file = fn + ".stderr";
                var error = deltalib.execSync(predicate.cmd + " '" + fn + "'" +
                    " >'" + stdout_file + "'" +
                    " 2>'" + stderr_file + "'");
                var end = new Date();
                var stdout = fs.readFileSync(stdout_file, "utf-8"),
                    stderr = fs.readFileSync(stderr_file, "utf-8");
                return predicate.checkResult(error, stdout, stderr, end - start);
            };
        }
    }

    var src = fs.readFileSync(file, 'utf-8');

    // figure out file extension; default is 'js'
    ext = (file.match(/\.(\w+)$/) || [, 'js'])[1]
    // hack to make JSON work
    if (ext === 'json')
        src = '(' + src + ')';

    // parse given file
    ast = esprima.parse(src);

    // determine a suitable temporary directory
    for (i = 0; fs.existsSync(tmp_dir = config.tmp_dir + "/tmp" + i); ++i);
    fs.mkdirSync(tmp_dir);


    // the smallest test case so far is kept here
    smallest = tmp_dir + "/delta_js_smallest." + ext;

    testSucceededAtLeastOnce = false;
    // test the current test case

    // save a copy of the original input
    var orig = getTempFileName(),
        input = fs.readFileSync(file, 'utf-8');
    fs.writeFileSync(orig, input);
    fs.writeFileSync(smallest, input);

    // get started
    var res = predicate.test(orig);
    if (record)<commit_msg>Add minimal interface for jsdelta-multifile<commit_after>exports = module.exports = {};

var fs = require("fs"),
    util = require("util"),
    esprima = require("esprima"),
    escodegen = require("escodegen"),
    estraverse = require("estraverse"),
    cp = require("child_process"),
    config = require(__dirname + "/config.js");

var DEBUG = false;
function log_debug(msg) {
    if (DEBUG)
        console.log(msg);
}

//Globals
var tmp_dir,
    // keep track of the number of attempts so far
    round = 0,
    ext,
    ast,
    smallest,
    testSucceededAtLeastOnce;

//Options
var /** only knock out entire statements */
    quick = false,

    /** Repeat until a fixpoint is found */
    findFixpoint = true,

    /** command to invoke to determine success/failure */
    cmd = null,

    /** error message indicating failure of command */
    errmsg = null,

    /** message indicating failure of command, either on stdout or stderr */
    msg = null,

    /** file to minimise */
    file = null,

    /** predicate to use for minimisation */
    predicate = {},

    /** arguments to pass to the predicate */
    predicate_args = [],

    /** file to record predicate results to */
    record = null,

    /** array to read predicate results from */
    replay = null, replay_idx = -1;

function parseOptions(options) {
    quick = options.quick;
    findFixpoint = options.findFixpoint;
    cmd = options.cmd;
    errmsg = options.errmsg;
    msg = options.msg;
    file = options.file;
    predicate = options.predicate; 
    predicate_args = options.predicate_args;
    record = options.record;
    replay = options.replay;
    replay_idx = options.replay_idx;
}


exports.main = function (options) {
    parseOptions(options);
    // check that we have something to minimise
    if (!file)
        usage();

        // initialise predicate module
    if (typeof predicate.init === 'function')
        predicate.init(predicate_args);

    // if no predicate module was specified, synthesise one from the other options
    if (!predicate.test) {
        predicate.cmd = predicate.cmd || cmd;

        if (replay) {
            predicate.test = function (fn) {
                var stats = fs.statSync(fn);
                console.log("Testing candidate " + fn +
                    " (" + stats.size + " bytes)");
                var res = replay[replay_idx++] === 'true';
                if (res)
                    console.log("    aborted with relevant error (recorded)");
                else
                    console.log("    completed successfully (recorded)");
                return res;
            };
        } else {
            if (!predicate.cmd) {
                console.error("No test command specified.");
                process.exit(-1);
            }

            if (typeof predicate.checkResult !== 'function') {
                if (errmsg || msg) {
                    predicate.checkResult = function (error, stdout, stderr) {
                        if ((errmsg && stderr && stderr.indexOf(errmsg) !== -1) ||
                            (msg && ((stderr && stderr.indexOf(msg) !== -1) ||
                                (stdout && stdout.indexOf(msg) !== -1)))) {
                            console.log("    aborted with relevant error");
                            return true;
                        } else if (error) {
                            console.log("    aborted with other error");
                            return false;
                        } else {
                            console.log("    completed successfully");
                            return false;
                        }
                    };
                } else {
                    predicate.checkResult = function (error, stdout, stderr) {
                        if (error) {
                            console.log("    aborted with error");
                            return true;
                        } else {
                            console.log("    completed successfully");
                            return false;
                        }
                    };
                }
            }

            predicate.test = function (fn) {
                var stats = fs.statSync(fn);
                console.log("Testing candidate " + fn +
                    " (" + stats.size + " bytes)");
                var start = new Date();
                var stdout_file = fn + ".stdout",
                    stderr_file = fn + ".stderr";
                var error = deltalib.execSync(predicate.cmd + " '" + fn + "'" +
                    " >'" + stdout_file + "'" +
                    " 2>'" + stderr_file + "'");
                var end = new Date();
                var stdout = fs.readFileSync(stdout_file, "utf-8"),
                    stderr = fs.readFileSync(stderr_file, "utf-8");
                return predicate.checkResult(error, stdout, stderr, end - start);
            };
        }
    }

    var src = fs.readFileSync(file, 'utf-8');

    // figure out file extension; default is 'js'
    ext = (file.match(/\.(\w+)$/) || [, 'js'])[1]
    // hack to make JSON work
    if (ext === 'json')
        src = '(' + src + ')';

    // parse given file
    ast = esprima.parse(src);

    // determine a suitable temporary directory
    for (i = 0; fs.existsSync(tmp_dir = config.tmp_dir + "/tmp" + i); ++i);
    fs.mkdirSync(tmp_dir);

    // the smallest test case so far is kept here
    smallest = tmp_dir + "/delta_js_smallest." + ext;

    testSucceededAtLeastOnce = false;
    // test the current test case

    // save a copy of the original input
    var orig = getTempFileName(),
        input = fs.readFileSync(file, 'utf-8');
    fs.writeFileSync(orig, input);
    fs.writeFileSync(smallest, input);

    // get started
    var res = predicate.test(orig);
    if (record)
{'fraction_non_alphanumeric': 0.12029494984137871, 'fraction_numerical': 0.0022292720569321786, 'mean_word_length': 1.5154194522320465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '592729', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3036, 'n_words': 859}