/**
 * Run tests.
 * @function ci.worker.runTest
 * @param {object} config - Work configuration.
 * @param {string[]} config.files - mocha test files.
 * @param {string} config.runnerFile - Test runner file.
 * @param {string[]} config.runArgs - Args to pass to the runner
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */

var file = require('../../lib/file'),
    expandGlob = file.expandGlob,
    fork = require('child_process').fork;

exports = module.exports = function (config, callback) {
    if (!callback) callback = new Function;
    expandGlob(config.files || config, function (err, filenames) {
        if (err) {
            callback(err);
            return;
        }
        var runner = fork(config.runnerFile, config.runArgs, {
            env: process.env
        });
        runner.send(filenames);
        runner.on('message', function (data) {
            runner.kill();
            var err = data.err && '\nTest failed!\n';
            callback(err, data.report);
        });
    });
};