/** @overview nodeunit test case. */

var testResource = require('../../../test_resource'),
    fs = require('fs'),
    mockInjector = require('../../../mock_injector'),
    generateTestFiles = require('../../../../ci/worker/generate_test_files.js');

exports.setUp = function (done) {
    done();
};

exports.tearDown = function (done) {
    mockInjector.restoreAll();
    done();
};


exports['Generate test files.'] = function (test) {
    testResource.prepareCleanWorkDirForTestFile(__filename, function (err, workDir) {
        var srcDir = testResource.resolveMockFile('.');
        generateTestFiles({
            srcDir: srcDir,
            destDir: workDir,
            pattern: '*.js',
            tmpl: testResource.resolveMockFile('mock-template.hbs')
        }, function (err) {
            test.ifError(err);
            generateTestFiles({
                srcDir: srcDir,
                destDir: workDir,
                pattern: '*.js',
                tmpl: testResource.resolveMockFile('mock-template.hbs')
            }, function (err) {
                test.ifError(err);
                test.done();
            });
        });
    });
};

exports['Require name.'] = function (test) {
    test.ok(generateTestFiles._requireName('foo/bar/baz', 'quz'));
    test.done();
};

exports['Handle errors.'] = function (test) {
    testResource.prepareCleanWorkDirForTestFile(__filename, function (err, workDir) {
        var srcDir = testResource.resolveMockFile('.');
        generateTestFiles({
            srcDir: srcDir,
            destDir: workDir,
            pattern: '*.js',
            tmpl: 'foo/bar/__not_exists'
        }, function (err) {
            test.ok(err);
            mockInjector.inject(fs, 'stat', function (filename, callback) {
                callback(new Error('mock stat error'));
            });
            generateTestFiles({
                srcDir: srcDir,
                destDir: workDir,
                pattern: '*.js',
                tmpl: 'foo/bar/__not_exists'
            }, function (err) {
                mockInjector.restore();
                test.ok(err);
                test.done();
            });
        });
    });
};