def test_bear_test_fun_1(self):
     from pyprint.ConsolePrinter import ConsolePrinter
     printer = ConsolePrinter()
     bears = {'Python': [TestLocalBear, TestGlobalBear]}
     relevant_bears = {'test':
                       {TestLocalBear, TestGlobalBear, }}
     bear_settings_obj = collect_bear_settings(relevant_bears)
     file_dict = {'A.py': {'a\n', 'b\n'}, 'C.py': {'c\n', 'd\n'}}
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     file_names = ['A.py', 'C.py']
     non_op_results, unified_results = bear_test_fun(
         bears, bear_settings_obj, file_dict, [], contents,
         file_names, 5, 5, printer)
     test_non_op_results = [{TestLocalBear:
                             [{'filename': 'A.py'},
                              {'filename': 'C.py'}]},
                            {TestGlobalBear: [{}]}]
     test_unified_results = [{TestLocalBear:
                              [{'filename': 'A.py',
                                'yield_results': False},
                               {'filename': 'C.py',
                                'yield_results': False}]},
                             {TestGlobalBear: [{'yield_results': False}]}]
     self.assertCountEqual(non_op_results[1][TestGlobalBear],
                           test_non_op_results[1][TestGlobalBear])
     self.assertCountEqual(unified_results[1][TestGlobalBear],
                           test_unified_results[1][TestGlobalBear])
     self.assertCountEqual(non_op_results[0][TestLocalBear],
                           test_non_op_results[0][TestLocalBear])
     self.assertCountEqual(unified_results[0][TestLocalBear],
                           test_unified_results[0][TestLocalBear])
Ejemplo n.º 2
0
 def test_bear_test_fun_4(self):
     from pyprint.ConsolePrinter import ConsolePrinter
     printer = ConsolePrinter()
     bears = {'Python': [AnotherTestLocalDepBear]}
     relevant_bears = {'test': {AnotherTestLocalDepBear}}
     bear_settings_obj = collect_bear_settings(relevant_bears)
     file_dict = {'A.py': {'a\n', 'b\n'}, 'C.py': {'c\n', 'd\n'}}
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     file_names = ['A.py', 'C.py']
     non_op_results, unified_results = bear_test_fun(
         bears, bear_settings_obj, file_dict, [], contents, file_names, 1,
         1, printer)
     print('nonop:', non_op_results)
     print('op:', unified_results)
     test_results = [{
         AnotherTestLocalDepBear: [{
             'filename': 'A.py'
         }, {
             'filename': 'C.py'
         }]
     }]
     self.assertCountEqual(non_op_results[0][AnotherTestLocalDepBear],
                           test_results[0][AnotherTestLocalDepBear])
     self.assertCountEqual(unified_results, [None])
Ejemplo n.º 3
0
 def test_bear_test_fun_1(self):
     from pyprint.ConsolePrinter import ConsolePrinter
     printer = ConsolePrinter()
     bears = {'Python': [TestLocalBear, TestGlobalBear]}
     relevant_bears = {
         'test': {
             TestLocalBear,
             TestGlobalBear,
         }
     }
     bear_settings_obj = collect_bear_settings(relevant_bears)
     file_dict = {'A.py': {'a\n', 'b\n'}, 'C.py': {'c\n', 'd\n'}}
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     file_names = ['A.py', 'C.py']
     non_op_results, unified_results = bear_test_fun(
         bears, bear_settings_obj, file_dict, [], contents, file_names, 5,
         5, printer)
     test_non_op_results = [{
         TestLocalBear: [{
             'filename': 'A.py'
         }, {
             'filename': 'C.py'
         }]
     }, {
         TestGlobalBear: [{}]
     }]
     test_unified_results = [{
         TestLocalBear: [{
             'filename': 'A.py',
             'yield_results': False
         }, {
             'filename': 'C.py',
             'yield_results': False
         }]
     }, {
         TestGlobalBear: [{
             'yield_results': False
         }]
     }]
     self.assertCountEqual(non_op_results[1][TestGlobalBear],
                           test_non_op_results[1][TestGlobalBear])
     self.assertCountEqual(unified_results[1][TestGlobalBear],
                           test_unified_results[1][TestGlobalBear])
     self.assertCountEqual(non_op_results[0][TestLocalBear],
                           test_non_op_results[0][TestLocalBear])
     self.assertCountEqual(unified_results[0][TestLocalBear],
                           test_unified_results[0][TestLocalBear])
 def test_bear_test_fun_3(self):
     from pyprint.ConsolePrinter import ConsolePrinter
     printer = ConsolePrinter()
     bears = {'Python': [TestLocalDepBear]}
     relevant_bears = {'test':
                       {TestLocalDepBear}}
     bear_settings_obj = collect_bear_settings(relevant_bears)
     file_dict = {'A.py': {'a\n', 'b\n'}, 'C.py': {'c\n', 'd\n'}}
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     file_names = ['A.py', 'C.py']
     non_op_results, unified_results = bear_test_fun(
         bears, bear_settings_obj, file_dict, [], contents,
         file_names, 1, 1, printer)
     print('nonop:', non_op_results)
     print('op:', unified_results)
     test_results = [{TestLocalDepBear: []}]
     self.assertCountEqual(non_op_results[0][TestLocalDepBear],
                           test_results[0][TestLocalDepBear])
     self.assertCountEqual(unified_results, [None])
Ejemplo n.º 5
0
def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
               op_args_limit, value_to_op_args_limit, project_files,
               printer=None):
    """
    Runs the green mode of coala-quickstart.

    Generates '.project_data.yaml' which contains the files and directory
    structure of the project, runs the QuickstartBear which guesses some values
    of settings the can take an infinite set of values by parsing the
    file_dict and appends to `.project_data.yaml`. Runs some further linting
    options based on file names etc. Calls the methods which test out whether
    a setting value is green for a bear i.e. does not point out any error in
    the code base and further generates sections and writes the green config
    file for the project.
    :param project_dir:
        The project directory.
    :param ignore_globs:
        The globs of the files to ignore from the linting process.
    :param bears:
        The bears from Constants.GREEN_MODE_COMPATIBLE_BEAR_LIST along
        with Constants.IMPORTANT_BEAR_LIST.
    :param bear_settings_obj:
        The object of SettingsClass/BearSettings which stores the metadata
        about whether a setting takes a boolean value or any other value.
    :param op_args_limit:
        The maximum number of optional bear arguments allowed for guessing.
    :param project_files:
        The list of files in the project.
    :param value_to_op_args_limit:
        The maximum number of values to run the bear again and again for
        a optional setting.
    """
    from coala_quickstart.green_mode.filename_operations import (
        check_filename_prefix_postfix)
    ignore_globs.append(os.path.join(project_dir, '.git', '**'))
    project_data = project_dir + os.sep + PROJECT_DATA

    # Currently as a temporary measure, recreating the file at each run from
    # scratch, as there is no mechanism created uptil now to reuse this data.
    if os.path.isfile(project_data):
        os.remove(project_data)

    if not os.path.isfile(project_data):
        new_data = initialize_project_data(project_dir + os.sep, ignore_globs)
        data_to_dump = {'dir_structure': new_data}
        dump_yaml_to_file(project_data, data_to_dump)

    # Operations before the running of QuickstartBear are done over here.
    # Eg. do operations on filenames over here.
    project_data_contents = get_yaml_contents(project_data)
    project_data_contents = check_filename_prefix_postfix(
        project_data_contents)

    # Run QuickstartBear
    (project_data_contents, ignore_ranges, file_dict,
     file_names) = run_quickstartbear(
        project_data_contents, project_dir)

    final_non_op_results, final_unified_results = bear_test_fun(
        bears, bear_settings_obj, file_dict,
        ignore_ranges, project_data_contents, file_names,
        op_args_limit, value_to_op_args_limit, printer)

    # Call to create `.coafile` goes over here.
    settings_non_op = generate_data_struct_for_sections(
        final_non_op_results)
    settings_unified = generate_data_struct_for_sections(
        final_unified_results)

    # Combine the settings for the sections due to the missed out bears in
    # unified results due to the limitations on maximum number of optionanl
    # arguments and the values to those arguments that can be supplied.
    for bear in settings_non_op:
        if bear not in settings_unified:
            settings_unified[bear] = settings_non_op[bear]

    generate_green_mode_sections(
        settings_unified, project_dir, project_files, ignore_globs, printer)

    # Final Dump.
    dump_yaml_to_file(project_data, project_data_contents)

    # Delete .project_data.yaml for now as there is currently no mechanism
    # added to reuse this data.
    os.remove(project_data)