<commit_before>'use strict';

// Load modules

const Fs = require('fs');
const Path = require('path');
const Bossy = require('bossy');
const FindRc = require('find-rc');
const Hoek = require('hoek');
const Coverage = require('./coverage');
const Pkg = require('../package.json');
const Runner = require('./runner');
const Transform = require('./transform');
const Utils = require('./utils');
// .labrc configuration will be required if it exists
// index.js required below if setting assertions module


// Declare internals

const internals = {};

internals.rcPath = FindRc('lab');
internals.rc = internals.rcPath ? require(internals.rcPath) : {};


exports.run = function () {

    const settings = internals.options();

    settings.coveragePath = Path.join(process.cwd(), settings['coverage-path'] || '');
    settings.coverageExclude = ['node_modules', 'test', 'test_runner'];
    if (settings['coverage-exclude']) {
        settings.coverageExclude = settings.coverageExclude.concat(settings['coverage-exclude']);
    }

    settings.lintingPath = process.cwd();

    if (settings.coverage) {
        Coverage.instrument(settings);
    }
    else if (settings.transform) {
        Transform.install(settings);
    }

    if (settings.environment) {
        process.env.NODE_ENV = settings.environment;
    }

    if (settings.sourcemaps) {
        let sourceMapOptions = {};

        if (settings.transform) {
            sourceMapOptions = {
                retrieveFile: Transform.retrieveFile
            };
        }

        require('source-map-support').install(sourceMapOptions);
    }

    const scripts = internals.traverse(settings.paths, settings);
    return Runner.report(scripts, settings);
};


internals.traverse = function (paths, options) {

    const traverse = function (path) {

        let files = [];

        const pathStat = Fs.statSync(path);
        if (pathStat.isFile()) {
            return path;
        }

        Fs.readdirSync(path).forEach((filename) => {

            const file = Path.join(path, filename);
            const stat = Fs.statSync(file);
            if (stat.isDirectory() &&
                !options.flat) {

                files = files.concat(traverse(file, options));
                return;
            }

            if (stat.isFile() &&
                options.pattern.test(filename) &&
                Path.basename(file)[0] !== '.') {

                files.push(file);
            }
        });

        return files;
    };

    let testFiles = [];
    paths.forEach((path) => {

        testFiles = testFiles.concat(traverse(path));
    });

    if (options.pattern && !testFiles.length) {
        options.output.write('The pattern provided (-P or --pattern) didn\'t match any files.');
        process.exit(0);
    }

    testFiles = testFiles.map((path) => {

        return Path.resolve(path);
    });

    const scripts = [];
    testFiles.forEach((file) => {

        global._labScriptRun = false;
        file = Path.resolve(file);
        const pkg = require(file);
        if (pkg.lab &&
            pkg.lab._root) {

            scripts.push(pkg.lab);

            if (pkg.lab._cli) {
                Utils.applyOptions(options, pkg.lab._cli);
            }
        }
        else if (global._labScriptRun) {
            options.output.write('The file: ' + file + ' includes a lab script that is not exported via exports.lab');
            return process.exit(1);
        }
    });

    return scripts;
};


internals.options = function () {

    const definition = {
        assert: {
            alias: 'a',
            type: 'string',
            description: 'specify an assertion library module path to require and make available under Lab.assertions',
            default: null
        },
        bail: {
            type: 'boolean',
            description: 'exit the process with a non zero exit code on the first test failure',
            default: null
        },
        colors: {
            alias: 'C',
            type: 'boolean',
            description: 'enable color output (defaults to terminal capabilities)',
            default: null
        },<commit_msg>Friendly message on test file not found; also appended LFs to other error messages to place prompt on next line.<commit_after>'use strict';

// Load modules

const Fs = require('fs');
const Path = require('path');
const Bossy = require('bossy');
const FindRc = require('find-rc');
const Hoek = require('hoek');
const Coverage = require('./coverage');
const Pkg = require('../package.json');
const Runner = require('./runner');
const Transform = require('./transform');
const Utils = require('./utils');
// .labrc configuration will be required if it exists
// index.js required below if setting assertions module


// Declare internals

const internals = {};

internals.rcPath = FindRc('lab');
internals.rc = internals.rcPath ? require(internals.rcPath) : {};


exports.run = function () {

    const settings = internals.options();

    settings.coveragePath = Path.join(process.cwd(), settings['coverage-path'] || '');
    settings.coverageExclude = ['node_modules', 'test', 'test_runner'];
    if (settings['coverage-exclude']) {
        settings.coverageExclude = settings.coverageExclude.concat(settings['coverage-exclude']);
    }

    settings.lintingPath = process.cwd();

    if (settings.coverage) {
        Coverage.instrument(settings);
    }
    else if (settings.transform) {
        Transform.install(settings);
    }

    if (settings.environment) {
        process.env.NODE_ENV = settings.environment;
    }

    if (settings.sourcemaps) {
        let sourceMapOptions = {};

        if (settings.transform) {
            sourceMapOptions = {
                retrieveFile: Transform.retrieveFile
            };
        }

        require('source-map-support').install(sourceMapOptions);
    }

    const scripts = internals.traverse(settings.paths, settings);
    return Runner.report(scripts, settings);
};


internals.traverse = function (paths, options) {

    const traverse = function (path) {

        let files = [];

        const pathStat = Fs.statSync(path);
        if (pathStat.isFile()) {
            return path;
        }

        Fs.readdirSync(path).forEach((filename) => {

            const file = Path.join(path, filename);
            const stat = Fs.statSync(file);
            if (stat.isDirectory() &&
                !options.flat) {

                files = files.concat(traverse(file, options));
                return;
            }

            if (stat.isFile() &&
                options.pattern.test(filename) &&
                Path.basename(file)[0] !== '.') {

                files.push(file);
            }
        });

        return files;
    };

    let testFiles = [];
    let lastPath = null;
    try {
        paths.forEach((path) => {

            lastPath = path;
            testFiles = testFiles.concat(traverse(path));
        });
    }
    catch (err) {
        if (err.code !== 'ENOENT') {
            throw err;
        }
        options.output.write('Could not find test file or directory \'' + lastPath + '\'.\n');
        process.exit(0);
    }

    if (options.pattern && !testFiles.length) {
        options.output.write('The pattern provided (-P or --pattern) didn\'t match any files.\n');
        process.exit(0);
    }

    testFiles = testFiles.map((path) => {

        return Path.resolve(path);
    });

    const scripts = [];
    testFiles.forEach((file) => {

        global._labScriptRun = false;
        file = Path.resolve(file);
        const pkg = require(file);
        if (pkg.lab &&
            pkg.lab._root) {

            scripts.push(pkg.lab);

            if (pkg.lab._cli) {
                Utils.applyOptions(options, pkg.lab._cli);
            }
        }
        else if (global._labScriptRun) {
            options.output.write('The file: ' + file + ' includes a lab script that is not exported via exports.lab\n');
            return process.exit(1);
        }
    });

    return scripts;
};


internals.options = function () {

    const definition = {
        assert: {
            alias: 'a',
            type: 'string',
            description: 'specify an assertion library module path to require and make available under Lab.assertions',
            default: null
        },
        bail: {
            type: 'boolean',
            description: 'exit the process with a non zero exit code on the first test failure',
            default: null
        },
        colors: {
            alias: 'C',
            type: 'boolean',
            description: 'enable color output (defaults to terminal capabilities)',
            default: null
        },
{'fraction_non_alphanumeric': 0.1327362498564703, 'fraction_numerical': 0.000803766218854059, 'mean_word_length': 2.056140350877193, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6216604', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2338, 'n_words': 570}