<commit_before>/**
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var should = require('should');

var cli = require('../cli');
var capture = require('../util').capture;

var fs = require('fs');
var pathUtil = require('path');
if (!fs.existsSync) {
    fs.existsSync = pathUtil.existsSync;
};

var baseTestTempDir = "__temp";
var testDirBase = "test";
var testDirIndex = 0;
var testDir = "";

suite('cli', function () {
    suite('site deploymentscript', function () {
        setup(function () {
            // Reset site deploymentscript command
            for (var i in cli.categories.site.commands) {
                var command = cli.categories.site.commands[i];
                if (command.name == 'deploymentscript') {
                    delete command.php;
                    delete command.basic;
                    delete command.aspWAP;
                    delete command.aspWebSite;
                    delete command.python;
                    delete command.node;
                    delete command.repositoryRoot;
                    delete command.suppressPrompt;
                    delete command.scriptType;
                    delete command.solutionFile;
                }
            }

            testDirIndex++;
            testDir = pathUtil.join(baseTestTempDir, testDirBase + testDirIndex);
            ensurePathExists(testDir);
        });

        teardown(function () {
            removePath(testDir);
        });

        test('generate basic deployment script (--basic -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --basic -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate php deployment script (--php -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --php -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate python deployment script (--python -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --python -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate python deployment script twice with -y (--python -y -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --python -y -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, function () {
                runBasicSiteDeploymentScriptScenario(cmd, done);
            });
        });

        test('generate basic aspWebSite deployment script (--aspWebSite -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --aspWebSite -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('using exclusion flags together should fail (--aspWebSite --python ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --aspWebSite --python -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('using exclusion flags together should fail (--node --php ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node --php -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('using exclusion flags together should fail (--aspWAP . --basic ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --aspWAP . --basic -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('using exclusion flags together should fail (--basic --php ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --basic --php -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('--aspWAP requires project file path argument', function (done) {
            var cmd = ('node cli.js site deploymentscript -r ' + testDir + ' --aspWAP').split(' ');

            runErrorScenario(cmd, 'argument missing', done);
        });

        test('--node requires directory to contain server.js file (--node)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'Missing server.js/app.js file', done);
        });

        test('generate node deployment script (--node -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node -r ' + testDir).split(' ');

            runNodeSiteDeploymentScriptScenario(cmd, done);
        });
    });
});

function runBasicSiteDeploymentScriptScenario(cmd, callback) {
    runSiteDeploymentScriptScenario(
        cmd,
        ['Generating deployment script for Web Site', 'Generated deployment script (deploy.cmd and .deployment)'],
        ['echo Handling Basic Web Site deployment.'],
        'deploy.cmd',
        callback);
}

function runNodeSiteDeploymentScriptScenario(cmd, callback) {
    generateServerJsFile();

    runSiteDeploymentScriptScenario(
        cmd,
        ['Generating deployment script for node', 'Generated deployment script (deploy.cmd and .deployment)'],
        ['echo Handling node.js deployment.'],
        'deploy.cmd',
        callback);
}

function runSiteDeploymentScriptScenario(cmd, outputContains, scriptContains, scriptFileName, callback) {
    runCommand(cmd, function (result, e) {
        if (e) {
            callback(e);
            return;
        }

        result.exitStatus.should.equal(0, 'Received an error status exit code');

        for (var i = 0; i < outputContains.length; i++) {
            result.text.should.include(outputContains[i]);
        }

        var deployCmdContent = getFileContent(scriptFileName);
        for (var i = 0; i < scriptContains.length; i++) {
            deployCmdContent.should.include(scriptContains[i]);
        }<commit_msg>Added several bash script generation tests.<commit_after>/**
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var should = require('should');

var cli = require('../cli');
var capture = require('../util').capture;

var fs = require('fs');
var pathUtil = require('path');
if (!fs.existsSync) {
    fs.existsSync = pathUtil.existsSync;
};

var baseTestTempDir = "__temp";
var testDirBase = "test";
var testDirIndex = 0;
var testDir = "";

suite('cli', function () {
    suite('site deploymentscript', function () {
        setup(function () {
            // Reset site deploymentscript command
            for (var i in cli.categories.site.commands) {
                var command = cli.categories.site.commands[i];
                if (command.name == 'deploymentscript') {
                    delete command.php;
                    delete command.basic;
                    delete command.aspWAP;
                    delete command.aspWebSite;
                    delete command.python;
                    delete command.node;
                    delete command.repositoryRoot;
                    delete command.suppressPrompt;
                    delete command.scriptType;
                    delete command.solutionFile;
                }
            }

            testDirIndex++;
            testDir = pathUtil.join(baseTestTempDir, testDirBase + testDirIndex);
            ensurePathExists(testDir);
        });

        teardown(function () {
            removePath(testDir);
        });

        test('generate batch basic deployment script (--basic -t batch -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --basic -t batch -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done, /*bash*/false);
        });

        test('generate bash basic deployment script (--basic -t bash -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --basic -t bash -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done, /*bash*/true);
        });

        test('generate batch php deployment script (--php -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --php -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate bash php deployment script (--php -t bash -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --php -t bash -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done, /*bash*/true);
        });

        test('generate batch python deployment script (--python -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --python -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate batch python deployment script twice with -y (--python -y -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --python -y -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, function () {
                runBasicSiteDeploymentScriptScenario(cmd, done);
            });
        });

        test('generate bash python deployment script (--python --scriptType bash -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --python --scriptType bash -r ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done, /*bash*/true);
        });

        test('generate batch basic aspWebSite deployment script (--aspWebSite --repositoryRoot)', function (done) {
            var cmd = ('node cli.js site deploymentscript --aspWebSite --repositoryRoot ' + testDir).split(' ');

            runBasicSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate node deployment script (--node -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node -r ' + testDir).split(' ');

            runNodeSiteDeploymentScriptScenario(cmd, done);
        });

        test('generate bash node deployment script (--node -t bash -r)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node -t bash -r ' + testDir).split(' ');

            runNodeSiteDeploymentScriptScenario(cmd, done, /*bash*/true);
        });

        test('using exclusion flags together should fail (--aspWebSite --python ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --aspWebSite --python -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('using exclusion flags together should fail (--node --php ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node --php -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('using exclusion flags together should fail (--aspWAP . --basic ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --aspWAP . --basic -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('using exclusion flags together should fail (--basic --php ...)', function (done) {
            var cmd = ('node cli.js site deploymentscript --basic --php -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'specify only one of these flags', done);
        });

        test('--aspWAP requires project file path argument', function (done) {
            var cmd = ('node cli.js site deploymentscript -r ' + testDir + ' --aspWAP').split(' ');

            runErrorScenario(cmd, 'argument missing', done);
        });

        test('--node requires directory to contain server.js file (--node)', function (done) {
            var cmd = ('node cli.js site deploymentscript --node -r ' + testDir).split(' ');

            runErrorScenario(cmd, 'Missing server.js/app.js file', done);
        });
    });
});

function runBasicSiteDeploymentScriptScenario(cmd, callback, bash) {
    var scriptFileName = bash ? 'deploy.sh' : 'deploy.cmd';
    var scriptExtraInclue = bash ? '#!/bin/bash' : '@echo off';

    runSiteDeploymentScriptScenario(
        cmd,
        ['Generating deployment script for Web Site', 'Generated deployment script (' + scriptFileName + ' and .deployment)'],
        ['echo Handling Basic Web Site deployment.', scriptExtraInclue],
        scriptFileName,
        callback);
}

function runNodeSiteDeploymentScriptScenario(cmd, callback, bash) {
    generateServerJsFile();

    var scriptFileName = bash ? 'deploy.sh' : 'deploy.cmd';
    var scriptExtraInclue = bash ? '#!/bin/bash' : '@echo off';

    runSiteDeploymentScriptScenario(
        cmd,
        ['Generating deployment script for node', 'Generated deployment script (' + scriptFileName + ' and .deployment)'],
        ['echo Handling node.js deployment.', scriptExtraInclue],
        scriptFileName,
        callback);
}

function runSiteDeploymentScriptScenario(cmd, outputContains, scriptContains, scriptFileName, callback) {
    runCommand(cmd, function (result, e) {
        if (e) {
            callback(e);
            return;
        }

        result.exitStatus.should.equal(0, 'Received an error status exit code');

        for (var i = 0; i < outputContains.length; i++) {
            result.text.should.include(outputContains[i]);
        }

        var deployCmdContent = getFileContent(scriptFileName);
        for (var i = 0; i < scriptContains.length; i++) {
            deployCmdContent.should.include(scriptContains[i]);
        }
{'fraction_non_alphanumeric': 0.12557200538358007, 'fraction_numerical': 0.0016150740242261105, 'mean_word_length': 2.353870458135861, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5565', 'n_tokens_mistral': 3969, 'n_tokens_neox': 3840, 'n_words': 1199}