/**
 * Write unit test file.
 * @function ci.worker._writeUnitTestFiles
 * @param {string} dest - Destination directory.
 * @param {function} tmpl - Test file template.
 * @param {object} data - Render data.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 */
var path = require('path'),
    lib = require('../../lib'),
    file = lib.file,
    debug = require('./_debug');
module.exports = function (dest, tmpl, data, callback) {
    function resolveRelativePath(filename) {
        return path.relative(path.dirname(dest), path.resolve(filename));
    }

    data.testResourcePath = resolveRelativePath('test/test_resource');
    data.mockInjectorPath = resolveRelativePath('test/mock_injector');
    var content = tmpl(data);
    file.writeReadonlyFile(dest, content, function (err) {
        if (!err) {
            debug.didCreateFile(dest);
        }
        callback(err);
    });
};