<commit_before>"""Command line interface to hitchtest."""
from click import command, group, argument, option
from hitchtest.utils import log, warn, signals_trigger_exit
from sys import exit, executable
from os import path, walk, chdir
from hitchtest import module
from hitchtest import suite
import yaml as pyyaml
import fnmatch
import signal
import json


# TODO : Remove --results and put the setting in to settings instead.

@command()
@argument('filenames', nargs=-1)
@option('-y', '--yaml', is_flag=True, help='Output the YAML tests (for debugging).')
@option('-q', '--quiet', is_flag=True, help='Quiet mode. Do not print test output to screen.')
@option('-r', '--results', default=None, help='Specify a template to display test results with.')
@option('-s', '--settings', default=None, help="Load settings from specified file.")
@option('-e', '--extra', default=None, help="""Load extra settings on command line as JSON (e.g. --extra '{"postgres_version": "3.5.5"}'""")
def cli(filenames, yaml, quiet, results, settings, extra):
    """Run test files or entire directories containing .test files."""
    # .hitch/virtualenv/bin/python <- 4 directories up from where the python exec resides
    engine_folder = path.abspath(path.join(executable, "..", "..", "..", ".."))
    settings_dict = {'engine_folder': engine_folder, 'quiet': quiet, }
    filenames = [path.abspath(path.relpath(filename, engine_folder)) for filename in filenames]
    chdir(engine_folder)
    default_settings_filename = path.join(engine_folder, "settings.yml")
    settings_filename = default_settings_filename if settings is None else path.join(engine_folder, settings)

    if settings is not None and not path.exists(settings_filename):
        warn("Settings file '{}' could not be found!\n".format(settings_filename))
        exit(1)

    # Load settings from file, if it exists
    if path.exists(settings_filename):
        with open(settings_filename) as settingsfile_handle:
            settings_dict.update(pyyaml.load(settingsfile_handle.read()))

    # Load extra settings from command line JSON
    if extra is not None:
        settings_dict.update(json.loads(extra).items())

    if len(filenames) == 0:
        warn("No tests specified.\n")
        exit(1)

    # Get list of files from specified files/directories
    matches = []
    for filename in filenames:
        if path.isdir(filename):
            for root, dirnames, filenames_in_dir in walk(filename):
                for filename_in_dir in fnmatch.filter(filenames_in_dir, '*.test'):
                    matches.append(path.join(root, filename_in_dir))
        else:
            matches.append(filename)

    # Get list of modules from matching directly specified files from command line
    # and indirectly (in the directories of) directories specified from cmd line
    test_modules = []
    for filename in matches:
        if filename.endswith(".test"):
            test_modules.append(module.Module(filename, settings_dict))
        else:
            warn("I didn't understand {}\n".format(filename))
            exit(1)

    test_suite = suite.Suite(test_modules, settings_dict)

    if yaml:
        test_suite.printyaml()
    else:
        returned_results = test_suite.run(quiet=quiet)
        # Lines must be split to prevent stdout blocking
        for line in returned_results.to_template(template=results).split('\n'):
            log("{}\n".format(line))<commit_msg>BUG : Give clearer error message when no test is found.<commit_after>"""Command line interface to hitchtest."""
from click import command, group, argument, option
from hitchtest.utils import log, warn, signals_trigger_exit
from sys import exit, executable
from os import path, walk, chdir
from hitchtest import module
from hitchtest import suite
import yaml as pyyaml
import fnmatch
import signal
import json


# TODO : Remove --results and put the setting in to settings instead.

@command()
@argument('filenames', nargs=-1)
@option('-y', '--yaml', is_flag=True, help='Output the YAML tests (for debugging).')
@option('-q', '--quiet', is_flag=True, help='Quiet mode. Do not print test output to screen.')
@option('-r', '--results', default=None, help='Specify a template to display test results with.')
@option('-s', '--settings', default=None, help="Load settings from specified file.")
@option('-e', '--extra', default=None, help="""Load extra settings on command line as JSON (e.g. --extra '{"postgres_version": "3.5.5"}'""")
def cli(filenames, yaml, quiet, results, settings, extra):
    """Run test files or entire directories containing .test files."""
    # .hitch/virtualenv/bin/python <- 4 directories up from where the python exec resides
    engine_folder = path.abspath(path.join(executable, "..", "..", "..", ".."))
    settings_dict = {'engine_folder': engine_folder, 'quiet': quiet, }
    filenames = [path.abspath(path.relpath(filename, engine_folder)) for filename in filenames]
    chdir(engine_folder)
    default_settings_filename = path.join(engine_folder, "settings.yml")
    settings_filename = default_settings_filename if settings is None else path.join(engine_folder, settings)

    if settings is not None and not path.exists(settings_filename):
        warn("Settings file '{}' could not be found!\n".format(settings_filename))
        exit(1)

    # Load settings from file, if it exists
    if path.exists(settings_filename):
        with open(settings_filename) as settingsfile_handle:
            settings_dict.update(pyyaml.load(settingsfile_handle.read()))

    # Load extra settings from command line JSON
    if extra is not None:
        settings_dict.update(json.loads(extra).items())

    if len(filenames) == 0:
        warn("No tests specified.\n")
        exit(1)

    # Get list of files from specified files/directories
    matches = []
    test_not_found = False
    for filename in filenames:
        if not path.exists(filename):
            warn("Test '{}' not found.\n".format(filename))
            test_not_found = True
        if path.isdir(filename):
            for root, dirnames, filenames_in_dir in walk(filename):
                for filename_in_dir in fnmatch.filter(filenames_in_dir, '*.test'):
                    matches.append(path.join(root, filename_in_dir))
        else:
            matches.append(filename)

    if test_not_found:
        exit(1)

    # Get list of modules from matching directly specified files from command line
    # and indirectly (in the directories of) directories specified from cmd line
    test_modules = []
    for filename in matches:
        if filename.endswith(".test"):
            test_modules.append(module.Module(filename, settings_dict))
        else:
            warn("I didn't understand {}\n".format(filename))
            exit(1)

    test_suite = suite.Suite(test_modules, settings_dict)

    if yaml:
        test_suite.printyaml()
    else:
        returned_results = test_suite.run(quiet=quiet)
        # Lines must be split to prevent stdout blocking
        for line in returned_results.to_template(template=results).split('\n'):
            log("{}\n".format(line))
{'fraction_non_alphanumeric': 0.11627580396011795, 'fraction_numerical': 0.0026681645836258952, 'mean_word_length': 3.448469706433479, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, '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': '322772', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1980, 'n_words': 635}