<commit_before>/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true nomen:true, node:true continue:true*/

/*
 * Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */

var fs = require("fs");
var glob = require("glob");
var path = require('path');
var Properties = require("../util/properties");
var log4js = require("log4js");
var LibManager = require('../util/libmanager');
var FileUtil = require("../util/fileutil");
var ErrorManager = require("../util/errormanager");

function ArrowSetup(config, argv) {
    this.config = config;
    this.argv = argv;
}

ArrowSetup.prototype.setup = function () {
    this.setuplog4js();
    this.setupReportDir();
    this.setupDefaultDriverName();
    this.setupTestEngine();
    this.setupDefaultLib();
    this.setupHeadlessParam();
    this.setupCapabilities();
    this.setupMisc();
    this.setupReplaceParam();
    this.setupDefaultParam();
    this.errorCheck(); // keep this as last function call for checking errors.

};

ArrowSetup.prototype.setupReplaceParam = function () {
    if (this.argv.replaceParamJSON) {
        this.config.replaceParamJSON = this.argv.replaceParamJSON;
    }
};

ArrowSetup.prototype.setupDefaultParam = function () {
    if (this.argv.defaultParamJSON) {
        this.config.defaultParamJSON = this.argv.defaultParamJSON;
    }
};


ArrowSetup.prototype.errorCheck = function () {
    var self = this, em = ErrorManager.getInstance(),
    Environment = function() {
        if (self.argv.argv.cooked) {
            // em.errorLog("self.argv.argv.cooked {0}",  JSON.stringify(self.argv.argv.cooked));
            var cooked = JSON.stringify(self.argv.argv.cooked), proc = self.mock || process;
            cooked.replace(/"--context","([\w\W]*?)"/, function (match, context) {
                // em.errorLog("match is {0}, context is {1}", match, context);
                context.replace(/environment:([\w\W]*)/, function (match, env) {
                    // em.errorLog("match is {0}, env is {1}", match, env);
                    if (self.dimensions[0].environment[env] === undefined) {
                        em.errorLog(1000, env, self.dimensionsFile);
                        proc.exit(1);
                    } else {
                        self.environment = env;
                    }
                });
            });
        }
    },
    Dimensions = function() {
        // To Do : should check dimensions file with Json schema
        // em.errorLog("config {0} argv {1}", JSON.stringify(self.config), JSON.stringify(self.argv));
        var dimensions = self.argv.dimensions || self.config.dimensions || "", dimJson, i = 0, proc = self.mock || process,
            errorMessage, argv = self.argv.argv || {}, remain = JSON.stringify(argv.remain) || "",
            dimensionsExist = function () {
                return remain.match(/"[\w\W]*\.json"/) && dimensions.length > 0;
            };

        if (dimensionsExist()) {
            try {
                self.dimensionsFile = dimensions;
                dimJson = JSON.parse(fs.readFileSync(dimensions, "utf-8"));
            } catch (e) {
                errorMessage = e.message;
            }
            if (dimJson && dimJson.length > 0 && dimJson[i].dimensions) {
                self.dimensions = dimJson[i].dimensions;
                // em.errorLog("dimensions {0}", JSON.stringify(self.dimensions));
            } else {
                em.errorLog(1001, dimensions, errorMessage || JSON.stringify(dimJson));
                proc.exit(1);
            }
            Environment();
        }
    };

    em.logger = self.mock || em.logger;
    Dimensions();
};

ArrowSetup.prototype.setupReportDir = function () {

    global.reportFolder = "";
    // To generate the reports, if either report is true or reportFolder is passed
    if (this.argv.reportFolder || true === this.argv.report) {

        var fileUtil = new FileUtil(),
            targetFolderPath,
            reportFolderPath;

        // If reportFolder is passed in the argument,
        if (this.argv.reportFolder) {
            targetFolderPath =  path.resolve(global.workingDirectory, this.argv.reportFolder, 'arrow-target');
        } else {
            // Report folder not passed by the user
            // By default, reports shall go under arrow-target/reports
            targetFolderPath =  path.resolve(global.workingDirectory, 'arrow-target');
        }

        // Cleanup report folder if keepTestReport set to false or undefined
        if (this.argv.keepTestReport === undefined || false === this.argv.keepTestReport) {
            fileUtil.removeDirectory(targetFolderPath);
        }

        reportFolderPath = path.resolve(targetFolderPath, 'arrow-report');

        fileUtil.createDirectory(reportFolderPath);

        this.argv.reportFolder = targetFolderPath;
        global.reportFolder = this.argv.reportFolder || "";

    }

};

ArrowSetup.prototype.setupMisc = function () {

    if (this.argv.coverage !== undefined) {
        this.config.coverage = this.argv.coverage;
    }

    if (this.argv.report === undefined && this.config.report) {
        this.argv.report = this.config.report;
    }

    if (this.argv.dimensions) {
        this.argv.dimensions = path.resolve(global.workingDirectory, this.argv.dimensions);
        this.config.dimensions = this.argv.dimensions;
    }
};

ArrowSetup.prototype.setuplog4js = function () {
    var logLevel;
    logLevel = this.config["logLevel"];
    log4js.setGlobalLogLevel(logLevel);
    log4js.restoreConsole();
    this.logger = log4js.getLogger("ArrowSetup");
};

ArrowSetup.prototype.setupDefaultDriverName = function () {
    if (!this.argv.driver) {
        // turn on reuseSession, if browser is being reused
        // adding support for "reuse-" too, just in case if a null version is passed with browser.
        if ("reuse" === this.argv.browser || "reuse-" === this.argv.browser) {
            this.argv.reuseSession = true;
            delete this.argv.browser;
        }

        if (this.argv.reuseSession) {
            this.argv.driver = "selenium";
        }
    }

    // setup the selenium host using the auto hookup if possible
    this.setupSeleniumHost();
};

ArrowSetup.prototype.setupSeleniumHost = function () {
    var wdHubHost,
        wdStatusFile = "/tmp/arrow_sel_server.status";

    // setup the selenium host using the auto hookup if possible
    wdHubHost = this.config["seleniumHost"];
    if (0 === wdHubHost.length) {
        // check if we have a hooked up server
        try {
            fs.statSync(wdStatusFile).isFile();
            wdHubHost = fs.readFileSync(wdStatusFile, "utf-8");
        } catch (ex) {
        }

        // final default
        if (wdHubHost.length === 0) {
            wdHubHost = "http://localhost:4444/wd/hub";
        }
        this.config["seleniumHost"] = wdHubHost;
    }<commit_msg>Fixing connection refused error when --driver=selenium is used when browser=reuse<commit_after>/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true nomen:true, node:true continue:true*/

/*
 * Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */

var fs = require("fs");
var glob = require("glob");
var path = require('path');
var Properties = require("../util/properties");
var log4js = require("log4js");
var LibManager = require('../util/libmanager');
var FileUtil = require("../util/fileutil");
var ErrorManager = require("../util/errormanager");

function ArrowSetup(config, argv) {
    this.config = config;
    this.argv = argv;
}

ArrowSetup.prototype.setup = function () {
    this.setuplog4js();
    this.setupReportDir();
    this.setupDefaultDriverName();
    this.setupTestEngine();
    this.setupDefaultLib();
    this.setupHeadlessParam();
    this.setupCapabilities();
    this.setupMisc();
    this.setupReplaceParam();
    this.setupDefaultParam();
    this.errorCheck(); // keep this as last function call for checking errors.

};

ArrowSetup.prototype.setupReplaceParam = function () {
    if (this.argv.replaceParamJSON) {
        this.config.replaceParamJSON = this.argv.replaceParamJSON;
    }
};

ArrowSetup.prototype.setupDefaultParam = function () {
    if (this.argv.defaultParamJSON) {
        this.config.defaultParamJSON = this.argv.defaultParamJSON;
    }
};


ArrowSetup.prototype.errorCheck = function () {
    var self = this, em = ErrorManager.getInstance(),
    Environment = function() {
        if (self.argv.argv.cooked) {
            // em.errorLog("self.argv.argv.cooked {0}",  JSON.stringify(self.argv.argv.cooked));
            var cooked = JSON.stringify(self.argv.argv.cooked), proc = self.mock || process;
            cooked.replace(/"--context","([\w\W]*?)"/, function (match, context) {
                // em.errorLog("match is {0}, context is {1}", match, context);
                context.replace(/environment:([\w\W]*)/, function (match, env) {
                    // em.errorLog("match is {0}, env is {1}", match, env);
                    if (self.dimensions[0].environment[env] === undefined) {
                        em.errorLog(1000, env, self.dimensionsFile);
                        proc.exit(1);
                    } else {
                        self.environment = env;
                    }
                });
            });
        }
    },
    Dimensions = function() {
        // To Do : should check dimensions file with Json schema
        // em.errorLog("config {0} argv {1}", JSON.stringify(self.config), JSON.stringify(self.argv));
        var dimensions = self.argv.dimensions || self.config.dimensions || "", dimJson, i = 0, proc = self.mock || process,
            errorMessage, argv = self.argv.argv || {}, remain = JSON.stringify(argv.remain) || "",
            dimensionsExist = function () {
                return remain.match(/"[\w\W]*\.json"/) && dimensions.length > 0;
            };

        if (dimensionsExist()) {
            try {
                self.dimensionsFile = dimensions;
                dimJson = JSON.parse(fs.readFileSync(dimensions, "utf-8"));
            } catch (e) {
                errorMessage = e.message;
            }
            if (dimJson && dimJson.length > 0 && dimJson[i].dimensions) {
                self.dimensions = dimJson[i].dimensions;
                // em.errorLog("dimensions {0}", JSON.stringify(self.dimensions));
            } else {
                em.errorLog(1001, dimensions, errorMessage || JSON.stringify(dimJson));
                proc.exit(1);
            }
            Environment();
        }
    };

    em.logger = self.mock || em.logger;
    Dimensions();
};

ArrowSetup.prototype.setupReportDir = function () {

    global.reportFolder = "";
    // To generate the reports, if either report is true or reportFolder is passed
    if (this.argv.reportFolder || true === this.argv.report) {

        var fileUtil = new FileUtil(),
            targetFolderPath,
            reportFolderPath;

        // If reportFolder is passed in the argument,
        if (this.argv.reportFolder) {
            targetFolderPath =  path.resolve(global.workingDirectory, this.argv.reportFolder, 'arrow-target');
        } else {
            // Report folder not passed by the user
            // By default, reports shall go under arrow-target/reports
            targetFolderPath =  path.resolve(global.workingDirectory, 'arrow-target');
        }

        // Cleanup report folder if keepTestReport set to false or undefined
        if (this.argv.keepTestReport === undefined || false === this.argv.keepTestReport) {
            fileUtil.removeDirectory(targetFolderPath);
        }

        reportFolderPath = path.resolve(targetFolderPath, 'arrow-report');

        fileUtil.createDirectory(reportFolderPath);

        this.argv.reportFolder = targetFolderPath;
        global.reportFolder = this.argv.reportFolder || "";

    }

};

ArrowSetup.prototype.setupMisc = function () {

    if (this.argv.coverage !== undefined) {
        this.config.coverage = this.argv.coverage;
    }

    if (this.argv.report === undefined && this.config.report) {
        this.argv.report = this.config.report;
    }

    if (this.argv.dimensions) {
        this.argv.dimensions = path.resolve(global.workingDirectory, this.argv.dimensions);
        this.config.dimensions = this.argv.dimensions;
    }
};

ArrowSetup.prototype.setuplog4js = function () {
    var logLevel;
    logLevel = this.config["logLevel"];
    log4js.setGlobalLogLevel(logLevel);
    log4js.restoreConsole();
    this.logger = log4js.getLogger("ArrowSetup");
};

ArrowSetup.prototype.setupDefaultDriverName = function () {

    // turn on reuseSession, if browser is being reused
    // adding support for "reuse-" too, just in case if a null version is passed with browser.
    if ("reuse" === this.argv.browser || "reuse-" === this.argv.browser) {
        this.argv.reuseSession = true;

        if (!this.argv.driver){
            this.argv.driver = "selenium";
        }
        delete this.argv.browser;
    }

    // setup the selenium host using the auto hookup if possible
    this.setupSeleniumHost();
};

ArrowSetup.prototype.setupSeleniumHost = function () {
    var wdHubHost,
        wdStatusFile = "/tmp/arrow_sel_server.status";

    // setup the selenium host using the auto hookup if possible
    wdHubHost = this.config["seleniumHost"];
    if (0 === wdHubHost.length) {
        // check if we have a hooked up server
        try {
            fs.statSync(wdStatusFile).isFile();
            wdHubHost = fs.readFileSync(wdStatusFile, "utf-8");
        } catch (ex) {
        }

        // final default
        if (wdHubHost.length === 0) {
            wdHubHost = "http://localhost:4444/wd/hub";
        }
        this.config["seleniumHost"] = wdHubHost;
    }
{'fraction_non_alphanumeric': 0.13356262539409572, 'fraction_numerical': 0.0058756090570364, 'mean_word_length': 2.327849308536004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5683322', 'n_tokens_mistral': 4027, 'n_tokens_neox': 3899, 'n_words': 865}