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_get_kwargs_1(self):
     relevant_bears = {
         'test': {
             AllKindsOfSettingsBaseBear,
         }
     }
     __location__ = os.path.realpath(
         os.path.join(os.getcwd(), os.path.dirname(__file__)))
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     bear_settings_obj = collect_bear_settings(relevant_bears)
     non_optional_settings = bear_settings_obj[0].non_optional_settings
     bear_settings_obj[0].optional_settings
     contents = initialize_project_data(dir_path, ignore_globs)
     contents = {
         'dir_structure':
         contents,
         settings_key: [{
             'some_rubbish_setting': 'some_rubbish',
             'max_line_lengths': 60
         }]
     }
     kwargs = get_kwargs(non_optional_settings,
                         [AllKindsOfSettingsBaseBear], contents,
                         __location__)
     test_kwargs = {
         'use_bear': [True, False],
         'max_line_lengths': [60],
         'no_line': [1, 2]
     }
     self.assertEqual(kwargs, test_kwargs)
Example #3
0
 def test_run_quickstartbear_with_file_None(self):
     # Mocking the method
     QuickstartBear.execute = lambda *args, **kwargs: [None]
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     contents = {'dir_structure': contents, settings_key: []}
     test_contents = deepcopy(contents)
     (final_contents, ignore_ranges, complete_file_dict,
      complete_filename_list) = run_quickstartbear(contents, dir_path)
     self.assertEqual(test_contents, final_contents)
 def test_run_quickstartbear_with_file_None(self):
     # Mocking the method
     QuickstartBear.execute = lambda *args, **kwargs: [None]
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     contents = {'dir_structure': contents, settings_key: []}
     test_contents = deepcopy(contents)
     (final_contents, ignore_ranges, complete_file_dict,
      complete_filename_list) = run_quickstartbear(contents, dir_path)
     self.assertEqual(test_contents, final_contents)
 def test_initialize_project_data(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     final_data = initialize_project_data(dir_path, ignore_globs)
     test_final_data = [
         'QuickstartBearTest.py', 'example_.project_data.yaml',
         'green_modeTest.py', 'filename_operationsTest.py',
         'bear_settings.yaml', {
             'test_dir': ['test_file.py']
         }
     ]
     self.assertCountEqual(final_data, test_final_data)
 def test_generate_complete_filename_list(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     data = initialize_project_data(dir_path, ignore_globs)
     final_data = generate_complete_filename_list(data, dir_path[:-1])
     prefix = dir_path
     test_final_data = [
         'QuickstartBearTest.py', 'example_.project_data.yaml',
         'bear_settings.yaml', 'green_modeTest.py',
         'filename_operationsTest.py', 'test_dir' + os.sep + 'test_file.py'
     ]
     test_final_data = [prefix + x for x in test_final_data]
     self.assertCountEqual(final_data, test_final_data)
 def test_generate_complete_filename_list(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     data = initialize_project_data(dir_path, ignore_globs)
     final_data = generate_complete_filename_list(data, dir_path[:-1])
     prefix = dir_path
     test_final_data = ['QuickstartBearTest.py',
                        'example_.project_data.yaml',
                        'bear_settings.yaml',
                        'green_modeTest.py',
                        'test_dir' + os.sep + 'file_aggregatorTest.py',
                        'filename_operationsTest.py',
                        'test_dir' + os.sep + 'test_file.py']
     test_final_data = [prefix + x for x in test_final_data]
     self.assertCountEqual(final_data, test_final_data)
Example #8
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_run_quickstartbear(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     contents = initialize_project_data(dir_path, ignore_globs)
     contents = {'dir_structure': contents, settings_key: []}
     settings_key_values = [{'max_lines_per_file': 1000},  # default value
                            {'max_line_length': 80},
                            {'min_lines_per_file': 5}]
     test_contents = deepcopy(contents)
     test_contents[settings_key] = settings_key_values
     (final_contents, ignore_ranges, complete_file_dict,
      complete_filename_list) = run_quickstartbear(contents, dir_path)
     ignore_file_name = dir_path + 'test_dir' + os.sep + 'test_file.py'
     start = SourcePosition(ignore_file_name, line=3, column=1)
     stop = SourcePosition(ignore_file_name, line=4, column=20)
     self.assertEqual(test_contents, final_contents)
Example #10
0
 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])
 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])
 def test_get_kwargs_2(self):
     relevant_bears = {'test':
                       {AllKindsOfSettingsBaseBear, }}
     __location__ = os.path.realpath(
         os.path.join(os.getcwd(), os.path.dirname(__file__)))
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     bear_settings_obj = collect_bear_settings(relevant_bears)
     bear_settings_obj[0].non_optional_settings
     optional_settings = bear_settings_obj[0].optional_settings
     contents = initialize_project_data(dir_path, ignore_globs)
     contents = {'dir_structure': contents,
                 settings_key: [{'some_rubbish_setting': 'some_rubbish',
                                 'max_line_lengths': 60}]}
     kwargs = get_kwargs(optional_settings,
                         [AllKindsOfSettingsBaseBear],
                         contents, __location__)
     test_kwargs = {'use_space': [True, False],
                    'use_tab': [True, False]}
     self.assertEqual(kwargs, test_kwargs)
Example #13
0
 def test_initialize_project_data(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     final_data = initialize_project_data(dir_path, ignore_globs)
     test_final_data = [
         'QuickstartBearTest.py', 'example_.project_data.yaml',
         'green_modeTest.py', 'filename_operationsTest.py',
         'bear_settings.yaml', {
             'test_dir': ['file_aggregatorTest.py', 'test_file.py']
         }
     ]
     for i in final_data:
         if not isinstance(i, dict):
             self.assertIn(i, test_final_data)
         else:
             for key in i:
                 for j in test_final_data:
                     if isinstance(j, dict):
                         if key in j:
                             to_test = j[key]
                 self.assertCountEqual(i[key], to_test)
 def test_initialize_project_data(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     final_data = initialize_project_data(dir_path, ignore_globs)
     test_final_data = ['QuickstartBearTest.py',
                        'example_.project_data.yaml',
                        'green_modeTest.py',
                        'filename_operationsTest.py',
                        'bear_settings.yaml',
                        {'test_dir': ['file_aggregatorTest.py',
                                      'test_file.py']}]
     for i in final_data:
         if not isinstance(i, dict):
             self.assertIn(i, test_final_data)
         else:
             for key in i:
                 for j in test_final_data:
                     if isinstance(j, dict):
                         if key in j:
                             to_test = j[key]
                 self.assertCountEqual(i[key], to_test)
Example #15
0
 def test_initialize_project_data(self):
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     pycache_index = -1
     for index, content in enumerate(contents):
         if isinstance(content, dict):
             if '__pycache__' in content.keys():
                 pycache_index = index
                 break
     if not pycache_index == -1:
         del contents[pycache_index]
     list_indices = []
     for index, content in enumerate(contents):
         if content[-4:] == 'orig':
             list_indices.append(index)
     for i in range(0, len(list_indices)):
         del contents[list_indices[i]]
         for j in range(i + 1, len(list_indices)):
             list_indices[j] = list_indices[j] - 1
     self.assertIn([
         'QuickstartBearTest.py', 'example_.project_data.yaml',
         'green_modeTest.py', 'filename_operationsTest.py'
     ], contents)
 def test_initialize_project_data(self):
     dir_path = str(Path(__file__).parent) + os.sep
     contents = initialize_project_data(dir_path, [])
     pycache_index = -1
     for index, content in enumerate(contents):
         if isinstance(content, dict):
             if '__pycache__' in content.keys():
                 pycache_index = index
                 break
     if not pycache_index == -1:
         del contents[pycache_index]
     list_indices = []
     for index, content in enumerate(contents):
         if content[-4:] == 'orig':
             list_indices.append(index)
     for i in range(0, len(list_indices)):
         del contents[list_indices[i]]
         for j in range(i + 1, len(list_indices)):
             list_indices[j] = list_indices[j] - 1
     self.assertIn(
         ['QuickstartBearTest.py', 'example_.project_data.yaml',
          'green_modeTest.py', 'filename_operationsTest.py'],
         contents)
Example #17
0
 def test_run_quickstartbear(self):
     dir_path = str(Path(__file__).parent) + os.sep
     ignore_globs = ['*pycache*', '**.pyc', '**.orig']
     contents = initialize_project_data(dir_path, ignore_globs)
     contents = {'dir_structure': contents, settings_key: []}
     settings_key_values = [
         {
             'max_lines_per_file': 1000
         },  # default value
         {
             'max_line_length': 80
         },
         {
             'min_lines_per_file': 5
         }
     ]
     test_contents = deepcopy(contents)
     test_contents[settings_key] = settings_key_values
     (final_contents, ignore_ranges, complete_file_dict,
      complete_filename_list) = run_quickstartbear(contents, dir_path)
     ignore_file_name = dir_path + 'test_dir' + os.sep + 'test_file.py'
     start = SourcePosition(ignore_file_name, line=3, column=1)
     stop = SourcePosition(ignore_file_name, line=4, column=20)
     self.assertEqual(test_contents, final_contents)
Example #18
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)