var installNodeModules = require('../../../../ci/worker/install_node_modules'),
    childProcess = require('child_process'),
    mockInjector = require('../../../mock_injector');

exports.setUp = function (done) {
    process.env.CI_LOG_DISABLED = true;
    done();
};
exports.tearDown = function (done) {
    process.env.CI_LOG_DISABLED = false;
    mockInjector.restoreAll();
    done();
};


exports['Get package names'] = function (test) {
    var data = installNodeModules._packages({
        "commander": "~2.1.0",
        "cli-color": "~0.2.3",
        "tek": "~0.3.42",
        "prettyjson": "~0.10.0"
    });
    test.equal(data.length, 4);
    test.done();
};

exports['Do log.'] = function (test) {
    mockInjector.inject(console, 'log', function () {
    });
    mockInjector.inject(console, 'error', function () {
    });
    installNodeModules._log('foo');
    installNodeModules._log('ERR!');
    mockInjector.restoreAll();
    test.done();
};

exports['Install npm modules.'] = function (test) {
    mockInjector.inject(installNodeModules, '_npm', function () {
        return childProcess.spawn('echo');
    });
    installNodeModules({
        packages: {
            foo: 'bar'
        }
    }, function (err) {
        test.ifError(err);

        mockInjector.restore();
        mockInjector.inject(installNodeModules, '_log', function () {

        });
        mockInjector.inject(installNodeModules, '_npm', function () {
            return childProcess.spawn('cd', ['/foo/bar/__not_exist']);
        });
        installNodeModules({
            packages: {
                foo: 'bar'
            }
        }, function (err) {
            test.ifError(err);
            mockInjector.restore();
            mockInjector.restore();
            test.done();
        });
    });
};

exports['Try with empty packages.'] = function (test) {
    installNodeModules({
        packages: {}
    }, function (err) {
        test.ok(err);
        test.done();
    });
};